Guest User

Untitled

a guest
Mar 4th, 2016
353
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import Basics as midi
  2.  
  3. from keras.models import Sequential
  4. from keras.layers.core import Dense, Activation, Dropout, RepeatVector, Reshape
  5. from keras.layers.recurrent import LSTM
  6. from keras.preprocessing import sequence
  7. import numpy as np
  8. np.set_printoptions(threshold=np.nan)
  9.  
  10. midi_data = midi.extract_data()
  11. lahead = 1
  12.  
  13. #Put music data in 3D numpy array
  14. def gen_note_data(note_data):
  15.     notes = np.zeros((len(note_data[0]), 1, 4), dtype=object)
  16.     for i in range(len(note_data[0])):
  17.         notes[i, 0] = note_data[0][i]
  18.     return notes
  19.  
  20. n_in = gen_note_data(midi_data)
  21.  
  22. #Numpy array with expected output. For each note at time t in the sequence
  23. #we get the note at t+1
  24. n_out = np.zeros((len(n_in), 4), dtype=object)
  25. for i in range(len(n_in) - lahead):
  26.     n_out[i] = n_in[i + 1:i + lahead + 1]
  27.  
  28.  
  29. model = Sequential()
  30. model.add(LSTM(100, input_dim=4, batch_input_shape=(len(n_in[0]), 1, 4), return_sequences=True))
  31. model.add(LSTM(100, batch_input_shape=(len(n_in[0]), 1, 4)))
  32. model.add(Dense(4))
  33.  
  34. model.compile(loss='categorical_crossentropy',
  35.               optimizer='rmsprop')
  36. print 'Training...'
  37.  
  38. model.fit(n_in, n_out, batch_size=len(n_in), nb_epoch=100, show_accuracy=True, verbose=2)
  39.  
  40. print model.predict(n_in, batch_size=len(n_in))
RAW Paste Data