All checks were successful
s449288-training/pipeline/head This commit looks good
79 lines
1.9 KiB
Python
79 lines
1.9 KiB
Python
import tensorflow as tf
|
|
from keras import layers
|
|
from keras.models import save_model
|
|
import pandas as pd
|
|
import numpy as np
|
|
import matplotlib.pyplot as plt
|
|
import sys
|
|
|
|
# Pobranie przykładowego argumentu trenowania
|
|
EPOCHS_NUM = int(sys.argv[1])
|
|
|
|
# Wczytanie danych
|
|
data_train = pd.read_csv('lego_sets_clean_train.csv')
|
|
data_test = pd.read_csv('lego_sets_clean_test.csv')
|
|
|
|
# Wydzielenie zbiorów dla predykcji ceny zestawu na podstawie liczby klocków, którą zawiera
|
|
train_piece_counts = np.array(data_train['piece_count'])
|
|
train_prices = np.array(data_train['list_price'])
|
|
test_piece_counts = np.array(data_test['piece_count'])
|
|
test_prices = np.array(data_test['list_price'])
|
|
|
|
# Normalizacja
|
|
normalizer = layers.Normalization(input_shape=[1, ], axis=None)
|
|
normalizer.adapt(train_piece_counts)
|
|
|
|
# Inicjalizacja
|
|
model = tf.keras.Sequential([
|
|
normalizer,
|
|
layers.Dense(units=1)
|
|
])
|
|
|
|
# Kompilacja
|
|
model.compile(
|
|
optimizer=tf.optimizers.Adam(learning_rate=0.1),
|
|
loss='mean_absolute_error'
|
|
)
|
|
|
|
# Trening
|
|
history = model.fit(
|
|
train_piece_counts,
|
|
train_prices,
|
|
epochs=EPOCHS_NUM,
|
|
verbose=0,
|
|
validation_split=0.2
|
|
)
|
|
|
|
# Prosta ewaluacja
|
|
test_results = {'model': model.evaluate(
|
|
test_piece_counts,
|
|
test_prices, verbose=0)
|
|
}
|
|
|
|
# Wykonanie wielu predykcji
|
|
x = tf.linspace(100, 7000, 6901)
|
|
y = model.predict(x)
|
|
|
|
# Zapis predykcji do pliku
|
|
results = pd.DataFrame({"input_piece_count": x.numpy().tolist(), "predicted_price": [a[0] for a in y.tolist()]})
|
|
results.to_csv(r'lego_reg_results.csv', index=False, header=True)
|
|
|
|
# Zapis modelu do pliku
|
|
model.save('lego_reg_model')
|
|
|
|
# Opcjonalne statystyki, wykresy
|
|
'''
|
|
print(test_results)
|
|
|
|
hist = pd.DataFrame(history.history)
|
|
hist['epoch'] = history.epoch
|
|
print(hist.tail())
|
|
|
|
plt.scatter(train_piece_counts, train_prices, label='Data')
|
|
plt.plot(x, y, color='k', label='Predictions')
|
|
plt.xlabel('pieces')
|
|
plt.ylabel('price')
|
|
plt.legend()
|
|
plt.show()
|
|
'''
|