def move_function(board): global net best_max_move = None max_value = -1000 best_min_move = None min_value = 1000 #value is the chance of black winning for m in board.get_moves(): nextboard = board.peek_move(m) value = net.activate(board_to_input(nextboard)) if value > max_value: max_value = value best_max_move = m if value < min_value: min_value = value best_min_move = m ds = SupervisedDataSet(97, 1) best_move = None #active player if board.active == BLACK: ds.addSample(board_to_input(board), max_value) best_move = best_max_move elif board.active == WHITE: ds.addSample(board_to_input(board), min_value) best_move = best_min_move trainer = BackpropTrainer(net, ds) trainer.train() NetworkWriter.writeToFile(net, 'CheckersMini/synapsemon_random_black_mini_140.xml') NetworkWriter.writeToFile(net, 'SynapsemonPie/synapsemon_random_black_mini_140_copy.xml') return best_move
def reinforce(self, feedback, state=None, replace_last=False, end=None): """ Processes a feedback signal (e.g. reward or punishment). Applies a feedback to every state in the episode decayed proportionally to how long ago the action happened. The network will be taught to associate the state right before the feedback with the feedback most strongly, whereas a state at the beginning of the episode will have a very weak association. """ if not end: return self.rewards.append(feedback) # Build training set. #ds = SupervisedDataSet(inp=9, target=1) ds = SupervisedDataSet(inp=self.lengths[0], target=self.lengths[-1]) r0 = feedback for normalized_state in reversed(self.history): # print 'sample:',normalized_state,r0 ds.addSample(normalized_state, (r0,)) r0 *= self.lambda_decay trainer = BackpropTrainer( self.network, ds, learningrate=self.alpha) trainer.train()
def Predict(self, ticker, day): endDay = day-datetime.timedelta(1) startDay = endDay - datetime.timedelta(self.trainingPeriod) try: stockData = data.DataReader(ticker, 'yahoo', startDay, endDay) except: return [0] rawTrainFeatures = [] rawTrainResponses = [] for currentDay in range(self.windowLength, len(stockData)): window = stockData[currentDay-self.windowLength:currentDay] currentPrice = stockData.iloc[currentDay]['Open'] response = stockData.iloc[currentDay]['Close'] rawTrainFeatures.append(self.GetFeature(window)) rawTrainResponses.append(response) rawTestFeatures = self.GetFeature(stockData[len(stockData)-self.windowLength:len(stockData)]) # normalTrainFeatures, normalTestFeatures = self.NormalizeFeatures(rawTrainFeatures, rawTestFeatures) alldata = SupervisedDataSet(len(rawTrainFeatures[0]), 1) for index in range(0, len(rawTrainFeatures)): alldata.addSample(rawTrainFeatures[index],[rawTrainResponses[index]]) self.network = buildNetwork(alldata.indim, (alldata.indim+alldata.outdim)/2, alldata.outdim, hiddenclass=SigmoidLayer, outclass=LinearLayer) trainer = BackpropTrainer(self.network, dataset=alldata) activations = [] for i in range(50): for x in range(5): trainer.train() return float(self.network.activate(rawTestFeatures))
def getErrorPercent(training_dataset, eval_dataset_list, num_hidden, num_epochs): num_datapoints = len(training_dataset) num_inputs = len(training_dataset[0][0]) num_outputs = len(training_dataset[0][1]) # print "Num Inputs:", num_inputs # print "Num Outputs:", num_outputs # print "Num Hidden Nodes:", num_hidden NN = buildNetwork(num_inputs, num_hidden, num_outputs, bias=True, hiddenclass=SigmoidLayer, outclass=SigmoidLayer) dataset = SupervisedDataSet(num_inputs, num_outputs) for datapoint in training_dataset: dataset.addSample(datapoint[0], datapoint[1]) trainer = BackpropTrainer(NN, dataset=dataset, momentum=0.0, verbose=False, weightdecay=0.0) for epoch in range(0, num_epochs): #print epoch trainer.train() errors = [] for eval_set in eval_dataset_list: total_percent_errors = [0]*num_outputs for jj in range(0, len(eval_set)): nn_out = NN.activate(eval_set[jj][0]) percent_error = computeError(eval_set[jj][1], nn_out) #print percent_error total_percent_errors = map(operator.add, percent_error, total_percent_errors) #print total_percent_errors errors.append(map(operator.div, total_percent_errors, [len(dataset)]*num_outputs)) #print errors return errors
def train(self): ''' Perform batch regression ''' self.getTrainingData2() trainer = BackpropTrainer(self.net, self.ds) trainer.train()
def pybrain_high(): back=[] alldate=New_stock.objects.filter().exclude(name='CIHKY')[0:100] wholelen=len(alldate) test=New_stock.objects.filter(name__contains="CIHKY") testlen=len(test) # test dateset testdata= SupervisedDataSet(5, 1) testwhole=newalldate(test,testlen) for i in testwhole: testdata.addSample((i[0],i[2],i[3],i[4],i[5]), (0,)) # 实验 dateset data= SupervisedDataSet(5, 1) wholedate=newalldate(alldate,wholelen) for i in wholedate: data.addSample((i[0],i[2],i[3],i[4],i[5]), (i[1])) #print testwhole # 建立bp神经网络 net = buildNetwork(5, 3, 1,bias=True,hiddenclass=TanhLayer, outclass=SoftmaxLayer) trainer = BackpropTrainer(net,data) trainer.trainEpochs(epochs=100) # train and test the network # print trainer.train() trainer.train() print 'ok' out=net.activateOnDataset(testdata) for j in test: back.append((j.high)) print back print out backout=backnormal(back,out) print 'okokokoko' print backout # 输出22的测试集合 return out
def __init__(self, histogram_list): self.net = buildNetwork(1024, 100, 1) ds = SupervisedDataSet(1024, 1) for histogram in histogram_list: #print (histogram) ds.addSample(histogram, (1,)) for x in range(0,15): ds.addSample(numpy.random.random((1024)) * 255, (0,)) # this noise should never be a face #print (numpy.random.random((1024)) * 255) trainer = BackpropTrainer(self.net, ds) #trainer.trainUntilConvergence() for x in range(2000): print ("count:\t" + str(x) + "\terror:\t" + str(trainer.train())) #trainer.train() print (trainer.train()) """
class Brain: def __init__(self, hiddenNodes = 30): # construct neural network self.myClassifierNet = buildNetwork(12, hiddenNodes, 1, bias=True, hiddenclass=TanhLayer) #parameters to buildNetwork are inputs, hidden, output # set up dataset self.myDataset = SupervisedDataSet(12, 1) self.myClassifierTrainer = BackpropTrainer(self.myClassifierNet, self.myDataset) def addSampleImageFromFile(self, imageFile, groupId): "adds a data sample from an image file, including needed processing" myImage = Image.open(imageFile) self.myDataset.addSample(twelveToneParallel(myImage), (groupId,)) def train(self): #myClassifierTrainer.trainUntilConvergence() #this will take forever (possibly literally in the pathological case) for i in range(0, 15): self.myClassifierTrainer.train() #this may result in an inferior network, but in practice seems to work fine def save(self, saveFileName="recognizernet.brain"): saveFile = open(saveFileName, 'w') pickle.dump(self.myClassifierNet, saveFile) saveFile.close() def load(self, saveFileName="recognizernet.brain"): saveFile = open(saveFileName, 'r') myClassifierNet = pickle.load(saveFile) saveFile.close() def classify(self, fileName): myImage = Image.open(fileName) if self.myClassifierNet.activate(twelveToneParallel(myImage)) < 0.5: return 0 else: return 1
def learn_mfcc(mfcc, j_input, epochs = 50, inter_layer = 20): from pybrain.structure.modules import TanhLayer """ (np.array, np.array, [int], [int]) -> pybrain object Takes mfcc and joint input and returns a neural net trained using backpropagation. mfcc: mfcc object j_input: joint input, length of 0th dimension must be same as mfcc epochs: # of iterations (default 100) inter_layer: number of hidden layers (default 20) """ #print "Learning" n_mfcc = mfcc.shape[1] #j_input = np.concatenate((j_delta, j_0), axis = 1) dims = np.shape(j_input) #j_input = j_input/np.max(j_input) #mfcc = mfcc/np.max(mfcc) tr_ds = datasets.SupervisedDataSet(dims[1], n_mfcc) for i in range(0, dims[0]): tr_ds.addSample(j_input[i, :], mfcc[i, :]) #net = buildNetwork(1, 1, 1, bias=True) net = buildNetwork(dims[1], inter_layer, inter_layer, inter_layer, n_mfcc, bias=True)#, hiddenclass = TanhLayer) #print net.modules trainer = BackpropTrainer(net, tr_ds) #error = np.empty(epochs) for epo in range(0, epochs): trainer.train() #trainer.train() return net
def nnTest(tx, ty, rx, ry, iterations): print "NN start" print strftime("%a, %d %b %Y %H:%M:%S", localtime()) resultst = [] resultsr = [] positions = range(iterations) network = buildNetwork(16, 16, 1, bias=True) ds = ClassificationDataSet(16, 1, class_labels=["1", "0"]) for i in xrange(len(tx)): ds.addSample(tx[i], [ty[i]]) trainer = BackpropTrainer(network, ds, learningrate=0.05) validator = CrossValidator(trainer, ds, n_folds=10) print validator.validate() for i in positions: print trainer.train() resultst.append(sum((np.array([round(network.activate(test)) for test in tx]) - ty)**2)/float(len(ty))) resultsr.append(sum((np.array([round(network.activate(test)) for test in rx]) - ry)**2)/float(len(ry))) print i, resultst[i], resultsr[i] plt.plot(positions, resultst, 'g-', positions, resultsr, 'r-') plt.axis([0, iterations, 0, 1]) plt.ylabel("Percent Error") plt.xlabel("Network Epoch") plt.title("Neural Network Error") plt.savefig('nn.png', dpi=500) print "NN end" print strftime("%a, %d %b %Y %H:%M:%S", localtime())
def nn_predict(train, test, prediction_cols, to_predict, n_nodes, hiddenclass, learningrate, num_epochs, verbose = True): ds = make_pybrain_ds(train, pour_predire_cols, to_predict) ds_test = make_pybrain_ds(test, pour_predire_cols, to_predict) net = buildNetwork( ds.indim, n_nodes, ds.outdim, bias = True, hiddenclass = eval(hiddenclass)) trainer = BackpropTrainer(net, dataset=ds, learningrate= learningrate, lrdecay=1.0, momentum=0.0, verbose=False, batchlearning=False, weightdecay=0.0) if to_predict == 'place_geny': train = train[train.is_place] if verbose: print 'XXXXXXXXXXXXXXXXXXXXXXXXXX' print 'Predicting :', to_predict print 'n_nodes_1 :', n_nodes_1 print 'n_nodes_2 :', n_nodes_2 print 'Layer :', hiddenclass print 'learningrate :', learningrate for epoch in range(num_epochs): trainer.train() a = pd.DataFrame(net.activateOnDataset(ds_test)) a.columns = [to_predict + '_predict'] a.index = test.index test[to_predict + '_predict'] = a[to_predict + '_predict'] return (trainer, test)
def ffnn(lr, num_epochs, inp, out, hidden, hidden2=-1, rec=False, mom=0.0): ds = ClassificationDataSet(inp, out) for tr_x, tr_y in zip(train_X, train_Y): #print "DEBUG:", tr_x, tr_y ds.addSample(tr_x, tr_y) if hidden2 != -1: ffnn = build_2ffnn(inp, hidden, hidden2, out) elif rec==True: ffnn = build_rec(inp, hidden, out) else: ffnn = buildNetwork(inp, hidden, out, bias=True, hiddenclass=TanhLayer, outclass=SoftmaxLayer) trainer = BackpropTrainer(ffnn, ds,learningrate = lr, momentum=mom, weightdecay=0.0, verbose=True) ###### numEpochs = num_epochs for _ in range(0, numEpochs): trainer.train() if not triple_class: test_model(ffnn, train_X, train_Y) test_model(ffnn, test_X, test_Y) # else: # test_multiclass(ffnn, train_X, train_Y) # test_multiclass(ffnn, test_X, test_Y) return ffnn
def nntester(tx, ty, rx, ry, iterations): """ builds, tests, and graphs a neural network over a series of trials as it is constructed """ resultst = [] resultsr = [] positions = range(iterations) network = buildNetwork(100, 50, 1, bias=True) ds = ClassificationDataSet(100,1, class_labels=["valley", "hill"]) for i in xrange(len(tx)): ds.addSample(tx[i], [ty[i]]) trainer = BackpropTrainer(network, ds, learningrate=0.01) for i in positions: print trainer.train() resultst.append(sum((np.array([round(network.activate(test)) for test in tx]) - ty)**2)/float(len(ty))) resultsr.append(sum((np.array([round(network.activate(test)) for test in rx]) - ry)**2)/float(len(ry))) print i, resultst[i], resultsr[i] NetworkWriter.writeToFile(network, "network.xml") plt.plot(positions, resultst, 'ro', positions, resultsr, 'bo') plt.axis([0, iterations, 0, 1]) plt.ylabel("Percent Error") plt.xlabel("Network Epoch") plt.title("Neural Network Error") plt.savefig('3Lnn.png', dpi=300)
def makeNet(learning_rate): ds = SupervisedDataSet(20, 20) with open('data/misspellingssmall.csv', 'rbU') as f: reader = csv.reader(f) for row in reader: ds.addSample(convert(row[0]),convert(row[1])) testds, trainds = ds.splitWithProportion(0.2) net = buildNetwork(20, 20, 20) trainer = BackpropTrainer(net, dataset=trainds, learningrate=learning_rate) myscore = float("inf") i = 0 while myscore > 5: i += 1 trainer.train() #trainer.trainEpochs(5) #trainer.trainUntilConvergence(verbose=True) myscore = score(net, testds) print "Epoch #" + str(i) + ": " + str(myscore) + " (" + unconvert(net.activate(convert("ecceptable"))) + ")" global lastNet lastNet = net print "Network done with score " + str(myscore) return score
def main(): start_time = time.time() novice = ArtificialNovice() genius = ArtificialGenius() game = HangmanGame(genius, novice) if __debug__: print "------------------- EVALUATION ------------------------" network = NetworkReader.readFrom("../IA/network_weight_1000.xml") j = 0 while j < 1: game.launch(False, None, network) j += 1 print ("--- %s total seconds ---" % (time.time() - start_time)) else: print "------------------- LEARNING ------------------------" network = buildNetwork(3, 4, 1, hiddenclass=SigmoidLayer) ds = SupervisedDataSet(3, 1) i = 0 while i < 100: game.launch(True, ds) i += 1 print " INITIATE trainer : " trainer = BackpropTrainer(network, ds) print " START trainer : " start_time_trainer = time.time() trainer.train() print ("--- END trainer in % seconds ---" % (time.time() - start_time_trainer)) print " START EXPORT network : " NetworkWriter.writeToFile(network, "../IA/network_weight_test_learning.xml") print " END EXPORT network : "
def agent_step(self,reward, observation): self.reward += reward self.step += 1 self.total_reward += reward thisDoubleAction=self.agent_step_action(observation.doubleArray) if(self.isRisk(observation.doubleArray,thisDoubleAction)): self.times += 1 thisDoubleAction = util.baselinePolicy(observation.doubleArray) from pybrain.supervised.trainers import BackpropTrainer from pybrain.datasets import SupervisedDataSet ds = SupervisedDataSet(12, 4) ds.addSample(observation.doubleArray,self.best.activate(observation.doubleArray)) trainer = BackpropTrainer(self.network, ds) trainer.train() returnAction=Action() returnAction.doubleArray = thisDoubleAction self.lastObservation=copy.deepcopy(observation) self.lastAction=copy.deepcopy(returnAction) self.lastReward = reward return returnAction
def fit(self, X, y): """ Train the regressor model. :param X: pandas.DataFrame of shape [n_samples, n_features] :param y: values - array-like of shape [n_samples] :return: self """ dataset = self._prepare_net_and_dataset(X, y, 'regression') trainer = BackpropTrainer(self.net, dataset, learningrate=self.learningrate, lrdecay=self.lrdecay, momentum=self.momentum, verbose=self.verbose, batchlearning=self.batchlearning, weightdecay=self.weightdecay) if self.epochs < 0: trainer.trainUntilConvergence(maxEpochs=self.max_epochs, continueEpochs=self.continue_epochs, verbose=self.verbose, validationProportion=self.validation_proportion) else: for i in range(self.epochs): trainer.train() self.__fitted = True return self
def train(nn, data, N, predictionLength, iterations, validationSize): loss = 0. lossSize = 1. for n in range(iterations): dataSet = SupervisedDataSet(5 * N, 1) start = 1. * (len(data) - validationSize - 1 - N - predictionLength) / iterations * n end = 1. * (len(data) - validationSize - 1 - N - predictionLength) / iterations * (n + 1) - validationSize validation = end + validationSize start = int(start) end = int(end) validation = int(validation) for i in range(start, end): sample, mainValue = data.contiguousArray(i, i + N) output = data.normalizedMax(i + N + 1, i + N + predictionLength + 1, mainValue) dataSet.addSample(sample, (output,)) print "iteration: ", n, " start: ", start, " end: ", end trainer = BackpropTrainer(nn, dataSet) trainer.train() dataSet.clear() for i in range(end, validation): sample, mainValue = data.contiguousArray(i, i + N) realOutput = data.max(i + N + 1, i + N + predictionLength + 1) nnOutputValue = nn.activate(sample)[0] + mainValue dt = data.date(i + N + 1) currentLoss = nnOutputValue - realOutput loss += currentLoss * currentLoss print '============================' print dt print "NN: ", "{0:.10f}".format(nnOutputValue), " Real: ", "{0:.10f}".format(realOutput) print "LOSS: ", "{0:.10f}".format(currentLoss) print "LOSS TOTAL: ", "{0:.10f}".format(sqrt(loss / lossSize)) print '============================' lossSize += 1.
def ANN(name, attr): x = getSamplesL(name, attr)[0] y = getSamplesL(name, attr)[1] t = getSamplesL(name, attr)[2] net = buildNetwork(40, 250, 5) ds = SupervisedDataSet(40, 5) for e in range(len(x)): ds.addSample(x[e], y[e]) trainer = BackpropTrainer(net, ds) for i in range(20): trainer.train() error = 0 count = 0 for i in range(len(x))[::10]: count = count + 1 tresult = net.activate(x[i]) ans = y[i] for j in range(len(ans)): error = error + abs(tresult[j] - ans[j]) / ans[j] error = error / (count * 5) / 4 print error # tresults = x[50:100] result = net.activate(t) print result return (result, error)
def prepare_neural_models(): train_df = pd.read_csv("train_set.csv") prod_options = ['a','b','c','d','e','f','g'] neural_models = [] for opt in prod_options: if opt == "a": train_cols = [train_df.columns[3],train_df.columns[4],train_df.columns[15]] elif opt == "b": train_cols = [train_df.columns[3],train_df.columns[4],train_df.columns[16]] elif opt == "c": train_cols = [train_df.columns[3],train_df.columns[4],train_df.columns[17]] elif opt == "d": train_cols = [train_df.columns[3],train_df.columns[4],train_df.columns[18]] elif opt == "e": train_cols = [train_df.columns[3],train_df.columns[4],train_df.columns[19]] elif opt == "f": train_cols = [train_df.columns[3],train_df.columns[4],train_df.columns[20]] elif opt == "g": train_cols = [train_df.columns[3],train_df.columns[4],train_df.columns[21]] dataset = SupervisedDataSet(3,1) for df in train_df: dataset.addSample((df[1][train_cols[0]],df[1][train_cols[1]],df[1][train_cols[2]]),(df[opt],)) #neural_ds.append(dataset) net = buildNetwork(3, 3, 1, bias=True, hiddenclass=TanhLayer) neural_trainer = BackpropTrainer(net,dataset) neural_trainer.train() neural_models.append(neural_trainer) return neural_models
def train(self, x, y): ''' Trains on the given inputs and labels for either a fixed number of epochs or until convergence. Normalizes the input with a z-transform''' print "training..." # normalize input m = x.mean() s = x.std() x = self.z_transform(x, m, s) ds = SupervisedDataSet(x.shape[1], 1) ds.setField('input', x) ds.setField('target', y) trainer = BackpropTrainer(self.n,ds, learningrate=self.learning_rate, momentum=self.momentum, verbose=True) if (self.epochs == 0): trainer.trainUntilConvergence() else: for i in range(0, self.epochs): start_time = time.time() trainer.train() print "epoch: ", i print "time: ", time.time() - start_time, " seconds" print "finished"
def handle(self, *args, **options): better_thans = BetterThan.objects.all() #.filter(pk__lte=50) ds = SupervisedDataSet(204960, 1) for better_than in better_thans: bt = imread(better_than.better_than.image.file) wt = imread(better_than.worse_than.image.file) better_than.better_than.image.file.close() better_than.worse_than.image.file.close() # bt = filters.sobel(bt) # wt = filters.sobel(wt) bt_input_array = np.reshape(bt, (bt.shape[0] * bt.shape[1])) wt_input_array = np.reshape(wt, (wt.shape[0] * wt.shape[1])) input_1 = np.append(bt_input_array, wt_input_array) input_2 = np.append(wt_input_array, bt_input_array) ds.addSample(np.append(bt_input_array, wt_input_array), [-1]) ds.addSample(np.append(wt_input_array, bt_input_array), [1]) net = buildNetwork(204960, 2, 1) train_ds, test_ds = ds.splitWithProportion(options['train_test_split']) _, test_ds = ds.splitWithProportion(options['test_split']) trainer = BackpropTrainer(net, ds) print 'Looking for -1: {0}'.format(net.activate(np.append(bt_input_array, wt_input_array))) print 'Looking for 1: {0}'.format(net.activate(np.append(wt_input_array, bt_input_array))) trainer.train() print 'Looking for -1: {0}'.format(net.activate(np.append(bt_input_array, wt_input_array))) print 'Looking for 1: {0}'.format(net.activate(np.append(wt_input_array, bt_input_array)))
def nn_routine_ds(ds): feasize = ds['input'].shape[1] trainDS, testDS = ds.splitWithProportion(0.8) net = _FeedForwardNetwork() inLayer = LinearLayer(feasize) net.addInputModule(inLayer) net, hl = add_fullyC_layer(net, inLayer, TanhLayer(feasize*8)) net, hl = add_fullyC_layer(net, hl, TanhLayer(feasize*4)) net, hl = add_fullyC_layer(net, hl, TanhLayer(feasize*2)) net, hl = add_fullyC_layer(net, hl, TanhLayer(feasize)) net, hl = add_fullyC_layer(net, hl, TanhLayer(feasize/2)) net, hl = add_fullyC_layer(net, hl, TanhLayer(feasize/4)) net, hl = add_fullyC_layer(net, hl, TanhLayer(feasize/8)) outLayer = SigmoidLayer(1) net.addOutputModule(outLayer) hidden_last_to_out = FullConnection(hl, outLayer) net.addConnection(hidden_last_to_out) net.sortModules() trainer = BackpropTrainer(net, trainDS, learningrate=0.001, momentum=.1, verbose=True) epochs = 10000 for zzz in range(epochs): if zzz % 100 == 0: print "%2.3f percent complete" % (100.*zzz/epochs) trainer.train() res = np.append(testDS['target'], net.activateOnDataset(testDS), axis=1) return (net, trainer, trainDS, testDS, res)
def retrain(N, dataset, net): ds = SupervisedDataSet(20, 20) for data in dataset: ds.addSample(data[0], data[1]) trainer = BackpropTrainer(net, ds) for i in range(N): trainer.train() return net
def train(self): self.loadnet() while True: self.filefft={} ds=self.gettraining() trainer = BackpropTrainer(self.net,ds) print trainer.train() self.savenet()
def __init__(self, data, targets, cv_data, cv_targets, extra, layers, epochs=1, smoothing=1, new=True, filename_in=False): if len(cv_data) != len(cv_targets): raise Exception("Number of CV data and CV targets must be equal") if len(data) != len(targets): raise Exception("Number of data and targets must be equal") if new: class_tr_targets = [str(int(t[0]) - 1) for t in targets] # for pybrain's classification datset print "...training the DNNRegressor" if len(layers) > 2: # TODO testing only net = DNNRegressor(data, extra, class_tr_targets, layers, hidden_layer="TanhLayer", final_layer="SoftmaxLayer", compression_epochs=epochs, bias=True, autoencoding_only=False) print "...running net.fit()" net = net.fit() elif len(layers) == 2: net = buildNetwork(layers[0], layers[-1], outclass=SoftmaxLayer, bias=True) ds = ClassificationDataSet(len(data[0]), 1, nb_classes=9) bag = 1 noisy, _ = self.dropout(data, noise=0.0, bag=bag, debug=True) bagged_targets = [] for t in class_tr_targets: for b in range(bag): bagged_targets.append(t) for i,d in enumerate(noisy): t = bagged_targets[i] ds.addSample(d, t) ds._convertToOneOfMany() print "...smoothing for epochs: ", smoothing self.model = net preds = [self.predict(d) for d in cv_data] cv = score(preds, cv_targets, debug=False) preds = [self.predict(d) for d in data] tr = score(preds, targets, debug=False) trainer = BackpropTrainer(net, ds, verbose=True, learningrate=0.0008, momentum=0.04, weightdecay=0.05) # best score 0.398 after 50 compression epochs and 200 epochs with lr=0.0008, weightdecay=0.05, momentum=0.04. Used dropout of 0.2 in compression, 0.5 in softmax pretraining, and no dropout in smoothing. print "Train score before training: ", tr print "CV score before training: ", cv for i in range(smoothing): trainer.train() self.model = net preds = [self.predict(d) for d in cv_data] cv = score(preds, cv_targets, debug=False) preds = [self.predict(d) for d in data] tr = score(preds, targets, debug=False) print "Train/CV score at epoch ", (i+1), ': ', tr, '/', cv #if i == 1: #print "...saving the model" #save("data/1000_ex_4_hidden/net_epoch_1.txt", net) #elif i == 3: #print "...saving the model" #save("data/1000_ex_4_hidden/net_epoch_3.txt", net) #elif i == 5: #print "...saving the model" #save("data/1000_ex_4_hidden/net_epoch_5.txt", net) print "...saving the model" #save("data/1000_ex_4_hidden/net_epoch_10.txt", net) else: model = load(filename_in) self.model = model
def fit(self, X, y): n = X.shape[1] self.nn = self.build_network([n]+self.hidden_layers+[1]) ds = SupervisedDataSet(n, 1) for i, row in enumerate(X): ds.addSample(row.tolist(), y[i]) trainer = BackpropTrainer(self.nn, ds) for i in xrange(100): trainer.train()
def build_bid2_nn(training_data): ds = SupervisedDataSet(7, 1) for game_state in training_data: winning_sample = extract_sample(game_state) ds.addSample(tuple(winning_sample[0:7]), (winning_sample[8],)) net = buildNetwork(7, 9, 9, 1, bias=True) trainer = BackpropTrainer(net, ds) for i in range(REPEATS): trainer.train() return net
def build_bid1_nn(training_data): ds = SupervisedDataSet(1, 1) for game_state in training_data: winning_sample = extract_sample(game_state) ds.addSample((winning_sample[0],), (winning_sample[1],)) net = buildNetwork(1, 1) trainer = BackpropTrainer(net, ds) for i in range(REPEATS): trainer.train() return net
def main(): trndata, tstdata = createDS() for repeat in xrange(repeats): iter_trn_results = [] iter_tst_results = [] nn = createNN(4, 6, 3) nn.randomize() print nn.params hiddenAstrocyteLayer, outputAstrocyteLayer = associateAstrocyteLayers(nn) trainer = BackpropTrainer(nn, dataset=trndata, learningrate=0.01, momentum=0.1, verbose=False, weightdecay=0.0) for grand_iter in xrange(iterations): if grand_iter == 0: trainer.train() if grand_iter > iterations/3 and grand_iter < iterations/3: inputs = trndata['input'][:] random.shuffle(inputs) for inpt in trndata['input']: nn.activate(inpt) for minor_iter in range(hiddenAstrocyteLayer.astrocyte_processing_iters): hiddenAstrocyteLayer.update() outputAstrocyteLayer.update() hiddenAstrocyteLayer.reset() outputAstrocyteLayer.reset() trainer.train() trnresult = percentError(trainer.testOnClassData(), trndata['class']) iter_trn_results.append(trnresult) tstresult = percentError(trainer.testOnClassData(dataset=tstdata), tstdata['class']) iter_tst_results.append(tstresult) if not grand_iter%100: print 'epoch %4d' %trainer.totalepochs, 'train error %5.2f%%' %trnresult, \ 'test error %5.2f%%' %tstresult all_trn_results.append(iter_trn_results) all_tst_results.append(iter_tst_results) path = '/home/david/Dropbox/programming/python/ANN (Case Conflict 1)/pybrain/' f = plt.figure(figsize=(10,5)) plotErrorBar(all_trn_results) plotErrorBar(all_tst_results) plt.legend(('Training', 'Test')) f.savefig(path+'angn_bpThenAstro_error_bar.svg', format='svg') f = plt.figure(figsize=(10,5)) side_text = 'Trials='+str(repeats)+'\n\nFinal\ntrain\nerror\n' plotNoErrorBar(all_trn_results, label=side_text, xytxt=(1.02, 0.7)) plotNoErrorBar(all_tst_results, label='Final\ntest\nerror\n', xytxt=(1.02, 0.4)) plt.legend(('Training', 'Test')) f.savefig(path+'angn_bpThenAstro_no_error_bar.svg', format='svg') plt.close()
dataset=dataTrain, learningrate=0.01, lrdecay=1.0, momentum=0.0, verbose=False, batchlearning=False, weightdecay=0.0) # variar learningrate - 0.1 , 1.0, 0.001 ##################Treina com teste treino e validação################# #back.trainUntilConvergence(maxEpochs=None, verbose=True, continueEpochs=10, validationProportion=0.25) ######################################################## ########################## Treino só com conj de testes e treino############################# for i in range(3000): error = back.train() #quando é só treino e teste usa esse if error < 0.01: break print('Epoch: ' + str(i) + 'Error: ' + str(error)) ############################################################################## computed = network.activateOnDataset(dataTest) target = dataTest['target'] sentiment = None for i in range(0, dataTest.getLength()): if computed[i] >= 0.5: sentiment = 'positive' else:
def main(): try: start_time = time.time() elapse_time = time.time() - start_time print 'Start reading images to generate data ...' \ + time.strftime(" %H:%M:%S", time.gmtime(elapse_time)) \ + ' has past. ' #Read Data instanceSize = 10 step = 2 TrainDs = GenerateTrainDataSet(instanceSize, step) elapse_time = (time.time() - start_time) print 'Start training neutral neutwrok ...' \ + time.strftime(" %H:%M:%S", time.gmtime(elapse_time)) \ + ' has past. ' #Build Netwrok LAYERS_NUM = 3 net = build_network(TrainDs['input'].shape[1], LAYERS_NUM, 1) # net = buildNetwork(m*n, LAYERS_NUM, 1) #Training trainer = BackpropTrainer(net, dataset=TrainDs) t1 = (time.time() - start_time) print trainer.train() t2 = (time.time() - start_time) print 'Training Time for One Epoch %.1f s' % (t2 - t1) """ error_past = 0 error_now = 1 error_thre = 1E-9 num_epos = 10 i = 0 while (np.abs(error_now - error_past) > error_thre and i <= num_epos): i = i + 1; error_past = error_now error_now = trainer.train() print [error_now, error_past] trainer.trainUntilConvergence()#""" #Tesing elapse_time = (time.time() - start_time) print 'Start tesing instances ...' \ + time.strftime(" %H:%M:%S", time.gmtime(elapse_time)) \ + ' has past. ' begin_time = (time.time() - start_time) index = -1 number = np.zeros((41, 3)) number_filtered = np.zeros((41, 3)) for det in range(1, 8): if (det != 6): num_n = 6 else: num_n = 5 for n in range(0, num_n): index = index + 1 temp = np.zeros(3) temp_filtered = np.zeros(3) for angle in range(1, 4): filename = 'detector_' + str(det) + '_no_' + str(n) \ + '_angle_' + str(angle) + '.jpg' elapse_time = (time.time() - begin_time) if ((index * 3 + angle - 1) >= 1): remain_time = elapse_time / (index * 3 + angle - 1) * 41 * 3 - elapse_time print 'Tesing instances in ' + filename \ + time.strftime(" %H:%M:%S", \ time.gmtime(time.time() - start_time)) \ + ' has past. ' + 'Remaining time: ' \ + time.strftime(" %H:%M:%S", time.gmtime(remain_time)) else: print 'Tesing instances in ' + filename \ + time.strftime(" %H:%M:%S", \ time.gmtime(time.time() - start_time)) \ + ' has past' # filename = 'detector_2_no_5_angle_1.jpg' image = io.imread(filename) [temp[angle-1], temp_filtered[angle-1]] = \ countBubbles(net, image, instanceSize, step, plot_show = 0) print[temp[angle - 1], temp_filtered[angle - 1]] number[index, 1] = np.mean(temp) number[index, 2] = np.std(temp) number_filtered[index, 1] = np.mean(temp_filtered) number_filtered[index, 2] = np.std(temp_filtered) manual_count = np.array([1,27,40,79,122,160,1,18,28,42,121,223,0,11,24,46,\ 142,173,3,19,23,76,191,197,0,15,24,45,91,152,0,\ 16,27,34,88,0,9,12,69,104,123]) number[:, 0] = manual_count.T number_filtered[:, 0] = manual_count.T data_filename = 'neutralnetwork.txt' data_filename_filtered = 'neutralnetwork_filtered.txt' number.tofile(data_filename, sep=", ") number_filtered.tofile(data_filename_filtered, sep=", ") except KeyboardInterrupt: print "Shutdown requested... exiting" except Exception: traceback.print_exc(file=sys.stdout) sys.exit(0)
train_labels_temp = data['train_labels'] print (train_temp.shape) print (train_labels_temp.shape) image_array = np.vstack((image_array, train_temp)) label_array = np.vstack((label_array, train_labels_temp)) t1 = time.time() for i in range(train_temp.shape[0]): target.addSample(image_array[i],label_array[i]) print 'loading time : ' , time.time() - t1 # train while True: try: while True: errors = trainer.train() if (j % 5) == 0: print ("epoch%d error : %f" % (j, errors)) elif(errors < 2e-2) : print ("epoch%d error : %f" % (j, errors)) break j += 1 NetworkWriter.writeToFile(network, XML) except: print ("epoch%d error : %f" % (j, errors)) break finally: NetworkWriter.writeToFile(network, XML) break raw_input('>')
class_labels=possibilities['readmitted']) for row in cursor.execute("select %s from diabetic_data limit 0, 10000" % columns): xd, yd = createNPRow(row) dataset.addSample(xd, yd) nn = buildNetwork(dataset.indim, 20, dataset.outdim, outclass=SoftmaxLayer) trainer = BackpropTrainer(nn, dataset=dataset, momentum=0.1, verbose=True, weightdecay=0.01) print possibilities['readmitted'] print dataset.getField('target') for x in range(10): error = trainer.train() print error errors, success = 0, 0 for row in cursor.execute("select %s from diabetic_data limit 50000, 101766" % columns): xd, yd = createNPRow(row) check = int(round(nn.activate(xd[:46])[0])) if check > 1: check = 1 prediction = possibilities['readmitted'][check] actual = possibilities['readmitted'][yd] if prediction == actual: match = "match" success += 1 else: match = "no match"
test_err = zeros(len(net)) # We will train each NN for 50 epochs max_epochs = 50 # Convert the boston dataset into SupervisedDataset for j in range(1, len(X_train)): ds.addSample(X_train[j], y_train[j]) for i in range(1, len(net)): # Setup a trainer that will use backpropogation for training trainer = BackpropTrainer(net[i], ds) # Run backprop for max_epochs number of times for k in range(1, max_epochs): train_err[i] = trainer.train() # Find the labels for test set y = zeros(len(X_test)) for j in range(0, len(X_test)): y[j] = net[i].activate(X_test[j]) # Calculate MSE for all samples in the test set test_err[i] = mean_squared_error(y, y_test) # Plot training and test error as a function of the number of hidden layers pl.figure() pl.title('Neural Networks: Performance vs Model Complexity') pl.plot(net_arr, test_err, lw=2, label = 'test error') pl.plot(net_arr, train_err, lw=2, label = 'training error')
class CNeuralNet: def __init__(self,learningrate = 0.001,inputneurons = 2,hiddenneurons =50,outputneurons = 2,testondata= True, \ momentum = 0.2,train_percent = 99,recurnet = False): """ Neural networks class assign a learning rate of your choice , default is 0.01 inputneurons = number of neurons on input layer: can be set to the input dimension of the data hiddenneurons= keep it more than inputneurons in general outputneurons = output dimension of your data testondata = If you want to print out the performance of your neural net, defaults to true """ assert (hiddenneurons > inputneurons), "Number of hiddenneurons can't be lesser than inputneurons" self.learningrate = learningrate self.inputneurons = inputneurons self.hiddenneurons = hiddenneurons self.outputneurons = outputneurons #momentum is the parameter to realize how efficiently the learning will get out of a local minima, #not sure what to put as an appropriate value self.momentum = momentum #Construct network here self.mlpnetwork = buildNetwork(self.inputneurons, self.hiddenneurons, self.outputneurons, bias=True,recurrent=recurnet) self.mlpnetwork.sortModules() print self.mlpnetwork,"-----------------------------------" self.trainer = None self.validation = testondata self.data = None self.learnedNetwork = None self.train_percent = (1. *train_percent )/100 #creating a feedforward network self.ffn = FeedForwardNetwork() inlayer = LinearLayer(inputneurons) hiddenlayer1 = SigmoidLayer(4) hiddenlayer2 = SigmoidLayer(2) outlayer = LinearLayer(outputneurons) #assigning them to layers self.ffn.addInputModule(inlayer) self.ffn.addModule(hiddenlayer1) self.ffn.addModule(hiddenlayer2) self.ffn.addOutputModule(outlayer) #defining connections in_to_hidden1 = FullConnection(inlayer,hiddenlayer1) hidden1_to_hidden2 = FullConnection(hiddenlayer1,hiddenlayer2) hidden2_to_out = FullConnection(hiddenlayer2,outlayer) #explicitly adding them to network self.ffn.addConnection(in_to_hidden1) self.ffn.addConnection(hidden1_to_hidden2) self.ffn.addConnection(hidden2_to_out) #explicitly call sortmodules self.ffn.sortModules() print "created network successfully...." def train(self,filename,trainepochs = 1000): """ train: call this function to train the network inputdata = set of input params trainepochs = number of times to iterate through this dataset """ self.trainer = BackpropTrainer(self.mlpnetwork, dataset=self.data,verbose=True, learningrate=self.learningrate, momentum=self.momentum) #self.trainer = BackpropTrainer(self.ffn, dataset=self.data, verbose=True, learningrate=self.learningrate, momentum=self.momentum) #self.trainer.trainEpochs(epochs=trainepochs) print "training in progress..." for i in xrange( trainepochs ): mse = self.trainer.train() rmse = np.sqrt(mse) print "training RMSE, epoch {}: {}".format( i + 1, rmse ) ''' fl = filename.split('.log')[0] +str(time.strftime('%H_%M_%S'))+ "_Vivek_ActiveState_New.pickle" with open(fl, "wb") as f: pickle.dump(self.mlpnetwork, f) err = filename.split('.log')[0] + "_validation_errors.pkl" with open(err, "wb") as f: pickle.dump(self.trainer.validationErrors, f) ''' print "training done..." def loadTrainedModel(self, pickleFile=None): """ call this function to load the trained model Please call loadTrainedModel once, before calling predict, so as to load the trained model and then predict things :) """ if pickleFile == None: # If there are many pre-computed neural-nets, load the first one from glob import glob pickleFile = glob("./*.pickle")[0] assert '.pickle' in pickleFile, "Invalid Neural-Net loaded..." with open(pickleFile, "rb") as f: self.mlpnetwork = pickle.load(f) return self.mlpnetwork def predict(self, testData): """ testData = input data which is to be predicted on a given trained model if you trained the model earlier and want to reuse it """ #assert (self.trainer != None) , "Train the model before you predict with it..." return self.mlpnetwork.activate(testData) def createTrainingData(self,filename,inputdim, outputdim): """ create training data by reading file=filename inputdim = inputdimension of data outputdim = output dim expected """ if filename is not None: finaldf = pd.read_csv(paths+filename, parse_dates=[0], delimiter=";",index_col=0); finaldf = finaldf.reset_index() finaldf['hour'] = pd.DatetimeIndex(finaldf['TIMESTAMP']).hour for col in finaldf: if(col not in ['TIMESTAMP','hour']): print col print "hhhhhhhhhhhhhhhhhhh" finaldf[col] /= finaldf[col].iloc[0].astype(np.float64) print finaldf.head(10) #split data into percentages msk = np.random.rand(len(finaldf)) < self.train_percent train = finaldf[msk].copy() test = finaldf[~msk].copy() test = test.reset_index() train = train.reset_index() self.train_input = train[inputparams] self.train_output = train[outputparams] #normalize train_output #self.train_output = 1. * self.train_output/self.train_output.max() #print self.train_output.head(10) self.test_input = test[inputparams] self.test_output = test[outputparams] self.data = SupervisedDataSet(inputdim,outputdim) totalLength = len(self.train_input) for line in xrange(0,totalLength-1): #print self.train_input.values[line], self.train_output.values[:,0][line] self.data.addSample(self.train_input.values[line], self.train_output.values[:,0][line]) print "data loaded..." def createXORData(self,inputdim,outputdim): self.data = SupervisedDataSet(inputdim,outputdim) self.data.addSample([1,1],[0]) self.data.addSample([1,0],[1]) self.data.addSample([0,1],[1]) self.data.addSample([0,0],[0])
def benchmark(clf=None, n_hidden=10, n_epochs=10): for col in ['AP']:#, 'COP', 'AP', 'LS', 'MA']:#, 'PPR', '2X', '3X', '4X', '5X', 'AU', 'UNI', 'MEM']: print "*" * 80 print type(clf) print col X = merged[numerical_columns + ['LivingArea']] #X = merged.drop(['MlsNumber', 'Lat', 'Lng', 'BuyPrice'], axis=1, inplace=False) X_cat = merged[categorical_columns] Y = merged[['BuyPrice']] mask = merged[col]==1 X, X_cat, Y = X[mask], X_cat[mask], Y[mask] print 'X.shape: ', X.shape print 'Y.shape: ', Y.shape # filter rows with NaN mask = ~np.isnan(X).any(axis=1) X, X_cat, Y = X[mask], X_cat[mask], Y[mask] mask = ~np.isnan(Y).any(axis=1) X, X_cat, Y = X[mask], X_cat[mask], Y[mask] print 'After NaN filter: ', X.shape X, X_cat, Y = np.array(X), np.array(X_cat), np.array(Y) if USE_LOG: Y = np.log(Y) Y = Y.reshape(Y.shape[0]) print "mean: ", np.mean(Y) print "median: ", np.median(Y) print "std: ", Y.std() # remove outliers mask = Y > 10**5 X, X_cat, Y = X[mask], X_cat[mask], Y[mask] mask = Y < 10**6 X, X_cat, Y = X[mask], X_cat[mask], Y[mask] # one-hot encode categorical features X_cat_enc = [] for i, cat in enumerate(categorical_columns): col = X_cat[:,i] col = LabelEncoder().fit_transform(col).reshape((-1,1)) col_enc = OneHotEncoder(sparse=False).fit_transform(col) X_cat_enc.append(col_enc) X_cat = np.concatenate(X_cat_enc, axis=1) print 'X_cat.shape: ', X_cat.shape skf = KFold(n=X.shape[0], n_folds=10, shuffle=True, random_state=42) L = { 'rmse': [], 'corr': [], 'r2': [], 'diff': [], 'mae': [], 'explained_var': [], 'var': []} for train_indices, test_indices in skf: X_train, X_train_cat, Y_train = X[train_indices], X_cat[train_indices], Y[train_indices] X_test, X_test_cat, Y_test = X[test_indices], X_cat[test_indices], Y[test_indices] scaler = StandardScaler() X_train = scaler.fit_transform(X_train) X_test = scaler.transform(X_test) X_train = np.concatenate([X_train, X_train_cat], axis=1) X_test = np.concatenate([X_test, X_test_cat], axis=1) if USE_NEURALNET: print 'n_hidden: %d' % n_hidden Y_train, Y_test = Y_train.reshape(-1, 1), Y_test.reshape(-1, 1) train_ds = SupervisedDataSet(X_train.shape[1], Y_train.shape[1]) train_ds.setField('input', X_train) train_ds.setField('target', Y_train) net = buildNetwork(X_train.shape[1], n_hidden, Y_train.shape[1], bias=True) trainer = BackpropTrainer(net, train_ds) for i in xrange(n_epochs): mse = trainer.train() rmse = math.sqrt(mse) print "epoch: %d, rmse: %f" % (i, rmse) test_ds = SupervisedDataSet(X_test.shape[1], Y_test.shape[1]) test_ds.setField('input', X_test) test_ds.setField('target', Y_test) preds = net.activateOnDataset(test_ds) else: clf.fit(X_train, Y_train) preds = clf.predict(X_test).astype(float) if USE_LOG: Y_test_10 = np.exp(Y_test) preds_10 = np.exp(preds) else: Y_test_10 = Y_test preds_10 = preds rmse = math.sqrt(metrics.mean_squared_error(Y_test_10, preds_10)) corr = pearsonr(preds_10, Y_test_10) diff = np.array([abs(p-a)/a for (p,a) in zip(Y_test_10, preds_10)]) mae = metrics.mean_absolute_error(Y_test_10, preds_10) explained_var = metrics.explained_variance_score(Y_test_10, preds_10) r2 = metrics.r2_score(Y_test_10, preds_10) var = np.var(diff) L['rmse'].append(rmse) L['corr'].append(corr[0]) L['diff'].append(diff.mean()) L['mae'].append(mae) L['explained_var'].append(explained_var) L['r2'].append(r2) L['var'].append(var) if GENERATE_PLOTS: plt.plot(Y_test_10, preds_10, 'ro') plt.show() break if USE_NEURALNET: break for key in L.keys(): print "Mean %s: %f" % (key, np.array(L[key]).mean()) return L
Y = pd.read_csv('Data/Train/Train_Combine.csv', usecols=['PM 2.5']) X = X.values Y = Y.values hidden_size = 100 epochs = 20 input_size = X.shape[1] target_size = Y.shape[1] ds = SDS(input_size, target_size) ds.setField('input', X) ds.setField('target', Y) net = buildNetwork(input_size, hidden_size, target_size, bias=True, hiddenclass=TanhLayer) trainer = BackpropTrainer(net, ds) print("training for {} epochs...".format(epochs)) for i in range(epochs): mse = trainer.train() rmse = sqrt(mse) print("training RMSE, epoch {}: {}".format(i + 1, rmse)) pickle.dump(net, open(output_model_file, 'wb'))
# [1 0] corresponding to 0 # [0 1] corresponding to 1 # it is considered as classification problem with class 0 and class 1 # convert output of net to real result using function output above net = buildNetwork(2, 5, 2, hiddenclass=TanhLayer, outclass=SoftmaxLayer) ds = SupervisedDataSet(2, 2) # might need more data to train # but in this example, ds with 4 samples is pretty enough for i in xrange(1): ds.addSample((0.0, 0.0), (1.0, 0.0)) ds.addSample((0.0, 1.0), (0.0, 1.0)) ds.addSample((1.0, 0.0), (0.0, 1.0)) ds.addSample((1.0, 1.0), (1.0, 0.0)) print len(ds) trainer = BackpropTrainer(net, ds, learningrate=0.01, momentum=0.99) # train 100 epoches for i in xrange(100): print trainer.train() print "test on data", trainer.testOnData() # test print output(net.activate((0.0, 0.0))) # 1 0 --> 0 print output(net.activate((1.0, 0.0))) # 0 1 --> 1 print output(net.activate((0.0, 1.0))) # 0 1 --> 1 print output(net.activate((1.0, 1.0))) # 1 0 --> 0
def predict_ball(hidden_nodes, is_elman=True, training_data=5000, epoch=-1, parameters={}, predict_count=128): # build rnn n = construct_network(hidden_nodes, is_elman) # make training data ep = 1 if epoch < 0 else epoch initial_v = ball_data.gen_velocity(BOX_SIZE) data_set = ball_data.bounce_ball((training_data + 1) * ep, BOX_SIZE, None, initial_v=initial_v) total_avg = np.average(data_set, axis=0) total_std = np.std(data_set, axis=0) # initial_p = data_set[np.random.choice(range(training_data))][:2] training_ds = [] normalized_d = __normalize(data_set) for e_index in range(ep): t_ds = SupervisedDataSet(4, 4) e_begin = e_index * training_data for j in range(e_begin, e_begin + training_data): # from current, predict next p_in = normalized_d[j].tolist() p_out = normalized_d[j + 1].tolist() t_ds.addSample(p_in, p_out) training_ds.append(t_ds) del data_set # release memory # training network err1 = 0 if epoch < 0: trainer = BackpropTrainer(n, training_ds[0], **parameters) err1 = trainer.train() else: trainer = BackpropTrainer(n, **parameters) epoch_errs = [] for ds in training_ds: trainer.setData(ds) epoch_errs.append(trainer.train()) err1 = max(epoch_errs) del training_ds # release memory # predict initial_p = ball_data.gen_position(BOX_SIZE) predict = None next_pv = np.hstack((initial_p, initial_v)) n.reset() for i in range(predict_count): predict = next_pv if predict is None else np.vstack((predict, next_pv)) p_normalized = (next_pv - total_avg) / total_std next_pv = n.activate(p_normalized.tolist()) restored = np.array(next_pv) * total_std + total_avg next_pv = restored real = ball_data.bounce_ball(predict_count, BOX_SIZE, initial_p, initial_v) err_matrix = (predict - real) ** 2 err_distance = np.sqrt(np.sum(err_matrix[:, 0:2], axis=1)).reshape((predict_count, 1)) err_velocity = np.sum(np.sqrt(err_matrix[:, 2:4]), axis=1).reshape((predict_count, 1)) err2 = np.hstack((err_distance, err_velocity)) return predict, real, err1, err2
inputs = [[1.0, 1.0], [0.0, 1.0], [1.0, 0.0]] outputs = [[1.0], [0.0], [0.0]] people = {} people.update({'murat': [1.0, 0.0]}) people.update({'ali': [0.0, 1.0]}) from pybrain.tools.shortcuts import buildNetwork from pybrain.datasets import SupervisedDataSet from pybrain.supervised.trainers import BackpropTrainer net = buildNetwork(2, 3, 1, bias=True) ds = SupervisedDataSet(2, 1) for i, j in zip(inputs, outputs): ds.addSample(tuple(i), tuple(j)) print(ds) back = BackpropTrainer(net, ds) # eğitim algoritması for epoch in range(1000): print(back.train()) for person, features in people.items(): compute = net.activate(features) prop = compute[0] print(person, ' is ', prop, '% satın alma ihtimali')
def trainNetwork(train_ds, test_ds, train_ds_labels, test_ds_labels, features, learningrate, lrdecay, momentum, weightdecay, hidden_layers, time_limit_seconds): fnn = FeedForwardNetwork() inLayer = LinearLayer(train_ds.indim) fnn.addInputModule(inLayer) lastLayer = inLayer connection_number = 0 # connection-0 is the connection from the input layer. for hidden_layer_size in hidden_layers: # hiddenLayer = SigmoidLayer(hidden_layer_size) hiddenLayer = TanhLayer(hidden_layer_size) fnn.addModule(hiddenLayer) fnn.addConnection( FullConnection(lastLayer, hiddenLayer, name="connection-%d" % connection_number)) connection_number = connection_number + 1 bias = BiasUnit() fnn.addModule(bias) fnn.addConnection(FullConnection(bias, hiddenLayer)) lastLayer = hiddenLayer outLayer = SigmoidLayer(train_ds.outdim) fnn.addOutputModule(outLayer) fnn.addConnection( FullConnection(lastLayer, outLayer, name="connection-%d" % connection_number)) bias = BiasUnit() fnn.addModule(bias) fnn.addConnection(FullConnection(bias, outLayer)) fnn.sortModules() trainer = BackpropTrainer(fnn, dataset=train_ds, learningrate=learningrate, lrdecay=lrdecay, momentum=momentum, verbose=False, weightdecay=weightdecay) # Train (initial_train_error, initial_train_F1) = percentClassErrorAndF1(fnn, train_ds, train_ds_labels, features) train_errors = [initial_train_error] train_F1s = [initial_train_F1] (initial_test_error, initial_test_F1) = percentClassErrorAndF1(fnn, test_ds, test_ds_labels, features) test_errors = [initial_test_error] test_F1s = [initial_test_F1] train_algo_errors = [trainer.testOnData(train_ds) * 100] test_algo_errors = [trainer.testOnData(test_ds) * 100] epochs = [0] try: start_time = time.time() for i in range(200): for _ in xrange(50): train_algo_error = trainer.train() * 100.0 if math.isnan(train_algo_error): break if math.isnan(train_algo_error): break (trnresult, trnF1) = percentClassErrorAndF1(fnn, train_ds, train_ds_labels, features) (tstresult, tstF1) = percentClassErrorAndF1(fnn, test_ds, test_ds_labels, features) test_algo_error = trainer.testOnData(test_ds)* 100 now_time = time.time() time_left = time_limit_seconds - (now_time - start_time) print("epoch %3d:" % trainer.totalepochs, " train error: %6.4f%%" % train_algo_error, " test error: %6.4f%%" % test_algo_error, " train F1: %s" % ", ".join([("%.2f" % x) for x in trnF1]), " test F1: %s" % ", ".join([("%.2f" % x) for x in tstF1]), " %ds left" % int(round(time_left))) epochs.append(trainer.totalepochs) train_errors.append(trnresult) train_F1s.append(trnF1) test_errors.append(tstresult) test_F1s.append(tstF1) train_algo_errors.append(train_algo_error) test_algo_errors.append(test_algo_error) if time_left <= 0: print("Timeout: Time to report the results.") break; # if test_algo_errors[-1] < 4: # print("Good enough? Don't want to overtrain") # break; except KeyboardInterrupt: # Someone pressed Ctrl-C, try to still plot the data. print("Aborted training...") pass return (fnn, epochs, train_algo_errors, test_algo_errors, train_F1s, test_F1s)
def treinamentoRedeNeural(rede, dados): trainer = BackpropTrainer(rede, dados) error = 1 while error > 0.0001: error = trainer.train()
hiddenclass=LSTMLayer, outclass=SigmoidLayer, recurrent=True) ds = ClassificationDataSet(12, 1) for i, j in zip(train_features, train_labels): ds.addSample(i, j) # In[104]: trainer = BackpropTrainer(net, ds) # In[105]: epochs = 10 for i in range(epochs): trainer.train() # In[106]: predicted = list() for i in test_features: #print net.activate(i) predicted.append(int(net.activate(i) > 0.5)) predicted = numpy.array(predicted) # In[107]: print(accuracy_score(test_labels, predicted)) print(recall_score(test_labels, predicted)) print(precision_score(test_labels, predicted))
def run(): # Parameters used for program HIDDEN_LAYERS = [ 35, 35 ] LEARNING_DECAY = 1 # Set in range [0.9, 1] LEARNING_RATE = 0.096 # Set in range [0, 1] MOMENTUM = 0.1 # Set in range [0, 0.5] TRAINING_ITERATIONS = 1500 BATCH_LEARNING = False VALIDATION_PROPORTION = 0.0 # Import the data for the two spirals Task dataset, classes = csv.loadCSV(path.abspath('spirals/SpiralOut.txt')) # Set up the network and trainer inDimension = dataset.indim outDimension = dataset.outdim layers = [inDimension] + HIDDEN_LAYERS + [outDimension] neuralNet = buildNetwork(*layers) print neuralNet trainer = BackpropTrainer(neuralNet, dataset, learningrate=LEARNING_RATE, momentum=MOMENTUM, lrdecay=LEARNING_DECAY, batchlearning=BATCH_LEARNING) # Train the network trainingErrors = [] validationErrors = [] for i in xrange(TRAINING_ITERATIONS): print "Training iteration: ", i # Check if VALIDATION_PROPORTION is not 0. This will split the input dataset into # VALIDATION_PROPORTION % for Validation Data and # (1 - VALIDATION_PROPORTION) % for Training Data # e.g. 25% ValidationData and 75% Training Data if VALIDATION_PROPORTION == 0.0 or VALIDATION_PROPORTION == 0: # Cannot split the data set into Training and Validation Data. Train the # Neural Network by standard means. This will not calculate Validatinon Error # The result of training is the proportional error for the number of epochs run trainingError = trainer.train() trainingErrors.append(trainingError) # Display the result of training for the iteration print " Training error: ", trainingError else: trainingErrors, validationErrors = trainer.trainUntilConvergence(validationProportion=VALIDATION_PROPORTION) # create path if it doesn't exist generated_dir = path.abspath(path.join("generated", "TaskA-TrainedNN-{}".format(strftime("%Y-%m-%d_%H-%M-%S")))) if not path.exists(generated_dir): makedirs(generated_dir) # save parameters with open(path.normpath(path.join(generated_dir, "params.txt")), "a") as f: f.write("HIDDEN_LAYERS = {}\n".format(HIDDEN_LAYERS)) f.write("LEARNING_DECAY = {}\n".format(LEARNING_DECAY)) f.write("LEARNING_RATE = {}\n".format(LEARNING_RATE)) f.write("MOMENTUM = {}\n".format(MOMENTUM)) f.write("TRAINING_ITERATIONS = {}\n".format(TRAINING_ITERATIONS)) f.write("BATCH_LEARNING = {}\n".format(BATCH_LEARNING)) f.write("VALIDATION_PROPORTION = {}\n".format(VALIDATION_PROPORTION)) # Save the Trained Neural Network uniqueFileName = path.normpath(path.join(generated_dir, "data.pkl")) writeMode = 'wb' # Write Bytes pickle.dump(neuralNet, open(uniqueFileName, writeMode)) import matplotlib.pyplot as plot # Plot the results of training plot.plot(trainingErrors, 'b') plot.ylabel("Training Error") plot.xlabel("Training Steps") plot.savefig(path.normpath(path.join(generated_dir, "errors.png"))) plot.show() plot.clf() plot = NN2D.plotNN(network=neuralNet, lowerBound=-6.0, upperBound=6.0, step=0.1) if VALIDATION_PROPORTION != 0.0 or VALIDATION_PROPORTION != 0: plot = NN2D.plotBarComparison(trainingErrors, validationErrors) plot.savefig(path.normpath(path.join(generated_dir, "result.png"))) plot.show()
# d.addSample([1, 0], [1]) # d.addSample([1, 1], [0]) X = np.array([[0., 0.], [0., 1.], [1., 0.], [1., 1.]]) y = np.array([0., 1., 1., 0.]) for i in xrange(0, 4): d.addSample(X[i, :], y[i]) tol_max = 1e-3 max_iter = 1000 trainer = BackpropTrainer(n, d, learningrate=1e-3, momentum=0.9) erroDpc = [] iter_t = 0 while max_iter > 0: tol = trainer.train() erroDpc.append(tol) max_iter -= 1 if tol <= tol_max: break iter_t += 1 print n.activate([0, 0]) print n.activate([0, 1]) print n.activate([1, 0]) print n.activate([1, 1]) print iter_t plt.plot(erroDpc) plt.xlabel('Geracao')
from pybrain.tools.shortcuts import buildNetwork from pybrain.datasets import SupervisedDataSet from pybrain.supervised.trainers import BackpropTrainer from pybrain.structure import * import time last = time.time() net = buildNetwork(2, 3, 1, bias=True, hiddenclass=TanhLayer) ds = SupervisedDataSet(2, 1) ds.addSample((0, 0), (0, )) ds.addSample((0, 1), (1, )) ds.addSample((1, 0), (1, )) ds.addSample((1, 1), (0, )) for inpt, target in ds: print inpt, target trainer = BackpropTrainer(net, ds) d = 1 while d > 1e-5: d = trainer.train() # print d print "结果:" print net.activate([0, 0]) print net.activate([0, 1]) print net.activate([1, 0]) print net.activate([1, 1]) print time.time() - last
ds.addSample(puzzle.input_format, puzzle.output_format) ds.addSample(puzzle.output_format, puzzle.output_format) # stash it print 'Dataset size: {}'.format(len(ds)) save_dataset(ds) # see what it looks like to start print 'Starting hits:' puzzle = Sudoku() run_puzzle(puzzle, net) pre = 0 pre_time = time.time() for i in range(EPOCHS): puzzle = Sudoku() cur = trainer.train() save_network(net) delta = cur - pre pre = cur cur_time = time.time() time_delta = cur_time - pre_time pre_time = cur_time print 'Epoch: {:>5}\tError: {:9.7f}\tDelta: {:9.7f}\tTime delta: {}\tDataset size: {}'.format(i + 1, cur, delta, time_delta, len(ds)) run_puzzle(puzzle, net) if '--grow' in sys.argv: ds.addSample(puzzle.input_format, puzzle.output_format) ds.addSample(puzzle.output_format, puzzle.output_format) if not i % 10: save_dataset(ds)
def build(self, filename=None): """Build and train a network according to a supplied configuration file. The config file should be in the following format : line number == 1 | input layer size <tab> hidden layer size <tab> output layer size line number == 2 | acceptable error line number <= 3 | input 1 <tab> input 2 <tab> | <tab> output 1 <tab> output 2 <tab> : tab separator If multiple hidden layer sizes are specified on the first line , multiple hidden layers will be included in the built network. E.g. 5 <tab> 4 <tab> 3 <tab> 2 <tab> will built a net with 4 neurons at the input layer, 4 at the first hidden layer, 3 at the second hidden layer, and 2 at the output layer. Multiple input and output states can be specified in the training data in a similar way. Blank lines in the data set will be ignored. This is useful for separating training data for readability. See red_net_config.txt for an example network config file. """ if filename is None: # if no filename is supplied filename = self.filename # use filename given at init if filename != self.filename: # if new filename has been given self.filename = filename # store new filename config_file = open(filename, 'rb') # open the data file config_lines = list(csv.reader( config_file, delimiter='\t')) # read as csv with tab delim's layers = [int(size) for size in config_lines[0]] # split off layer config acceptable_error = float( config_lines[1][0]) # split off acceptable error dataset = config_lines[2:] # split off data set # build empty network TODO variable network types self.network = FeedForwardNetwork() # build the net # form layers TODO variable layer types input_size = layers[0] output_size = layers[-1] input_layer = LinearLayer(input_size) # form input layer hidden_layers = [SigmoidLayer(size) for size in layers[1:-1]] # form hidden layers output_layer = SigmoidLayer(output_size) # form output layer # add layers to network self.network.addInputModule(input_layer) # add input layer [self.network.addModule(layer) for layer in hidden_layers] # add hidden layers self.network.addOutputModule(output_layer) # add output layer # form connections TODO variable connection types and topologies in_to_h = FullConnection( input_layer, hidden_layers[0]) # form input -> first hidden h_to_out = FullConnection(hidden_layers[-1], output_layer) # form last hidden -> output h_to_h = [] # list for hidden conn's for x in range(len(hidden_layers)): # count through hidden layers if x is not len(hidden_layers) - 1: # if not at last hidden layer hh_conn = FullConnection( hidden_layers[x], # form hidden n -> hidden_layers[x + 1]) # hidden n + 1 connection h_to_h.append(hh_conn) # add to list of hidden conn's # add connections to network self.network.addConnection(in_to_h) # add input -> first hidden self.network.addConnection(h_to_out) # add last hidden -> output [self.network.addConnection(hh_conn) for hh_conn in h_to_h] # add hidden n -> hidden n + 1 # solidify network self.network.sortModules() # sort network topology # train network TODO variable trainer types self.dataset = SupervisedDataSet(input_size, output_size) # form data set for mapping in dataset: input_data = tuple( [float(input_state) for input_state in mapping[0:input_size]]) output_data = tuple( float(output_state) for output_state in mapping[0 - output_size:]) if input_data is not tuple([]): self.dataset.addSample(input_data, output_data) trainer = BackpropTrainer(self.network, self.dataset) # form trainer trained = False # set trained flag to False epoch = 0 # set epoch to 0 self._pause = False while trained is False: # as long as net isn't trained if self._pause: time.sleep(1) # if paused, wait a second else: epoch += 1 # increment epoch counter error = trainer.train() # reduce the error print('epoch : %i error : %f' % (epoch, error) ) # print current error if error < acceptable_error: # if error is acceptable trained = True # set trained flag to True
ds = SupervisedDataSet(2, 1) # input of 2 (elements: 0.8 and 0.4) elements, and output 1 element (element: 0.7) # adds the parameters ds.addSample((0.8, 0.4), (0.7)) ds.addSample((0.5, 0.7), (0.5)) ds.addSample((1.0, 0.8), (0.95)) # creates the neural network nn = buildNetwork(2, 4, 1, bias=True) # A network architecture: 2 input neurons, 4 neurons in the hidden layer and 1 neuron in the output layer, and places the bias as true - neural networks with bias generally learn faster # creates the trainer trainer = BackpropTrainer(nn, ds) # as arguments, we place the neural network plus the dataset (the parameters) # creates interaction # xrange is for python 2.7 # trains the neural network for i in xrange(2000): print(trainer.train()) # training of the neural network. The closer to 0, the better! # 2000 is the number of times it will be tested while True: sleep = float(raw_input('Sleep: \n')) # float value for the exit, ie: will ask how long the person slept study = float(raw_input('Study: \n')) # float value for output, ie: will ask how long the person studied z = nn.activate((sleep, study))[0] * 10.0 # activate is used to compute the values. This line serves to show if the person slept, or studied. The array at the end serves to get the first and only digit that the neural network creates. And, after being multiplied by 10, so that it is an equal measure of proof print('Accuracy of note: ', str(z)) # can predict the score according to the amount of hours spent and hours studied - shows this forecast
def main(): #sample patches from image database myCleaver = imageCleaver() #instantiate NN with size as defined in image cleaver class. #TODO: size should be input to imageCleaver class. # net = buildNetwork(myCleaver.sizePatches*myCleaver.sizePatches, (myCleaver.sizePatches*myCleaver.sizePatches)/2.0, myCleaver.sizePatches*myCleaver.sizePatches, bias = True) net = FeedForwardNetwork() inLayer = LinearLayer(myCleaver.sizePatches * myCleaver.sizePatches) hiddenLayer = SigmoidLayer( (myCleaver.sizePatches * myCleaver.sizePatches) / 4.0) outLayer = LinearLayer(myCleaver.sizePatches * myCleaver.sizePatches) net.addInputModule(inLayer) net.addModule(hiddenLayer) net.addOutputModule(outLayer) in_to_hidden = FullConnection(inLayer, hiddenLayer) hidden_to_out = FullConnection(hiddenLayer, outLayer) net.addConnection(in_to_hidden) net.addConnection(hidden_to_out) net.sortModules() # fileObject = open('pickledNet.dat','r') # net = pickle.load(fileObject) # fileObject.close() # net = NetworkReader.readFrom('filename.xml') # print(net.activate([2, 1])) #Put imageCleaver dataset into pyBrain dataset format. ds = SupervisedDataSet(myCleaver.sizePatches * myCleaver.sizePatches, myCleaver.sizePatches * myCleaver.sizePatches) for i in range(myCleaver.concImgArray.shape[1]): ds.addSample(myCleaver.concImgArray.T[i] / 256.0, myCleaver.concImgArray.T[i] / 256.0) # for inpt, target in ds: # print inpt, target trainer = BackpropTrainer(net, ds) for i in range(1): print(trainer.train()) # fileObject = open('pickledNet.dat', 'w') # pickle.dump(net, fileObject) # fileObject.close() # NetworkWriter.writeToFile(net, 'testNetwork8.xml') # saveNetParamsToFile(net) # loadNetParamsFromFile(net) imitationActivations = net.activate(myCleaver.concImgArray.T[0] / 256.0) imitation = np.reshape(imitationActivations, (myCleaver.sizePatches, myCleaver.sizePatches)) plt.figure(1) plt.title('Input vs output') plt.subplot(221) plt.imshow(myCleaver.patchDataBase[0], cmap=plt.cm.gray, interpolation='nearest', vmin=0, vmax=256) plt.title('input') plt.subplot(223) plt.imshow(imitation * 256, cmap=plt.cm.gray, interpolation='nearest', vmin=0, vmax=256) plt.title('imitation') ## plt.show() # print 'imitation' # print imitation*256 imitationActivations2 = net.activate(myCleaver.concImgArray.T[1] / 256.0) imitation2 = np.reshape(imitationActivations2, (myCleaver.sizePatches, myCleaver.sizePatches)) # plt.figure(2) # plt.title('Input vs output2') plt.subplot(222) plt.imshow(myCleaver.patchDataBase[1], cmap=plt.cm.gray, interpolation='nearest', vmin=0, vmax=256) plt.title('input2') plt.subplot(224) plt.imshow(imitation2 * 256, cmap=plt.cm.gray, interpolation='nearest', vmin=0, vmax=256) plt.title('imitation2') ## plt.subplot_tool() plt.show() # print 'imitation2' # print imitation2*256# ################################################################# #calculate and show each hidden nodes learned function, i.e. which input vector maximally excites the hidden node, with constrain ||x||^2 <=1 ################################################################# learned = [] #convert dims from float to integer i2hid = int(in_to_hidden.indim) i2hod = int(in_to_hidden.outdim) print i2hid print i2hod #go through each hidden node for i in range(i2hod): print('i: ', i) one_learned = [] sumOfWeights = 0 #go through weights for the ith hidden node, sum weights for j in range(i2hid): print('j1: ', j) #add to sum, the value of connection between input sumOfWeights += (in_to_hidden.params[i * i2hid + j])**2 #for each input, calculate effect on hidden node by summing all om inputs for j in range(i2hid): print('j2: ', j) one_learned.append(in_to_hidden.params[i * i2hid + j] / math.sqrt(sumOfWeights)) learned.append(one_learned) fig3, axes = plt.subplots(nrows=int(math.sqrt(len(learned))), ncols=(int(math.sqrt(len(learned))))) for dat, ax in zip(learned, axes.flat): # The vmin and vmax arguments specify the color limits im = ax.imshow(np.reshape( dat, (myCleaver.sizePatches, myCleaver.sizePatches)), cmap=plt.cm.gray, interpolation='nearest') #print(len(myCleaver.patchDataBase)) #print(myCleaver.getImages) #getTrainingSet() # print 'np.array(net.activate(myCleaver.concImgArray.T[0]/256.0))' # print net.activate(myCleaver.concImgArray.T[0]/256.0) # print 'np.array(net.activate(myCleaver.concImgArray.T[1]/256.0))' # print net.activate(myCleaver.concImgArray.T[1]/256.0) # print('') # # print('the two inputs') # print(ds.getSample(0)) # print(ds.getSample(1)) # print('') # # print('original inputs') # print(myCleaver.concImgArray.T[0]/256.0) # print(myCleaver.concImgArray.T[1]/256.0) # print('') # # print('first activation') # print(net.activate(myCleaver.concImgArray.T[0]/256.0)) # print('second activation') # print(net.activate(myCleaver.concImgArray.T[1]/256.0)) # print(net.params) # print(net) # print(net.outmodules) # # # net = buildNetwork(4,2,4, bias = True) # print('simple net activation') # print(net.activate((1,2,3,4))) # print('simple net activation2') # print(net.activate((5,4,3,2))) # print('') # for mod in net.modules: print "Module:", mod.name if mod.paramdim > 0: print "--parameters:", mod.params for conn in net.connections[mod]: print "-connection to", conn.outmod.name if conn.paramdim > 0: print "- parameters", conn.params if hasattr(net, "recurrentConns"): print "Recurrent connections" for conn in net.recurrentConns: print "-", conn.inmod.name, " to", conn.outmod.name if conn.paramdim > 0: print "- parameters", conn.params
net.randomize() #--------------------- BUILD TRAINER --------------------------- trainer = BackpropTrainer(net, dataset=traindata, learningrate=0.1) #--------------------- TRAIN on trainingset --------------------------- mse, score, i = 1, 1, 0 if (nr_pos < 1000): cut_score = 0.0001 else: cut_score = 0.001 if (args.c == "True"): while (i < 1000 and score > cut_score): score = trainer.train() i = i + 1 #print "Score: ", score, " loop: ", i print >> FH, "Score: ", score, " loop: ", i FH.flush() mse = score # Store last MSE value as network MSE mse = round(mse, 4) netfilename = netname + ".mse_" + str(mse) + ".net" NetworkWriter.writeToFile(trainer.module, netfilename) # Save network as XML file #--------------------- TEST NETWORK on testset --------------------------- net2 = NetworkReader.readFrom(netfilename) rp, rn, tp09, fp09, fn09, tn09 = 0, 0, 0, 0, 0, 0
hiddenclass = SigmoidLayer, bias = False) print(rede['in']) print(rede['hidden0']) print(rede['out']) print(rede['bias'])''' rede = buildNetwork(2, 3, 1) base = SupervisedDataSet(2, 1) base.addSample((0, 0), (0, )) base.addSample((0, 1), (1, )) base.addSample((1, 0), (1, )) base.addSample((1, 1), (0, )) #print(base['input']) #print(base['target']) treinamento = BackpropTrainer(rede, dataset=base, learningrate=0.01, momentum=0.06) for i in range(1, 100000): erro = treinamento.train() if i % 20000 == 0: print(f"Erro: {erro}") print(rede.activate([0, 0])) print(rede.activate([0, 1])) print(rede.activate([1, 0])) print(rede.activate([1, 1]))
'''net = buildNetwork(2, 3, 1, outclass = SoftmaxLayer, hiddenclass = SigmoidLayer, bias = False) print(net['in']) # Input Layer print(net['hidden0']) print(net['out']) print(net['bias'])''' net = buildNetwork(2, 3, 1) base = SupervisedDataSet(2,1) # 2 prevision attributes and 1 class base.addSample((0,0), (0,)) base.addSample((0,1), (1,)) base.addSample((1,0), (1,)) base.addSample((1,1), (0,)) print(base['input']) print(base['target']) training = BackpropTrainer(net, dataset=base, learningrate=0.01, momentum=0.06) for i in range(1, 30000): error = training.train() if i % 1000 == 0: print("Erro: %s" % error) print(net.activate([0,0])) print(net.activate([1,0])) print(net.activate([0,1])) print(net.activate([1,1]))
hidden_layer_size = X_train.shape[1] / 2 #5 feedfwdNeuNet = buildNetwork( networkTrainDataset.indim, hidden_layer_size, networkTrainDataset.outdim, bias=True, outclass=SoftmaxLayer ) #buildNetwork( X_train.shape[1], 5, len(np.unique(y_test)), outclass=SoftmaxLayer ) trainer = BackpropTrainer(feedfwdNeuNet, dataset=networkTrainDataset, momentum=0.1, verbose=False, weightdecay=0.01) print "Training MLP..." for i in range(epochs): err = trainer.train() rmse = sqrt(err) print "Root Mean Square Error, epoch {}: {}".format( i + 1, rmse), " and Error:", err print "Training MLP complete for ", epochs, " epochs" #Prediction # # calculate the performance y_probabilities = classifier_bayes.predict_proba(X_test)[:, 1] y_predicted = classifier_bayes.predict(X_test) performance.printResults(y_probabilities, y_predicted, 'Naive Bayes') y_probabilities = classifier_forest.predict_proba(X_test)[:, 1] y_predicted = classifier_forest.predict(X_test) performance.printResults(y_probabilities, y_predicted, 'Random Forests')
def stock_data(symbol): data = list() if symbol == 'SPY': url = '/app/daily_historical_prices/spy.csv' # Heroku route # url = '/Users/deepakshah/Documents/Digital Crafts/Machine Learning/Financial Modeling/daily_historical_prices/spy.csv' # local route elif symbol == 'AAPL': url = '/app/daily_historical_prices/aapl.csv' # Heroku route # url = '/Users/deepakshah/Documents/Digital Crafts/Machine Learning/Financial Modeling/daily_historical_prices/aapl.csv' # local route elif symbol == 'GOOG': url = '/app/daily_historical_prices/goog.csv' # Heroku route # url = '/Users/deepakshah/Documents/Digital Crafts/Machine Learning/Financial Modeling/daily_historical_prices/goog.csv' # local route elif symbol == 'FB': url = '/app/daily_historical_prices/fb.csv' # Heroku route # url = '/Users/deepakshah/Documents/Digital Crafts/Machine Learning/Financial Modeling/daily_historical_prices/fb.csv' # local route elif symbol == 'AMZN': url = '/app/daily_historical_prices/amzn.csv' # Heroku route # url = '/Users/deepakshah/Documents/Digital Crafts/Machine Learning/Financial Modeling/daily_historical_prices/amzn.csv' # local route elif symbol == 'DIS': url = '/app/daily_historical_prices/dis.csv' # Heroku route # url = '/Users/deepakshah/Documents/Digital Crafts/Machine Learning/Financial Modeling/daily_historical_prices/dis.csv' # local route elif symbol == 'MSFT': url = '/app/daily_historical_prices/msft.csv' # Heroku route # url = '/Users/deepakshah/Documents/Digital Crafts/Machine Learning/Financial Modeling/daily_historical_prices/msft.csv' # local route with open(url, 'r') as f: reader = csv.reader(f) for row in reader: data.append(row) data = numpy.array(data) data = data[1:, 1:] data = data.astype(float) labels = ((data[:, 3] - data[:, 0]) > 0).astype(int) data, labels = multiple_days_forward(data, 1) print numpy.shape(labels) print numpy.shape(data) def p_high(t, X): return max(X[:-t]) def p_low(t, X): return min(X[:-t]) def volume_high(t, X): return max(X[:-t]) def volume_low(t, X): return min(X[:-t]) # Feature Extraction def extract_features(data, indices): data = data[:, [0, 1, 2, 3, 5]] # remove the first row because it is a header data2 = data[1:, :] features = data[:-1] - data2 price_high = p_high(5, data[:, 1]) price_low = p_low(5, data[:, 2]) vol_high = volume_high(5, data[:, 4]) vol_low = volume_low(5, data[:, 4]) var1_diff_by_highlow = features[:, 0] / float(price_high - price_low) var2_diff_by_highlow = features[:, 1] / float(price_high - price_low) mov_avg_by_data = list() for i in range(len(features)): mov_avg_by_data.append( numpy.mean(data[:i + 1, :], axis=0) / data[i, :]) mov_avg_by_data = numpy.array(mov_avg_by_data) features = numpy.column_stack((features, var1_diff_by_highlow, var2_diff_by_highlow, mov_avg_by_data)) print numpy.shape(features) return features[:, indices], data features, data = extract_features(data, [0, 1, 2, 3, 4]) train_features = features[:1000] test_features = features[1000:] train_labels = labels[:1000] test_labels = labels[1000:-1] clf = svm.SVC(kernel='rbf', C=1.2, gamma=0.001) # rbf kernel clf.fit(train_features, train_labels) predicted = clf.predict(test_features) predicted_svm = predicted[-1] if predicted_svm > 0: predicted_svm = "Positive" else: predicted_svm = "Negative" accuracy_svm = accuracy_score(test_labels, predicted) precision_svm = recall_score(test_labels, predicted) recall_svm = precision_score(test_labels, predicted) print "Accuracy: ", accuracy_svm print "Precision: ", precision_svm print "Recall: ", recall_svm ## RNN net = buildNetwork(5, 20, 1, hiddenclass=LSTMLayer, outclass=SigmoidLayer, recurrent=True) ds = ClassificationDataSet(5, 1) for i, j in zip(train_features, train_labels): ds.addSample(i, j) trainer = BackpropTrainer(net, ds) epochs = 10 for i in range(epochs): trainer.train() predicted = list() for i in test_features: predicted.append(int(net.activate(i) > 0.5)) predicted = numpy.array(predicted) predicted_rnn = predicted[-1] if predicted_rnn > 0: predicted_rnn = "Positive" else: predicted_rnn = "Negative" accuracy_rnn = accuracy_score(test_labels, predicted) recall_rnn = recall_score(test_labels, predicted) precision_rnn = precision_score(test_labels, predicted) print "Accuracy: ", accuracy_score(test_labels, predicted) print "Recall: ", recall_score(test_labels, predicted) print "Precision: ", precision_score(test_labels, predicted) data_algo_daily = { 'SVM Accuracy': accuracy_svm, 'SVM Precision': precision_svm, 'SVM Recall': recall_svm, 'Predicted SVM': predicted_svm, 'RNN Accuracy': accuracy_rnn, 'RNN Precision': precision_rnn, 'RNN Recall': recall_rnn, 'Predicted RNN': predicted_rnn } return data_algo_daily