75 lines
2.4 KiB
Python
75 lines
2.4 KiB
Python
import pandas as pd
|
|
from keras.models import Sequential
|
|
from keras.layers import Dense
|
|
from sacred import Experiment
|
|
import json
|
|
from sacred.observers import MongoObserver
|
|
from sacred.observers import FileStorageObserver
|
|
|
|
ex = Experiment("z487183", interactive=True)
|
|
ex.observers.append(MongoObserver(url='mongodb://admin:IUM_2021@172.17.0.1:27017', db_name='sacred'))
|
|
ex.observers.append(FileStorageObserver('my_runs'))
|
|
|
|
@ex.config
|
|
def my_config():
|
|
optimizer = 'sgd'
|
|
loss_function = 'binary_crossentropy'
|
|
|
|
@ex.automain
|
|
def my_main(optimizer, loss_function, _run):
|
|
# prepare dataset
|
|
features = ['Rooms', 'Distance', 'Bedroom2', 'Bathroom']
|
|
|
|
X_train_resource = ex.open_resource('X_train.csv')
|
|
y_train_resource = ex.open_resource('Y_train.csv')
|
|
|
|
X_dev_resource = ex.open_resource('X_val.csv')
|
|
y_dev_resource = ex.open_resource('Y_val.csv')
|
|
|
|
X_test_resource = ex.open_resource('X_test.csv')
|
|
y_test_resource = ex.open_resource('Y_test.csv')
|
|
|
|
X_train = pd.read_csv(X_train_resource).values
|
|
y_train = pd.read_csv(y_train_resource).values
|
|
|
|
X_dev = pd.read_csv(X_dev_resource).values
|
|
y_dev = pd.read_csv(y_dev_resource).values
|
|
|
|
X_test = pd.read_csv(X_test_resource).values
|
|
y_test = pd.read_csv(y_test_resource).values
|
|
|
|
# model definition
|
|
model = Sequential([
|
|
Dense(32, activation='relu', input_shape=(len(features),)),
|
|
Dense(32, activation='relu'),
|
|
Dense(1, activation='sigmoid'),
|
|
])
|
|
|
|
#compile and train
|
|
model.compile(optimizer=optimizer, loss=loss_function, metrics=['accuracy'])
|
|
hist = model.fit(X_train, y_train,
|
|
batch_size=32, epochs=100,
|
|
validation_data=(X_dev, y_dev))
|
|
model.save('model.h5')
|
|
|
|
loss, accuracy = model.evaluate(X_test, y_test)
|
|
_run.log_scalar("training.loss", loss)
|
|
_run.log_scalar("training.accuracy", accuracy)
|
|
|
|
with open("sacred-files/parameters.json", 'w') as file:
|
|
json.dump(my_config(), file)
|
|
|
|
with open('property_model.py', 'r') as current_file:
|
|
file_content = current_file.read()
|
|
with open('sacred-files/source_code.py', 'w') as new_file:
|
|
new_file.write(file_content)
|
|
|
|
with open("sacred-files/metrics.txt", 'w') as file:
|
|
file.write(f"loss: {loss}")
|
|
file.write(f"accuracy: {accuracy}")
|
|
|
|
ex.add_artifact("model.h5")
|
|
ex.add_artifact("sacred-files/parameters.json")
|
|
ex.add_artifact("sacred-files/source_code.py")
|
|
ex.add_artifact("sacred-files/metrics.txt")
|