<jupyter_start><jupyter_text>Import Dataset<jupyter_code>def load_cup():
    ml_cup = np.delete(np.genfromtxt('ML-CUP20-TR.csv', 
                                     delimiter=','), obj=0, axis=1)
    return ml_cup[:, :-2], ml_cup[:, -2:]

def load_cup_blind():
    return np.delete(np.genfromtxt('ML-CUP20-TS.csv',
                                   delimiter=','), obj=0, axis=1)
    
def mean_euclidean_error(y_true, y_pred):
    assert y_true.shape == y_pred.shape
    return np.mean(np.linalg.norm(y_pred - y_true, axis=1))
X, y = load_cup()

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.1)
print(X.shape)
print(y.shape)
from tensorflow.keras.models import Sequential
from tensorflow.keras import layers
from tensorflow.keras.layers import Dense
from tensorflow.keras.optimizers import SGD
from tensorflow.keras.regularizers import L2

# Function to create model, required for KerasRegressor
def create_model(learn_rate=0.01, momentum=0.9, neurons_layer_1=130,
                 init_mode='uniform', activation='sigmoid',  weight_decay = 0.00001,
                 nesterov = True, neurons_layer_2 = 30):
  # create model
    model = Sequential()
    model.add(layers.InputLayer(input_shape=(10)))
    model.add(Dense(neurons_layer_1, activation=activation,
                    kernel_initializer=init_mode, kernel_regularizer= L2(weight_decay)))
    model.add(Dense(neurons_layer_2, activation=activation,
                    kernel_initializer=init_mode, kernel_regularizer= L2(weight_decay)))
    model.add(Dense(2))
    optimizer = SGD(
      learning_rate=learn_rate, momentum=momentum, nesterov=nesterov)
    # Compile model
    model.compile(optimizer=optimizer,
              loss=euclidean_distance_loss)
    return model<jupyter_output>time: 14.6 ms (started: 2021-01-23 11:04:34 +00:00)<jupyter_text>Test v5<jupyter_code>model = create_model()
history = model.fit(X_train, y_train,
                    batch_size=128,
                    epochs= 1000,
                    validation_data=(X_test, y_test),
                    verbose=2)
plot_loss(history)
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'], '--')
plt.ylim(2,4)
plt.xlim(100,300)
data = [
    history.history['loss'][-1],
    0,
    history.history['val_loss'][-1],
    0,
]
table_info(data)<jupyter_output>MSE     Accuracy
-----------------------------
Train	|2.2716708|	0.00|
Test	|2.8985262|	0.00|
time: 2.35 ms (started: 2021-01-23 11:05:32 +00:00)<jupyter_text>Predict v5<jupyter_code>model = create_model()

history = model.fit(X_train, y_train,
                    batch_size=128,
                    epochs= 1000,
                    verbose=2)
plt.plot(history.history['loss'])
data = [
    history.history['loss'][-1],
    0,
    0,
    0,
]
table_info(data)
mean_euclidean_error(model.predict(X_test), y_test)<jupyter_output><empty_output><jupyter_text>Test v6<jupyter_code>model = create_model(momentum= 0.8)
history = model.fit(X_train, y_train,
                    batch_size=64,
                    epochs= 1000,
                    validation_data=(X_test, y_test),
                    verbose=2)
plot_loss(history)
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'], '--')
plt.ylim(2,4)
plt.xlim(100,300)
data = [
    history.history['loss'][-1],
    0,
    history.history['val_loss'][-1],
    0,
]
table_info(data)<jupyter_output>MSE     Accuracy
-----------------------------
Train	|2.3284807|	0.00|
Test	|2.8837643|	0.00|
time: 2.74 ms (started: 2021-01-23 11:06:57 +00:00)<jupyter_text>Predict v6<jupyter_code>model = create_model()

history = model.fit(X_train, y_train,
                    batch_size=64,
                    epochs= 1000,
                    verbose=2)
plt.plot(history.history['loss'])
data = [
    history.history['loss'][-1],
    0,
    0,
    0,
]
table_info(data)
mean_euclidean_error(model.predict(X_test), y_test)<jupyter_output><empty_output><jupyter_text>Test v6 - 2°<jupyter_code>model = create_model(momentum= 0.8)
history = model.fit(X_train, y_train,
                    batch_size=128,
                    epochs= 1000,
                    validation_data=(X_test, y_test),
                    verbose=2)
plot_loss(history)
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'], '--')
plt.ylim(2,4)
plt.xlim(100,300)
data = [
    history.history['loss'][-1],
    0,
    history.history['val_loss'][-1],
    0,
]
table_info(data)<jupyter_output>MSE     Accuracy
-----------------------------
Train	|2.5571041|	0.00|
Test	|2.8858905|	0.00|
time: 3.93 ms (started: 2021-01-23 11:08:22 +00:00)<jupyter_text>Predict v6 - 2°<jupyter_code>model = create_model()

history = model.fit(X_train, y_train,
                    batch_size=128,
                    epochs= 1000,
                    verbose=2)
plt.plot(history.history['loss'])
data = [
    history.history['loss'][-1],
    0,
    0,
    0,
]
table_info(data)
mean_euclidean_error(model.predict(X_test), y_test)<jupyter_output><empty_output><jupyter_text>Test v5 - FULL<jupyter_code>model = create_model(momentum= 0.9)
history = model.fit(X_train, y_train,
                    batch_size=len(X_train),
                    epochs= 10000,
                    validation_data=(X_test, y_test),
                    verbose=2)
plot_loss(history)
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'], '--')
plt.ylim(2,4)
plt.xlim(1000,3000)
data = [
    history.history['loss'][-1],
    0,
    history.history['val_loss'][-1],
    0,
]
table_info(data)<jupyter_output>MSE     Accuracy
-----------------------------
Train	|2.1850023|	0.00|
Test	|2.9353304|	0.00|
time: 2.83 ms (started: 2021-01-23 11:27:06 +00:00)<jupyter_text>Predict v5 - FULL<jupyter_code>model = create_model(momentum= 0.9)

history = model.fit(X_train, y_train,
                    batch_size=len(X_train),
                    epochs= 10000,
                    verbose=2)
plt.plot(history.history['loss'])
data = [
    history.history['loss'][-1],
    0,
    0,
    0,
]
table_info(data)
mean_euclidean_error(model.predict(X_test), y_test)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15565581695133743, 'fraction_numerical': 0.0602642603931679, 'mean_word_length': 2.653325485579753, 'pattern_counts': {'":': 0, '<': 32, '<?xml version=': 0, '>': 32, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '433348', 'license': 'MIT', 'n_tokens_mistral': 2523, 'n_tokens_neox': 2226, 'n_words': 338, 'path': 'src/keras/CUP_Keras_NN.ipynb', 'repo_name': 'luca-demartino/machine-learning-project', 'size': '6206'}