-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
41 lines (30 loc) · 1.24 KB
/
train.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
import tensorflow.keras as keras
from preprocess import generate_training_sequences, SEQUENCE_LENGTH
from constants import OUTPUT_UNITS, NUM_UNITS, LOSS, LEARNING_RATE, EPOCHS, BATCH_SIZE, SAVE_MODEL_PATH
def build_model(output_units, num_units, loss, learning_rate):
# Create the model architecture
input = keras.layers.Input(shape=(None, output_units))
x = keras.layers.LSTM(num_units[0])(input)
x = keras.layers.Dropout(0.2)(x)
output = keras.layers.Dense(output_units, activation="softmax")(x)
model = keras.Model(input, output)
# Compile model
model.compile(
loss=loss,
optimizer=keras.optimizers.legacy.Adam(learning_rate=learning_rate),
metrics=["accuracy"]
)
model.summary()
return model
def train(output_units=OUTPUT_UNITS, num_units=NUM_UNITS, loss=LOSS, learning_rate=LEARNING_RATE):
# Generate the training sequences
inputs, targets = generate_training_sequences(SEQUENCE_LENGTH)
# Build the network
model = build_model(output_units, num_units, loss, learning_rate)
# Train the model
model.fit(inputs, targets, epochs=EPOCHS, batch_size=BATCH_SIZE)
# Save the model
model.save(SAVE_MODEL_PATH)
return model
if __name__ == "__main__":
train()