def create_net (group_id, num_neurons_hidden): from pyfann import libfann from network_functions import save_tests from network_functions import create_net_record import MySQLdb as mdb print "Create Net - " + str(group_id) + ", " + str(num_neurons_hidden) # To Do # Needs to querry Net_Group for training file name, test filename, ticker, input_nurons, output_neurons con = mdb.connect('localhost', 'root', 'fil1202job', 'network') sql = "select a.ticker, a.train_data, a.test_data, b.no_input, b.no_output from network.net_group a, network.net_type b where a.type = b.id and a.id =" + str(group_id) cursor = con.cursor() cursor.execute(sql) for row in cursor.fetchall(): ticker = str(row[0]) train_file = str(row[1]) test_file = str(row[2]) num_input = row[3] num_output = row[4] # disconnect from server #con.close() #Parameters that will be passed in #group_id = 191 # create empty net record and get number net_id = create_net_record(group_id) print "Net ID = " + str(net_id) #train_file = "/home/user/Documents/TrainandTestdata/trainingdataFANN-ACN_out-train.dat" #test_file = "/home/user/Documents/TrainandTestdata/testdataFANN-ACN_out-train.dat" #ticker = "ACN" ### # create file name as ticker_netgroup_netnumber.net net_name = "/home/user/Documents/Networks/" + str(ticker) + "_" + str(group_id) + "_" + str(net_id) + ".net" sql2 = "UPDATE `network`.`network` SET `net_file` = \"" + net_name + "\" where id = " + str(net_id) #print sql2 cursor2 = con.cursor() cursor2.execute(sql2) con.commit() connection_rate = 1 learning_rate = 0.7 #num_input = 7 #num_neurons_hidden = 7 #num_output = 1 desired_error = 0.0001 max_iterations = 100000 iterations_between_reports = 10000 ann = libfann.neural_net() #ann.create_sparse_array(connection_rate, (num_input, num_neurons_hidden, num_output)) ann.create_standard_array([num_input, num_neurons_hidden, num_neurons_hidden, num_output]) ann.set_learning_rate(learning_rate) ann.set_activation_function_output(libfann.SIGMOID_SYMMETRIC_STEPWISE) train_data = libfann.training_data() train_data.read_train_from_file(train_file) test_data = libfann.training_data() test_data.read_train_from_file(test_file) ann.train_on_file(train_file, max_iterations, iterations_between_reports, desired_error) print "\nTrain error: %f, Test error: %f\n\n" %( ann.test_data(train_data),ann.test_data(test_data)) #print "Testing network again" ann.reset_MSE() vinput = test_data.get_input() output = test_data.get_output() for i in range(len(vinput)): #print "###" #print ann.test(vinput[i], output[i]) #print "###'" outval = ann.test(vinput[i], output[i]) outval = str(outval) outval = outval.translate(None, '[]') targetout = str(output[i]) #print "###" #print targetout targetout = targetout.translate(None, '[]') #print targetout # Store test output in net_test table as [netNumber, record(test row number), prediction, target] # Stillinserts into temp table net_tests2 save_tests(net_id, i + 1, outval, targetout) #printingstring = 'Output number ' + str(i) + ' is '+ outval + 'Should have been: ' + targetout #print printingstring #print "MSE error on test data: %f" % ann.get_MSE() ann.save(net_name)
train_data = libfann.training_data() train_data.read_train_from_file(train_file) test_data = libfann.training_data() test_data.read_train_from_file(test_file) ann.train_on_file(train_file, max_iterations, iterations_between_reports, desired_error) print "\nTrain error: %f, Test error: %f\n\n" %( ann.test_data(train_data),ann.test_data(test_data)) print "Testing network again" ann.reset_MSE() vinput = test_data.get_input() output = test_data.get_output() for i in range(len(vinput)): outval = ann.test(vinput[i], output[i]) outval = str(outval) outval = outval.translate(None, '[]') targetout = str(output[i]) targetout = targetout.translate(None, '[]') # Store test output in net_test table as [netNumber, record(test row number), prediction, target] # Stillinserts into temp table net_tests2 save_tests(net_id, i + 1, outval, targetout) printingstring = 'Output number ' + str(i) + ' is '+ outval + 'Should have been: ' + targetout print printingstring print "MSE error on test data: %f" % ann.get_MSE() ann.save(net_name)