Skip to content

Commit

Permalink
Added gru and composer
Browse files Browse the repository at this point in the history
  • Loading branch information
vanpelt committed Feb 3, 2019
1 parent 0d3689b commit 72d8cbc
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 16 deletions.
19 changes: 10 additions & 9 deletions keras-audio/gru-composer.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import Dropout
from keras.layers import LSTM, CuDNNGRU
from keras.layers import LSTM, CuDNNGRU, GRU
from keras.layers import Activation
from keras.utils import np_utils
from keras.callbacks import ModelCheckpoint, Callback
Expand All @@ -17,11 +17,12 @@
import base64
wandb.init()


def ensure_midi(dataset="mario"):
if not os.path.exists("data/%s" % dataset):
print("Downloading %s dataset..." % dataset)
subprocess.check_output(
"curl -SL https://storage.googleapis.com/wandb/%s.tar.gz | tar xz" % dataset, shell=True) #finalfantasy
"curl -SL https://storage.googleapis.com/wandb/%s.tar.gz | tar xz" % dataset, shell=True) # finalfantasy
open("data/%s" % dataset, "w").close()


Expand Down Expand Up @@ -111,15 +112,15 @@ def prepare_sequences(notes, n_vocab):
def create_network(network_input, n_vocab):
""" create the structure of the neural network """
model = Sequential()
model.add(CuDNNGRU(
model.add(GRU(
256,
input_shape=(network_input.shape[1], network_input.shape[2]),
return_sequences=True
))
model.add(Dropout(0.3))
model.add(CuDNNGRU(128, return_sequences=True))
model.add(GRU(128, return_sequences=True))
model.add(Dropout(0.3))
model.add(CuDNNGRU(64))
model.add(GRU(64))
model.add(Dense(256))
model.add(Dropout(0.3))
model.add(Dense(n_vocab))
Expand All @@ -133,6 +134,7 @@ class Midi(Callback):
"""
Callback for sampling a midi file
"""

def sample(self, preds, temperature=1.0):
# helper function to sample an index from a probability array
preds = np.asarray(preds).astype('float64')
Expand All @@ -141,7 +143,7 @@ def sample(self, preds, temperature=1.0):
preds = exp_preds / np.sum(exp_preds)
probas = np.random.multinomial(1, preds, 1)
return np.argmax(probas)

def generate_notes(self, network_input, pitchnames, n_vocab):
""" Generate notes from the neural network based on a sequence of notes """
# pick a random sequence from the input as a starting point for the prediction
Expand All @@ -161,8 +163,7 @@ def generate_notes(self, network_input, pitchnames, n_vocab):

prediction = model.predict(prediction_input, verbose=0)

# TODO: add random picking
index = np.argmax(prediction)#self.sample(prediction)#np.argmax
index = self.sample(prediction[0], temperature=0.5)#np.argmax
result = int_to_note[index]
prediction_output.append(result)

Expand Down Expand Up @@ -243,5 +244,5 @@ def train(model, network_input, network_output):


if __name__ == '__main__':
ensure_midi("finalfantasy")
ensure_midi("mario")
train_network()
7 changes: 0 additions & 7 deletions keras-audio/gru.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -323,13 +323,6 @@
"source": [
"m2.predict(np.array([0.4,0.5,0.6,0.7,0.8]).reshape((1,5,1)))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand Down

0 comments on commit 72d8cbc

Please sign in to comment.