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 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(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"
MyMIDI = MIDIFile(1) track = 0 time = 0 MyMIDI.addTrackName(track,time,"Sample Track") #tempo = 120 tempo = 120 MyMIDI.addTempo(track,time,tempo) i = 0 time = 0 prev_pitch_ar = np.array([]) # Preform seeding (although seeding is not random, it seeds the original midi song) for (sample, target) in ds.getSequenceIterator(0): #print track_fi # Part of code used to have generator predict based on its own prev notes ''' if i != 0: sample = prev_ac_ar ''' pred_ar = net.activate(sample) tick_n = int(pred_ar[2]) pitch_n = int(pred_ar[0]) velocity_n = int(pred_ar[1]) # To remove negative numbers and turn them into zero
print("\r epoch {}/{}".format(epoch, EPOCHS), end="") stdout.flush() print() print("final error =", train_errors[-1]) ## Plot the data and the training import matplotlib.pyplot as plt plt.plot(range(0, EPOCHS, EPOCHS_PER_CYCLE), train_errors) plt.xlabel('epoch') plt.ylabel('error') plt.show() mape_error = 0 count = 0 ## Predict new examples for sample, actual in ds.getSequenceIterator(0): PredictedValue = net.activate(sample) count = count + 1 # MAPE Error CurrentError = abs((actual - PredictedValue) * 100.00 / actual) mape_error = mape_error + CurrentError print( " sample = %4.3f. Prediction = %4.3f. Actual = %4.3f. Error = %4.3f. Normalised Error = %4.3f " % (sample, PredictedValue, actual, (actual - PredictedValue), CurrentError)) print("Total Mean Absolute Percentage Error = %4.3f Percentage" % (mape_error / count))
# Start constructing the new song (Midiutil version) MyMIDI = MIDIFile(1) track = 0 time = 0 MyMIDI.addTrackName(track, time, "Sample Track") #tempo = 120 tempo = 120 MyMIDI.addTempo(track, time, tempo) i = 0 time = 0 prev_pitch_ar = np.array([]) # Preform seeding (although seeding is not random, it seeds the original midi song) for (sample, target) in ds.getSequenceIterator(0): #print track_fi # Part of code used to have generator predict based on its own prev notes ''' if i != 0: sample = prev_ac_ar ''' pred_ar = net.activate(sample) tick_n = int(pred_ar[2]) pitch_n = int(pred_ar[0]) velocity_n = int(pred_ar[1]) # To remove negative numbers and turn them into zero
DC = 0 tab_avg_error = [] the_errors = [] MAT = 20 circular_array = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] detector = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] # Thresholds for collective anomaly detection, up to the user and their systems to set them AVERAGE_THRESH = 0.8 DC_Thresh = 8 RET = 0.9 # Testing and detecting collective errors for current_sample, target in dataset_bis.getSequenceIterator(0): # Test the data -- comp is the output of the network comp = network.activate(current_sample) # Error calculation erreur = comp - target the_errors.insert(my_error_position, abs(erreur)) position += 1 position = position % MAT # Integrating the newly calculated error value to the circular array circular_array.pop(position) circular_array.insert(position, abs(erreur))
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() # test output predict_list = [] actual_list = [] err = [] for sample, target in test_ds.getSequenceIterator(0): p = net.activate(sample) a = np.argmax(p) d1 = p[a] if a == 1: d1 = -p[a] t = np.argmax(target) d2 = target[t] if t == 1: d2 = -target[t] predict = (d1 * sample[3]) + sample[3] actual = (d2 * sample[3]) + sample[3] err.append(abs((d2 - d1) / d1)) # compute error rate predict_list.append(predict) actual_list.append(actual)
trainer.trainEpochs(EPOCHS_PER_CYCLE) # train on the given data set for given number of epochs 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]) ## Plot the data and the training import matplotlib.pyplot as plt plt.plot(range(0, EPOCHS, EPOCHS_PER_CYCLE), train_errors) plt.xlabel('epoch') plt.ylabel('error') plt.show() mape_error = 0 count = 0 ## Predict new examples for sample, actual in ds.getSequenceIterator(0): PredictedValue = net.activate(sample) count = count + 1 # MAPE Error CurrentError = abs ((actual - PredictedValue) * 100.00 / actual ) mape_error = mape_error + CurrentError print(" sample = %4.3f. Prediction = %4.3f. Actual = %4.3f. Error = %4.3f. Normalised Error = %4.3f " % (sample, PredictedValue, actual, (actual - PredictedValue ), CurrentError ) ) print ("Total Mean Absolute Percentage Error = %4.3f Percentage" % (mape_error/count) )