ium_434788/Zadanie_06_training.py

75 lines
2.3 KiB
Python
Raw Normal View History

2021-05-13 19:18:15 +02:00
from tensorflow.keras.models import Sequential, load_model
from tensorflow.keras.layers import Dense
from sklearn.metrics import accuracy_score, classification_report
import pandas as pd
from sklearn.model_selection import train_test_split
import numpy as np
2021-05-15 17:24:05 +02:00
import sys
2021-05-15 16:59:57 +02:00
from sklearn.preprocessing import StandardScaler, LabelEncoder
from tensorflow.keras.optimizers import Adam
2021-05-15 20:26:44 +02:00
from sacred.observers import FileStorageObserver, MongoObserver
2021-05-15 20:16:39 +02:00
from sacred import Experiment
from datetime import datetime
2021-05-15 20:19:58 +02:00
import os
import pymongo
2021-05-15 15:04:56 +02:00
2021-05-15 20:26:44 +02:00
ex = Experiment("434788-mongo", 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'))
2021-05-13 19:18:15 +02:00
2021-05-15 20:26:44 +02:00
@ex.config
def my_config():
batch_param = int(sys.argv[1])
epoch_param = int(sys.argv[2])
2021-05-13 19:18:15 +02:00
2021-05-15 20:26:44 +02:00
@ex.capture
def prepare_model(epoch_param, batch_param, _run):
_run.info["prepare_model_ts"] = str(datetime.now())
2021-05-13 19:18:15 +02:00
2021-05-15 20:26:44 +02:00
wine=pd.read_csv('train.csv')
2021-05-13 19:18:15 +02:00
2021-05-15 20:26:44 +02:00
y = wine['quality']
x = wine.drop('quality', axis=1)
2021-05-13 19:18:15 +02:00
2021-05-15 20:26:44 +02:00
citricacid = x['fixed acidity'] * x['citric acid']
citric_acidity = pd.DataFrame(citricacid, columns=['citric_accidity'])
2021-05-13 19:18:15 +02:00
2021-05-15 20:26:44 +02:00
density_acidity = x['fixed acidity'] * x['density']
density_acidity = pd.DataFrame(density_acidity, columns=['density_acidity'])
2021-05-13 19:18:15 +02:00
2021-05-15 20:26:44 +02:00
x = wine.join(citric_acidity).join(density_acidity)
2021-05-15 16:59:57 +02:00
2021-05-15 20:26:44 +02:00
bins = (2, 5, 8)
gnames = ['bad', 'nice']
y = pd.cut(y, bins = bins, labels = gnames)
2021-05-15 16:59:57 +02:00
2021-05-15 20:26:44 +02:00
enc = LabelEncoder()
yenc = enc.fit_transform(y)
2021-05-15 16:59:57 +02:00
2021-05-15 20:26:44 +02:00
scale = StandardScaler()
scaled_x = scale.fit_transform(x)
2021-05-15 16:59:57 +02:00
2021-05-15 20:26:44 +02:00
NeuralModel = Sequential([
Dense(128, activation='relu', input_shape=(14,)),
Dense(32, activation='relu'),
Dense(64, activation='relu'),
Dense(64, activation='relu'),
Dense(64, activation='relu'),
Dense(1, activation='sigmoid')
])
2021-05-15 16:59:57 +02:00
2021-05-15 20:26:44 +02:00
rms = Adam(lr=0.0003)
2021-05-15 16:59:57 +02:00
2021-05-15 20:26:44 +02:00
NeuralModel.compile(optimizer=rms, loss='binary_crossentropy', metrics=['accuracy'])
NeuralModel.fit(scaled_x, yenc, batch_size= batch_param, epochs = epoch_param) #verbose = 1
NeuralModel.save('wine_model.h5')
@ex.main
def my_main(train_size_param, test_size_param):
print(prepare_model())
r = ex.run()
ex.add_artifact("saved_model/saved_model.pb")