ium_434684/ium_zadanie6_training.py

67 lines
1.8 KiB
Python
Raw Normal View History

2021-05-02 17:12:44 +02:00
import sys
2021-05-15 15:17:07 +02:00
from tensorflow.keras import layers, Sequential
2021-05-02 17:12:44 +02:00
# from keras.layers import Flatten,Dense,Dropout, GlobalAveragePooling2D
from tensorflow.keras.optimizers import Adam
2021-05-15 15:17:07 +02:00
from tensorflow import convert_to_tensor
2021-05-02 17:12:44 +02:00
import numpy as np
import pandas as pd
from sklearn.metrics import mean_squared_error
2021-05-16 11:55:28 +02:00
from sacred.observers import FileStorageObserver, MongoObserver
from sacred import Experiment
from sacred.observers import MongoObserver
from datetime import datetime
2021-05-16 22:25:46 +02:00
import mlflow
2021-05-02 17:12:44 +02:00
2021-05-16 11:55:28 +02:00
ex = Experiment("434684", interactive=False, save_git_info=False)
ex.observers.append(MongoObserver(url='mongodb://mongo_user:mongo_password_IUM_2021@172.17.0.1:27017', db_name='sacred'))
ex.observers.append(FileStorageObserver('my_runs'))
2021-05-02 17:12:44 +02:00
2021-05-16 11:55:28 +02:00
@ex.config
def my_config():
learning_rate = float(sys.argv[1])
2021-05-02 17:12:44 +02:00
2021-05-16 12:10:52 +02:00
@ex.capture
2021-05-16 11:55:28 +02:00
def prepare_train_model(learning_rate, _run):
2021-05-16 22:25:46 +02:00
with mlflow.start_run():
_run.info["prepare_model"] = str(datetime.now())
movies_train = pd.read_csv('movies_train.csv')
x_train = movies_train.copy()
y_train = x_train.pop('rottentomatoes_audience_score')
x_train.pop('Unnamed: 0')
model = Sequential()
model.add(layers.Input(shape=(22,)))
model.add(layers.Dense(64))
model.add(layers.Dense(64))
model.add(layers.Dense(32))
model.add(layers.Dense(1))
mlflow.log_param("learning_rate", learning_rate)
model.compile(loss='mean_absolute_error', optimizer=Adam(learning_rate))
_run.info["train model"] = str(datetime.now())
2021-05-16 11:55:28 +02:00
2021-05-16 22:25:46 +02:00
history = model.fit(
x = convert_to_tensor(x_train, np.float32),
y = y_train,
verbose=0, epochs=99)
loss = history.history['loss'][-1]
_run.info["Loss"] = str(loss)
mlflow.log_metric("Loss", loss)
model.save('model_movies.h5')
2021-05-16 11:55:28 +02:00
@ex.main
def my_main(learning_rate):
print(prepare_train_model())
2021-05-02 17:12:44 +02:00
2021-05-16 11:55:28 +02:00
r = ex.run()
ex.add_artifact("model_movies.h5")