-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
44 lines (37 loc) · 1.53 KB
/
model.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
from tensorflow.keras.callbacks import LambdaCallback
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Activation, LSTM
from tensorflow.keras.layers import Dropout, TimeDistributed
try:
from tensorflow.python.keras.layers import CuDNNLSTM as lstm
except:
from tensorflow.keras.layers import Dense, Activation, LSTM as lstm
from tensorflow.keras.layers import Dropout
from tensorflow.keras.optimizers import RMSprop
from tensorflow.keras.models import load_model as lm
import numpy as np
import random
import sys
import io
from midi import Midi
class Model:
def create(self, size, unique_notes, optimizer=None, hidden_size=128):
self.model = Sequential()
self.model.add(lstm(hidden_size, input_shape=(
size, unique_notes), return_sequences=True))
self.model.add(lstm(hidden_size))
self.model.add(Dropout(0.2))
self.model.add(Dense(unique_notes))
self.model.add(Activation('softmax'))
self.model.compile(loss='categorical_crossentropy', optimizer=RMSprop(
lr=0.01) if optimizer == None else optimizer)
def load_from_file(self, name="model.h5"):
self.model = lm(name)
def save_to_file(self, name="model.h5"):
self.model.save(name)
def learn(self, inputs, outputs, batch_size=256, epochs=185):
self.model.fit(inputs, outputs,
batch_size=batch_size,
epochs=epochs, verbose=True)
def predict(self, arr):
return self.model.predict(arr)