def __init__(self, dim_state, dim_action, action_positve=False, hidden_layer_action='auto', hidden_layer_value='auto', gamma=0.3, sigma=1): if hidden_layer_action == 'auto': hidden_layer_action = ceil((dim_state + dim_action) / 2) if hidden_layer_value == 'auto': hidden_layer_value = ceil((dim_state + 1) / 2) self.gamma = gamma self.sigma = sigma self.ann = libfann.neural_net() self.ann.create_standard_array([dim_state, hidden_layer_action, dim_action]) self.vnn = libfann.neural_net() self.vnn.create_standard_array([dim_state, hidden_layer_value, 1]) self.dim_action = dim_action
def create_net(in_layers, hidden_layers, out_layers): net = fann.neural_net() net.create_sparse_array(1, (in_layers, hidden_layers, out_layers)) net.set_activation_function_hidden(fann.SIGMOID_SYMMETRIC) net.set_activation_function_output(fann.LINEAR) return net
def create_net(layers, funcs): net = fann.neural_net() net.create_sparse_array(1, layers) net.set_activation_function_hidden(funcs[0]) net.set_activation_function_output(funcs[1]) return net
def train(self, train_data): self.set_train_data(train_data) hidden_layers = [self.hidden_neurons] * self.hidden_layers layers = [self.train_data.num_input] layers.extend(hidden_layers) layers.append(self.train_data.num_output) sys.stderr.write("Network layout:\n") sys.stderr.write("* Neuron layers: %s\n" % layers) sys.stderr.write("* Connection rate: %s\n" % self.connection_rate) if self.training_algorithm not in ('TRAIN_RPROP',): sys.stderr.write("* Learning rate: %s\n" % self.learning_rate) sys.stderr.write("* Activation function for the hidden layers: %s\n" % self.activation_function_hidden) sys.stderr.write("* Activation function for the output layer: %s\n" % self.activation_function_output) sys.stderr.write("* Training algorithm: %s\n" % self.training_algorithm) self.ann = libfann.neural_net() self.ann.create_sparse_array(self.connection_rate, layers) self.ann.set_learning_rate(self.learning_rate) self.ann.set_activation_function_hidden(getattr(libfann, self.activation_function_hidden)) self.ann.set_activation_function_output(getattr(libfann, self.activation_function_output)) self.ann.set_training_algorithm(getattr(libfann, self.training_algorithm)) fann_train_data = libfann.training_data() fann_train_data.set_train_data(self.train_data.get_input(), self.train_data.get_output()) self.ann.train_on_data(fann_train_data, self.epochs, self.iterations_between_reports, self.desired_error) return self.ann
def create_and_save(scaling=False): ''' Creates and saves a heatmap if sys.argv[1] = create ''' # Load the saved neural net ann = lf.neural_net() ann.create_from_file(net_location) # Get the names, put into array names_list = [line.rstrip('\n') for line in open(names_location)] #print names_list # Get inputs to create heat array with with open(inputs_location) as f: inputs_csv = csv.reader(f, delimiter=',') inputs = [float(i) for i in inputs_csv.next()] #Create heatmap = hmg.create_heat_map(ann, inputs, names_list, bias=False, save_path=save_path, scaling=False) #Save hmg.save_heatmap_array(heatmap, "order.p") return heatmap
def __init__(self, site, picsize, charset): self.site = site self.trainpath = TRAINS_PATH + self.site + '.trn' self.netpath = NETS_PATH + self.site + '.ann' self.symbolpath = SYMBOLS_PATH + self.site self.charset = charset self.input = picsize[0] * picsize[1] self.output = len(charset) self.hidden = self.input / 3 self.layers = 3 self.desiredError = 0.00006 self.maxEpochs = 50000 self.epochsBetweenReports = 1000 self.ann = libfann.neural_net() self.ann.create_sparse_array(self.layers, (self.input, self.hidden, self.output)) self.ann.set_activation_function_hidden( libfann.SIGMOID_SYMMETRIC_STEPWISE) self.ann.set_activation_function_output( libfann.SIGMOID_SYMMETRIC_STEPWISE) log.info( 'init(): site: %s, size: %sx%s, charset: %s, input: %s, hidden: %s, output: %s' % colorize((site, picsize[0], picsize[1], charset, self.input, self.hidden, self.output)))
def __init__(self, size=2): if (size < 1): raise Exception("An ensemble must consist of at least 1 ANN") self.ANNs = [] for i in range(size): self.ANNs.append(libfann.neural_net())
def main(): # setting the prediction parameters known_days = 7 predict_days = 1 verify_days = 30 # setting up the parameters of the network connection_rate = 1 learning_rate = 0.1 num_input = known_days * 2 num_hidden = 60 num_output = predict_days # setting up the parameters of the network, continued desired_error = 0.000040 max_iterations = 10000 iteration_between_reports = 100 # setting up the network net = libfann.neural_net() net.create_sparse_array(connection_rate, (num_input, num_hidden, num_output)) net.set_learning_rate(learning_rate) net.set_activation_function_output(libfann.SIGMOID_SYMMETRIC_STEPWISE) # read the input file and format data fin = open("cw3.in") lines = fin.readlines() fin.close() rawdata = list(map(float, lines))[-1000:] datain0 = rawdata[0::2] datain1 = rawdata[1::2] n0 = max(datain0) * 1.4 n1 = max(datain1) * 1.4 datain0 = list(map(lambda x: x / n0, datain0)) datain1 = list(map(lambda x: x / n1, datain1)) # train the network data = libfann.training_data() drange = range(len(datain0) - known_days - verify_days) data.set_train_data( map(lambda x: datain0[x:][:known_days] + datain1[x:][:known_days], drange), map(lambda x: datain0[x + known_days:][:predict_days], drange) ) net.train_on_data(data, max_iterations, iteration_between_reports, desired_error) # result = [] for i in range(verify_days): dtest = datain0[-known_days - verify_days + i:][:known_days] + datain1[-known_days - verify_days + i:][:known_days] result += [net.run(dtest)[0] * n0] plot.plot(list(map(lambda x: x * n0, datain0[-verify_days: -verify_days])) + result, "r") plot.plot(map(lambda x: x * n0, datain0[-verify_days:]), "b") #plot.plot(list(map(lambda x: x * n0, datain0[-verify_days * 2: -verify_days])) + result, "r") #plot.plot(map(lambda x: x * n0, datain0[-verify_days * 2:]), "b") plot.show() # net.train_on_file("cw3.in", max_iterations, iteration_between_reports, desired_error) #print(net.run([1,1])) print("hehe") return
def train(self, inputs, outputs, params): self.p = inputs.shape[1] #number of input features self.n_r = outputs.shape[1] #size of output grid in rows self.n_c = outputs.shape[2] #size of output grid in cols self.out_min = outputs.min() self.out_max = outputs.max() d = self.out_max - self.out_min self.out_min -= d / 98 self.out_max -= d / 98 outputs = (outputs - self.out_min) / (self.out_max - self.out_min) assert inputs.shape[0] == outputs.shape[0] nn = libfann.neural_net() #nn.create_standard_array((self.p, 50, 50, self.n_r*self.n_c)) nn.create_shortcut_array((self.p, self.n_r*self.n_c)) nn.set_learning_rate(.7) nn.set_activation_function_hidden(libfann.SIGMOID_SYMMETRIC) nn.set_activation_function_output(libfann.SIGMOID) data = libfann.training_data() data.set_train_data(inputs, outputs.reshape((-1, self.n_r*self.n_c))) #nn.train_on_data(data, 500, 10, .001) nn.cascadetrain_on_data(data, 15, 1, .001) nn.save('nn.net') nn.destroy()
def test(self, ann_file, test_file): """Test an artificial neural network.""" if not os.path.isfile(ann_file): raise IOError("Cannot open %s (no such file)" % ann_file) if not os.path.isfile(test_file): raise IOError("Cannot open %s (no such file)" % test_file) # Get the prefix for the classification columns. try: dependent_prefix = self.config.data.dependent_prefix except: dependent_prefix = OUTPUT_PREFIX self.ann = libfann.neural_net() self.ann.create_from_file(ann_file) self.test_data = TrainData() try: self.test_data.read_from_file(test_file, dependent_prefix) except IOError as e: logging.error("Failed to process the test data: %s" % e) exit(1) logging.info("Testing the neural network...") fann_test_data = libfann.training_data() fann_test_data.set_train_data(self.test_data.get_input(), self.test_data.get_output()) self.ann.test_data(fann_test_data) mse = self.ann.get_MSE() logging.info("Mean Square Error on test data: %f" % mse)
def trainNet(): ann = libfann.neural_net() ann.create_sparse_array(connection_rate, (num_inputs, num_hidden, num_outputs)) ann.set_learning_rate(learning_rate) ann.set_activation_function_output(libfann.SIGMOID_SYMMETRIC) ann.train_on_file(train_file, max_iterations, iterations_between_reports, desired_error) ann.save(nn_file)
def main(): """ Train a neural network to recognize if a sentence is written in english or in french. It's based on the char frequency in the sentence and it try to figure out a pattern from the training set. """ if len(argv) != 3: stderr.write('Usage: python model.py <training_set file> <output file>\n') return 1 ann = fann.neural_net() ann.create_sparse_array(CONNECTION_RATE, (NUM_INPUT, NUM_HIDDEN, NUM_OUTPUT)) ann.set_learning_rate(LEARNING_RATE) ann.set_activation_function_output(fann.SIGMOID) ann.train_on_file(argv[1], MAX_ITERATIONS, ITERATION_REPORT, DESIRED_ERROR) while 1: print "Write your text to test your model:" text = stdin.readline() if len(text) <= 1: return 0 o = np.array(ann.run(text_to_vector(text.lower()))) predict = np.argmax(o) if predict == 0: print "%s: is written in french !\n" % (text.replace('\n', '')) else: print "%s: is written in english !\n" % (text.replace('\n', '')) ann.save(argv[2]) return 0
def doTrain(self, checkin): # train_data = libfann.training_data() print 'doTrain' ann = libfann.neural_net() filename = self.netFileName() ann.create_from_file(filename) ann.train(checkin.get_inputs(), [checkin.checkin_points]) # print sys.path script = sys.path[0] + '/get_vis.py' process = subprocess.Popen(["python", script, filename], stdout=subprocess.PIPE) result = process.communicate()[0] self.visualization = result # print result # print 'pre redirect' # old_stdout = sys.stdout # silly = common.sillystring() # sys.stdout = silly # print 'post redirect' # ann.print_connections() # self.visualization = silly.content ann.save(filename) self.exists = True self.save()
def __init__(self, topology=(3, 250, 2), inputMomentum = 0.05, learning_rate = 0.01, connection_rate = 1): """ Constr. @param topology: A vector of integers, specifying the number of neurons in each layer. Must not be None. Must have more than 1 element. @param inputMomentum: The training momentum. Must be in the interval [0,1). @param learning_rate: The learning rate. Must be in the interval [0,1). @param connection_rate: The FANN connection rate. Must be an integer greater or equal to 1. """ assert topology is not None and len(topology) > 1, "Topology %s is invalid" % str(topology) assert reduce(lambda x,y: x and y, map(lambda z : isinstance(z, int) and z > 0, topology)), "Topology %s contains invalid elements" % str(topology) assert inputMomentum is not None and 0 <= inputMomentum < 1, "Input momentum %s is invalid" % inputMomentum assert learning_rate is not None and 0 <= learning_rate < 1, "Learning rate %s is invalid" % learning_rate assert connection_rate is not None and connection_rate >= 1, "Connection rate %s is invalid" % connection_rate self.topology = topology self.momentum = inputMomentum self.learning_rate = learning_rate self.connection_rate = connection_rate self.ann = libfann.neural_net() self.ann.create_sparse_array(connection_rate, topology) self.ann.set_learning_rate(learning_rate) self.ann.set_learning_momentum(inputMomentum) self.ann.set_activation_function_hidden(libfann.SIGMOID) self.ann.set_activation_function_output(libfann.LINEAR) self.ann.set_training_algorithm(libfann.TRAIN_INCREMENTAL) self.ann.randomize_weights(-0.1, 0.1)
def execute(self, possible_venues): print 'execute!' if not self.exists: return 'The net does not exist yet, stop trying to execute it' ann = libfann.neural_net() filename= self.netFileName() ann.create_from_file(filename) processed_venues=[] #for tweaking scaling maxScore = 0 minScore = 100 for v in possible_venues: # log.info(v) score=ann.run(common.get_inputs(v))[0] name= v['name'] vid= v['id'] #also for tweaking scaling if score > maxScore: maxScore= score if score < minScore: minScore=score processed_venues.append([name, score, vid]) print "min, max:" print minScore print maxScore print maxScore - minScore return processed_venues
def __init__(self, name = "Eve", generation = 1, connection_rate = 0.5, learning_rate = 0.5, max_iterations = 50, bornBefore = 0, trainAlg = libfann.FANN_TRAIN_RPROP, learning_momentum = 0.0, neurons = [], connectionType = "Sparse"): settings.netsTried += 1 self.name = name self.generation = generation self.connection_rate = connection_rate self.learning_rate = learning_rate self.max_iterations = max_iterations self.ann = "" self.childrenHad = 0 self.bornBefore = bornBefore self.trainAlg = trainAlg self.learning_momentum = learning_momentum self.mseHistory = [] self.testmseHistory = [] self.summedError = 1.0 self.neurons = copy.deepcopy(neurons) if (self.neurons == []): self.neurons = [[[settings.flist[random.randrange(len(settings.flist))],0.0001+(0.9999*random.random())], [settings.flist[random.randrange(len(settings.flist))],0.0001+(0.9999*random.random())]] , [[settings.flist[random.randrange(len(settings.flist))],0.0001+(0.9999*random.random())] for i in range(settings.num_output)]] self.foodcost = (0.001*(len(self.neurons)-1)) + (0.0001*sum(map(len,self.neurons[0:-1]))) self.connectionType = connectionType if self.ann =="": self.ann = libfann.neural_net()
def __setstate__(self, odict): ann = libfann.neural_net() fake_file_call_s2f( ann.create_from_file, odict.pop('fann_save') ) self.__dict__.update(odict) self.ann = ann
def classify_image(self, im_path, ann_path, config, codebookfile=None): """Classify an image file and return the codeword. Preprocess and extract features from the image `im_path` as defined in the configuration object `config`, and use the features as input for the neural network `ann_path` to obtain a codeword. If necessary the 'codebookfile' is used to create the codeword. """ if not os.path.isfile(im_path): raise IOError("Cannot open %s (no such file)" % im_path) if not os.path.isfile(ann_path): raise IOError("Cannot open %s (no such file)" % ann_path) if 'preprocess' not in config: raise ConfigurationError("preprocess settings not set") if 'features' not in config: raise ConfigurationError("features settings not set") if codebookfile and not os.path.isfile(codebookfile): raise IOError("Cannot open %s (no such file)" % codebookfile) ann = libfann.neural_net() ann.create_from_file(str(ann_path)) # Get the MD5 hash for the image. hasher = hashlib.md5() with open(im_path, 'rb') as fh: buf = fh.read() hasher.update(buf) # Get a hash that that is unique for this image/preprocess/features # combination. hashables = get_config_hashables(config) hash_ = combined_hash(hasher.hexdigest(), config.features, *hashables) if hash_ in self.cache: phenotype = self.cache[hash_] else: phenotyper = Phenotyper() phenotyper.set_image(im_path) if self.roi: phenotyper.set_roi(self.roi) phenotyper.set_config(config) phenotype = phenotyper.make() # Cache the phenotypes, in case they are needed again. self.cache[hash_] = phenotype # Convert phenotype to BagOfWords-code if necessary. use_bow = getattr(config.features['surf'], 'bow_clusters', False) if use_bow: with open(codebookfile, "rb") as cb: codebook = load(cb) phenotype = get_bowcode_from_surf_features(phenotype, codebook) logging.debug("Using ANN `%s`" % ann_path) codeword = ann.run(phenotype) return codeword
def main(): # Load neural network ann = lf.neural_net() ann.create_from_file(net_location) # Replace this nonsense with argparse later on # But for now, we live in anarchy # Create or load heatmap based on ANN if len(sys.argv) > 1 and sys.argv[1].lower() == 'create': heatmap = create_and_save(scaling=False) elif len(sys.argv) > 1 and sys.argv[1].lower() == 'load': #load from file heatmap = pickle.load( open(os.path.join(basedir, '../data/heatmaps/order.p'), 'rb')) else: print "Please input a valid argument to {0}, either `create` or `load`".format( sys.argv[0]) sys.exit(1) Heatmap = hmg.Heatmap(heatmap) Heatmap.get_row_avgs() Heatmap.get_col_avgs() names_list = [line.rstrip('\n') for line in open(names_location)] names_dict = {} # key=name, value=index for i, v in enumerate(names_list): names_dict[v] = str(i) rmse, bray_curtis = an.test_network(ann, test_location, save_path) # Now we have the heatmap object so lets do some stuff g = an.create_fuzzy_hairball(Heatmap, save_path) # Create heatmap value csv heatmap_csv = csv.writer(open("heatmap_values.csv", 'w'), delimiter=',') heatmap_csv.writerow([""] + names_list[0:-6]) for i, v in enumerate(Heatmap.heatmap): heatmap_csv.writerow([names_list[i]] + [str(s) for s in v]) in_degree_dict = an.get_degrees(g, names_list, "in") out_degree_dict = an.get_degrees(g, names_list, "out") total_degree_dict = an.get_degrees(g, names_list, "all") with open('degrees.csv', 'w') as degree_csv_filehandle: degree_csv = csv.writer(degree_csv_filehandle) degree_csv.writerow(['Taxon', 'In', 'Out', 'Total']) for k in total_degree_dict.keys(): ins = str(in_degree_dict[k]) outs = str(out_degree_dict[k]) total = str(total_degree_dict[k]) degree_csv.writerow([k, ins, outs, total]) # Get top ten most connected and create fuzzy hairball (connectivity network) top_ten = an.create_topten_bar(total_degree_dict, save_path, names_dict) g = an.create_fuzzy_hairball_fixed(Heatmap, save_path, 10, total_degree_dict, names_list) np.set_printoptions(precision=4) print rmse print bray_curtis
def run_predictions(): import MySQLdb as mdb from pyfann import libfann #from datetime import date from network_functions import save_prediction mydate = "" con = None con = mdb.connect('localhost', 'root', 'fil1202job', 'stock'); with con: cur = con.cursor(mdb.cursors.DictCursor) cur1 = con.cursor() # # Get a list of all networks # cur.execute("SELECT a.id, a.group, b.ticker, b.predict_data, a.net_file FROM `network`.`network` a, network.net_group b where a.group = b.id;") rows = cur.fetchall() for row in rows: # # For each network get the training data - only most recent data at the moment # #seldate = "select latest_prediction from network.network where id = " + str(row["id"]) #cur2.execute(seldate) #latestdate = cur2.fetchone() #latestdate1 = latestdate[0] #print latestdate1 cur1.execute(row["predict_data"]) for row1 in cur1.fetchall(): # # Extract Date # mydate = row1[(len(row1) - 1)] row1b = list(row1) del row1b[(len(row1b) - 1)] # # Set up network # ann = libfann.neural_net() ann.create_from_file(row["net_file"]) # # Run Prediction # print row1b print ann.run(row1b) prediction = ann.run(row1b) prediction = str(prediction).translate(None, '[]') # # Store results in db - Function # save_prediction(row["id"], mydate, prediction) calc_signals()
def network(inputsize,h1,h2,h3,h4,h5,h6,outputsize): connect_rate = 0.1 ann = libfann.neural_net() ann.create_sparse_array(connect_rate, (inputsize, h1,h2,h3,h4,h5,h6, outputsize)) ann.set_activation_function_output(libfann.SIGMOID_SYMMETRIC_STEPWISE) return ann
def setup_ann(self, nn_input): hidden_activation = libfann.SIGMOID_SYMMETRIC try: num_inputs = int(nn_input) except: num_inputs = len(nn_input) network = [num_inputs] for n in self.networkConfig: network.append(n) self.actor = libfann.neural_net() if (self.actor.create_from_file(self.actor_file)): print 'Loaded Actor from file' else: self.actor.create_standard_array(network) self.actor.randomize_weights(-self.random_range, self.random_range) self.actor.set_activation_function_hidden(hidden_activation) self.actor.set_activation_function_output(libfann.LINEAR) self.critic = libfann.neural_net() if (self.critic.create_from_file(self.critic_file)): print 'Loaded Critic from file' else: network[len(network) - 1] = 1 self.critic.create_standard_array(network) self.critic.randomize_weights(-self.random_range, self.random_range) self.critic.set_activation_function_hidden(hidden_activation) self.critic.set_activation_function_output(libfann.LINEAR) self.sigma = max(self.min_sigma, self.sigma * (self.random_decay**self.progress)) self.epsilon = max(self.min_epsilon, self.epsilon * (self.random_decay**self.progress)) self.alpha = max(self.min_alpha, self.alpha * (self.learning_decay**self.progress)) self.beta = max(self.min_beta, self.beta * (self.learning_decay**self.progress)) self.actor.set_learning_rate(self.alpha) self.critic.set_learning_rate(self.beta)
def __init__(self, network=[2, 10, 1], learning_rate=0.2, connection_rate=1): self.ann = libfann.neural_net() self.ann.create_sparse_array(connection_rate, tuple(network)) self.ann.set_learning_rate(learning_rate) self.ann.set_activation_function_hidden(libfann.SIGMOID_SYMMETRIC) self.ann.set_activation_function_output(libfann.LINEAR)
def testNet(): data = libfann.training_data() data.read_train_from_file(test_file); ann = libfann.neural_net() ann.create_from_file(nn_file) ann.reset_MSE() ann.test_data(data) print("Mean square error: {0}".format(ann.get_MSE()));
def firstTrain(self, checkin): print 'firstTrain!' # train_data = libfann.training_data() ann = libfann.neural_net() # ann.create_standard(nLAYERS, nINPUTS, nHIDDEN1, nHIDDEN2, nOUTPUTS) ann.create_standard_array((nINPUTS, nHIDDEN1, nHIDDEN2, nOUTPUTS)) # log.info(checkin.get_inputs()) ann.train(checkin.get_inputs(), [checkin.checkin_points]) filename = self.netFileName() ann.save(filename)
def test(annFile, testFile): ann = libfann.neural_net() ann.create_from_file(annFile) resultFile = testFile + "result" with open(testFile) as testFileReader, open(resultFile, "w+") as resultFileWriter: for line in testFileReader: if len(line.strip()) > 0: tempResult = ann.run([float(x) for x in line.split()]) tempImageClass = tempResult.index(max(tempResult)) # resultFileWriter.write(str(tempImageClass) + "\n") resultFileWriter.write(" ".join([str(x) for x in tempResult]) + " " + str(tempImageClass) + "\n")
def train(trainFile, layerNumber, neuronNumber, maxIteration): desiredError = 1e-2 #maxIteration = 1000 iterationBetweenReports = 100 ann = libfann.neural_net() ann.create_standard_array(tuple(neuronNumber)) ann.set_learning_rate(0.7) ann.set_activation_function_output(libfann.SIGMOID_SYMMETRIC_STEPWISE) ann.set_activation_function_hidden(libfann.SIGMOID_SYMMETRIC_STEPWISE) ann.train_on_file(trainFile, maxIteration, iterationBetweenReports, desiredError) ann.save(trainFile + ".net")
def create_ann(self, network_load_file=None, connection_rate=1, learning_rate=0.2, num_neurons_hidden=15, config_file=None): self.connection_rate = connection_rate self.learning_rate = learning_rate self.num_neurons_hidden = num_neurons_hidden if config_file: execfile(config_file) self.ann = libfann.neural_net() if (network_load_file and os.path.exists(network_load_file)): print "Loading network" self.ann.create_from_file(network_load_file) else: print "Creating network" self.ann.create_sparse_array( self.connection_rate, (self.dim_input, self.num_neurons_hidden, self.dim_output)) self.ann.set_activation_function_hidden(libfann.SIGMOID) #self.ann.set_activation_function_hidden(libfann.SIGMOID_SYMMETRIC) #self.ann.set_activation_function_hidden(libfann.SIGMOID_STEPWISE) #self.ann.set_activation_function_hidden(libfann.SIGMOID_SYMMETRIC_STEPWISE) self.ann.set_activation_function_output(libfann.SIGMOID) #self.ann.set_activation_function_target(libfann.SIGMOID_SYMMETRIC) #self.ann.set_activation_function_target(libfann.SIGMOID_STEPWISE) #self.ann.set_activation_function_target(libfann.SIGMOID_SYMMETRIC_STEPWISE) self.ann.set_train_error_function(libfann.ERRORFUNC_TANH) #self.ann.set_train_error_function(libfann.ERRORFUNC_LINEAR) #self.ann.set_training_algorithm(libfann.TRAIN_INCREMENTAL) self.ann.set_training_algorithm(libfann.TRAIN_RPROP) #self.ann.set_training_algorithm(libfann.TRAIN_QUICKPROP) print "initializing weights" #self.ann.init_weights(self.train_data) #self.ann.randomize_weights(-.10,.10) self.ann.set_learning_rate(self.learning_rate) self.ann.set_rprop_increase_factor(1.2) self.ann.set_rprop_decrease_factor(0.5) self.ann.set_rprop_delta_min(0) self.ann.set_rprop_delta_max(50) self.ann.print_parameters()
def TrainOnData(filename, output): conf = __import__("config.train.%s" % args.neural_config, globals(), locals(), ['*']) inputNodes = int(open(filename).readline().split()[1]) outputNodes = int(open(filename).readline().split()[2]) ann = libfann.neural_net() ann.create_sparse_array(conf.connection_rate, (inputNodes, conf.hiddenNodes, outputNodes)) ann.set_learning_rate(conf.learning_rate) ann.set_activation_function_output(libfann.SIGMOID_SYMMETRIC_STEPWISE) ann.train_on_file(filename, conf.max_iterations, conf.iterations_between_reports, conf.desired_error) ann.save(output)
def create_network(input_nodes, output_nodes): ann = libfann.neural_net() if input_nodes > 6: hidden_nodes = int((input_nodes + output_nodes)/2) else: hidden_nodes = 2 layers = [input_nodes, hidden_nodes, output_nodes] #ann.create_standard_array(layers) ann.create_sparse_array(0.7, layers) ann.randomize_weights(-0.5, 0.5) ann.set_learning_rate(0.6) ann.set_activation_function_hidden(libfann.SIGMOID_SYMMETRIC_STEPWISE) ann.set_activation_function_output(libfann.SIGMOID_SYMMETRIC_STEPWISE) return ann
def classify_image(self, im_path, ann_path, config): """Classify an image file and return the codeword. Preprocess and extract features from the image `im_path` as defined in the configuration object `config`, and use the features as input for the neural network `ann_path` to obtain a codeword. """ if not os.path.isfile(im_path): raise IOError("Cannot open %s (no such file)" % im_path) if not os.path.isfile(ann_path): raise IOError("Cannot open %s (no such file)" % ann_path) if 'preprocess' not in config: raise ConfigurationError("preprocess settings not set") if 'features' not in config: raise ConfigurationError("features settings not set") ann = libfann.neural_net() logging.debug("Instantiated FANN object") ann.create_from_file(str(ann_path)) logging.debug("Loaded ANN topology from file") # Get the MD5 hash for the image. hasher = hashlib.md5() with open(im_path, 'rb') as fh: buf = fh.read() hasher.update(buf) # Get a hash that that is unique for this image/preprocess/features # combination. hashables = get_config_hashables(config) hash_ = combined_hash(hasher.hexdigest(), config.features, *hashables) if hash_ in self.cache: phenotype = self.cache[hash_] else: phenotyper = Phenotyper() phenotyper.set_image(im_path) if self.roi: phenotyper.set_roi(self.roi) phenotyper.set_config(config) phenotype = phenotyper.make() # Cache the phenotypes, in case they are needed again. self.cache[hash_] = phenotype logging.debug("Using ANN '%s'" % ann_path) codeword = ann.run(phenotype) logging.debug("ANN classifier returned '%s'" % codeword) return codeword
def main(): config = Configuration().parse_args() network = libfann.neural_net() network.create_from_file(config.netfile) f = open(config.testfile, 'r') full_file = f.read().split("\n")[1:-1] f.close() assert len(full_file) % 2 == 0 true_f = 0 true_m = 0 notsure_f = 0 notsure_m = 0 false_f = 0 false_m = 0 wtf_res = 0 for i in range(len(full_file) / 2): in_index = i * 2 out_index = in_index + 1 ins = full_file[in_index] outs = full_file[out_index] inp = [float(x) for x in ins.split()] real_out = rnd(float(outs)) net_out = rnd(network.run(inp)[0]) print "In : %s\nOut : %s\nRealOut : %s\n" % (ins, net_out, real_out) if real_out == net_out: if real_out == 'F': true_f += 1 elif real_out == 'M': true_m += 1 elif net_out == '?': if real_out == 'F': notsure_f += 1 elif real_out == 'M': notsure_m += 1 elif real_out == 'F': false_m += 1 elif real_out == 'M': false_f += 1 print "- %i OK" % (true_f + true_m) print "- %i NotSure" % (notsure_f + notsure_m) print "- %i Wrong" % (false_f + false_m)
def MagicRegognition(img, ann): ann = libfann.neural_net() ann.create_from_file('fann.data') sample = [] for i in img.size[1]: for j in img.size[0]: if colordist(img.getpixel((j, i)), bgcolor) < 10: sample[j + i * img.size[0]] = 0 else: sample[j + i * img.size[0]] = 1 res = ann.run(sample) return res.index(max(res))
def _train_and_save(self): """ method which trains the neural network """ print "-> Training network..." ann = libfann.neural_net() ann.create_sparse_array( self._connection_rate, (self._num_input, self._num_hidden, self._num_output)) ann.set_learning_rate(self._learning_rate) ann.set_activation_function_output(libfann.SIGMOID_SYMMETRIC_STEPWISE) ann.train_on_file("training_data/fann/training_data.data", self._max_iterations, self._iterations_between_reports, self._desired_error) print "-> Training complete..." print "-> Saving network...." ann.save("network_states/fann/fann.net")
def new_net(): conn_rate = 5 learn_rate = 0.5 num_in = 30 num_hid = 6 num_out = 1 network = libfann.neural_net() network.create_sparse_array(conn_rate, (num_in, num_hid, num_out)) network.set_learning_rate(learn_rate) network.set_activation_function_output(libfann.SIGMOID_SYMMETRIC_STEPWISE) network.set_activation_function_hidden(libfann.SIGMOID_SYMMETRIC_STEPWISE) return network
def train_ann(training_set): '''Train the anni ''' #Constants total_datapoints = len(training_set) num_inputs = len(training_set[0][0]) num_outputs = len(training_set[0][1]) learning_rate = 0.01 momentum = 0.95 desired_error = 0.0001 iterations_between_reports = 0 maximum_iterations = 10000 num_hiddens1 = roundup(num_inputs * 0.95) num_hiddens2 = roundup(num_inputs * 0.85) #Gotta make a training data file data_file = "../data/validation/training.data" f = open(data_file, 'w') f.write("%s %s %s\n" % (total_datapoints, num_inputs, num_outputs)) for datapoint in training_set: inp = ' '.join(str(x) for x in datapoint[0]) targ = ' '.join(str(x) for x in datapoint[1]) f.write("%s\n" % inp) f.write("%s\n" % targ) f.close() mse = 1 while mse > desired_error: ann = lf.neural_net() ann.create_standard_array( [num_inputs, num_hiddens1, num_hiddens2, num_outputs]) ann.set_learning_rate(learning_rate) ann.set_learning_momentum(momentum) ann.set_activation_function_hidden( lf.SIGMOID_SYMMETRIC ) #This is to ensure negative and positive influences can be detected ann.set_activation_function_output( lf.SIGMOID) #Ensure output isnt negative ann.set_bit_fail_limit(0.01) #ann.set_train_error_function(lf.ERRORFUNC_TANH) # Train ann.train_on_file(data_file, maximum_iterations, iterations_between_reports, desired_error) mse = ann.get_MSE() return ann
def classify_image(self, im_path, ann_path, config): """Classify an image file and return the codeword. Preprocess and extract features from the image `im_path` as defined in the configuration object `config`, and use the features as input for the neural network `ann_path` to obtain a codeword. """ if not os.path.isfile(im_path): raise IOError("Cannot open %s (no such file)" % im_path) if not os.path.isfile(ann_path): raise IOError("Cannot open %s (no such file)" % ann_path) if 'preprocess' not in config: raise ConfigurationError("preprocess settings not set") if 'features' not in config: raise ConfigurationError("features settings not set") ann = libfann.neural_net() ann.create_from_file(str(ann_path)) # Get the MD5 hash for the image. hasher = hashlib.md5() with open(im_path, 'rb') as fh: buf = fh.read() hasher.update(buf) # Get a hash that that is unique for this image/preprocess/features # combination. hashables = get_config_hashables(config) hash_ = combined_hash(hasher.hexdigest(), config.features, *hashables) if hash_ in self.cache: phenotype = self.cache[hash_] else: phenotyper = Phenotyper() phenotyper.set_image(im_path) if self.roi: phenotyper.set_roi(self.roi) phenotyper.set_config(config) phenotype = phenotyper.make() # Cache the phenotypes, in case they are needed again. self.cache[hash_] = phenotype logging.debug("Using ANN `%s`" % ann_path) codeword = ann.run(phenotype) return codeword
def train(self, training_file, net_file, num_input, num_neurons_hidden, num_output): """ Creates an ANN, specifies parameters for the ANN, and trains it to a file exported by swvgsleaf.LeafContainer """ ann = libfann.neural_net() ann.create_sparse_array(CONNECTION_RATE, (num_input, num_neurons_hidden, num_output)) ann.set_learning_rate(LEARNING_RATE) ann.set_activation_function_output(libfann.SIGMOID_SYMMETRIC) ann.train_on_file(training_file, MAX_ITERATIONS, ITERATIONS_BETWEEN_REPORTS, DESIRED_ERROR) ann.save(net_file) ann.destroy() #frees memory associated with the ANN #loads the ANN from the net_file self.load_from_file(net_file)
def __init__(self, name="Eve", generation=1, connection_rate=0.5, learning_rate=0.5, max_iterations=50, bornBefore=0, trainAlg=libfann.TRAIN_RPROP, learning_momentum=0.0, neurons=[], connectionType="Sparse"): settings.netsTried += 1 self.name = name self.generation = generation self.connection_rate = connection_rate self.learning_rate = learning_rate self.max_iterations = max_iterations self.ann = "" self.childrenHad = 0 self.bornBefore = bornBefore self.trainAlg = trainAlg self.learning_momentum = learning_momentum self.mseHistory = [] self.testmseHistory = [] self.summedError = 1.0 self.neurons = copy.deepcopy(neurons) if (self.neurons == []): self.neurons = [ [[ settings.flist[random.randrange(len(settings.flist))], 0.0001 + (0.9999 * random.random()) ], [ settings.flist[random.randrange(len(settings.flist))], 0.0001 + (0.9999 * random.random()) ]], [[ settings.flist[random.randrange(len(settings.flist))], 0.0001 + (0.9999 * random.random()) ] for i in range(settings.num_output)] ] self.foodcost = (0.001 * (len(self.neurons) - 1)) + ( 0.0001 * sum(map(len, self.neurons[0:-1]))) self.connectionType = connectionType if self.ann == "": self.ann = libfann.neural_net()
def multiprocessing_eval(ind): """ Internal used by the multiprocessing """ ann = libfann.neural_net() layers = GAnnConsts.CNetwork.get_layer_array() ann.create_sparse_array(GAnnConsts.CNetwork.get_connection_rate(), layers) ann.set_activation_function_hidden(GAnnConsts.CNetwork.get_activation_function(1,0)) ann.set_activation_function_output(GAnnConsts.CNetwork.get_activation_function(len(layers)-1,0)) ann.set_activation_steepness_hidden(GAnnConsts.CNetwork.get_activation_steepness(1,0)) ann.set_activation_steepness_output(GAnnConsts.CNetwork.get_activation_steepness(len(layers)-1,0)) ann.set_rprop_increase_factor(GAnnConsts.CNetwork.get_rprop_increase_factor()) ann.set_rprop_decrease_factor(GAnnConsts.CNetwork.get_rprop_decrease_factor()) ann.set_rprop_delta_min(GAnnConsts.CNetwork.get_rprop_delta_min()) ann.set_rprop_delta_max(GAnnConsts.CNetwork.get_rprop_delta_max()) # ann.print_parameters() ind.evaluate(network=ann, data=GAnnConsts.CTrainData) return ind.score
def create_ann(self, network_load_file=None, connection_rate=1, learning_rate=0.2, num_neurons_hidden=15, config_file=None): self.connection_rate = connection_rate self.learning_rate = learning_rate self.num_neurons_hidden = num_neurons_hidden if config_file: execfile(config_file) self.ann = libfann.neural_net() if (network_load_file and os.path.exists(network_load_file)): print "Loading network" self.ann.create_from_file(network_load_file) else: print "Creating network" self.ann.create_sparse_array(self.connection_rate, (self.dim_input, self.num_neurons_hidden, self.dim_output)) self.ann.set_activation_function_hidden(libfann.SIGMOID) #self.ann.set_activation_function_hidden(libfann.SIGMOID_SYMMETRIC) #self.ann.set_activation_function_hidden(libfann.SIGMOID_STEPWISE) #self.ann.set_activation_function_hidden(libfann.SIGMOID_SYMMETRIC_STEPWISE) self.ann.set_activation_function_output(libfann.SIGMOID) #self.ann.set_activation_function_target(libfann.SIGMOID_SYMMETRIC) #self.ann.set_activation_function_target(libfann.SIGMOID_STEPWISE) #self.ann.set_activation_function_target(libfann.SIGMOID_SYMMETRIC_STEPWISE) self.ann.set_train_error_function(libfann.ERRORFUNC_TANH) #self.ann.set_train_error_function(libfann.ERRORFUNC_LINEAR) #self.ann.set_training_algorithm(libfann.TRAIN_INCREMENTAL) self.ann.set_training_algorithm(libfann.TRAIN_RPROP) #self.ann.set_training_algorithm(libfann.TRAIN_QUICKPROP) print "initializing weights" #self.ann.init_weights(self.train_data) #self.ann.randomize_weights(-.10,.10) self.ann.set_learning_rate(self.learning_rate) self.ann.set_rprop_increase_factor(1.2) self.ann.set_rprop_decrease_factor(0.5) self.ann.set_rprop_delta_min(0) self.ann.set_rprop_delta_max(50) self.ann.print_parameters()
def __init__(self, new_network=False): """ init method which initialises network parameters and setting up the network """ self._connection_rate = 1 self._learning_rate = 0.01 self._num_input = 36 self._num_hidden = 6 self._num_output = 1 self._desired_error = 0.0001 self._max_iterations = 500 self._iterations_between_reports = 100 self._new_network = new_network self._ann = libfann.neural_net() if self._new_network: self._create_train_data() self._train_and_save()
def train(self, data_path, config_path): """Train a neural network on training data from `data_path`. Returns a FANN structure. """ # Load and update Aivolver configurations. with open(config_path, 'r') as fh: config = yaml.load(fh) if 'experiment' not in config: raise ValueError("Missing `experiment` configurations") if 'ann' not in config: raise ValueError("Missing `ann` configurations") config['ann']['error'] = self.desired_error config['ann']['epochs'] = self.epochs config['ann']['neurons'] = self.max_neurons sys.stderr.write("Training with Aivolver...\n") # Empty the working directory. workdir = config['experiment']['workdir'] delete_temp_dir(workdir, recursive=True) os.makedirs(workdir) # Write new configurations file for Aivolver. new_config_path = os.path.join(workdir, 'config.yml') with open(new_config_path, 'w') as fh: yaml.dump(config, fh) # Execute Aivolver. ann_path = os.path.join(workdir, 'fittest.ann') subprocess.check_call([ 'aivolver', new_config_path, '-d', "file=%s" % data_path, '-o', ann_path, ]) # Load the neural network from the temporary directory. self.ann = libfann.neural_net() self.ann.create_from_file(ann_path) return self.ann
def initNet(): learning_rate = 0.3 num_neurons_hidden = num_input / 3 #desired_error = 0.015 #max_iterations = 10000 #iterations_between_reports = 10 global ann ann = libfann.neural_net() ann.create_standard_array((num_input, num_neurons_hidden, num_output)) ann.set_learning_rate(learning_rate) ann.set_activation_function_hidden(libfann.SIGMOID_SYMMETRIC_STEPWISE) ann.set_activation_function_output(libfann.SIGMOID_SYMMETRIC_STEPWISE) train = libfann.training_data() train.read_train_from_file(train_file) ann.init_weights(train) train.destroy_train()
def train(self, n, expOutput, trainTimes = 1, revert=False, maxRMSE=None): """ Trains the underlying neural network, until trainTimes iterations have been performed, or the RMSE has been achieved. @param n: Number of users. Must not be None. Must be non-negative integer. @param expOutput: The expected output from the NN. Must be of appropriate size. Must not be None. @param trainTimes: How many training iterations to perform. An integer, greater or equal to 1. Must not be None. @param revert: Boolean flag, whether to revert the changes to the NN after the training. @param maxRMSE: If not None, trains until the RMSE is achieved. May be None. May not be 0 or negative. @return: The RMSE after the training. """ assert n is not None and n >= 0, "Invalid n: %s" % n assert expOutput is not None and len(expOutput) == self.topology[-1], "Invalid Expected Output: %s" % str(expOutput) assert trainTimes is not None and isinstance(trainTimes, int) and trainTimes > 0, "Invalid trainTimes: %s" % trainTimes assert revert is not None and isinstance(revert, bool), "Invalid revert param: %s" % revert assert maxRMSE is None or maxRMSE > 0, "Invalid maxRMSE param: %s" % maxRMSE inp = self._convertInput(n) assert len(inp) == self.topology[0], "Input size: {0}, expected size:{1}".format(len(inp), self.topology[0]) assert len(expOutput) == self.topology[-1], "Output size: {0}, expected size:{1}".format(len(expOutput), self.topology[-1]) log.info("Training for %s users %s times with input:%s and output:%s", n, trainTimes, str(inp), str(expOutput)) bufferAnnFile = os.path.expanduser("~/buffer-ann.txt") if revert: log.debug("Saving FANN's state to {0}".format(bufferAnnFile)) self.ann.save(bufferAnnFile) for _ in range(trainTimes): rmse = self.rmse(n, expOutput) if (maxRMSE is not None) and (rmse < maxRMSE): break self.ann.train(inp, expOutput) result = self.rmse(n, expOutput) if revert: log.debug("Restoring FANN's state from {0}".format(bufferAnnFile)) self.ann = libfann.neural_net() self.ann.create_from_file(bufferAnnFile) return result
def __init__(self, learning_rate=0.9, num_neurons_hidden=30, hidden_layers=2, max_iterations=5000, iterations_between_reports=100, train_pct=0.6, desired_error=0.0006, train_count=0, window_size=10): self.learning_rate = learning_rate self.num_input = 3 self.num_neurons_hidden = num_neurons_hidden self.num_output = 3 self.desired_error = desired_error self.train_pct = train_pct self.train_count = train_count self.max_iterations = max_iterations self.iterations_between_reports = iterations_between_reports self.ann = libfann.neural_net() self.hidden_layers = hidden_layers ann_layout = [] ann_layout.append(self.num_input) for i in range(self.hidden_layers): ann_layout.append(self.num_neurons_hidden) ann_layout.append(self.num_output) self.ann.create_standard_array(ann_layout) self.ann.set_learning_rate(self.learning_rate) self.ann.set_training_algorithm(libfann.TRAIN_RPROP) self.ann.set_activation_function_hidden(libfann.SIGMOID_SYMMETRIC) self.ann.set_activation_function_output(libfann.LINEAR) self.ann.set_train_error_function(libfann.STOPFUNC_MSE) self.window_size = window_size # print "ANN Setup with learning_rate:%0.1f neurons_hidden:%d hidden_layers:%d max_iterations:%d train_pct:%0.1f train_cnt:%d" % (learning_rate, num_neurons_hidden, hidden_layers, max_iterations, train_pct, train_count) print( "ANN Setup with learning_rate:%.1f neurons_hidden:%d hidden_layers:%d max_iterations:%d train_pct:%.1f train_cnt:%d window_size:%d" % (learning_rate, num_neurons_hidden, hidden_layers, max_iterations, train_pct, train_count, window_size))
def test(self): print "Creating network." train_data = libfann.training_data() train_data.read_train_from_file(tfile) ann = libfann.neural_net() ann.create_sparse_array( connection_rate, (len(train_data.get_input()[0]), num_neurons_hidden, len(train_data.get_output()[0]))) ann.set_learning_rate(learning_rate) ann.set_activation_function_hidden(libfann.SIGMOID_SYMMETRIC_STEPWISE) ann.set_activation_function_output(libfann.SIGMOID_STEPWISE) ann.set_training_algorithm(libfann.TRAIN_INCREMENTAL) ann.train_on_data(train_data, max_iterations, iterations_between_reports, desired_error) print "Testing network" test_data = libfann.training_data() test_data.read_train_from_file(test_file) ann.reset_MSE() ann.test_data(test_data) print "MSE error on test data: %f" % ann.get_MSE()
def train(self, train_data): self.set_train_data(train_data) hidden_layers = [self.hidden_neurons] * self.hidden_layers layers = [self.train_data.num_input] layers.extend(hidden_layers) layers.append(self.train_data.num_output) sys.stderr.write("Network layout:\n") sys.stderr.write("* Neuron layers: %s\n" % layers) sys.stderr.write("* Connection rate: %s\n" % self.connection_rate) if self.training_algorithm not in ('TRAIN_RPROP', ): sys.stderr.write("* Learning rate: %s\n" % self.learning_rate) sys.stderr.write("* Activation function for the hidden layers: %s\n" % self.activation_function_hidden) sys.stderr.write("* Activation function for the output layer: %s\n" % self.activation_function_output) sys.stderr.write("* Training algorithm: %s\n" % self.training_algorithm) self.ann = libfann.neural_net() self.ann.create_sparse_array(self.connection_rate, layers) self.ann.set_learning_rate(self.learning_rate) self.ann.set_activation_function_hidden( getattr(libfann, self.activation_function_hidden)) self.ann.set_activation_function_output( getattr(libfann, self.activation_function_output)) self.ann.set_training_algorithm( getattr(libfann, self.training_algorithm)) fann_train_data = libfann.training_data() fann_train_data.set_train_data(self.train_data.get_input(), self.train_data.get_output()) self.ann.train_on_data(fann_train_data, self.epochs, self.iterations_between_reports, self.desired_error) return self.ann
def __init__(self): self.ann = libfann.neural_net() self.network_file = ""
num_output = 1 desired_error = 0.0001 max_neurons = 40 neurons_between_reports = 1 steepnesses = [0.1, 0.2, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0, 1.1] train_data = libfann.training_data() train_data.read_train_from_file("../../benchmarks/datasets/two-spiral.train") test_data = libfann.training_data() test_data.read_train_from_file("../../benchmarks/datasets/two-spiral.test") train_data.scale_train_data(0, 1) test_data.scale_train_data(0, 1) ann = libfann.neural_net() ann.create_shortcut_array( [len(train_data.get_input()[0]), len(train_data.get_output()[0])]) ann.set_training_algorithm(libfann.TRAIN_RPROP) ann.set_activation_function_hidden(libfann.SIGMOID_SYMMETRIC) ann.set_activation_function_output(libfann.LINEAR_PIECE) ann.set_activation_steepness_hidden(0.5) ann.set_activation_steepness_output(0.5) ann.set_train_error_function(libfann.ERRORFUNC_LINEAR) ann.set_rprop_increase_factor(1.2) ann.set_rprop_decrease_factor(0.5)
def __init__(self): self.ann = libfann.neural_net()
for i in range(len(inputs))]]) print >>datafile, trainingset[n] datafile.close() print 'Training.' connection_rate = 1 learning_rate = 0.97 num_hidden = 7 num_output = 1 desired_error = 0.001 max_iterations = 1000 iterations_between_reports = 10 net = fann.neural_net() cascade = 'cascade' in sys.argv[2:] if cascade: net.create_shortcut_array((len(inputs), num_output)) net.set_activation_function_output(fann.LINEAR_PIECE_SYMMETRIC) net.cascadetrain_on_file("fann.trainingset", num_hidden, 1, desired_error) else: net.create_sparse_array(connection_rate, (len(inputs), num_hidden, num_output)) net.set_learning_rate(learning_rate) # net.set_activation_function_output(fann.COS_SYMMETRIC)
def run_fann(num_hidden=4, fname="ann_ws496.net", fname_data_prefix='', n_iter=100, disp=True, graph=True): print "num_hidden =", num_hidden fname_data_train = fname_data_prefix + "train_in.data" fname_data_test = fname_data_prefix + "test_in.data" connection_rate = 1 learning_rate = 0.7 num_input = 1024 #num_hidden = 40 num_output = 1 desired_error = 0.0001 max_iterations = 1 iterations_between_reports = 1 ann = libfann.neural_net() ann.create_sparse_array(connection_rate, (num_input, num_hidden, num_output)) ann.set_learning_rate(learning_rate) ann.set_activation_function_hidden(libfann.SIGMOID_SYMMETRIC) ann.set_activation_function_output(libfann.LINEAR) # train_data is loaded train_data = libfann.training_data() train_data.read_train_from_file(fname_data_train) # test_data is loaded test_data = libfann.training_data() test_data.read_train_from_file(fname_data_test) train_mse = list() test_mse = list() for ii in range(n_iter): # Training is performed with training data ann.reset_MSE() ann.train_on_data(train_data, max_iterations, iterations_between_reports, desired_error) # Testing is performed with test data ann.reset_MSE() ann.test_data(train_data) mse_train = ann.get_MSE() train_mse.append(mse_train) # Testing is performed with test data ann.reset_MSE() ann.test_data(test_data) mse_test = ann.get_MSE() test_mse.append(mse_test) if disp: print ii, "MSE of train, test", mse_train, mse_test ann.save(fname) # We show the results of ANN training with validation. if graph: plot(train_mse, label='train') plot(test_mse, label='test') legend(loc=1) xlabel('iteration') ylabel('MSE') grid() show() return train_mse, test_mse
info = (f.readline()).split(' ') num_inputs = int(info[1]) num_outputs = int(info[2]) f.close() ## Settings learning_rate = 0.1 momentum = 0.95 desired_error = 0.00001 iterations_between_reports = 100 maximum_iterations = 10000 ## Determine number of hidden nodes num_hiddens1 = roundup(num_inputs * 0.95) num_hiddens2 = roundup(num_inputs * 0.85) # Create network ann = lf.neural_net() ann.create_standard_array( [num_inputs, num_hiddens1, num_hiddens2, num_outputs]) ann.set_learning_rate(learning_rate) ann.set_learning_momentum(momentum) ann.set_activation_function_hidden( lf.SIGMOID_SYMMETRIC ) #This is to ensure negative and positive influences can be detected ann.set_activation_function_output(lf.SIGMOID) #Ensure output isnt negative ann.set_bit_fail_limit(0.01) # Train ann.train_on_file(data_file, maximum_iterations, iterations_between_reports, desired_error) ann.save(saved_net_file)
def main(): mfcc_coeff_vectors_dict = {} for i in range(1, 201): extractor = FeatureExtractor('../../../Dataset/Happiness/HappinessAudios/' + str(i) + '.wav') mfcc_coeff_vectors = extractor.calculate_mfcc() mfcc_coeff_vectors_dict.update({str(i) :(mfcc_coeff_vectors, mfcc_coeff_vectors.shape[0])}) for i in range(201, 401): extractor = FeatureExtractor('../../../Dataset/Sadness/SadnessAudios/' + str(i - 200) + '.wav') mfcc_coeff_vectors = extractor.calculate_mfcc() mfcc_coeff_vectors_dict.update({str(i) :(mfcc_coeff_vectors, mfcc_coeff_vectors.shape[0])}) processed_mfcc_coeff = preprocess_input_vectors(mfcc_coeff_vectors_dict, 0) # Prepare training dataset # train_data_file = open("training_data/fann/training_data.data", "w") # train_data_file.writelines("194226 " + str(36) + " 1\n") # for i in range(1, 151): # for each_vector in processed_mfcc_coeff[str(i)]: # train_data_file.writelines((" ").join(map(str, each_vector)) + "\n") # train_data_file.writelines("1\n") # # for i in range(201, 350): # for each_vector in processed_mfcc_coeff[str(i)]: # train_data_file.writelines((" ").join(map(str, each_vector)) + "\n") # train_data_file.writelines("0\n") # # for each_vector in processed_mfcc_coeff[str(350)]: # train_data_file.writelines((" ").join(map(str, each_vector)) + "\n") # train_data_file.writelines("0") # # train_data_file.close() # # print "Data prepared...." # # connection_rate = 1 # learning_rate = 0.01 # num_input = 36 # num_hidden = 6 # num_output = 1 # # desired_error = 0.0001 # max_iterations = 500 # iterations_between_reports = 100 # # # ann = libfann.neural_net() # ann.create_sparse_array(connection_rate, (num_input, num_hidden, num_output)) # ann.set_learning_rate(learning_rate) # ann.set_activation_function_output(libfann.SIGMOID_SYMMETRIC_STEPWISE) # # ann.train_on_file("training_data/fann/training_data.data", max_iterations, iterations_between_reports, desired_error) # # ann.save("network_states/fann/fann.net") # print "done!" # Create neural network from file ann = libfann.neural_net() ann.create_from_file("network_states/fann/fann.net") # A trained network ll be loaded # Test for happiness detection print "*" * 30, "Happiness Detection", "*" * 30 counter = { 'happiness': 0, 'sadness': 0 } for i in range(1, 151): mfcc_coeff_vectors = processed_mfcc_coeff[str(i)] frame_level_values = [] for each_vector in mfcc_coeff_vectors: output = ann.run(each_vector) if np.array(output) > np.array([0.5]): frame_level_values.append("happiness") else: frame_level_values.append("sadness") labels_count = Counter(frame_level_values) label = max(labels_count.iteritems(), key=operator.itemgetter(1))[0] print str(i) + ".wav: " + label counter[label] = counter[label] + 1 print print counter print # # # This is test for sadness detection print "*" * 30, "Sadness Detection", "*" * 30 counter = { 'happiness': 0, 'sadness': 0 } for i in range(201, 351): mfcc_coeff_vectors = processed_mfcc_coeff[str(i)] frame_level_values = [] for each_vector in mfcc_coeff_vectors: output = ann.run(each_vector) if np.array(output) > np.array([0.5]): frame_level_values.append("happiness") else: frame_level_values.append("sadness") labels_count = Counter(frame_level_values) label = max(labels_count.iteritems(), key=operator.itemgetter(1))[0] print str(i - 200) + ".wav: " + label counter[label] = counter[label] + 1 print print counter