SephsRL_modelv2 / Train_n_save.py
huninEye's picture
Update Train_n_save.py
b59b2fa verified
import tensorflow as tf
from tensorflow.keras.callbacks import ModelCheckpoint, EarlyStopping
import pandas as pd
from neural_network import create_neural_network_model # Import your custom model function
# Load training data from CSV using pandas
training_data = pd.read_csv('sensor_input.csv')
# Assuming your training data has columns like 'features', 'labels', and 'rewards'
train_inputs = training_data['features'] # Replace 'features' with the actual column name
train_labels = training_data['labels'] # Replace 'labels' with the actual column name
train_rewards = training_data['rewards'] # Replace 'rewards' with the actual column name
# Define your model architecture
seq_length = 128 # Example sequence length
d_model = 512 # Example dimension
action_space_size = 10 # Example action space size
model = create_neural_network_model(seq_length, d_model, action_space_size)
# Define loss functions and metrics
losses = {'Output': 'categorical_crossentropy', 'Reward': 'mean_squared_error'}
metrics = {'Output': 'accuracy'}
# Compile the model
opt = tf.keras.optimizers.Adam(learning_rate=0.001)
model.compile(optimizer=opt, loss=losses, metrics=metrics)
# Define callbacks (e.g., ModelCheckpoint, EarlyStopping) as needed
callbacks = [
ModelCheckpoint(filepath='model_weights.h5', save_best_only=True),
EarlyStopping(patience=5, restore_best_weights=True)
]
# Train the model
history = model.fit(
x=train_inputs, # Your training data
y={'Output': train_labels, 'Reward': train_rewards}, # Your training labels and rewards
batch_size=32,
epochs=50,
callbacks=callbacks
)
# Save the trained model
model.save('Sephs_model.h5')
# You can also save training history for analysis and plotting
import pickle
with open('training_history.pickle', 'wb') as history_file:
pickle.dump(history.history, history_file)