ium_470607/lab5/train/train.py

68 lines
2.1 KiB
Python
Raw Permalink Normal View History

2021-05-16 14:30:55 +02:00
from datetime import datetime
2021-05-16 23:01:34 +02:00
import mlflow
2021-05-21 16:26:48 +02:00
from mlflow.models.signature import infer_signature
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):
2021-05-16 23:01:34 +02:00
with mlflow.start_run():
_run.info["prepare_model"] = str(datetime.now())
2021-05-02 22:01:32 +02:00
2021-05-16 23:01:34 +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 23:01:34 +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 23:01:34 +02:00
Y_valid = X_valid.pop('stabf')
Y_valid = pd.get_dummies(Y_valid)
2021-05-16 14:30:55 +02:00
2021-05-16 23:01:34 +02:00
model = tensorflow.keras.Sequential([
layers.Input(shape=(12,)),
layers.Dense(32),
layers.Dense(16),
layers.Dense(2, activation='softmax')
])
2021-05-16 14:30:55 +02:00
2021-05-16 23:01:34 +02:00
model.compile(
loss=tensorflow.keras.losses.BinaryCrossentropy(),
optimizer=tensorflow.keras.optimizers.Adam(learning_rate=learning_rate),
metrics=[tensorflow.keras.metrics.BinaryAccuracy()])
2021-05-16 14:30:55 +02:00
2021-05-16 23:01:34 +02:00
history = model.fit(X_train, Y_train, epochs=2, validation_data=(X_valid, Y_valid))
2021-05-16 14:30:55 +02:00
2021-05-16 23:01:34 +02:00
model.save('grid-stability-dense.h5')
2021-05-21 16:26:48 +02:00
mlflow.keras.save_model(keras_model=model,
path='grid-stability-dense',
signature=infer_signature(X_train, Y_train),
input_example=X_train.iloc[0])
2021-05-16 14:30:55 +02:00
2021-05-16 23:01:34 +02:00
_run.info['history'] = str(history.history['loss'][-1])
mlflow.log_metric('loss', history.history['loss'][-1])
mlflow.log_param('learning_rate', learning_rate)
2021-05-16 16:04:31 +02:00
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')