def nn_controller(filename, activation, keras=False): """ Return the network controller function """ if not keras: filename = 'nn_retrained/' + filename # Obtain the trained parameters and assign the value to res with open(filename) as inputfile: lines = inputfile.readlines() length = len(lines) res = np.zeros(length) for i, text in enumerate(lines): res[i] = eval(text) # Set the controller NN_controller = NN(res, activation) controller = NN_controller.controller else: # load json and create model json_file = open('model.json', 'r') loaded_model_json = json_file.read() json_file.close() loaded_model = model_from_json(loaded_model_json) # load weights into new model loaded_model.load_weights('model.h5') print("Loaded kera model from disk.") NN_controller = NN(keras=True, model=loaded_model) controller = NN_controller.keras_model return controller
def testDataAssignedCorrectly(self): NN.pat = zip(self.trn_d['input'], self.trn_d['target']) pyb_ws = self.net.params.copy() nn = NN() nn.wi = pyb_ws[:nn.wi.size].reshape(NN.nh, NN.ni).T nn.wo = pyb_ws[nn.wi.size:].reshape(NN.no, NN.nh).T correct = 0 wrong = 0 all_aos = [] for i, x in enumerate(self.trn_d['input']): nn.activate(x) out = self.net.activate(x) all_aos.append(nn.ao) if not (out - self.trn_d['target'][i]).any(): correct += 1 else: wrong += 1 for i in range(len(array(NN.pat)[:, 0])): npt.assert_array_equal(self.trn_d['input'][i], array(NN.pat)[:, 0][i]) npt.assert_array_equal(self.trn_d['input'][i], array(nn.pat)[:, 0][i]) npt.assert_array_equal(self.trn_d['target'][i], array(NN.pat)[:, 1][i]) npt.assert_array_equal(self.trn_d['target'][i], array(nn.pat)[:, 1][i])
def main(_): pp.pprint(flags.FLAGS.__flags) #gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.333) run_config = tf.ConfigProto() #run_config.gpu_options.allow_growth=True # Data initialisation data_path = os.path.join(FLAGS.data_dir, FLAGS.dataset, FLAGS.input_fname_pattern) data = ImageDataset(regex_file = data_path, input_height = FLAGS.input_height, input_width = FLAGS.input_width) with tf.Session() as sess : net = NN(FLAGS.input_height*FLAGS.input_width*3, 2, hidden_layers = [60,10, 30], learning_rate = FLAGS.learning_rate, build = True, name = 'FCNeuralNet') model = Classifier1D(sess, [net] ,dataset, checkpoint_dir= FLAGS.checkpoint_dir, model_name = FLAGS.model_name) old, step = model.load(FLAGS.checkpoint_dir) print("(*) Already train : {} {}".format(old, ("--> step "+str(step) if old else ""))) if FLAGS.train: model.train(FLAGS) else: if not model.load(FLAGS.checkpoint_dir)[0]: raise Exception("(!) Train a model first, then run test mode")
def testPercentErrorIsSame(self): NN.pat = zip(self.trn_d['input'], self.trn_d['target']) pyb_ws = self.net.params.copy() nn = NN() nn.wi = pyb_ws[:nn.wi.size].reshape(NN.nh, NN.ni).T nn.wo = pyb_ws[nn.wi.size:].reshape(NN.no, NN.nh).T correct = 0 wrong = 0 argmax_cor = 0 argmax_wng = 0 all_aos = [] for i, x in enumerate(self.trn_d['input']): nn.activate(x) out = self.net.activate(x) # print 'ga bp trg', nn.ao, out, self.trn_d['target'][i], '++++' if not (out - self.trn_d['target'][i]).any() else '-' all_aos.append(nn.ao.copy()) if not (out - self.trn_d['target'][i]).any(): correct += 1 else: wrong += 1 if argmax(out) == argmax(self.trn_d['target'][i]): argmax_cor += 1 else: argmax_wng += 1 print 'actual', wrong, 'wrong', correct, 'correct', float(wrong) / ( wrong + correct) * 100 print 'using argmax', argmax_wng, 'wrong', argmax_cor, 'correct', float( argmax_wng) / (argmax_wng + argmax_cor) * 100 argmax_perc_err = float(argmax_wng) / (argmax_wng + argmax_cor) * 100 res = nn.sumErrors() nn_perc_err = 100 - res[1] pb_nn_perc_err = percentError(self.trainer.testOnClassData(), self.trn_d['class']) self.assertAlmostEqual(nn_perc_err, pb_nn_perc_err) self.assertAlmostEqual(nn_perc_err, pb_nn_perc_err, argmax_perc_err)
def setUp(self): weights_in = -2 + np.random.rand(NN.ni, NN.nh) * 4 weights_out = -2 + np.random.rand(NN.nh, NN.no) * 4 self.net1 = NN() self.net1.wi = weights_in.copy() self.net1.wo = weights_out.copy() self.net2 = NN() self.net2.wi = weights_in.copy() self.net2.wo = weights_out.copy() inputs = -2 + np.random.rand(4) * 8 for i in range(NN.ni): self.net1.ai[i] = np.tanh(inputs[i]) for j in range(NN.nh): self.net1.ah[j] = sigmoid(sum([self.net1.ai[i] * self.net1.wi[i][j] for i in range(NN.ni)])) for k in range(NN.no): self.net1.ao[k] = sum([self.net1.ah[j] * self.net1.wo[j][k] for j in range(NN.nh)]) self.net1.ao = np.where(self.net1.ao > 0.5, 1.0, 0.0) self.net2.activate(inputs)
def objective(x, v): controller = NN(x).controller trackers = [] for i in range(N_TRAIN_DIRECTIONS): for traj in paths: trackers.append( [x, v, i * 2 * pi / N_TRAIN_DIRECTIONS, traj]) costs = pool.map(function_x, trackers) return max(costs)
def main(): """ Fresh population of NN individuals are evalutated, paired with their fitness scores and ordered by fitness in descending order (fittest first) """ # Rank first random population pop = createPop() pairedPop = pairPop(pop) # ordered by fitness in descending order rankedPop = sorted(pairedPop, key=itemgetter(-1), reverse=True) # tst.correctlyranked(rankedPop) # Keep iterating new pops until max_iterations iters = 0 tops, avgs = [], [] while iters != max_iterations: if iters % 1 == 0: print 'Iteration'.rjust(150), iters newpopW = evolveNewPop(rankedPop) rankedPop, toperr, avgerr = rankPop(newpopW, pop) tops.append(toperr) avgs.append(avgerr) iters += 1 # test a NN with the fittest weights tester = NN() fittestWeights = rankedPop[0][0] tester.assignWeights(fittestWeights) results, targets = tester.test(testpat) title2 = 'Test after ' + str(iters) + ' iterations' plt.title(title2) plt.ylabel('Node output') plt.xlabel('Instances') plt.plot(results, 'xr', linewidth=1.5, label='Results') plt.plot(targets, 's', color='black', linewidth=3, label='Targets') plt.legend(loc='lower right') plt.figure(2) plt.subplot(121) plt.title('Top individual error evolution') plt.ylabel('Inverse error') plt.xlabel('Iterations') plt.plot(tops, '-g', linewidth=1) plt.subplot(122) plt.plot(avgs, '-g', linewidth=1) plt.title('Population average error evolution') plt.ylabel('Inverse error') plt.xlabel('Iterations') plt.show() print 'max_iterations',max_iterations,'\tpop_size', \ NN.pop_size,'pop_size*0.15', \ int(NN.pop_size*0.15),'\tmutation_rate', \ NN.mutation_rate,'crossover_rate', \ NN.crossover_rate,'ni, nh, no', NN.ni, NN.nh, NN.no
def testFittestInd(rankedPop): tester = NN() fittestWeights = rankedPop[0][0] tester.assignWeights(fittestWeights) count = 0 for i, t in tester.test_pat: o = tester.activate(i) print i, o, t, '++' if list(o) == list(t) else '----' count += 1 if list(o) == list(t) else -1 print float(count) / len(tester.test_pat) err, per = tester.sumErrors(test_data=True) print per return err, per
def testWeightsAndActivationsEquivalent(self): pyb_ws = self.net.params nn = NN() nn.wi = pyb_ws[:nn.wi.size].reshape(NN.nh, NN.ni).T nn.wo = pyb_ws[nn.wi.size:].reshape(NN.no, NN.nh).T for i, x in enumerate(self.trn_d['input']): nn.activate(x) out = self.net.activate(x) npt.assert_array_equal(nn.ai, self.net['in'].outputbuffer[0]) # self.assertItemsEqual(list(nn.ah), list(self.net['hidden0'].outputbuffer[0])) for j, pb_ah in enumerate(self.net['hidden0'].outputbuffer[0]): self.assertAlmostEqual(nn.ah[j], pb_ah) for k, pb_ao in enumerate(out): self.assertAlmostEqual(nn.ao[k], pb_ao)
def testFittestInd(rankedPop): tester = NN() fittestWeights = rankedPop[0][0] tester.assignWeights(fittestWeights) right = 0 for i, t, c, l in tester.test_pat: o = tester.activate(i) print i, o, t, '++' if list(o) == list(t) else '----' right += 1 if list(o) == list(t) else 0 print right, len(tester.test_pat) print 100 * (float(right) / len(tester.test_pat)) err, per = tester.sumErrors(test_data=True) print per return err, per
def nn_controller_details(filename, activation, reuse=False): """ Return weights and bias """ filename = 'nn/' + filename with open(filename) as inputfile: lines = inputfile.readlines() length = len(lines) res = np.zeros(length) for i, text in enumerate(lines): res[i] = eval(text) # Set the controller NN_controller = NN(res, activation, reuse=reuse) return NN_controller
def testFittestInd(rankedPop): tester = NN() fittestWeights = rankedPop[0][0] tester.assignWeights(fittestWeights) return tester.sumErrors(test_data=True)
def setUp(self): self.nn = NN() self.in_to_hidden = self.nn.wi self.hidAstroL = AstrocyteLayer(self.nn.ah, self.in_to_hidden)
def setUp(self): self.net = NN()
-0.8418091583257694, 0.4202498209051319, 0.5564186113435203, 0.555311886669758, -0.832890394661856, -0.98009859172038, -0.39445237156212176, -0.037779768457022134, -0.012204472378917697, 0.03488907526535291, 0.7708496093822486, -0.8784655950195925, 0.8746477155355499, -0.8575269090735795, -0.29599438495441377, -0.6668498658177002, -0.6385950413298903, 0.9697562635185897, 0.8786459903244533, 0.6482911089121831, -0.11993920927832091, -0.772720183218961, 0.17790750188249205, -0.18653760475670889, 0.3960711998381077, -0.0033684132198099626, -0.21067234485329456, -0.3753479446358803, 0.055153910155083516, -0.16949742782793037, -0.9052446924824077, 0.380809384299709, -0.8306618551590639, -0.2029395897397982, 0.6109089072465596, 0.9753912435095556, 0.9997622353974134, 0.1909207578297727, -0.6091875451850151, -0.7803408332873301, -0.004608696712309346, -0.19606055624248586 ]) NN_controller = NN(x).controller #simple = Simple_controller().controller track = Tracker(1., 0, traj, NN_controller) plt.plot(pathListx, pathListy, '-') cost, carListx, carListy = track.run() plt.plot(carListx, carListy, '-') plt.show() print cost #not perfect bc of float error y=x # # #case 2 # c = Car(0., 0., 0., 1.) # pathListx = [0] # pathListy = [0] # for i in range(5): # delta_theta = 0 # x, y, _ = c.update(delta_theta)
def createPop(): return [NN() for i in range(NN.pop_size)]
def function_x(args): x, v, dir, traj = args controller = NN(x).controller traj = Trajectory(traj) tracker = Tracker(v, dir, traj, controller) return tracker.run()[0]
[x, v, i * 2 * pi / N_TRAIN_DIRECTIONS, traj]) costs = pool.map(function_x, trackers) return max(costs) x = 2 * rand(1, 4 * N_NEURONS + 1)[0] - 1 res = fmin2(objective, x, .5, args=(VELOCITY, ), options={ 'popsize': 256, 'bounds': [-1, 1], 'maxiter': 256 }) # 5th is mean of final sample distribution res = res[1].result[0] controller = NN(res).controller trackers = [] for i in range(N_TRAIN_DIRECTIONS): for t in paths: traj = Trajectory(t) orientation = i * 2 * pi / N_TRAIN_DIRECTIONS trackers.append( Tracker(VELOCITY, orientation, traj, controller)) traces = map(lambda x: x.run(), trackers) for i, trace in enumerate(traces): plt.plot(trace[1], trace[2], ':', color=hsv_to_rgb(linspace(0, 1, len(traces))[i], 1, 1)) for path in paths: plt.plot(*zip(*path))
def setUp(self): self.nn = NN() self.inputs = [1, 2, 3, 4] self.nn.activate(self.inputs)
self.train_labels = dataset[0][1] self.valid_labels = dataset[1][1] self.test_labels = dataset[2][1] self.n_features = self.train_data.shape[1] self.n_categories = np.unique(self.train_labels).shape[0] if __name__ == '__main__': moon = moon() #1 et 2 nn = NN(2, 4, 2) normal = nn.bprop_slow(moon.train_data[:1], moon.train_labels[:1]) numerical = nn.FD_gradients(moon.train_data[:1], moon.train_labels[:1]) print("\n" "-----Gradient par les formules-----" + "\n") print(sorted(normal.items())) print("\n" + "-----Gradient par difference finie-----" + "\n") print(sorted(numerical.items())) #3 et 4 normal = nn.bprop_slow(moon.train_data[:10], moon.train_labels[:10]) numerical = nn.FD_gradients(moon.train_data[:10], moon.train_labels[:10]) print("\n" "-----Gradient par les formules-----" + "\n")
def setUp(self): self.nn = NN()
plt.ylabel("skewness") #plt.show() processed_data = list(zip(mean, kurt)) training_data = np.reshape(training_data, [16508, 3]) print(training_data) print(np.shape(training_data)) data_cluster = Cluster(processed_data, training_data, ideal_data) feature_sets, labels = data_cluster.cluster() feature_sets = np.asarray(feature_sets) print(np.shape(feature_sets)) plt.figure(figsize=(10, 10)) plt.scatter(feature_sets[:, 2], feature_sets[:, 0], c=feature_sets[:, 3], cmap='rainbow') plt.show() ideal_label = max(labels) + 1 print(np.shape(feature_sets)) print(feature_sets) fault_predictor = NN(feature_sets, 1500, ideal_label, 256, 0.1, 'tanh', 10) fault_predictor.processData() fault_predictor.buildModel() fault_predictor.trainModel()