2024-05-26 14:23:37 +02:00
|
|
|
from sacred import Experiment
|
|
|
|
from sacred.observers import MongoObserver, FileStorageObserver
|
2024-04-30 10:49:32 +02:00
|
|
|
import tensorflow as tf
|
|
|
|
import pandas as pd
|
|
|
|
from sklearn.preprocessing import LabelEncoder, StandardScaler
|
|
|
|
from sklearn.model_selection import train_test_split
|
|
|
|
import json
|
2024-05-26 14:16:50 +02:00
|
|
|
|
2024-05-26 14:23:37 +02:00
|
|
|
ex = Experiment("s464906_experiment")
|
2024-04-30 10:49:32 +02:00
|
|
|
|
2024-05-26 14:23:37 +02:00
|
|
|
mongo_url = "mongodb://admin:IUM_2021@tzietkiewicz.vm.wmi.amu.edu.pl:27017"
|
|
|
|
ex.observers.append(MongoObserver(url=mongo_url, db_name='sacred'))
|
|
|
|
ex.observers.append(FileStorageObserver('logs'))
|
2024-04-30 10:49:32 +02:00
|
|
|
|
2024-05-26 14:23:37 +02:00
|
|
|
@ex.config
|
|
|
|
def cfg():
|
|
|
|
epochs = 100
|
2024-04-30 10:49:32 +02:00
|
|
|
|
2024-05-26 14:23:37 +02:00
|
|
|
@ex.automain
|
|
|
|
def train_model(epochs):
|
|
|
|
df = pd.read_csv('OrangeQualityData.csv')
|
2024-04-30 10:49:32 +02:00
|
|
|
|
2024-05-26 14:23:37 +02:00
|
|
|
encoder = LabelEncoder()
|
|
|
|
df["Color"] = encoder.fit_transform(df["Color"])
|
|
|
|
df["Variety"] = encoder.fit_transform(df["Variety"])
|
|
|
|
df["Blemishes"] = df["Blemishes (Y/N)"].apply(lambda x: 1 if x.startswith("Y") else 0)
|
2024-04-30 10:49:32 +02:00
|
|
|
|
2024-05-26 14:23:37 +02:00
|
|
|
df.drop(columns=["Blemishes (Y/N)"], inplace=True)
|
2024-04-30 10:49:32 +02:00
|
|
|
|
2024-05-26 14:23:37 +02:00
|
|
|
X = df.drop(columns=["Quality (1-5)"])
|
|
|
|
y = df["Quality (1-5)"]
|
2024-04-30 10:49:32 +02:00
|
|
|
|
2024-05-26 14:23:37 +02:00
|
|
|
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
|
2024-04-30 10:49:32 +02:00
|
|
|
|
2024-05-26 14:23:37 +02:00
|
|
|
scaler = StandardScaler()
|
|
|
|
X_train_scaled = scaler.fit_transform(X_train)
|
|
|
|
X_test_scaled = scaler.transform(X_test)
|
2024-04-30 10:49:32 +02:00
|
|
|
|
2024-05-26 14:23:37 +02:00
|
|
|
model = tf.keras.Sequential([
|
|
|
|
tf.keras.layers.Dense(64, activation='relu', input_shape=(X_train_scaled.shape[1],)),
|
|
|
|
tf.keras.layers.Dense(32, activation='relu'),
|
|
|
|
tf.keras.layers.Dense(1)
|
|
|
|
])
|
2024-05-26 14:16:50 +02:00
|
|
|
|
2024-05-26 14:23:37 +02:00
|
|
|
model.compile(optimizer='sgd', loss='mse')
|
2024-05-26 14:16:50 +02:00
|
|
|
|
2024-05-26 14:23:37 +02:00
|
|
|
history = model.fit(X_train_scaled, y_train, epochs=epochs, verbose=0, validation_data=(X_test_scaled, y_test))
|
|
|
|
|
|
|
|
ex.log_scalar("epochs", epochs)
|
|
|
|
|
|
|
|
ex.add_artifact(__file__)
|
2024-04-30 10:49:32 +02:00
|
|
|
|
2024-05-26 14:16:50 +02:00
|
|
|
model.save('orange_quality_model_tf.h5')
|
2024-05-26 14:23:37 +02:00
|
|
|
ex.add_artifact('orange_quality_model_tf.h5')
|
2024-04-30 10:49:32 +02:00
|
|
|
|
2024-05-26 14:23:37 +02:00
|
|
|
for key, value in history.history.items():
|
|
|
|
ex.log_scalar(key, value[-1])
|
2024-04-30 10:49:32 +02:00
|
|
|
|
2024-05-26 14:23:37 +02:00
|
|
|
predictions = model.predict(X_test_scaled)
|
2024-05-26 14:16:50 +02:00
|
|
|
with open('predictions_tf.json', 'w') as f:
|
|
|
|
json.dump(predictions.tolist(), f, indent=4)
|
2024-05-26 14:23:37 +02:00
|
|
|
ex.add_artifact('predictions_tf.json')
|
|
|
|
|
|
|
|
return 'Training completed successfully'
|