ium_470607/lab5/train/train.py

59 lines
1.5 KiB
Python
Raw Normal View History

2021-05-16 14:30:55 +02:00
from datetime import datetime
2021-05-02 22:01:32 +02:00
import pandas as pd
2021-05-16 14:30:55 +02:00
from sacred import Experiment
2021-05-16 16:00:12 +02:00
from sacred.observers import FileStorageObserver, MongoObserver
2021-05-15 17:34:13 +02:00
import sys
2021-05-15 19:01:47 +02:00
import tensorflow
2021-05-02 22:01:32 +02:00
from tensorflow.keras import layers
2021-05-16 14:30:55 +02:00
ex = Experiment("470607", interactive=False, save_git_info=False)
2021-05-16 16:04:31 +02:00
ex.observers.append(
MongoObserver(url='mongodb://mongo_user:mongo_password_IUM_2021@172.17.0.1:27017', db_name='sacred'))
2021-05-16 14:30:55 +02:00
ex.observers.append(FileStorageObserver('my_runs'))
2021-05-02 22:01:32 +02:00
2021-05-16 16:04:31 +02:00
2021-05-16 14:30:55 +02:00
@ex.config
def my_config():
learning_rate = float(sys.argv[1])
2021-05-02 22:01:32 +02:00
2021-05-16 16:04:31 +02:00
2021-05-16 14:30:55 +02:00
@ex.capture
def prepare_train_model(learning_rate, _run):
_run.info["prepare_model"] = str(datetime.now())
2021-05-02 22:01:32 +02:00
2021-05-16 14:30:55 +02:00
X_train = pd.read_csv('train.csv')
X_valid = pd.read_csv('valid.csv')
2021-05-02 22:01:32 +02:00
2021-05-16 14:30:55 +02:00
Y_train = X_train.pop('stabf')
Y_train = pd.get_dummies(Y_train)
2021-05-02 22:01:32 +02:00
2021-05-16 14:30:55 +02:00
Y_valid = X_valid.pop('stabf')
Y_valid = pd.get_dummies(Y_valid)
model = tensorflow.keras.Sequential([
2021-05-16 16:04:31 +02:00
layers.Input(shape=(12,)),
layers.Dense(32),
layers.Dense(16),
layers.Dense(2, activation='softmax')
2021-05-16 14:30:55 +02:00
])
model.compile(
loss=tensorflow.keras.losses.BinaryCrossentropy(),
2021-05-16 16:02:00 +02:00
optimizer=tensorflow.keras.optimizers.Adam(learning_rate=learning_rate),
2021-05-16 14:30:55 +02:00
metrics=[tensorflow.keras.metrics.BinaryAccuracy()])
history = model.fit(X_train, Y_train, epochs=2, validation_data=(X_valid, Y_valid))
model.save('grid-stability-dense.h5')
2021-05-16 16:04:31 +02:00
_run['history'] = str(history.history[:, -1])
2021-05-16 14:30:55 +02:00
@ex.main
def my_main(learning_rate):
print(prepare_train_model())
r = ex.run()
ex.add_artifact('grid-stability-dense.h5')