def visulizeDataSet(network, data, seqno, in_labels, out_labels): seq = data.getSequence(seqno) tmpDs = SequentialDataSet(data.indim, data.outdim) tmpDs.newSequence() for i in xrange(data.getSequenceLength(seqno)): tmpDs.addSample(seq[0][i], seq[1][i]) nplots = len(in_labels) + len(out_labels) for i in range(len(in_labels)): p = PL.subplot(nplots, 1, i + 1) p.clear() p.plot(tmpDs['input'][:, i]) p.set_ylabel(in_labels[i]) for i in range(len(out_labels)): p = PL.subplot(nplots, 1, i + 1 + len(in_labels)) p.clear() output = ModuleValidator.calculateModuleOutput(network, tmpDs) p.plot(tmpDs['target'][:, i], label='train') p.plot(output[:, i], label='sim') p.legend() p.set_ylabel(out_labels[i])
def train(self, params): self.net.reset() ds = SequentialDataSet(self.nDimInput, self.nDimOutput) trainer = RPropMinusTrainer(self.net, dataset=ds, verbose=False) history = self.window(self.history, params) resets = self.window(self.resets, params) for i in xrange(params['prediction_nstep'], len(history)): if not resets[i-1]: ds.addSample(self.inputEncoder.encode(history[i-params['prediction_nstep']]), self.outputEncoder.encode(history[i][0])) if resets[i]: ds.newSequence() # print ds.getSample(0) # print ds.getSample(1) # print ds.getSample(1000) # print " training data size", ds.getLength(), " len(history) ", len(history), " self.history ", len(self.history) # print ds if len(history) > 1: trainer.trainEpochs(params['num_epochs']) self.net.reset() for i in xrange(len(history) - params['prediction_nstep']): symbol = history[i] output = self.net.activate(ds.getSample(i)[0]) if resets[i]: self.net.reset()
def main(): generated_data = [0 for i in range(10000)] rate, data = get_data_from_wav("../../data/natabhairavi_violin.wav") data = data[1000:190000] print("Got wav") ds = SequentialDataSet(1, 1) for sample, next_sample in zip(data, cycle(data[1:])): ds.addSample(sample, next_sample) net = buildNetwork(1, 5, 1, hiddenclass=LSTMLayer, outputbias=False, recurrent=True) trainer = RPropMinusTrainer(net, dataset=ds) train_errors = [] # save errors for plotting later EPOCHS_PER_CYCLE = 5 CYCLES = 10 EPOCHS = EPOCHS_PER_CYCLE * CYCLES for i in xrange(CYCLES): trainer.trainEpochs(EPOCHS_PER_CYCLE) train_errors.append(trainer.testOnData()) epoch = (i+1) * EPOCHS_PER_CYCLE print("\r epoch {}/{}".format(epoch, EPOCHS), end="") stdout.flush() # predict new values old_sample = [100] for i in xrange(500000): new_sample = net.activate(old_sample) old_sample = new_sample generated_data[i] = new_sample[0] print(new_sample) wavfile.write("../../output/test.wav", rate, np.array(generated_data))
def train(self, params): n = params['encoding_num'] net = buildNetwork(n, params['num_cells'], n, hiddenclass=LSTMLayer, bias=True, outputbias=params['output_bias'], recurrent=True) net.reset() ds = SequentialDataSet(n, n) trainer = RPropMinusTrainer(net, dataset=ds) history = self.window(self.history, params) resets = self.window(self.resets, params) for i in xrange(1, len(history)): if not resets[i-1]: ds.addSample(self.encoder.encode(history[i-1]), self.encoder.encode(history[i])) if resets[i]: ds.newSequence() if len(history) > 1: trainer.trainEpochs(params['num_epochs']) net.reset() for i in xrange(len(history) - 1): symbol = history[i] output = self.net.activate(self.encoder.encode(symbol)) predictions = self.encoder.classify(output, num=params['num_predictions']) if resets[i]: net.reset() return net
def getPyBrainDataSet(sequence, nTrain, predictionStep=1, useTimeOfDay=True, useDayOfWeek=True): print "generate a pybrain dataset of sequences" print "the training data contains ", str(nTrain - predictionStep), "records" inDim = 1 + int(useTimeOfDay) + int(useDayOfWeek) ds = SequentialDataSet(inDim, 1) if useTimeOfDay: print "include time of day as input field" if useDayOfWeek: print "include day of week as input field" for i in xrange(nTrain - predictionStep): if useTimeOfDay and useDayOfWeek: sample = np.array([ sequence['data'][i], sequence['timeofday'][i], sequence['dayofweek'][i] ]) elif useTimeOfDay: sample = np.array([sequence['data'][i], sequence['timeofday'][i]]) elif useDayOfWeek: sample = np.array([sequence['data'][i], sequence['dayofweek'][i]]) else: sample = np.array([sequence['data'][i]]) ds.addSample(sample, sequence['data'][i + predictionStep]) return ds
def visulizeDataSet(network, data, seqno, in_labels, out_labels): seq = data.getSequence(seqno) tmpDs = SequentialDataSet(data.indim, data.outdim) tmpDs.newSequence() for i in xrange(data.getSequenceLength(seqno)): tmpDs.addSample(seq[0][i], seq[1][i]) nplots = len(in_labels) + len(out_labels) for i in range(len(in_labels)): p = PL.subplot(nplots, 1, i + 1) p.clear() p.plot(tmpDs['input'][:, i]) p.set_ylabel(in_labels[i]) for i in range(len(out_labels)): p = PL.subplot(nplots, 1, i + 1 + len(in_labels)) p.clear() output = ModuleValidator.calculateModuleOutput(network, tmpDs) p.plot(tmpDs['target'][:, i], label='train') p.plot(output[:, i], label='sim') p.legend() p.set_ylabel(out_labels[i])
def train(self, params): """ Train LSTM network on buffered dataset history After training, run LSTM on history[:-1] to get the state correct :param params: :return: """ if params['reset_every_training']: n = params['encoding_num'] self.net = buildNetwork(n, params['num_cells'], n, hiddenclass=LSTMLayer, bias=True, outputbias=params['output_bias'], recurrent=True) self.net.reset() # prepare training dataset ds = SequentialDataSet(params['encoding_num'], params['encoding_num']) history = self.window(self.history, params) resets = self.window(self.resets, params) for i in xrange(1, len(history)): if not resets[i - 1]: ds.addSample(self.encoder.encode(history[i - 1]), self.encoder.encode(history[i])) if resets[i]: ds.newSequence() if params['num_epochs'] > 1: trainer = RPropMinusTrainer(self.net, dataset=ds, verbose=params['verbosity'] > 0) if len(history) > 1: trainer.trainEpochs(params['num_epochs']) # run network on buffered dataset after training to get the state right self.net.reset() for i in xrange(len(history) - 1): symbol = history[i] output = self.net.activate(self.encoder.encode(symbol)) self.encoder.classify(output, num=params['num_predictions']) if resets[i]: self.net.reset() else: self.trainer.setData(ds) self.trainer.train() # run network on buffered dataset after training to get the state right self.net.reset() for i in xrange(len(history) - 1): symbol = history[i] output = self.net.activate(self.encoder.encode(symbol)) self.encoder.classify(output, num=params['num_predictions']) if resets[i]: self.net.reset()
def train(self, params): """ Train LSTM network on buffered dataset history After training, run LSTM on history[:-1] to get the state correct :param params: :return: """ if params['reset_every_training']: n = params['encoding_num'] self.net = buildNetwork(n, params['num_cells'], n, hiddenclass=LSTMLayer, bias=True, outputbias=params['output_bias'], recurrent=True) self.net.reset() # prepare training dataset ds = SequentialDataSet(params['encoding_num'], params['encoding_num']) history = self.window(self.history, params) resets = self.window(self.resets, params) for i in xrange(1, len(history)): if not resets[i - 1]: ds.addSample(self.encoder.encode(history[i - 1]), self.encoder.encode(history[i])) if resets[i]: ds.newSequence() print "Train LSTM network on buffered dataset of length ", len(history) if params['num_epochs'] > 1: trainer = RPropMinusTrainer(self.net, dataset=ds, verbose=params['verbosity'] > 0) if len(history) > 1: trainer.trainEpochs(params['num_epochs']) # run network on buffered dataset after training to get the state right self.net.reset() for i in xrange(len(history) - 1): symbol = history[i] output = self.net.activate(self.encoder.encode(symbol)) self.encoder.classify(output, num=params['num_predictions']) if resets[i]: self.net.reset() else: self.trainer.setData(ds) self.trainer.train() # run network on buffered dataset after training to get the state right self.net.reset() for i in xrange(len(history) - 1): symbol = history[i] output = self.net.activate(self.encoder.encode(symbol)) self.encoder.classify(output, num=params['num_predictions']) if resets[i]: self.net.reset()
def rnn_dataset(self, X, y): # Create an empty dataset. ds = SequentialDataSet(len(X.columns), len(y.columns)) # And add all rows... for i in range(0, len(X.index)): ds.addSample(tuple(X.ix[i, :].values), tuple(y.ix[i, :].values)) return ds
def rnn_dataset(X, y): """ Create a recurrent neural network dataset according to the pybrain specification and return this new format. """ # Create an empty dataset ds = SequentialDataSet(len(X.columns), len(y.columns)) # And add all rows for i in range(0, len(X.index)): ds.addSample(tuple(X.iloc[i, :].values), tuple(y.iloc[i, :].values)) return ds
def train(self, params, verbose=False): if params['reset_every_training']: if verbose: print 'create lstm network' random.seed(6) if params['output_encoding'] == None: self.net = buildNetwork(self.nDimInput, params['num_cells'], self.nDimOutput, hiddenclass=LSTMLayer, bias=True, outputbias=True, recurrent=True) elif params['output_encoding'] == 'likelihood': self.net = buildNetwork(self.nDimInput, params['num_cells'], self.nDimOutput, hiddenclass=LSTMLayer, bias=True, outclass=SigmoidLayer, recurrent=True) self.net.reset() ds = SequentialDataSet(self.nDimInput, self.nDimOutput) networkInput = self.window(self.networkInput, params) targetPrediction = self.window(self.targetPrediction, params) # prepare a training data-set using the history for i in xrange(len(networkInput)): ds.addSample(self.inputEncoder.encode(networkInput[i]), self.outputEncoder.encode(targetPrediction[i])) mycount = 0 if params['num_epochs'] > 1: trainer = RPropMinusTrainer(self.net, dataset=ds, verbose=verbose) if verbose: print " train LSTM on ", len( ds), " records for ", params['num_epochs'], " epochs " if len(networkInput) > 1: trainer.trainEpochs(params['num_epochs']) else: self.trainer.setData(ds) self.trainer.train() # run through the training dataset to get the lstm network state right self.net.reset() for i in xrange(len(networkInput)): self.net.activate(ds.getSample(i)[0])
def ltsmXY(tin, tout, title='ltsm.png'): #datain = zip(tin[:-3], tin[1:-2], tin[2:-1]) #datain = zip(tin[:-8], tin[1:-7], tin[2:-6], tin[3:-5], tin[4:-4], tin[5:-3],tin[6:-2], tin[7:-1]) #datain = zip(tin[:-12], tin[1:-11], tin[2:-10], tin[3:-9], tin[4:-8], tin[5:-7],tin[6:-6], tin[7:-5], tin[8:-4], tin[9:-3], tin[10:-2], tin[11:-1]) datain = zip(tin[:-16], tin[1:-15], tin[2:-14], tin[3:-13], tin[4:-12], tin[5:-11],tin[6:-10], tin[7:-9], tin[8:-8], tin[9:-7], tin[10:-6], tin[11:-5], tin[12:-4], tin[13:-3], tin[14:-2], tin[15:-1]) #dataout = tout[3:] #dataout = tout[8:] #dataout = tout[12:] dataout = tout[16:] #ds = SequentialDataSet(3, 1) #ds = SequentialDataSet(8, 1) #ds = SequentialDataSet(12, 1) ds = SequentialDataSet(16, 1) for x, y in zip(datain[:len(datain)/2], dataout[:len(datain)/2]): ds.addSample(x, y) # add layers until overfitting the training data #net = buildNetwork(3,5,1,hiddenclass=LSTMLayer, outputbias=False, recurrent=True) #net = buildNetwork(8, 8, 1, hiddenclass=LSTMLayer, outputbias=False, recurrent=True) #net = buildNetwork(12, 20, 1, hiddenclass=LSTMLayer, outputbias=False, recurrent=True) net = buildNetwork(16, 20, 1, hiddenclass=LSTMLayer, outputbias=False, recurrent=True) trainer = RPropMinusTrainer(net, dataset=ds) train_errors = [] EPOCHS_PER_CYCLE = 5 CYCLES = 100 EPOCHS = EPOCHS_PER_CYCLE * CYCLES for i in xrange(CYCLES): trainer.trainEpochs(EPOCHS_PER_CYCLE) train_errors.append(trainer.testOnData()) epoch = (i+1) * EPOCHS_PER_CYCLE #print "\r epoch {}/{}".format(epoch, EPOCHS) stdout.flush() print "final error =", train_errors[-1] pred_out = [] for i in range(len(datain)): pred_out.append(net.activate(datain[i])) fig = plt.figure() #tout[16:].plot(ax=ax, title='Occupancy') plt.plot(tout[16:].index, tout[16:], 'y', linewidth=1.5) plt.plot(tout[16:].index, pred_out, 'b+') plt.legend(['Occupancy', 'LTSM']) fig.tight_layout() plt.savefig(title,inches='tight')
def create_train_set (consumption): #create train/test set global active_max ds = SequentialDataSet(1, 1) consumption_data = normalize (consumption) active_max = max(consumption_data[1],active_max) consumption = consumption_data[0] size = len (consumption) for i in range(0, size-1): ds.addSample(consumption[i], consumption[i+1]) return ds
def create_train_set(consumption): #create train/test set global active_max ds = SequentialDataSet(1, 1) consumption_data = normalize(consumption) active_max = max(consumption_data[1], active_max) consumption = consumption_data[0] size = len(consumption) for i in range(0, size - 1): ds.addSample(consumption[i], consumption[i + 1]) return ds
def buildAppropriateDataset(module): """ build a sequential dataset with 2 sequences of 3 samples, with arndom input and target values, but the appropriate dimensions to be used on the provided module. """ if module.sequential: d = SequentialDataSet(module.indim, module.outdim) for dummy in range(2): d.newSequence() for dummy in range(3): d.addSample(randn(module.indim), randn(module.outdim)) else: d = SupervisedDataSet(module.indim, module.outdim) for dummy in range(3): d.addSample(randn(module.indim), randn(module.outdim)) return d
def ltsm(data): from pybrain.datasets import SequentialDataSet from itertools import cycle datain = zip(data[:-6], data[1:-5], data[2:-4], data[3:-3], data[4:-2], data[5:-1]) dataout = data[6:] ds = SequentialDataSet(6, 1) for x, y in zip(datain, dataout): ds.addSample(x, y) from pybrain.tools.shortcuts import buildNetwork from pybrain.structure.modules import LSTMLayer net = buildNetwork(6, 7, 1, hiddenclass=LSTMLayer, outputbias=False, recurrent=True) from pybrain.supervised import RPropMinusTrainer from sys import stdout trainer = RPropMinusTrainer(net, dataset=ds) train_errors = [] EPOCHS_PER_CYCLE = 5 CYCLES = 100 EPOCHS = EPOCHS_PER_CYCLE * CYCLES for i in xrange(CYCLES): trainer.trainEpochs(EPOCHS_PER_CYCLE) train_errors.append(trainer.testOnData()) epoch = (i+1) * EPOCHS_PER_CYCLE #print "\r epoch {}/{}".format(epoch, EPOCHS) stdout.flush() print "final error =", train_errors[-1] ''' plt.figure() plt.plot(range(0, EPOCHS, EPOCHS_PER_CYCLE), train_errors) plt.xlabel('epoch') plt.ylabel('error') plt.show() ''' test_error = 0. cnt = 0 for sample, target in ds.getSequenceIterator(0): #print "sample = ", sample #print "predicted next sample = %4.1f" % net.activate(sample) #print "actual next sample = %4.1f" % target test_error += abs(net.activate(sample) - target) cnt += 1 test_error /= cnt print "test (train) error =", test_error
def buildAppropriateDataset(module): """ build a sequential dataset with 2 sequences of 3 samples, with arndom input and target values, but the appropriate dimensions to be used on the provided module. """ if module.sequential: d = SequentialDataSet(module.indim, module.outdim) for dummy in range(2): d.newSequence() for dummy in range(3): d.addSample(randn(module.indim), randn(module.outdim)) else: d = SupervisedDataSet(module.indim, module.outdim) for dummy in range(3): d.addSample(randn(module.indim), randn(module.outdim)) return d
def getPyBrainDataSetScalarEncoder(sequence, nTrain, encoderInput, encoderOutput, predictionStep=1, useTimeOfDay=True, useDayOfWeek=True): """ Use scalar encoder for the data :param sequence: :param nTrain: :param predictionStep: :param useTimeOfDay: :param useDayOfWeek: :return: """ print "generate a pybrain dataset of sequences" print "the training data contains ", str(nTrain - predictionStep), "records" if encoderInput is None: inDim = 1 + int(useTimeOfDay) + int(useDayOfWeek) else: inDim = encoderInput.n + int(useTimeOfDay) + int(useDayOfWeek) if encoderOutput is None: outDim = 1 else: outDim = encoderOutput.n ds = SequentialDataSet(inDim, outDim) if useTimeOfDay: print "include time of day as input field" if useDayOfWeek: print "include day of week as input field" for i in xrange(nTrain - predictionStep): sample = getSingleSample(i, sequence, useTimeOfDay, useDayOfWeek) if encoderOutput is None: dataSDROutput = [sequence['normdata'][i + predictionStep]] else: dataSDROutput = encoderOutput.encode( sequence['data'][i + predictionStep]) ds.addSample(sample, dataSDROutput) return ds
def train(d, cycles=100, epochs_per_cycle=7): ds = SequentialDataSet(1, 1) net = buildNetwork(1, 5, 1, hiddenclass=LSTMLayer, outputbias=False, recurrent=False) for sample, next_sample in zip(d, cycle(d[1:])): ds.addSample(sample, next_sample) trainer = RPropMinusTrainer(net, dataset=ds) train_errors = [] # save errors for plotting later for i in xrange(cycles): trainer.trainEpochs(epochs_per_cycle) train_errors.append(trainer.testOnData()) stdout.flush() return net, train_errors
def create_train_set(open_price, close_price): global open_max global close_max ds = SequentialDataSet(1, 1) open_data = normalize(open_price) close_data = normalize(close_price) open_max = open_data[1] close_max = close_data[1] open_price = open_data[0] close_price = close_data[0] size = len(open_price) for i in range(0, size): ds.addSample(open_price[i], close_price[i]) return ds
def create_train_set (open_price, close_price): global open_max global close_max ds = SequentialDataSet(1, 1) open_data = normalize (open_price) close_data = normalize (close_price) open_max = open_data[1] close_max = close_data[1] open_price = open_data[0] close_price = close_data[0] size = len (open_price) for i in range(0, size): ds.addSample(open_price[i], close_price[i]) return ds
def train(context, trainX, trainY): ds = SequentialDataSet(4, 1) for dataX, dataY in zip(trainX, trainY): ds.addSample(dataX, dataY) net = buildNetwork(4, 1, 1, hiddenclass=LSTMLayer, outputbias=False, recurrent=True) trainer = RPropMinusTrainer(net, dataset=ds) EPOCHS_PER_CYCLE = 5 CYCLES = 5 for i in range(CYCLES): trainer.trainEpochs(EPOCHS_PER_CYCLE) return net, trainer.testOnData()
def getPyBrainDataSetScalarEncoder(sequence, nTrain, encoderInput, encoderOutput, predictionStep=1, useTimeOfDay=True, useDayOfWeek=True): """ Use scalar encoder for the data :param sequence: :param nTrain: :param predictionStep: :param useTimeOfDay: :param useDayOfWeek: :return: """ print "generate a pybrain dataset of sequences" print "the training data contains ", str(nTrain-predictionStep), "records" if encoderInput is None: inDim = 1 + int(useTimeOfDay) + int(useDayOfWeek) else: inDim = encoderInput.n + int(useTimeOfDay) + int(useDayOfWeek) if encoderOutput is None: outDim = 1 else: outDim = encoderOutput.n ds = SequentialDataSet(inDim, outDim) if useTimeOfDay: print "include time of day as input field" if useDayOfWeek: print "include day of week as input field" for i in xrange(nTrain-predictionStep): sample = getSingleSample(i, sequence, useTimeOfDay, useDayOfWeek) if encoderOutput is None: dataSDROutput = [sequence['normdata'][i+predictionStep]] else: dataSDROutput = encoderOutput.encode(sequence['data'][i+predictionStep]) ds.addSample(sample, dataSDROutput) return ds
def handle(self, *args, **options): ticker = args[0] print("****** STARTING PREDICTOR " + ticker + " ******* ") prices = Price.objects.filter( symbol=ticker).order_by('-created_on').values_list('price', flat=True) data = normalization(list(prices[0:NUM_MINUTES_BACK].reverse())) data = [int(x * MULT_FACTOR) for x in data] ds = SequentialDataSet(1, 1) for sample, next_sample in zip(data, cycle(data[1:])): ds.addSample(sample, next_sample) net = buildNetwork(1, 5, 1, hiddenclass=LSTMLayer, outputbias=False, recurrent=True) trainer = RPropMinusTrainer(net, dataset=ds) train_errors = [] # save errors for plotting later EPOCHS_PER_CYCLE = 5 CYCLES = 100 EPOCHS = EPOCHS_PER_CYCLE * CYCLES for i in xrange(CYCLES): trainer.trainEpochs(EPOCHS_PER_CYCLE) train_errors.append(trainer.testOnData()) epoch = (i + 1) * EPOCHS_PER_CYCLE print("\r epoch {}/{}".format(epoch, EPOCHS), end="") stdout.flush() print() print("final error =", train_errors[-1]) for sample, target in ds.getSequenceIterator(0): show_pred_sample = net.activate(sample) / MULT_FACTOR show_sample = sample / MULT_FACTOR show_target = target / MULT_FACTOR show_diff = show_pred_sample - show_target show_diff_pct = 100 * show_diff / show_pred_sample print("{} => {}, act {}. ({}%)".format( show_sample[0], round(show_pred_sample[0], 3), show_target[0], int(round(show_diff_pct[0], 0))))
def train(self, params, verbose=False): if params['reset_every_training']: if verbose: print 'create lstm network' random.seed(6) if params['output_encoding'] == None: self.net = buildNetwork(self.nDimInput, params['num_cells'], self.nDimOutput, hiddenclass=LSTMLayer, bias=True, outputbias=True, recurrent=True) elif params['output_encoding'] == 'likelihood': self.net = buildNetwork(self.nDimInput, params['num_cells'], self.nDimOutput, hiddenclass=LSTMLayer, bias=True, outclass=SigmoidLayer, recurrent=True) self.net.reset() ds = SequentialDataSet(self.nDimInput, self.nDimOutput) networkInput = self.window(self.networkInput, params) targetPrediction = self.window(self.targetPrediction, params) # prepare a training data-set using the history for i in xrange(len(networkInput)): ds.addSample(self.inputEncoder.encode(networkInput[i]), self.outputEncoder.encode(targetPrediction[i])) if params['num_epochs'] > 1: trainer = RPropMinusTrainer(self.net, dataset=ds, verbose=verbose) if verbose: print " train LSTM on ", len(ds), " records for ", params['num_epochs'], " epochs " if len(networkInput) > 1: trainer.trainEpochs(params['num_epochs']) else: self.trainer.setData(ds) self.trainer.train() # run through the training dataset to get the lstm network state right self.net.reset() for i in xrange(len(networkInput)): self.net.activate(ds.getSample(i)[0])
def trainNetwork(dirname): numFeatures = 5000 ds = SequentialDataSet(numFeatures, 1) tracks = glob.glob(os.path.join(dirname, 'train??.wav')) for t in tracks: track = os.path.splitext(t)[0] # load training data print "Reading %s..." % track data = numpy.genfromtxt(track + '_seg.csv', delimiter=",") labels = numpy.genfromtxt(track + 'REF.txt', delimiter='\t')[0::10,1] numData = data.shape[0] # add the input to the dataset print "Adding to dataset..." ds.newSequence() for i in range(numData): ds.addSample(data[i], (labels[i],)) # initialize the neural network print "Initializing neural network..." net = buildNetwork(numFeatures, 50, 1, hiddenclass=LSTMLayer, outputbias=False, recurrent=True) # train the network on the dataset print "Training neural net" trainer = RPropMinusTrainer(net, dataset=ds) ## trainer.trainUntilConvergence(maxEpochs=50, verbose=True, validationProportion=0.1) error = -1 for i in range(100): new_error = trainer.train() print "error: " + str(new_error) if abs(error - new_error) < 0.1: break error = new_error # save the network print "Saving neural network..." NetworkWriter.writeToFile(net, os.path.basename(dirname) + 'net')
def main(): generated_data = [0 for i in range(10000)] rate, data = get_data_from_wav("../../data/natabhairavi_violin.wav") data = data[1000:190000] print("Got wav") ds = SequentialDataSet(1, 1) for sample, next_sample in zip(data, cycle(data[1:])): ds.addSample(sample, next_sample) net = buildNetwork(1, 5, 1, hiddenclass=LSTMLayer, outputbias=False, recurrent=True) trainer = RPropMinusTrainer(net, dataset=ds) train_errors = [] # save errors for plotting later EPOCHS_PER_CYCLE = 5 CYCLES = 10 EPOCHS = EPOCHS_PER_CYCLE * CYCLES for i in xrange(CYCLES): trainer.trainEpochs(EPOCHS_PER_CYCLE) train_errors.append(trainer.testOnData()) epoch = (i + 1) * EPOCHS_PER_CYCLE print("\r epoch {}/{}".format(epoch, EPOCHS), end="") stdout.flush() # predict new values old_sample = [100] for i in xrange(500000): new_sample = net.activate(old_sample) old_sample = new_sample generated_data[i] = new_sample[0] print(new_sample) wavfile.write("../../output/test.wav", rate, np.array(generated_data))
def train(self, params): n = params['encoding_num'] net = buildNetwork(n, params['num_cells'], n, hiddenclass=LSTMLayer, bias=True, outputbias=params['output_bias'], recurrent=True) net.reset() ds = SequentialDataSet(n, n) trainer = RPropMinusTrainer(net, dataset=ds) history = self.window(self.history, params) resets = self.window(self.resets, params) for i in xrange(1, len(history)): if not resets[i - 1]: ds.addSample(self.encoder.encode(history[i - 1]), self.encoder.encode(history[i])) if resets[i]: ds.newSequence() if len(history) > 1: trainer.trainEpochs(params['num_epochs']) net.reset() for i in xrange(len(history) - 1): symbol = history[i] output = net.activate(self.encoder.encode(symbol)) predictions = self.encoder.classify(output, num=params['num_predictions']) if resets[i]: net.reset() return net
def getReberDS(maxLength, display=0): """ @param maxLength (int): maximum length of the sequence """ [in_seq, out_seq] = generateSequencesVector(maxLength) target = out_seq last_target = target[-1] last_target[np.argmax(out_seq[-1])] = 1 target[-1] = last_target ds = SequentialDataSet(7, 7) i = 0 for sample, next_sample in zip(in_seq, target): ds.addSample(sample, next_sample) if display: print(" sample: %s" % sample) print(" target: %s" % next_sample) print("next sample: %s" % out_seq[i]) print() i += 1 return (ds, in_seq, out_seq)
def getPyBrainDataSet(sequence, nTrain, predictionStep=1, useTimeOfDay=True, useDayOfWeek=True): print "generate a pybrain dataset of sequences" print "the training data contains ", str(nTrain-predictionStep), "records" inDim = 1 + int(useTimeOfDay) + int(useDayOfWeek) ds = SequentialDataSet(inDim, 1) if useTimeOfDay: print "include time of day as input field" if useDayOfWeek: print "include day of week as input field" for i in xrange(nTrain-predictionStep): if useTimeOfDay and useDayOfWeek: sample = np.array([sequence['data'][i], sequence['timeofday'][i], sequence['dayofweek'][i]]) elif useTimeOfDay: sample = np.array([sequence['data'][i], sequence['timeofday'][i]]) elif useDayOfWeek: sample = np.array([sequence['data'][i], sequence['dayofweek'][i]]) else: sample = np.array([sequence['data'][i]]) ds.addSample(sample, sequence['data'][i+predictionStep]) return ds
def getReberDS(maxLength, display = 0): """ @param maxLength (int): maximum length of the sequence """ [in_seq, out_seq] = generateSequencesVector(maxLength) target = out_seq last_target = target[-1] last_target[np.argmax(out_seq[-1])] = 1 target[-1] = last_target ds = SequentialDataSet(7, 7) i = 0 for sample, next_sample in zip(in_seq, target): ds.addSample(sample, next_sample) if display: print(" sample: %s" % sample) print(" target: %s" % next_sample) print("next sample: %s" % out_seq[i]) print() i += 1 return (ds, in_seq, out_seq)
def say_hello_text(username = "******",text="You are good"): object_data_new = pd.read_csv('/Users/ruiyun_zhou/Documents/cmpe-274/data/data.csv') data_area_new = object_data_new[object_data_new.Area==username] data_area_new_1=data_area_new[data_area_new.Disease== text] data_list_new = data_area_new_1['Count'].values.tolist() print data_list_new.__len__() data_list=data_list_new ds = SequentialDataSet(1,1) isZero=0; for sample,next_sample in zip(data_list,cycle(data_list[1:])): ds.addSample(sample, next_sample) if sample: isZero=1 if(isZero==0): return '[0, 0]' net = buildNetwork(1,5,1,hiddenclass=LSTMLayer,outputbias=False,recurrent=True) trainer = RPropMinusTrainer(net, dataset=ds) train_errors = [] # save errors for plotting later EPOCHS_PER_CYCLE = 5 CYCLES = 10 EPOCHS = EPOCHS_PER_CYCLE * CYCLES for i in xrange(CYCLES): print "Doing epoch %d" %i trainer.trainEpochs(EPOCHS_PER_CYCLE) train_errors.append(trainer.testOnData()) epoch = (i+1) * EPOCHS_PER_CYCLE # return '<p>%d</p>\n' % (data_list_new.__len__()) # print("final error =", train_errors[-1]) # print "Value for last week is %4.1d" % abs(data_list[-1]) # print "Value for next week is %4.1d" % abs(net.activate(data_list[-1])) # result = (abs(data_list[-1])) result = (abs(net.activate(data_list[-1]))) result_1 = (abs(net.activate(result))) return '[%d, %d]' % (result,result_1)
def train(data,name): ds = SequentialDataSet(1, 1) for sample, next_sample in zip(data, cycle(data[1:])): ds.addSample(sample, next_sample) net = buildNetwork(1, 200, 1, hiddenclass=LSTMLayer, outputbias=False, recurrent=True) trainer = RPropMinusTrainer(net, dataset=ds) train_errors = [] # save errors for plotting later EPOCHS_PER_CYCLE = 5 CYCLES = 20 EPOCHS = EPOCHS_PER_CYCLE * CYCLES store=[] for i in xrange(CYCLES): trainer.trainEpochs(EPOCHS_PER_CYCLE) train_errors.append(trainer.testOnData()) epoch = (i+1) * EPOCHS_PER_CYCLE print("\r epoch {}/{}".format(epoch, EPOCHS)) print tm.time()-atm stdout.flush() for sample, target in ds.getSequenceIterator(0): store.append(net.activate(sample)) abcd=pd.DataFrame(store) abcd.to_csv(pwd+"lstmdata/"+name+".csv",encoding='utf-8') print "result printed to file"
def handle(self, *args, **options): ticker = args[0] print("****** STARTING PREDICTOR " + ticker + " ******* ") #prices = Price.objects.filter(symbol=ticker).order_by('-created_on').values_list('price',flat=True) #data = prices[0:NUM_MINUTES_BACK].reverse() #data = [ x * MULT_FACTOR for x in data] from pybrain.tools.shortcuts import buildNetwork from pybrain.supervised.trainers import BackpropTrainer from pybrain.datasets import SequentialDataSet from pybrain.structure import SigmoidLayer, LinearLayer from pybrain.structure import LSTMLayer import itertools import numpy as np INPUTS = 5 OUTPUTS = 1 HIDDEN = 40 net = buildNetwork(INPUTS, HIDDEN, OUTPUTS, hiddenclass=LSTMLayer, outclass=LinearLayer, recurrent=True, bias=True) ds = SequentialDataSet(INPUTS, OUTPUTS) ds.addSample([0,1,2,3,4],[5]) ds.addSample([5,6,7,8,9],[10]) ds.addSample([10,11,12,13,14],[15]) ds.addSample([16,17,18,19,20],[21]) net.randomize() trainer = BackpropTrainer(net, ds) for _ in range(1000): trainer.train() x=net.activate([0,1,2,3,4]) print x
lines = myFile.readlines() inputData = [] outputData = [] for line in lines: allData = (eval(line)) if len(allData) == 4: outputData.append([allData[3]]) else: inputData.append(allData[2]) inputSize = len(inputData[0]) outputSize = len(outputData[0]) ds = SequentialDataSet(inputSize, outputSize) for i in range(0, len(outputData)): ds.addSample(inputData[i], outputData[i]) from pybrain.tools.shortcuts import buildNetwork from pybrain.structure.modules import LSTMLayer from pybrain.structure.modules import SigmoidLayer net = buildNetwork(inputSize, inputSize, outputSize, hiddenclass=LSTMLayer, outclass=SigmoidLayer, outputbias=False, recurrent=True) from pybrain.supervised import RPropMinusTrainer from sys import stdout
def trainNetwork(dirname): numFeatures = 2000 ds = SequentialDataSet(numFeatures, 1) tracks = glob.glob(os.path.join(dirname, "*.csv")) for t in tracks: track = os.path.splitext(t)[0] # load training data print "Reading %s..." % t data = numpy.genfromtxt(t, delimiter=",") numData = data.shape[0] # add the input to the dataset print "Adding to dataset..." ds.newSequence() for i in range(numData): # ds.addSample(data[i], (labels[i],)) input = data[i] label = input[numFeatures] if label > 0: label = midi_util.frequencyToMidi(label) ds.addSample(input[0:numFeatures], (label,)) # initialize the neural network print "Initializing neural network..." # net = buildNetwork(numFeatures, 50, 1, # hiddenclass=LSTMLayer, bias=True, recurrent=True) # manual network building net = RecurrentNetwork() inlayer = LinearLayer(numFeatures) # h1 = LSTMLayer(70) # h2 = SigmoidLayer(50) octaveLayer = LSTMLayer(5) noteLayer = LSTMLayer(12) combinedLayer = SigmoidLayer(60) outlayer = LinearLayer(1) net.addInputModule(inlayer) net.addOutputModule(outlayer) # net.addModule(h1) # net.addModule(h2) net.addModule(octaveLayer) net.addModule(noteLayer) net.addModule(combinedLayer) # net.addConnection(FullConnection(inlayer, h1)) # net.addConnection(FullConnection(h1, h2)) # net.addConnection(FullConnection(h2, outlayer)) net.addConnection(FullConnection(inlayer, octaveLayer)) net.addConnection(FullConnection(inlayer, noteLayer)) # net.addConnection(FullConnection(octaveLayer,combinedLayer)) for i in range(5): net.addConnection( FullConnection( octaveLayer, combinedLayer, inSliceFrom=i, inSliceTo=i + 1, outSliceFrom=i * 12, outSliceTo=(i + 1) * 12 ) ) net.addConnection(FullConnection(noteLayer, combinedLayer)) net.addConnection(FullConnection(combinedLayer, outlayer)) net.sortModules() # train the network on the dataset print "Training neural net" trainer = RPropMinusTrainer(net, dataset=ds) ## trainer.trainUntilConvergence(maxEpochs=50, verbose=True, validationProportion=0.1) error = -1 for i in range(150): new_error = trainer.train() print "error: " + str(new_error) if abs(error - new_error) < 0.005: break error = new_error # save the network print "Saving neural network..." NetworkWriter.writeToFile(net, os.path.basename(dirname) + "designnet")
def rnn(): # load dataframe from csv file df = pi.load_data_frame('../../data/NABIL.csv') # column name to match with indicator calculating modules # TODO: resolve issue with column name df.columns = [ 'Transactions', 'Traded_Shares', 'Traded_Amount', 'High', 'Low', 'Close'] data = df.Close.values # TODO: write min_max normalization # normalization # cp = dataframe.pop(' Close Price') # x = cp.values temp = np.array(data).reshape(len(data),1) min_max_scaler = preprocessing.MinMaxScaler() data = min_max_scaler.fit_transform(temp) # dataframe[' Close Price'] = x_scaled # prepate sequential dataset for pyBrain rnn network ds = SequentialDataSet(1, 1) for sample, next_sample in zip(data, cycle(data[1:])): ds.addSample(sample, next_sample) # build rnn network with LSTM layer # if saved network is available if(os.path.isfile('random.xml')): net = NetworkReader.readFrom('network.xml') else: net = buildNetwork(1, 20, 1, hiddenclass=LSTMLayer, outputbias=False, recurrent=True) # build trainer trainer = RPropMinusTrainer(net, dataset=ds, verbose = True) train_errors = [] # save errors for plotting later EPOCHS_PER_CYCLE = 5 CYCLES = 5 EPOCHS = EPOCHS_PER_CYCLE * CYCLES for i in range(CYCLES): trainer.trainEpochs(EPOCHS_PER_CYCLE) train_errors.append(trainer.testOnData()) epoch = (i+1) * EPOCHS_PER_CYCLE print("\r epoch {}/{}".format(epoch, EPOCHS), end="") sys.stdout.flush() # save the network NetworkWriter.writeToFile(net,'network.xml') print() print("final error =", train_errors[-1]) predicted = [] for dat in data: predicted.append(net.activate(dat)[0]) # data = min_max_scaler.inverse_transform(data) # predicted = min_max_scaler.inverse_transform(predicted) predicted_array = min_max_scaler.inverse_transform(np.array(predicted).reshape(-1,1)) print(predicted_array[-1]) plt.figure() legend_actual, = plt.plot(range(0, len(data)),temp, label = 'actual', linestyle = '--', linewidth = 2, c = 'blue') legend_predicted, = plt.plot(range(0, len(data)), predicted_array, label = 'predicted', linewidth = 1.5, c='red') plt.legend(handles=[legend_actual, legend_predicted]) plt.savefig('error.png') plt.show()
def rnn(): # load dataframe from csv file df = pi.load_data_frame('../../data/NABIL.csv') # column name to match with indicator calculating modules # TODO: resolve issue with column name df.columns = [ 'Transactions', 'Traded_Shares', 'Traded_Amount', 'High', 'Low', 'Close' ] data = df.Close.values # TODO: write min_max normalization # normalization # cp = dataframe.pop(' Close Price') # x = cp.values temp = np.array(data).reshape(len(data), 1) min_max_scaler = preprocessing.MinMaxScaler() data = min_max_scaler.fit_transform(temp) # dataframe[' Close Price'] = x_scaled # prepate sequential dataset for pyBrain rnn network ds = SequentialDataSet(1, 1) for sample, next_sample in zip(data, cycle(data[1:])): ds.addSample(sample, next_sample) # build rnn network with LSTM layer # if saved network is available if (os.path.isfile('random.xml')): net = NetworkReader.readFrom('network.xml') else: net = buildNetwork(1, 20, 1, hiddenclass=LSTMLayer, outputbias=False, recurrent=True) # build trainer trainer = RPropMinusTrainer(net, dataset=ds, verbose=True) train_errors = [] # save errors for plotting later EPOCHS_PER_CYCLE = 5 CYCLES = 5 EPOCHS = EPOCHS_PER_CYCLE * CYCLES for i in range(CYCLES): trainer.trainEpochs(EPOCHS_PER_CYCLE) train_errors.append(trainer.testOnData()) epoch = (i + 1) * EPOCHS_PER_CYCLE print("\r epoch {}/{}".format(epoch, EPOCHS), end="") sys.stdout.flush() # save the network NetworkWriter.writeToFile(net, 'network.xml') print() print("final error =", train_errors[-1]) predicted = [] for dat in data: predicted.append(net.activate(dat)[0]) # data = min_max_scaler.inverse_transform(data) # predicted = min_max_scaler.inverse_transform(predicted) predicted_array = min_max_scaler.inverse_transform( np.array(predicted).reshape(-1, 1)) print(predicted_array[-1]) plt.figure() legend_actual, = plt.plot(range(0, len(data)), temp, label='actual', linestyle='--', linewidth=2, c='blue') legend_predicted, = plt.plot(range(0, len(data)), predicted_array, label='predicted', linewidth=1.5, c='red') plt.legend(handles=[legend_actual, legend_predicted]) plt.savefig('error.png') plt.show()
# Shuffle to partition test/train random.shuffle(samples) # Set train & test data train_data, test_data = samples[:50], samples[200:] # Initialize ds for rnn for 1 obsv and 1 next ds = SequentialDataSet(1, 1) # Add each timeseries (ts) for ts in train_data: ds.newSequence() # Add obsv and next for t_1, t_2 in zip(ts, ts[1:]): ds.addSample(t_1, t_2) # RNN with 1-5-1 architecture: 1 input, 5 hidden, 1 output layer rnn = buildNetwork(1, 5, 1, hiddenclass=LSTMLayer, outputbias=False, recurrent=True) # Initialize trainer trainer = RPropMinusTrainer(rnn, dataset=ds) # Predefine iterations: epochs & cycles EPOCHS_PER_CYCLE = 5 CYCLES = 100
# data.extend([data1[i]*4 for i in range(len(data1))]) #data = [float(i*1)/max(data) for i in data] #data = [24924.5,46039.49,41595.55,19403.54,21827.9,21043.39,22136.64,26229.21,57258.43,42960.91,17596.96,16145.35,16555.11,17413.94,18926.74,14773.04,15580.43,17558.09,16637.62,16216.27,16328.72,16333.14,17688.76,17150.84,15360.45,15381.82,17508.41,15536.4,15740.13,15793.87,16241.78,18194.74,19354.23,18122.52,20094.19,23388.03,26978.34,25543.04,38640.93,34238.88,19549.39,19552.84,18820.29,22517.56,31497.65,44912.86,55931.23,19124.58,15984.24,17359.7,17341.47,18461.18,21665.76,37887.17,46845.87,19363.83,20327.61,21280.4,20334.23,20881.1,20398.09,23873.79,28762.37,50510.31,41512.39,20138.19,17235.15,15136.78,15741.6,16434.15,15883.52,14978.09,15682.81,15363.5,16148.87,15654.85,15766.6,15922.41,15295.55,14539.79,14689.24,14537.37,15277.27,17746.68,18535.48,17859.3,18337.68,20797.58,23077.55,23351.8,31579.9,39886.06,18689.54,19050.66,20911.25,25293.49,33305.92,45773.03,46788.75,23350.88,16567.69,16894.4,18365.1,18378.16,23510.49,36988.49,54060.1,20124.22,20113.03,21140.07,22366.88,22107.7,28952.86,57592.12,34684.21,16976.19,16347.6,17147.44,18164.2,18517.79,16963.55,16065.49,17666,17558.82,16633.41,15722.82,17823.37,16566.18,16348.06,15731.18,16628.31,16119.92,17330.7,16286.4,16680.24,18322.37,19616.22,19251.5,18947.81,21904.47,22764.01,24185.27,27390.81] print(data) #exit(0) #data =[-0.025752496,0.091349779,0.112477983,-0.043485112,-0.076961041,0.175632569,0.131852131,0.000000000,-0.073203404,-0.172245905,-0.154150680,0.205443974] #data = data * 100 from pybrain.datasets import SequentialDataSet from itertools import cycle ds = SequentialDataSet(1, 1) for sample, next_sample in zip(data, cycle(data[1:])): ds.addSample(sample, next_sample) print(ds) from pybrain.tools.shortcuts import buildNetwork from pybrain.structure.modules import LSTMLayer # Buils a simple LSTM network with 1 input node, 1 output node and 5 LSTM cells net = buildNetwork(1, 12, 1, hiddenclass=LSTMLayer, peepholes=False, outputbias=False, recurrent=True) # net = buildNetwork(1, 1, 1, hiddenclass=LSTMLayer, peepholes = True, outputbias=False, recurrent=True) # rnn = buildNetwork( trndata.indim, 5, trndata.outdim, hiddenclass=LSTMLayer, outclass=SoftmaxLayer, outputbias=False, recurrent=True)
###### Test PyBrain RNN ####### from pybrain.tools.shortcuts import buildNetwork from pybrain.datasets import SequentialDataSet ######### Global Variables ############# dim_input = 16 hidden_layers = {512, 512} dim_output = 3 ###### Building Dataset ######### ds = SequentialDataSet(dim_input, dim_output) for i in range(len(dataset)): ds.addSample(data, target) ###### Building Network ######### net = buildNetwork(dim_input, 512, 512, dim_output, hiddenclass=TanhLayer, outclass=SoftmaxLayer)
class RWR(DirectSearchLearner): """ Reward-weighted regression. The algorithm is currently limited to discrete-action episodic tasks, subclasses of POMDPTasks. """ # parameters batchSize = 20 # feedback settings verbose = True greedyRuns = 20 supervisedPlotting = False # settings for the supervised training learningRate = 0.005 momentum = 0.9 maxEpochs = 20 validationProportion = 0.33 continueEpochs = 2 # parameters for the variation that uses a value function # TODO: split into 2 classes. valueLearningRate = None valueMomentum = None #valueTrainEpochs = 5 resetAllWeights = False netweights = 0.01 def __init__(self, net, task, valueNetwork=None, **args): self.net = net self.task = task self.setArgs(**args) if self.valueLearningRate == None: self.valueLearningRate = self.learningRate if self.valueMomentum == None: self.valueMomentum = self.momentum if self.supervisedPlotting: from pylab import ion ion() # adaptive temperature: self.tau = 1. # prepare the datasets to be used self.weightedDs = ImportanceDataSet(self.task.outdim, self.task.indim) self.rawDs = ReinforcementDataSet(self.task.outdim, self.task.indim) self.valueDs = SequentialDataSet(self.task.outdim, 1) # prepare the supervised trainers self.bp = BackpropTrainer(self.net, self.weightedDs, self.learningRate, self.momentum, verbose=False, batchlearning=True) # CHECKME: outsource self.vnet = valueNetwork if valueNetwork != None: self.vbp = BackpropTrainer(self.vnet, self.valueDs, self.valueLearningRate, self.valueMomentum, verbose=self.verbose) # keep information: self.totalSteps = 0 self.totalEpisodes = 0 def shapingFunction(self, R): return exp(self.tau * R) def updateTau(self, R, U): self.tau = sum(U) / dot((R - self.task.minReward), U) def reset(self): self.weightedDs.clear() self.valueDs.clear() self.rawDs.clear() self.bp.momentumvector *= 0.0 if self.vnet != None: self.vbp.momentumvector *= 0.0 if self.resetAllWeights: self.vnet.params[:] = randn(len(self.vnet.params)) * self.netweights def greedyEpisode(self): """ run one episode with greedy decisions, return the list of rewards recieved.""" rewards = [] self.task.reset() self.net.reset() while not self.task.isFinished(): obs = self.task.getObservation() act = self.net.activate(obs) chosen = argmax(act) self.task.performAction(chosen) reward = self.task.getReward() rewards.append(reward) return rewards def learn(self, batches): self.greedyAvg = [] self.rewardAvg = [] self.lengthAvg = [] self.initr0Avg = [] for b in range(batches): if self.verbose: print print 'Batch', b + 1 self.reset() self.learnOneBatch() self.totalEpisodes += self.batchSize # greedy measure (avg over some greedy runs) rws = 0. for dummy in range(self.greedyRuns): tmp = self.greedyEpisode() rws += (sum(tmp) / float(len(tmp))) self.greedyAvg.append(rws / self.greedyRuns) if self.verbose: print '::', round(rws / self.greedyRuns, 5), '::' def learnOneBatch(self): # collect a batch of runs as experience r0s = [] lens = [] avgReward = 0. for dummy in range(self.batchSize): self.rawDs.newSequence() self.valueDs.newSequence() self.task.reset() self.net.reset() acts, obss, rewards = [], [], [] while not self.task.isFinished(): obs = self.task.getObservation() act = self.net.activate(obs) chosen = drawIndex(act) self.task.performAction(chosen) reward = self.task.getReward() obss.append(obs) y = zeros(len(act)) y[chosen] = 1 acts.append(y) rewards.append(reward) avgReward += sum(rewards) / float(len(rewards)) # compute the returns from the list of rewards current = 0 returns = [] for r in reversed(rewards): current *= self.task.discount current += r returns.append(current) returns.reverse() for i in range(len(obss)): self.rawDs.addSample(obss[i], acts[i], returns[i]) self.valueDs.addSample(obss[i], returns[i]) r0s.append(returns[0]) lens.append(len(returns)) r0s = array(r0s) self.totalSteps += sum(lens) avgLen = sum(lens) / float(self.batchSize) avgR0 = mean(r0s) avgReward /= self.batchSize if self.verbose: print '***', round(avgLen, 3), '***', '(avg init exp. return:', round(avgR0, 5), ')', print 'avg reward', round(avgReward, 5), '(tau:', round(self.tau, 3), ')' print lens # storage: self.rewardAvg.append(avgReward) self.lengthAvg.append(avgLen) self.initr0Avg.append(avgR0) # if self.vnet == None: # # case 1: no value estimator: # prepare the dataset for training the acting network shaped = self.shapingFunction(r0s) self.updateTau(r0s, shaped) shaped /= max(shaped) for i, seq in enumerate(self.rawDs): self.weightedDs.newSequence() for sample in seq: obs, act, dummy = sample self.weightedDs.addSample(obs, act, shaped[i]) # else: # # case 2: value estimator: # # # # train the value estimating network # if self.verbose: print 'Old value error: ', self.vbp.testOnData() # self.vbp.trainEpochs(self.valueTrainEpochs) # if self.verbose: print 'New value error: ', self.vbp.testOnData() # # # produce the values and analyze # rminusvs = [] # sizes = [] # for i, seq in enumerate(self.valueDs): # self.vnet.reset() # seq = list(seq) # for sample in seq: # obs, ret = sample # val = self.vnet.activate(obs) # rminusvs.append(ret-val) # sizes.append(len(seq)) # # rminusvs = array(rminusvs) # shapedRminusv = self.shapingFunction(rminusvs) # # CHECKME: here? # self.updateTau(rminusvs, shapedRminusv) # shapedRminusv /= array(sizes) # shapedRminusv /= max(shapedRminusv) # # # prepare the dataset for training the acting network # rvindex = 0 # for i, seq in enumerate(self.rawDs): # self.weightedDs.newSequence() # self.vnet.reset() # for sample in seq: # obs, act, ret = sample # self.weightedDs.addSample(obs, act, shapedRminusv[rvindex]) # rvindex += 1 # train the acting network tmp1, tmp2 = self.bp.trainUntilConvergence(maxEpochs=self.maxEpochs, validationProportion=self.validationProportion, continueEpochs=self.continueEpochs, verbose=self.verbose) if self.supervisedPlotting: from pylab import plot, legend, figure, clf, draw figure(1) clf() plot(tmp1, label='train') plot(tmp2, label='valid') legend() draw() return avgLen, avgR0
def learn(self, pathdataset=["dstc4_train"], Pathdataroot="data", numberOfHiddenUnit=20, EPOCHS_PER_CYCLE=10, CYCLES=40, weightdecayw=0.01): print "Start learning LSTM, and make dictionary file" #Construct dictionary: variable name -> corresponding index of element in i/o vector print "Star make dictionary: variable name -> corresponding index of element in i/o vector" self.dictOut = { } #"TOPIC_SLOT_VALUE" -> corresponding index of element self.dictIn = { } #"SPEAKER_{val}"or"TOPIC_{val}","WORD_{word}" "BIO_{BIO}", "CLASS_{slot,value}", ""{defined label}-> corresponding index of element #-target vector dictionary index = 0 totalNumSlot = 0 for topic in self.tagsets.keys(): for slot in self.tagsets[topic].keys(): totalNumSlot += 1 for value in self.tagsets[topic][slot]: self.dictOut[topic + "_" + slot + "_" + value] = index index += 1 print "totalNumSlot:" + str(totalNumSlot) print "outputSize:" + str(len(self.dictOut.keys())) #-input dictionry dataset = [] for pathdat in pathdataset: dataset.append( dataset_walker.dataset_walker(pathdat, dataroot=Pathdataroot, labels=False)) #--(sub input vector 1) Class features i.e., Slot and value ratio (Similar to base line) index = 0 for topic in self.tagsets.keys(): for slot in self.tagsets[topic].keys(): if ("CLASS_" + slot) not in self.dictIn: self.dictIn["CLASS_" + slot] = index index += 1 for value in self.tagsets[topic][slot]: if ("CLASS_" + value) not in self.dictIn: self.dictIn["CLASS_" + value] = index index += 1 self.TOTALSIZEOFCLASSFeature = index f = open(self.FileNameofNumClassFeature, "wb") pickle.dump(self.TOTALSIZEOFCLASSFeature, f) f.close() #--(sub input vector 2) Sentence features if not self.isUseSentenceRepresentationInsteadofBOW: index = 0 for elemDataset in dataset: for call in elemDataset: for (uttr, _) in call: #General info1 (CLASS; this feature must be rejistered at first) if ("SPEAKER_" + uttr["speaker"]) not in self.dictIn: self.dictIn["SPEAKER_" + uttr["speaker"]] = index index += 1 if ("TOPIC_" + uttr["segment_info"]["topic"] ) not in self.dictIn: self.dictIn["TOPIC_" + uttr["segment_info"]["topic"]] = index index += 1 #General info2 #-BIO if ("BIO_" + uttr['segment_info']['target_bio'] ) not in self.dictIn: self.dictIn[ "BIO_" + uttr['segment_info']['target_bio']] = index index += 1 #BOW if LSTMWithBOWTracker.isIgnoreUtterancesNotRelatedToMainTask: if not (uttr['segment_info']['target_bio'] == "O"): #-BOW splitedtrans = self.__getRegurelisedBOW( uttr["transcript"]) for word in splitedtrans: if ("WORD_" + word) not in self.dictIn: self.dictIn["WORD_" + word] = index index += 1 self.TOTALSIZEOFSENTENCEFeature = index f = open(self.FileNameofNumSentenceFeature, "wb") pickle.dump(self.TOTALSIZEOFSENTENCEFeature, f) f.close() elif self.isUseSentenceRepresentationInsteadofBOW: index = 0 for i in range(0, LSTMWithBOWTracker.D2V_VECTORSIZE): self.dictIn[str(index) + "thElemPV"] = index index += 1 index = 0 for i in range(0, LSTMWithBOWTracker.D2V_VECTORSIZE): self.dictIn[str(index) + "thAvrWord"] = index index += 1 assert self.D2V_VECTORSIZE == LSTMWithBOWTracker.D2V_VECTORSIZE, "D2V_VECTORSIZE is restrected to be same over the class" else: assert False, "Unexpected block" #--(sub input vector 3) Features M1s defined index = 0 if self.isEnableToUseM1sFeature: rejisteredFeatures = self.__rejisterM1sInputFeatureLabel( self.tagsets, dataset) for rFeature in rejisteredFeatures: assert rFeature not in self.dictIn, rFeature + " already registered in input vector. Use different label name. " self.dictIn[rFeature] = index index += 1 self.TOTALSIZEOFM1DEFINEDFeature = index f = open(self.FileNameofNumM1Feature, "wb") pickle.dump(self.TOTALSIZEOFM1DEFINEDFeature, f) f.close() print "inputSize:" + str(len(self.dictIn.keys())) assert self.dictIn[ "CLASS_INFO"] == 0, "Unexpected index CLASS_INFO should has value 0" assert self.dictIn[ "CLASS_Fort Siloso"] == 334, "Unexpected index CLASS_Fort Siloso should has value 334" assert self.dictIn[ "CLASS_Yunnan"] == 1344, "Unexpected index CLASS_Yunnan should has value 1611" #--write fileObject = open('dictInput.pic', 'w') pickle.dump(self.dictIn, fileObject) fileObject.close() fileObject = open('dictOutput.pic', 'w') pickle.dump(self.dictOut, fileObject) fileObject.close() #Build RNN frame work print "Start learning Network" #Capability of network is: (30 hidden units can represents 1048576 relations) wherease (10 hidden units can represents 1024) #Same to Henderson (http://www.aclweb.org/anthology/W13-4073)? net = buildNetwork(len(self.dictIn.keys()), numberOfHiddenUnit, len(self.dictOut.keys()), hiddenclass=LSTMLayer, outclass=SigmoidLayer, outputbias=False, recurrent=True) #Train network #-convert training data into sequence of vector convDataset = [] #[call][uttr][input,targetvec] iuttr = 0 convCall = [] for elemDataset in dataset: for call in elemDataset: for (uttr, label) in call: if self.isIgnoreUtterancesNotRelatedToMainTask: if uttr['segment_info']['target_bio'] == "O": continue #-input convInput = self._translateUtteranceIntoInputVector( uttr, call) #-output convOutput = [0.0] * len( self.dictOut.keys()) #Occured:+1, Not occured:0 if "frame_label" in label: for slot in label["frame_label"].keys(): for value in label["frame_label"][slot]: convOutput[self.dictOut[ uttr["segment_info"]["topic"] + "_" + slot + "_" + value]] = 1 #-post proccess if self.isSeparateDialogIntoSubDialog: if uttr['segment_info']['target_bio'] == "B": if len(convCall) > 0: convDataset.append(convCall) convCall = [] convCall.append([convInput, convOutput]) #print "Converted utterance" + str(iuttr) iuttr += 1 if not self.isSeparateDialogIntoSubDialog: if len(convCall) > 0: convDataset.append(convCall) convCall = [] #Online learning trainer = RPropMinusTrainer(net, weightdecay=weightdecayw) EPOCHS = EPOCHS_PER_CYCLE * CYCLES for i in xrange(CYCLES): #Shuffle order ds = SequentialDataSet(len(self.dictIn.keys()), len(self.dictOut.keys())) datInd = range(0, len(convDataset)) random.shuffle( datInd ) #Backpropergation already implemeted data shuffling, however though RpropMinus don't. for ind in datInd: ds.newSequence() for convuttr in convDataset[ind]: ds.addSample(convuttr[0], convuttr[1]) #Evaluation and Train epoch = (i + 1) * EPOCHS_PER_CYCLE print "\r epoch {}/{} Error={}".format( epoch, EPOCHS, trainer.testOnData(dataset=ds)) stdout.flush() trainer.trainOnDataset(dataset=ds, epochs=EPOCHS_PER_CYCLE) NetworkWriter.writeToFile( trainer.module, "LSTM_" + "Epoche" + str(i + 1) + ".rnnw") NetworkWriter.writeToFile(trainer.module, "LSTM.rnnw")
# takes array of probabilities def turnArrayToChar(array): maxv = max(array) for x in range(len(array)): if array[x] == maxv: return ixToChar[x] return None # Turn it into a dataset: ds = SequentialDataSet(len(charToIx), len(charToIx)) for sample, next in zip(text, cycle(text[1:])): actual = createCharArray(sample) expected = createCharArray(next) ds.addSample(actual, expected) # Create the network: print("Creating network of:", len(chars), int(len(chars) - (len(chars) * 0.1)), len(chars)) net = buildNetwork( len(chars), int(len(chars) - (len(chars) * 0.1)), len(chars), hiddenclass=LSTMLayer, outputbias=False, recurrent=True, ) # create the trainer: trainer = RPropMinusTrainer(net, dataset=ds)
# plt.plot(training_label, tmin200[-dlen:], color=random.rand(3,1), label='tmin200') # plt.legend() # plt.show() # build network # net = buildNetwork(14, 54, 1, hiddenclass=LSTMLayer, outclass=TanhLayer, outputbias=False, recurrent=True) net = NetworkReader.readFrom('target_weight.xml') net.randomize() # build sequential dataset train_ds = SequentialDataSet(14, 1) for n, r, m, m7, m13, tx50, tn50, tx100, tn100, tx200, tn200, target in zip(training_list, rsi[-dlen:], macd[-dlen:], sma7[-dlen:], sma13[-dlen:], tmax50[-dlen:], tmin50[-dlen:], tmax100[-dlen:], tmin100[-dlen:], tmax200[-dlen:], tmin200[-dlen:], training_target): i = np.append(n, [r, m, m7, m13, tx50, tn50, tx100, tn100, tx200, tn200]) train_ds.addSample(i, target) # train network trainer = RPropMinusTrainer(net, dataset=train_ds) # trainer = BackpropTrainer(net, dataset=train_ds) train_errors = [] # save errors for plotting later EPOCHS_PER_CYCLE = 100 CYCLES = 20 EPOCHS = EPOCHS_PER_CYCLE * CYCLES for i in xrange(CYCLES): trainer.trainEpochs(EPOCHS_PER_CYCLE) train_errors.append(trainer.testOnData()) epoch = (i+1) * EPOCHS_PER_CYCLE print("\r epoch {}/{}".format(epoch, EPOCHS), end="") stdout.flush()
net = buildNetwork(1, num, 1, hiddenclass=LSTMLayer, outputbias=False, recurrent=True) # 1 input, 5 hidden layer and 1 output for i in range(kfolds+1): test_data = cv_data[i] train_data = [] for j in range (kfolds+1) : train_data.extend(cv_data[j] ) # make the train data continuous data print("test///", test_data) train_ds = SequentialDataSet(1, 1) # The next_sample picks a next number of the begin of the sample for sample, next_sample in zip(train_data, cycle(train_data[1:])): train_ds.addSample(sample, next_sample) test_ds = SequentialDataSet(1, 1) # The next_sample picks a next number of the begin of the sample for sample, next_sample in zip(test_data, cycle(test_data[1:])): test_ds.addSample(sample, next_sample) # Training trainer = RPropMinusTrainer(net, dataset=train_ds) train_errors = [] # save errors for plotting later EPOCHS_PER_CYCLE = 10 CYCLES = 10 EPOCHS = EPOCHS_PER_CYCLE * CYCLES for a in xrange(CYCLES):
import csv from numpy import * from pybrain.datasets import SequentialDataSet,UnsupervisedDataSet from pybrain.tools.shortcuts import buildNetwork from pybrain.supervised import BackpropTrainer sequencia = [] NEURONIOS = 10e4 if __name__ == "__main__": sequencias = SequentialDataSet(1,1) for x in range(0,100): sequencia.append(x) for i,v in enumerate(sequencia): if i+1 < len(sequencia): sequencias.addSample(v, sequencia[i+1]) print(sequencias) rn = buildNetwork(sequencias.indim, NEURONIOS, sequencias.outdim, recurrent=True) sorteio = BackpropTrainer(rn, sequencias, learningrate=1/(NEURONIOS/100)) while 1: try: print(sorteio.train()) except KeyboardInterrupt: sorteio.testOnData(verbose=True) break
net = initializeLSTMnet(nDim, nLSTMcells=50) net.reset() ds = SequentialDataSet(nDim, nDim) trainer = RPropMinusTrainer(net) trainer.setData(ds) for _ in xrange(1000): # Batch training mode # print "generate a dataset of sequences" import random random.shuffle(sequences) concat_sequences = [] for sequence in sequences: concat_sequences += sequence concat_sequences.append(random.randrange(100, 1000000)) for j in xrange(len(concat_sequences) - 1): ds.addSample(num2vec(concat_sequences[j], nDim), num2vec(concat_sequences[j + 1], nDim)) trainer.trainEpochs(rptNum) print print "test LSTM, repeats =", rptNum # test LSTM correct = [] for i in xrange(len(sequences)): net.reset() sequence = sequences[i] sequence = sequence + [random.randrange(100, 1000000)] print sequence predictedInput = [] for j in xrange(len(sequence)): sample = num2vec(sequence[j], nDim)
from pybrain.datasets import SequentialDataSet from itertools import cycle ds = SequentialDataSet(1, 1) with open('training.txt') as file: data = [1.0, 0.0] for line in file: for x in list(line): data.append(ord(x)) #data = [1] * 3 + [2] * 3 for sample, next_sample in zip(data, cycle(data[:1])): ds.addSample(sample, next_sample) from pybrain.tools.shortcuts import buildNetwork from pybrain.structure.modules import LSTMLayer net = buildNetwork(1, 100, 1, hiddenclass=LSTMLayer, outputbias=False, recurrent=True) from pybrain.supervised import RPropMinusTrainer from sys import stdout trainer = RPropMinusTrainer(net, dataset=ds) train_errors = [] # save errors for plotting later EPOCHS_PER_CYCLE = 5 CYCLES = 100 EPOCHS = EPOCHS_PER_CYCLE * CYCLES for i in xrange(CYCLES): trainer.trainEpochs(EPOCHS_PER_CYCLE) train_errors.append(trainer.testOnData()) epoch = (i+1) * EPOCHS_PER_CYCLE print "\r epoch {}/{}".format(epoch, EPOCHS)
class RWR(DirectSearchLearner): """ Reward-weighted regression. The algorithm is currently limited to discrete-action episodic tasks, subclasses of POMDPTasks. """ # parameters batchSize = 20 # feedback settings verbose = True greedyRuns = 20 supervisedPlotting = False # settings for the supervised training learningRate = 0.005 momentum = 0.9 maxEpochs = 20 validationProportion = 0.33 continueEpochs = 2 # parameters for the variation that uses a value function # TODO: split into 2 classes. valueLearningRate = None valueMomentum = None #valueTrainEpochs = 5 resetAllWeights = False netweights = 0.01 def __init__(self, net, task, valueNetwork=None, **args): self.net = net self.task = task self.setArgs(**args) if self.valueLearningRate == None: self.valueLearningRate = self.learningRate if self.valueMomentum == None: self.valueMomentum = self.momentum if self.supervisedPlotting: from pylab import ion ion() # adaptive temperature: self.tau = 1. # prepare the datasets to be used self.weightedDs = ImportanceDataSet(self.task.outdim, self.task.indim) self.rawDs = ReinforcementDataSet(self.task.outdim, self.task.indim) self.valueDs = SequentialDataSet(self.task.outdim, 1) # prepare the supervised trainers self.bp = BackpropTrainer(self.net, self.weightedDs, self.learningRate, self.momentum, verbose=False, batchlearning=True) # CHECKME: outsource self.vnet = valueNetwork if valueNetwork != None: self.vbp = BackpropTrainer(self.vnet, self.valueDs, self.valueLearningRate, self.valueMomentum, verbose=self.verbose) # keep information: self.totalSteps = 0 self.totalEpisodes = 0 def shapingFunction(self, R): return exp(self.tau * R) def updateTau(self, R, U): self.tau = sum(U) / dot((R - self.task.minReward), U) def reset(self): self.weightedDs.clear() self.valueDs.clear() self.rawDs.clear() self.bp.momentumvector *= 0.0 if self.vnet != None: self.vbp.momentumvector *= 0.0 if self.resetAllWeights: self.vnet.params[:] = randn(len( self.vnet.params)) * self.netweights def greedyEpisode(self): """ run one episode with greedy decisions, return the list of rewards recieved.""" rewards = [] self.task.reset() self.net.reset() while not self.task.isFinished(): obs = self.task.getObservation() act = self.net.activate(obs) chosen = argmax(act) self.task.performAction(chosen) reward = self.task.getReward() rewards.append(reward) return rewards def learn(self, batches): self.greedyAvg = [] self.rewardAvg = [] self.lengthAvg = [] self.initr0Avg = [] for b in range(batches): if self.verbose: print() print(('Batch', b + 1)) self.reset() self.learnOneBatch() self.totalEpisodes += self.batchSize # greedy measure (avg over some greedy runs) rws = 0. for dummy in range(self.greedyRuns): tmp = self.greedyEpisode() rws += (sum(tmp) / float(len(tmp))) self.greedyAvg.append(rws / self.greedyRuns) if self.verbose: print(('::', round(rws / self.greedyRuns, 5), '::')) def learnOneBatch(self): # collect a batch of runs as experience r0s = [] lens = [] avgReward = 0. for dummy in range(self.batchSize): self.rawDs.newSequence() self.valueDs.newSequence() self.task.reset() self.net.reset() acts, obss, rewards = [], [], [] while not self.task.isFinished(): obs = self.task.getObservation() act = self.net.activate(obs) chosen = drawIndex(act) self.task.performAction(chosen) reward = self.task.getReward() obss.append(obs) y = zeros(len(act)) y[chosen] = 1 acts.append(y) rewards.append(reward) avgReward += sum(rewards) / float(len(rewards)) # compute the returns from the list of rewards current = 0 returns = [] for r in reversed(rewards): current *= self.task.discount current += r returns.append(current) returns.reverse() for i in range(len(obss)): self.rawDs.addSample(obss[i], acts[i], returns[i]) self.valueDs.addSample(obss[i], returns[i]) r0s.append(returns[0]) lens.append(len(returns)) r0s = array(r0s) self.totalSteps += sum(lens) avgLen = sum(lens) / float(self.batchSize) avgR0 = mean(r0s) avgReward /= self.batchSize if self.verbose: print(( '***', round(avgLen, 3), '***', '(avg init exp. return:', round(avgR0, 5), ')', )) print(('avg reward', round(avgReward, 5), '(tau:', round(self.tau, 3), ')')) print(lens) # storage: self.rewardAvg.append(avgReward) self.lengthAvg.append(avgLen) self.initr0Avg.append(avgR0) # if self.vnet == None: # # case 1: no value estimator: # prepare the dataset for training the acting network shaped = self.shapingFunction(r0s) self.updateTau(r0s, shaped) shaped /= max(shaped) for i, seq in enumerate(self.rawDs): self.weightedDs.newSequence() for sample in seq: obs, act, dummy = sample self.weightedDs.addSample(obs, act, shaped[i]) # else: # # case 2: value estimator: # # # # train the value estimating network # if self.verbose: print('Old value error: ', self.vbp.testOnData()) # self.vbp.trainEpochs(self.valueTrainEpochs) # if self.verbose: print('New value error: ', self.vbp.testOnData()) # # # produce the values and analyze # rminusvs = [] # sizes = [] # for i, seq in enumerate(self.valueDs): # self.vnet.reset() # seq = list(seq) # for sample in seq: # obs, ret = sample # val = self.vnet.activate(obs) # rminusvs.append(ret-val) # sizes.append(len(seq)) # # rminusvs = array(rminusvs) # shapedRminusv = self.shapingFunction(rminusvs) # # CHECKME: here? # self.updateTau(rminusvs, shapedRminusv) # shapedRminusv /= array(sizes) # shapedRminusv /= max(shapedRminusv) # # # prepare the dataset for training the acting network # rvindex = 0 # for i, seq in enumerate(self.rawDs): # self.weightedDs.newSequence() # self.vnet.reset() # for sample in seq: # obs, act, ret = sample # self.weightedDs.addSample(obs, act, shapedRminusv[rvindex]) # rvindex += 1 # train the acting network tmp1, tmp2 = self.bp.trainUntilConvergence( maxEpochs=self.maxEpochs, validationProportion=self.validationProportion, continueEpochs=self.continueEpochs, verbose=self.verbose) if self.supervisedPlotting: from pylab import plot, legend, figure, clf, draw figure(1) clf() plot(tmp1, label='train') plot(tmp2, label='valid') legend() draw() return avgLen, avgR0
net = initializeLSTMnet(nDim, nLSTMcells=50) net.reset() ds = SequentialDataSet(nDim, nDim) trainer = RPropMinusTrainer(net) trainer.setData(ds) for _ in xrange(1000): # Batch training mode # print "generate a dataset of sequences" import random random.shuffle(sequences) concat_sequences = [] for sequence in sequences: concat_sequences += sequence concat_sequences.append(random.randrange(100, 1000000)) for j in xrange(len(concat_sequences) - 1): ds.addSample(num2vec(concat_sequences[j], nDim), num2vec(concat_sequences[j+1], nDim)) trainer.trainEpochs(rptNum) print print "test LSTM, repeats =", rptNum # test LSTM correct = [] for i in xrange(len(sequences)): net.reset() sequence = sequences[i] sequence = sequence + [random.randrange(100, 1000000)] print sequence predictedInput = [] for j in xrange(len(sequence)): sample = num2vec(sequence[j], nDim)
ys.append(1 if y_found else 0) ys_seq.append(y_seq) xs.append(row) maxlen = max(len(row), maxlen) max_features=generator.max_id() + 2 train_ds = SequentialDataSet(max_features, 1) for xrow, yrow in zip(xs, ys_seq): train_ds.newSequence() for x,y in zip(xrow, yrow): one_hot = [0] * max_features one_hot[x] = 1 train_ds.addSample(one_hot, y) from pybrain.tools.shortcuts import buildNetwork from pybrain.structure.modules import LSTMLayer net = buildNetwork(max_features, 32, 1, hiddenclass=LSTMLayer, outputbias=False, recurrent=True) from pybrain.supervised import RPropMinusTrainer trainer = RPropMinusTrainer(net, dataset=train_ds) train_errors = [] # save errors for plotting later EPOCHS_PER_CYCLE = 5 CYCLES = 100 EPOCHS = EPOCHS_PER_CYCLE * CYCLES for i in xrange(CYCLES): trainer.trainEpochs(EPOCHS_PER_CYCLE)
from pybrain.datasets import SequentialDataSet from itertools import cycle INPUT = 3 HIDDEN_LAYERS = 5 OUTPUT = 3 ds = SequentialDataSet(INPUT, OUTPUT) # Adding sequence of numbers (of both features) into neural network for (sample, next_sample, sam_v, next_sam_v, sam_t, next_sam_t) in zip(pitch_data, cycle(pitch_data[1:]), velocity_data, cycle(velocity_data[1:]), tick_data, cycle(tick_data[1:])): #ds.addSample((sample, velocity_data[i], tick_data[i]), next_sample) ds.addSample((sample, sam_v, sam_t), (next_sample, next_sam_v, next_sam_t)) # Build a simple LSTM networ end of the song somewhere around last 15 sec when my line pop. stupid friend decide it is a good time to send LL stickerk with 1 input node, 5 LSTM cells and 1 output node: from pybrain.tools.shortcuts import buildNetwork from pybrain.structure.modules import LSTMLayer print 'Constructing neutral network. . .' net = buildNetwork( INPUT, HIDDEN_LAYERS, OUTPUT, hiddenclass=LSTMLayer, outputbias=False, recurrent=True, )
return csv.reader(bd, delimiter=',') if __name__ == "__main__": # Prepara as variaveis principais concursosList = [] concursos = SequentialDataSet(6,6) arquivo = open_dataset() # Le o CSV e armazena em uma lista for concurso in arquivo: concursosList.append(get_numeros(concurso)) # Adiciona os elementos da lista ao dataset for i,concurso in enumerate(concursosList): if i+1 < len(concursosList): concursos.addSample(concurso, concursosList[i+1]) # Abre o treinamento de uma rede neural articificial salvo, # Se a mesmo não existir cria uma novo try: tmp = open("./cerebro.dump", "rb") with tmp as dump: print("[%] Carregando Rede Neural Artificial existente...") cerebro = pickle.load(dump) except (IOError, pickle.PickleError) as e: # Inicia a RNA print("[!] Nenhuma Rede Neural Artificial encontrada!") print("[+] Nova Rede Neural Artificial iniciada!") cerebro = buildNetwork(concursos.indim, NEURONIOS, concursos.outdim, recurrent=True, bias=True) # Prepara a RNA para o aprendizado
from pybrain.datasets import SequentialDataSet from itertools import cycle INPUT = 3 HIDDEN_LAYERS = 5 OUTPUT = 3 ds = SequentialDataSet(INPUT, OUTPUT) # Adding sequence of numbers (of both features) into neural network for (sample, next_sample, sam_v, next_sam_v, sam_t, next_sam_t) in zip(pitch_data, cycle(pitch_data[1:]), velocity_data, cycle(velocity_data[1:]), tick_data, cycle(tick_data[1:])): #ds.addSample((sample, velocity_data[i], tick_data[i]), next_sample) ds.addSample((sample, sam_v, sam_t), (next_sample, next_sam_v, next_sam_t)) # Build a simple LSTM networ end of the song somewhere around last 15 sec when my line pop. stupid friend decide it is a good time to send LL stickerk with 1 input node, 5 LSTM cells and 1 output node: from pybrain.tools.shortcuts import buildNetwork from pybrain.structure.modules import LSTMLayer print 'Constructing neutral network. . .' net = buildNetwork( INPUT, HIDDEN_LAYERS, OUTPUT, hiddenclass=LSTMLayer, outputbias=False, recurrent=True,
if __name__ == '__main__': import argparse parser = argparse.ArgumentParser(description="Trainer for LSTM Net"); parser.add_argument('filename', type=str, help="input training data"); parser.add_argument('--epochs-per-cycle', type=int, default=10, help="epochs per cycle, default value 10") parser.add_argument('--cycles', type=int, default=10, help="amount of cycles, default value 10") parser.add_argument('--learning', type=float, default=0.01, help="learning constant, default value 0.01") parser.add_argument('--debug', action="store_true", default=False, help="whether to recognize using debug algorithm or not") parser.add_argument("--input-nodes", type=int, default=5, help="number of input channels to the LSTM") parser.add_argument("--hidden-nodes", type=int, default=20, help="number of hidden channels to the LSTM") parser.add_argument("--output-nodes", type=int, default=1, help="number of output channels to the LSTM") parser.add_argument('--output', type=str, default="default.xml", help="set name of output file to store trained network"); parser.add_argument('--read-weights', type=str, help="set the input weights of this network, set name of directory to read weights"); opts = parser.parse_args(); ds = SequentialDataSet(opts.input_nodes, opts.output_nodes) data = [] with open(opts.filename, "rb") as csvfile: readr = csv.reader(csvfile, delimiter = ",") for row in readr: data.append(map(lambda x: float(x), row)) for i in xrange(0, len(data)-1): ds.addSample(data[i][2:], data[i+1][2]) net = RNN(opts.input_nodes, opts.hidden_nodes, opts.output_nodes) # if opts.debug: # print(net.n.module.indim) net.plotErrors(opts.epochs_per_cycle, opts.cycles, net.train(ds, opts.epochs_per_cycle, opts.cycles)) net.saveNetwork(opts.output)
# Initialization of the system: 1input & 1output dataset = SequentialDataSet(1, 1) dataset_2 = SequentialDataSet(1, 1) dataset_3 = SequentialDataSet(1, 1) dataset_4 = SequentialDataSet(1, 1) dataset_5 = SequentialDataSet(1, 1) dataset_6 = SequentialDataSet(1, 1) dataset_8 = SequentialDataSet(1, 1) dataset_9 = SequentialDataSet(1, 1) dataset_10 = SequentialDataSet(1, 1) dataset_bis = SequentialDataSet(1, 1) # Associates the current sample to the input, and the future sample to the output for current_sample, next_sample in zip(training_data, cycle(training_data[1:])): dataset.addSample(current_sample, next_sample) for current_sample, next_sample in zip(training_data_2, cycle(training_data_2[1:])): dataset_2.addSample(current_sample, next_sample) for current_sample, next_sample in zip(training_data_3, cycle(training_data_3[1:])): dataset_3.addSample(current_sample, next_sample) for current_sample, next_sample in zip(training_data_4, cycle(training_data_4[1:])): dataset_4.addSample(current_sample, next_sample) for current_sample, next_sample in zip(training_data_5, cycle(training_data_5[1:])): dataset_5.addSample(current_sample, next_sample) for current_sample, next_sample in zip(training_data_6, cycle(training_data_6[1:])): dataset_6.addSample(current_sample, next_sample) for current_sample, next_sample in zip(training_data_8, cycle(training_data_8[1:])): dataset_8.addSample(current_sample, next_sample) for current_sample, next_sample in zip(training_data_9, cycle(training_data_9[1:])): dataset_9.addSample(current_sample, next_sample) for current_sample, next_sample in zip(training_data_10, cycle(training_data_10[1:])):
ys.append(1 if y_found else 0) ys_seq.append(y_seq) xs.append(row) maxlen = max(len(row), maxlen) max_features = generator.max_id() + 2 train_ds = SequentialDataSet(max_features, 1) for xrow, yrow in zip(xs, ys_seq): train_ds.newSequence() for x, y in zip(xrow, yrow): one_hot = [0] * max_features one_hot[x] = 1 train_ds.addSample(one_hot, y) from pybrain.tools.shortcuts import buildNetwork from pybrain.structure.modules import LSTMLayer net = buildNetwork(max_features, 32, 1, hiddenclass=LSTMLayer, outputbias=False, recurrent=True) from pybrain.supervised import RPropMinusTrainer trainer = RPropMinusTrainer(net, dataset=train_ds) train_errors = [] # save errors for plotting later