target_fields = ['cnt', 'casual', 'registered'] features, targets = data.drop(target_fields, axis=1), data[target_fields] test_features, test_targets = test_data.drop( target_fields, axis=1), test_data[target_fields] # Hold out the last 60 days or so of the remaining data as a validation set train_features, train_targets = features[:-60 * 24], targets[:-60 * 24] val_features, val_targets = features[-60 * 24:], targets[-60 * 24:] iterations = 100 learning_rate = 0.5 hidden_nodes = 4 output_nodes = 1 N_i = train_features.shape[1] network = NeuralNetwork(N_i, hidden_nodes, output_nodes, learning_rate) losses = {'train': [], 'validation': []} for ii in range(iterations): # Go through a random batch of 128 records from the training data set batch = np.random.choice(train_features.index, size=128) X, y = train_features.ix[batch].values, train_targets.ix[batch]['cnt'] network.train(X, y) # Printing out the training progress train_loss = MSE( network.run(train_features).T, train_targets['cnt'].values) val_loss = MSE(network.run(val_features).T, val_targets['cnt'].values) sys.stdout.write("\rProgress: {:2.1f}".format(100 * ii / float(iterations)) \ + "% ... Training loss: " + str(train_loss)[:5] \
import json import io import sys sys.path.append('../') from NeuralNetwork import NeuralNetwork import numpy as np net = NeuralNetwork(2, 1, 20, 1) input = np.array([1, 1]) target = np.array([1, 1]) # output = net.predict(input) # net.learn(input, target) # Define dataset X = np.array([[0, 0], [0, 1], [1, 0], [1, 1]]) y = np.array([[0], [1], [1], [0]]) print("Training....") # Train the neural network errors = net.train(X, y, 0.3, 10000) # print('Accuracy: %.2f%%' % (net.accuracy(net.predict(X), y.flatten()) * 100)) print("Predicting....") for i in X: output = net.predict(i) print(output) for i in range(0, 30):
import random import numpy as np from mnist import MNIST from NeuralNetwork import NeuralNetwork # parameters learning_rate = 3 num_training_examples_per_epoch = 100 num_epochs = 100000 num_reports = 20 network = NeuralNetwork([784, 80, 20, 10]) # setup mnist_data = MNIST('mnist_data') mnist_data.gz = True training_images, training_labels = mnist_data.load_training() testing_images, testing_labels = mnist_data.load_testing() training_images = np.array( training_images) / 255 # normalization to prevent calculation overflow testing_images = np.array(testing_images) / 255 # trains a neural network to identify handwritten digits def main(): print(evaluate_network()) for i in range(num_epochs): images, labels = get_random_training_examples() network.train_on_minibatch(images, [one_hot(label) for label in labels], learning_rate) if i % (num_epochs // num_reports) == 0:
while x2 <= yEnd: arr.append([x1, x2]) x2 += xi x1 += yi nn.feedForward(np.array(arr)) for i in range(len(nn.layer[len(nn.layerN) - 1])): if nn.layer[3][i][0] > 0.5: plt.plot(nn.layer[0][i][0], nn.layer[0][i][1], 'rs') else: plt.plot(nn.layer[0][i][0], nn.layer[0][i][1], 'bs') plt.show() data = np.genfromtxt('DoubleMoon1.txt', delimiter=' ') # input data x = np.array([[data[i][j] for j in range(2)] for i in range(len(data))]) # np.array(data[:,:2]) # output data y = np.array([[data[i][2]] for i in range(len(data))]) n = NeuralNetwork([2, 6, 10, 6, 1]) n.train(x, y, epochs=200, batchSize=100) # print output layer after training n.feedForward(x) print(n.layer[len(n.layerN) - 1]) print(n.getAccuracy(x, y)) printBoundary(n, -6, 6, -6, 6, 0.5, 0.5)
def init(): "Initializes the neural network and the database." NeuralNetwork(nn_name, nn_structure, rewrite=True) return
filepath = "/Volumes/external/NBA_Data/NN_Each_Game_Distribution_4_15_ready.csv" header = None label_transformation = "L" ''' x = np.array([[0,0,0], [0,0,1], [0,1,0], [0,1,1], [1,0,0], [1,0,1], [1,1,1], [1,1,0]]) y = np.array([[0], [0], [0], [0], [1], [1], [1], [1]]) ''' dimension = ast.literal_eval(sys.argv[1]) # list #dimension = [4, 4] iteration = int(sys.argv[2]) # int nn = NeuralNetwork(filepath, -1, dimension, iteration, infer_header=header, label_transformation=label_transformation) nn.train() #print(nn.summary()) #print(nn.total_error()) #print(nn.sme())
#切分训练集和测试集 ''' random_state:伪随机数生成器 ''' X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=0.2, random_state=1) #对标记进行二值化,比如0000000000代表数字0, 0100000000代表数组1, 0010000000代表数字2,依次化为该形式 labels_train = LabelBinarizer().fit_transform(y_train) ###########构造神经网络模型################ #构建神经网络结构 #因为构造出的图片是14*14的 所以NeuralNetwork的第一个参数是14*14 不然矩阵相乘会错误 nn = NeuralNetwork([14 * 14, 100, 40, 10], 'logistic') #训练模型 nn.fit(X_train, labels_train, learning_rate=0.2, epochs=100) #保存模型 joblib.dump(nn, 'model/nnModel.m') #加载模型 # nn = joblib.load('model/nnModel.m') ###############数字识别#################### #存储预测结果 predictions = [] #对测试集进行预测 y_test = np.array(y_test) for i in range(y_test.shape[0]): out = nn.predict(X_test[i]) '''
import numpy as np from sklearn.datasets import load_digits from sklearn.metrics import confusion_matrix, classification_report from sklearn.preprocessing import LabelBinarizer from NeuralNetwork import NeuralNetwork from sklearn.model_selection import train_test_split digits = load_digits() X = digits.data y = digits.target X -= X.min() # normalize the values to bring them into the range 0-1 X /= X.max() nn = NeuralNetwork([64, 100, 10], 'logistic') X_train, X_test, y_train, y_test = train_test_split(X, y) labels_train = LabelBinarizer().fit_transform(y_train) # 转换为 0000100 类型 labels_test = LabelBinarizer().fit_transform(y_test) print("start fitting") nn.fit(X_train, labels_train, epochs=3000) predictions = [] for i in range(X_test.shape[0]): # shape 行 o = nn.predict(X_test[i]) predictions.append(np.argmax(o)) print(confusion_matrix(y_test, predictions)) print(classification_report(y_test, predictions))
# **************************************************************************** # # # # ::: :::::::: # # main_nn.py :+: :+: :+: # # +:+ +:+ +:+ # # By: fbenneto <*****@*****.**> +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2018/03/06 21:52:43 by fbenneto #+# #+# # # Updated: 2018/03/06 21:52:43 by fbenneto ### ########.fr # # # # **************************************************************************** # # coding:utf-8 from NeuralNetwork import NeuralNetwork brain = NeuralNetwork(3, 4, 2) def main(): pass if __name__ == '__main__': main()
3th = The number of inputs of the neural network 4th = The path to the training set ''' ############################################################### ############################################################### #### SUN SPOT TRAINING AND TEST START ##### ############################################################### ############################################################### ''' The Neural Network is invoked to be trained with the sunspots ''' cnf = NNConfiguration(None, 1, [2, 1], 2, "./src/dataset/DEBUG1/DEBUG_1_DATASET.txt") nn = NeuralNetwork(cnf) ''' The Neural Network is invoked to test unclassified instances with the sunspots. (Production) ''' ''' cnf = NNConfiguration( None, 1, [21, 1], 11, "./src/dataset/good_sunspot_test.dat", "./WEIGHTS_May_12_19_35.txt") ''' ''' nn = NeuralNetwork(cnf) '''
publish_action = rospy.Publisher('get_action', Float32MultiArray, queue_size=5) # store the action taken and the result action_data = Float32MultiArray() result = Float32MultiArray() state_space_size = 28 num_of_actions = 5 parameter_dictionary = None environment = Environment(num_of_actions) neural_network = NeuralNetwork(state_space_size, num_of_actions) scores, episodes = [], [] global_step = 0 start_time = time.time() for episode in range(neural_network.load_episode + 1, EPISODES): done = False # reset the environment first state = environment.reset() score = 0 for step in range(neural_network.episode_step): action = neural_network.getAction(state) # make a move next_state, reward, done = environment.step(action)
design_pipeline = ColumnTransformer([ ("scaler", StandardScaler(), num_attributes), ("onehot", OneHotEncoder(categories="auto"), cat_attributes) ], remainder="passthrough" ) designMatrix_prepared = design_pipeline.fit_transform(designMatrix) # exporting labels to a numpy array labels = df.loc[:, df.columns == 'default payment next month'].to_numpy().ravel() seed = 42 input_neurons = designMatrix_prepared.shape[1] layers = [input_neurons, 20, 20, 1] network = NeuralNetwork(layers, Sigmoid()) rate_range = np.logspace(-2.5, 0.3, 25, dtype=float) batch_range = np.logspace(0, 3, 10, dtype=int) # run tune hyperparameter funcition df_tuned = pf.tune_hyperparameter(designMatrix_prepared, labels, network, seed, rate_range, batch_range, n_epochs=10, test=None )
try: df = pd.read_csv(sys.argv[1], header=None) df = df.drop(df.columns[0], axis=1) except: exit("Error: Something went wrong with the dataset") label = df.iloc[:, 0].tolist() Y = np.array( [np.array([1, 0]) if x == 'M' else np.array([0, 1]) for x in label]) df = df.iloc[:, 1:] X = pd.get_dummies(df).to_numpy() model = NeuralNetwork() model.load() prediction = model.predict(X, normalize=True) prediction = np.argmax(prediction, axis=1) real = np.argmax(Y, axis=1) pred_test = model.predict(X) count = 0 for i in range(len(prediction)): if prediction[i] == real[i]: count += 1 print("Accuracy: ", count / len(prediction))
import numpy as np import time import matplotlib.pyplot as plt from NeuralNetwork import NeuralNetwork if __name__ == "__main__": hidden_neurons = input("How many neurons in hidden layer? (1 - 3):\t") is_bias = input("With or without bias? (0 - 1):\t") if (is_bias == 0): is_bias = False else: is_bias = True network = NeuralNetwork(4, int(hidden_neurons), 4, is_bias) training_input = np.genfromtxt("training_input.txt") training_output = np.genfromtxt("training_input.txt") # TESTING start_time = time.time() for i in range(2500): network.train(training_input, training_output) delta_time = (str)("--- %s seconds ---" % (time.time() - start_time)) output = (network.predict(training_input)) # For Overleaf raport in *.tax: result = '' for i in range(4): for j in range(4): if j == 3: result = result + str(output[i][j])[:12] + "\\" + '\\' else:
x = np.sort(np.random.uniform(0, 1, n)) y = np.sort(np.random.uniform(0, 1, n)) x, y = np.meshgrid(x, y) z = np.ravel( f.FrankeFunction(x, y) + 0.1 * np.random.randn(x.shape[0], x.shape[1])) z = z.reshape(-1, 1) # set up the design matrix data = DataPrep() X = data.design_matrix(x, y, degree=1)[:, 1:] # split data in train and test and scale it X_train, X_test, z_train, z_test = data.train_test_scale(X, z) # set up the neural network network = NeuralNetwork(X_train.shape[1], neurons, n_outputs, cost.MSE()) array_batches = [1, 50, 100, 500, 1000, 3500, len(X_train)] array_epochs = [50, 100, 200, 300, 400, 500] mse_heatmap = np.zeros((len(array_batches), len(array_epochs))) index_array = np.arange(len(X_train)) for i, n_batches in enumerate(array_batches): n_batches = int(n_batches) for j, epoch in enumerate(array_epochs): epoch = int(epoch) network.create_layers(hidden_act, output_act, seed) for k in range(epoch): np.random.shuffle(index_array) X_minibatches = np.split(X_train[index_array], n_batches) z_minibatches = np.split(z_train[index_array], n_batches)
# allows to import NeuralNetwork lib from different dir import sys # inserts path to access NeuralNetwork lib sys.path.insert(0, '../src') from NeuralNetwork import NeuralNetwork from HiddenNeuron import HiddenNeuron from OutputNeuron import OutputNeuron # this example recreates Dr. Kubat's neural network # for testing purposes nn = NeuralNetwork(2, 2, 2) # updating weights of hidden neurons to the ones in the example nn.hiddenNeurons[0].weights[0] = -1.0 nn.hiddenNeurons[0].weights[1] = 0.5 nn.hiddenNeurons[1].weights[0] = 0.1 nn.hiddenNeurons[1].weights[1] = 0.7 # updating weights of output neurons to the ones in the example nn.outputNeurons[0].weights[0] = 0.9 nn.outputNeurons[0].weights[1] = 0.5 nn.outputNeurons[1].weights[0] = -0.3 nn.outputNeurons[1].weights[1] = -0.1 # make the same input list as in the example inp = [0.8, 0.1]
[-1,-1, 1], [ 1, 0, 1]]]] x = np.asarray(x).reshape(1,3,5,5) f = np.asarray(f).reshape(2,3,3,3) y = convolve_2d(x, f, [1, 0], 1, 1) print('a)', y) print('b)', np.fmax(0,y)) #print needed values. np.set_printoptions(precision=5) n = NeuralNetwork((3,5,5), 0, 100) n.addLayer('ConvolutionLayer', kernelSize=(3,3), numKernels=2, stride=1, padding=1, weights=[f,[1,0]], name='conv3', activation=2) n.addLayer('MaxPoolingLayer', kernelSize=2, stride=1, name='maxpool2') n.addLayer('FlattenLayer', name='flatten') n.addLayer('FullyConnected', numOfNeurons=2, name='fully connected 2', activation=1) n.calculate(x) for l in n.layers: print('layer: ') print(l.name) print('\toutput:\n ') print(l.out) #problem 3
# Import the test data file: with open('mnist_full_test.csv', 'r') as f: reader = csv.reader(f, delimiter=',', quoting=csv.QUOTE_NONNUMERIC) data = list(reader) data = np.array(data) # Split input and target values X_test, y_test = data[:, 1:], data[:, 0] print("data loaded") ''' define the model ''' clf = NeuralNetwork(hidden_layers=[500, 400, 300], epochs=30, seed=0, batch_size=64, learning_rate=0.3, min_learning_rate=0.05, learning_rate_decay=0.95, leaky_factor=0.0, deactivation_prob=[0.0, 0.5, 0.4, 0.3]) ''' fitting the model ''' start = time.time() # time the fitting for diagnostic purposes clf.fit(X_train, y_train) # build/fit tree end = time.time() # calculate total fitting time fit_time = end - start print("Fitting time: ", fit_time) # print total fitting time ''' evaluate predictive power ''' start = time.time() # time the predictions for diagnostic purposes y_pred = clf.predict(X_test) # predict values for testing set
'''简单非线性关系数据集测试(XOR)''' from NeuralNetwork import NeuralNetwork import numpy as np nn = NeuralNetwork([2, 2, 1], 'tanh') X = np.array([[0, 0], [0, 1], [1, 0], [1, 1]]) y = np.array([0, 1, 1, 0]) nn.fit(X, y) for i in [[0, 0], [0, 1], [1, 0], [1, 1]]: print(i, nn.predict(i))
def test_network(layers, lr, train_images, train_labels, test_images, test_labels, iterations, is_noisy): start_time = time.time() overll_performance = [] round = 1 for _ in range(iterations): input_nodes = 784 output_nodes = 10 # 2/3 * input_nodes? hidden_nodes = 517 hidden_layers = layers lr = lr activation_function = "sigmoid" network = NeuralNetwork(input_nodes, output_nodes, hidden_nodes, hidden_layers, lr, activation_function) # Training for index in range(len(train_images)): current_target = np.zeros(10) + 0.1 current_target[train_labels[index]] = 0.99 current_input = [] for index_w in range(28): for index_h in range(28): current_input.append( (train_images[0:60000][index][index_w][index_h] / 255.0 * 0.99) + 0.01) network.train(current_input, current_target) # Testing scorecard = [] for index in range(len(test_images)): current_test = [] for index_w in range(28): for index_h in range(28): current_test.append( (test_images[0:10000][index][index_w][index_h] / 255.0 * 0.99) + 0.01) if is_noisy: current_test = noisy("s&p", np.asarray(current_test)) res = network.forward_propagation(current_test) if np.argmax(res) == test_labels[index]: scorecard.append(1.0) else: scorecard.append(0.0) # Calculating performance performance = float(np.asarray(scorecard).sum()) / float( len(scorecard)) overll_performance.append(performance) print(round, " round(s) finished") print("This rounds performance= ", performance * 100) round += 1 end_time = (time.time() - start_time) / 60 the_real_performance = (np.asarray(overll_performance).sum() / float(len(overll_performance))) * 100 return end_time, the_real_performance
import numpy as np from NeuralNetwork import NeuralNetwork def convert_output(o): if (o >= .5): return 1 else: return 0 vout = np.vectorize(convert_output) nn = NeuralNetwork(layers=[8, 12, 8], activations=['relu', 'sigmoid']) #input data X = np.array([[0, 0, 1, 0, 1, 1, 0, 1], [0, 1, 1, 1, 1, 0, 0, 0], [1, 0, 1, 0, 0, 0, 1, 1], [1, 1, 1, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 1, 1, 0, 0, 0, 1, 0], [0, 1, 1, 0, 1, 0, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1]]) #ouput data y = np.array([[1, 1, 0, 1, 0, 0, 1, 0], [1, 0, 0, 0, 0, 1, 1, 1], [0, 1, 0, 1, 1, 1, 0, 0], [0, 0, 0, 0, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1], [1, 0, 0, 1, 1, 1, 0, 1], [1, 0, 0, 1, 0, 1, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]]) nn.train(X, y, step_size=0.1, epochs=10000) print("Output after training: ") print(vout(nn.fprop(X)))
from NeuralNetwork import NeuralNetwork from sklearn.preprocessing import LabelBinarizer from sklearn.model_selection import train_test_split from sklearn.metrics import classification_report from sklearn import datasets print("[INFO] loading MNIST (sample) dataset...") digits = datasets.load_digits() data = digits.data.astype("float") data = (data - data.min()) / (data.max() - data.min()) print("[INFO] samples: {}, dim: {}".format(data.shape[0], data.shape[1])) (trainX, testX, trainY, testY) = train_test_split(data, digits.target, test_size=0.25) trainY = LabelBinarizer().fit_transform(trainY) testY = LabelBinarizer().fit_transform(testY) print("[INFO] trainig network...") nn = NeuralNetwork([trainX.shape[1], 32, 16, 10]) print("[INFO] {}".format(nn)) nn.fit(trainX, trainY, epochs=1000) print("[INFO] evaluating network...") prediction = nn.predict(testX) prediction = predictions.argmax(axis=1) print(classification_report(testY.argmax(axis=1), predictions))
def query(input_values): nn = NeuralNetwork(nn_name, nn_structure) print(nn.feedforward(input_values)) return
y_column_idx = features_check[features_set]["y_column_idx"] start = time.time() df = pd.read_csv(features_file) ######## Append artificial data by number of consecutive characters feature ######## if 2 in features_check[features_set]["features"]: mal = df[df[df.columns[y_column_idx]] == 1].sample(500).copy() mal["2"] = mal["2"].apply(lambda x: x * random.randint(3, 9)) df = df.append(mal, ignore_index=True) ######################################## END ####################################### use_columns = features_check[features_set]["features"] use_columns.append(y_column_idx) new_df = df[df.columns[use_columns]] new_df = np.array(new_df.values) #create new neural network nn = NeuralNetwork(dataset=new_df, learning_rate=learning_rate, threshold=threshold, kfolds=n_splits, training_epochs=training_epochs, degree=degree) # build the nn, train it and try to predict nn.build() nn.train(verbose=1) scores = nn.predict() nn.save_model("ann_model_base_t") end = time.time() print("\nTraining time:") print(end - start)
def __initData(self): self.__paintBoard = PaintBoard(self) self.__model = NeuralNetwork()
# set firing rates firingRateZero = np.array([[0, 0, 0, 0, 0, 0]]) firingRateOne = np.array([[0.9, 0.1, 0.03, 0.02, 0.1, 0.6]]) firingRateTwo = np.array([[0.02, 0.03, 0.015, 0.7, 0.8, 0.0]]) firingRateThree = np.array([[0.02, 0.8, 0.03, 0.1, 0.02, 0.4]]) firingRateFour = np.array([[0.03, 0.5, 0.9, 0.03, 0.6, 0.02]]) # create lists to store total spikes for each neuron during each input state inputOne = [] inputTwo = [] inputThree = [] inputFour = [] # create neural network object network = NeuralNetwork(inputLayerSize, 8, 8, 4, learningConstant, False, "networkOutputTwo.txt") # learning loop for i in range(n): network.learningLoop(firingRateOne, lim, 'red', False) network.learningLoop(firingRateTwo, lim, 'blue', False) network.learningLoop(firingRateThree, lim, 'yellow', False) network.learningLoop(firingRateFour, lim, 'green', False) # loop with no input to clear neuron potentials network.learningLoop(firingRateZero, 100, 'white', False) # testing loop for i in range(k): inputOne.append( network.learningLoop(np.array([[0.9, 0.0, 0.05, 0.01, 0.15, 0.5]]),
def prepare_targets(nodes, digit): targets = numpy.zeros(nodes) + 0.01 targets[int(digit)] = 0.99 return targets # plot_data(all_values[1:]) i = 784 h = 64 o = 10 lr = 0.1 epochs = 3 n = NeuralNetwork(i, h, o, lr) for e in range(epochs): for index, entry in enumerate(train_set): if index % 5000 == 0: print(e, index) all_values = numpy.asfarray(entry.split(',')) inputs = scale_data(all_values[1:]) targets = prepare_targets(o, all_values[0]) n.train(inputs, targets) pass n.set_learning_rate(n.lr / 2.0) pass predictions = []
import numpy as np from DataLoader import DataLoader import configure # (learningRate, noOfEpochs, noOfHidNeur, noOfEigenValues) configure.setUpConfig(0.05, 500, 100, 100) fe = FeatureExtractor("generatedData/eigenfaces.csv", "generatedData/average_face.csv") #prepare data for training: dl = DataLoader(configure.config_global.modeTrain) dl.load_all_images() datasetTrain = fe.generate_dataset(dl.images) #train NN: nn = NeuralNetwork(configure.config_global.noOfEigenValues, configure.config_global.noOfHidNeur) nn.trainNetwork(datasetTrain) #prepare data for testing: dl = DataLoader(configure.config_global.modeTest) dl.load_all_images() dataset = fe.generate_dataset(dl.images) #classification: output_guess = [] for sample in dataset: desired = sample[-1] calculated = nn.classify(np.transpose(np.asmatrix(sample[:-1]))) output_guess += [desired == calculated] print("testing data result:", sum(output_guess) / np.shape(dataset)[0])
import numpy as np from NeuralNetwork import NeuralNetwork from Utils import sigmoid, sigmoid_derivative if __name__ == "__main__": X = np.array([[0, 0], [1, 0], [0, 1], [1, 1]]) y = np.array([[0], [1], [1], [0]]) nn = NeuralNetwork(X, y, sigmoid, sigmoid_derivative) for i in range(20000): nn.feedforward() nn.backprop() print(nn.output)
return sigma / (2 * sizeK) inFile = open(sys.argv[1], "r") arrU = [] arrV = [] arrU, arrV = readFile(inFile) inFile.close() sizeI = len(arrU[0]) sizeK = len(arrU) settings = (0, 0, 0, 0) neurone = NeuralNetwork(sizeI, 2, 1, 0.1, settings) error = 1 errX = [] errY = [] it = 0 while error > pow(10, -5): for k in range(sizeK): neurone.train(arrU[k], arrV[k]) err = [] for k in range(sizeK): err.append(neurone.query(arrU[k])) error = countError(err, arrV)