def analayzer(self): vvec = vct.vectorizer #Neural Net initializing net = SNN.NetCnstr(21, 12, 2) net.load_weights( r'C:\Python34\Weights_store\Demands\input_to_hidden.npy', 'input_to_hidden') net.load_weights( r'C:\Python34\Weights_store\Demands\hidden_to_output.npy', 'hidden_to_output') print('Weights are loaded successfully') text = self.txt.get(index1='1.0', index2='end-1c') self.txt.delete(index1='1.0', index2='end-1c') splitted = text.split('\n') estimation_holder = [] for i in splitted: marks = net.query(vvec(i)) if marks[0] > marks[1]: estimation_holder.append(marks[0]) else: estimation_holder.append(0) print(list(enumerate(estimation_holder))) result = estimation_holder.index(max(estimation_holder)) splitted[result] = '<DEM ' + splitted[result] + ' /DEM>' self.splitted = splitted self.insert_tabs(mode='analyze') self.txt.highlight_pattern('\<.+\>', 'green')
"train": { "X": X[train_idxs], "y_one_hot": y_one_hot[train_idxs], "y": y[train_idxs], }, "validation": { "X": X[validation_idxs], "y_one_hot": y_one_hot[validation_idxs], "y": y[validation_idxs], }, } myNetwork = SNN.Network([ SNN.Layer(784, 100, activation="ReLU"), SNN.Layer(100, 100, activation="ReLU"), SNN.Layer(100, 10, activation="sigmoid") ], loss="CategoricalCrossEntropyWithSoftmax", optimizer="GradientDescentWithMomentum") myNetwork.setParameters(lr=1) myNetwork.fit(data['train']['X'], data['train']['y_one_hot'], epochs=200, batch_size=1024) y_test = data['validation']['y'] y_pred_onehot = myNetwork.predict(data['validation']['X']) y_pred = np.argmax(y_pred_onehot, axis=1) print(classification_report(y_test, y_pred))
''' Model Test ''' input = tf.placeholder(tf.float32) input_exp = tf.exp(input) groundtruth = tf.placeholder(tf.float32) try: w1 = np.load('weight_nslkdd11.npy') w2 = np.load('weight_nslkdd12.npy') w3 = np.load('weight_nslkdd13.npy') layer_in = SNN.SNNLayer(312, 100, w1) layer_out1 = SNN.SNNLayer(100, 100, w2) layer_out2 = SNN.SNNLayer(100, 5, w3) print('Weight loaded!') except: layer_in = SNN.SNNLayer(312, 100) layer_out1 = SNN.SNNLayer(100, 100) layer_out2 = SNN.SNNLayer(100, 5) print('No weight file found, use random weight') layerin_out = layer_in.forward(input_exp) layerout_out1 = layer_out1.forward(layerin_out) layerout_out2 = layer_out2.forward(layerout_out1) # the time of output nnout = tf.log(layerout_out2)
import numpy as np import os import matplotlib.pyplot as plt import SNN SAVE_PATH = os.getcwd() + '/weight_mnist' mnist = SNN.MnistData( path=["MNIST/t10k-images.idx3-ubyte", "MNIST/t10k-labels.idx1-ubyte"]) w1 = np.load(SAVE_PATH + '1.npy') w2 = np.load(SAVE_PATH + '2.npy') Ts = 1e-3 scale = 2 view_max = 2 l1 = SNN.SNNDiscrete(w1, Ts, scale) l2 = SNN.SNNDiscrete(w2, Ts, scale) xs, ys = mnist.next_batch(1, shuffle=True) xs = (1 - xs[0, :]) / Ts print(ys) input_mat = np.zeros([784, int(1 / Ts * view_max)]) input_mat[range(784), xs.astype(int)] = 1 l1out = l1.forward(input_mat) l2out = l2.forward(l1out) fg, ax = plt.subplots(nrows=2, ncols=1) for i in range(np.shape(l2.potential)[0]):
import matplotlib.pyplot as plt import SNN import data SAVE_PATH = os.getcwd() + '/weight_mnist' mnist = data.MNIST( path=["MNIST/t10k-images.idx3-ubyte", "MNIST/t10k-labels.idx1-ubyte"]) w1 = np.load(SAVE_PATH + '1.npy') w2 = np.load(SAVE_PATH + '2.npy') Ts = 1e-3 scale = 2 view_max = 2 l1 = SNN.SNNDiscrete(w1, Ts, scale) l2 = SNN.SNNDiscrete(w2, Ts, scale) correct = 0 for i in range(mnist.datasize): xs, ys = mnist.next_batch(1, shuffle=True) xs = (1 - xs[0, :]) / Ts input_mat = np.zeros([784, int(1 / Ts * view_max)]) input_mat[range(784), xs.astype(int)] = 1 l1out = l1.forward(input_mat) l2out = l2.forward(l1out) peak = np.argmax(l2out, axis=1)
import sys sys.path.append("..") import tensorflow as tf import numpy as np import os import SNN import data input = tf.placeholder(tf.float32) input_exp = tf.exp(input) groundtruth = tf.placeholder(tf.float32) layer_in = SNN.SNNLayer(784, 800) layer_out = SNN.SNNLayer(800, 10) layerin_out = layer_in.feedforward(input_exp) layerout_out = layer_out.feedforward(layerin_out) nnout = tf.log(layerout_out) layerout_groundtruth = tf.concat([layerout_out, groundtruth], 1) loss = tf.reduce_mean(tf.map_fn(SNN.loss_func, layerout_groundtruth)) wsc = layer_in.w_sum_cost() + layer_out.w_sum_cost() l2c = layer_in.l2_cost() + layer_out.l2_cost() K = 100 K2 = 1e-3 learning_rate = 1e-4 TRAINING_BATCH = 10
''' Model Test ''' input = tf.placeholder(tf.float32) input_exp = tf.exp(input) groundtruth = tf.placeholder(tf.float32) try: w1 = np.load('weight_awid1.npy') w2 = np.load('weight_awid2.npy') w3 = np.load('weight_awid3.npy') layer_in = SNN.SNNLayer(206, 100, w1) layer_out1 = SNN.SNNLayer(100, 100, w2) layer_out2 = SNN.SNNLayer(100, 4, w3) print('Weight loaded!') except: layer_in = SNN.SNNLayer(206, 100) layer_out1 = SNN.SNNLayer(100, 100) layer_out2 = SNN.SNNLayer(100, 4) print('No weight file found, use random weight') layerin_out = layer_in.forward(input_exp) layerout_out1 = layer_out1.forward(layerin_out) layerout_out2 = layer_out2.forward(layerout_out1) # the time of output nnout = tf.log(layerout_out2)
input_real = tf.placeholder(tf.float32) output_real = tf.placeholder(tf.float32) global_step = tf.Variable(1, dtype=tf.int64) step_inc_op = tf.assign(global_step, global_step + 1) ''' Here is a reshape, because TensorFlow DO NOT SUPPORT tf.extract_image_patches gradients operation for VARIABLE SIZE inputs ''' input_real_resize = tf.reshape(tf.exp(input_real),[TRAINING_BATCH,28,28,1]) try: w1 = np.load('weight_scnn1.npy') w2 = np.load('weight_scnn2.npy') w3 = np.load('weight_scnn3.npy') print('Done') layer1 = SNN.SCNNLayer(kernel_size=5,in_channel=1,out_channel=32,strides=2, w=w1) layer2 = SNN.SCNNLayer(kernel_size=5,in_channel=32,out_channel=16,strides=2, w=w2) layer3 = SNN.SNNLayer(in_size=784,out_size=10, w=w3) print('Weight loaded!') except: layer1 = SNN.SCNNLayer(kernel_size=5,in_channel=1,out_channel=32,strides=2) layer2 = SNN.SCNNLayer(kernel_size=5,in_channel=32,out_channel=16,strides=2) layer3 = SNN.SNNLayer(in_size=784,out_size=10) print('No weight file found, use random weight') layerout1 = layer1.forward(input_real_resize) layerout2 = layer2.forward(layerout1) layerout3 = layer3.forward(tf.reshape(layerout2,[TRAINING_BATCH,784])) wsc1 = layer1.kernel.w_sum_cost() wsc2 = layer2.kernel.w_sum_cost()
import sys sys.path.append("..") import tensorflow as tf import numpy as np import os import SNN input = tf.placeholder(tf.float32) input_exp = tf.exp(input) groundtruth = tf.placeholder(tf.float32) layer_in = SNN.SNNLayer(784, 800) layer_out = SNN.SNNLayer(800, 10) layerin_out = layer_in.forward(input_exp) layerout_out = layer_out.forward(layerin_out) nnout = tf.log(layerout_out) layerout_groundtruth = tf.concat([layerout_out, groundtruth], 1) loss = tf.reduce_mean(tf.map_fn(SNN.loss_func, layerout_groundtruth)) wsc = layer_in.w_sum_cost() + layer_out.w_sum_cost() l2c = layer_in.l2_cost() + layer_out.l2_cost() K = 100 K2 = 1e-3 learning_rate = 1e-4 TRAINING_BATCH = 10 SAVE_PATH = os.getcwd() + '/weight_mnist'
import sys import matplotlib.pyplot as plt sys.path.append("..") import SNN k = SNN.KittiData() d = k.getdata( "KITTI/2011_09_26/2011_09_26_drive_0005_sync/velodyne_points/data/0000000000.bin" ) print(d) plt.imshow(d) plt.show()
import ribosome import random as rd Generation_Num = int(input("Input Generation Number")) gene_list = [] for i in range(Generation_Num): gene_list = Genetic.Read_Gene() if (len(gene_list) == 0): import startup gene_score_list = [] selected_gene = [] descendants_gene = [] descendant_mutated = [] score_list = [] network_list = ribosome.Translate_Into_Networks(4, [1, 4], 2, -1) score_list = SNN.return_score(network_list, i) gene_score_list.append(score_list) selected_gene: list = NEAT.calc_and_select_gene(gene_score_list, gene_list) gene_num: int = int(len(gene_list) / 2) for k in range(gene_num): cross1 = rd.choice(selected_gene) cross2 = rd.choice(selected_gene) print(cross1, cross2) Agene1, Agene2 = NEAT.Crossover(cross1, cross2) descendants_gene.append(Agene1) descendants_gene.append(Agene2) for idx_gene, gene in enumerate(descendants_gene): descendant_mutated.append( NEAT.Mutate(gene, score_list[idx_gene], 1000000)) Genetic.Write_Gene(descendants_gene)
import sys import matplotlib.pyplot as plt import numpy as np sys.path.append("..") import SNN mnist = SNN.MnistData( path=["MNIST/train-images.idx3-ubyte", "MNIST/train-labels.idx1-ubyte"]) xs, ys = mnist.next_batch(batch_size=10, shuffle=True) for i in range(10): plt.imshow(np.reshape(xs[i], [28, 28])) print(ys[i]) plt.show()