|
import tensorflow as tf |
|
from tensorflow.keras.callbacks import ModelCheckpoint, EarlyStopping |
|
import pandas as pd |
|
from neural_network import create_neural_network_model |
|
|
|
|
|
training_data = pd.read_csv('sensor_input.csv') |
|
|
|
|
|
train_inputs = training_data['features'] |
|
train_labels = training_data['labels'] |
|
train_rewards = training_data['rewards'] |
|
|
|
|
|
seq_length = 128 |
|
d_model = 512 |
|
action_space_size = 10 |
|
|
|
model = create_neural_network_model(seq_length, d_model, action_space_size) |
|
|
|
|
|
losses = {'Output': 'categorical_crossentropy', 'Reward': 'mean_squared_error'} |
|
metrics = {'Output': 'accuracy'} |
|
|
|
|
|
opt = tf.keras.optimizers.Adam(learning_rate=0.001) |
|
model.compile(optimizer=opt, loss=losses, metrics=metrics) |
|
|
|
|
|
callbacks = [ |
|
ModelCheckpoint(filepath='model_weights.h5', save_best_only=True), |
|
EarlyStopping(patience=5, restore_best_weights=True) |
|
] |
|
|
|
|
|
history = model.fit( |
|
x=train_inputs, |
|
y={'Output': train_labels, 'Reward': train_rewards}, |
|
batch_size=32, |
|
epochs=50, |
|
callbacks=callbacks |
|
) |
|
|
|
|
|
model.save('Sephs_model.h5') |
|
|
|
|
|
import pickle |
|
with open('training_history.pickle', 'wb') as history_file: |
|
pickle.dump(history.history, history_file) |