In [3]:
import pandas as pd
import keras
from keras.models import Sequential
from keras.layers import *

RUN_NAME = "run 2 with 5 nodes"

training_data_df = pd.read_csv("Exercise Files/06/sales_data_training_scaled.csv")

X = training_data_df.drop('total_earnings', axis=1).values
Y = training_data_df[['total_earnings']].values

# Define the model
model = Sequential()
model.add(Dense(5, input_dim=9, activation='relu', name='layer_1'))
model.add(Dense(100, activation='relu', name='layer_2'))
model.add(Dense(50, activation='relu', name='layer_3'))
model.add(Dense(1, activation='linear', name='output_layer'))
model.compile(loss='mean_squared_error', optimizer='adam')

# Create a TensorBoard logger
logger = keras.callbacks.TensorBoard(
    log_dir='Exercise Files/06/logs/{}'.format(RUN_NAME),
#     histogram_freq=5,
    write_graph=True
)

# Train the model
model.fit(
    X,
    Y,
    epochs=50,
    shuffle=True,
    verbose=2,
    callbacks=[logger]
)

# Load the separate test data set
test_data_df = pd.read_csv("Exercise Files/06/sales_data_test_scaled.csv")

X_test = test_data_df.drop('total_earnings', axis=1).values
Y_test = test_data_df[['total_earnings']].values

test_error_rate = model.evaluate(X_test, Y_test, verbose=0)
print("The mean squared error (MSE) for the test data set is: {}".format(test_error_rate))
Epoch 1/50
 - 0s - loss: 0.0588
Epoch 2/50
 - 0s - loss: 0.0277
Epoch 3/50
 - 0s - loss: 0.0247
Epoch 4/50
 - 0s - loss: 0.0234
Epoch 5/50
 - 0s - loss: 0.0227
Epoch 6/50
 - 0s - loss: 0.0211
Epoch 7/50
 - 0s - loss: 0.0191
Epoch 8/50
 - 0s - loss: 0.0168
Epoch 9/50
 - 0s - loss: 0.0139
Epoch 10/50
 - 0s - loss: 0.0113
Epoch 11/50
 - 0s - loss: 0.0088
Epoch 12/50
 - 0s - loss: 0.0065
Epoch 13/50
 - 0s - loss: 0.0048
Epoch 14/50
 - 0s - loss: 0.0032
Epoch 15/50
 - 0s - loss: 0.0024
Epoch 16/50
 - 0s - loss: 0.0017
Epoch 17/50
 - 0s - loss: 0.0014
Epoch 18/50
 - 0s - loss: 0.0012
Epoch 19/50
 - 0s - loss: 0.0010
Epoch 20/50
 - 0s - loss: 8.8411e-04
Epoch 21/50
 - 0s - loss: 8.2826e-04
Epoch 22/50
 - 0s - loss: 7.6278e-04
Epoch 23/50
 - 0s - loss: 6.5110e-04
Epoch 24/50
 - 0s - loss: 6.3048e-04
Epoch 25/50
 - 0s - loss: 5.7419e-04
Epoch 26/50
 - 0s - loss: 5.7883e-04
Epoch 27/50
 - 0s - loss: 5.1502e-04
Epoch 28/50
 - 0s - loss: 4.3292e-04
Epoch 29/50
 - 0s - loss: 4.5275e-04
Epoch 30/50
 - 0s - loss: 3.9630e-04
Epoch 31/50
 - 0s - loss: 3.7888e-04
Epoch 32/50
 - 0s - loss: 3.9089e-04
Epoch 33/50
 - 0s - loss: 3.2177e-04
Epoch 34/50
 - 0s - loss: 3.0820e-04
Epoch 35/50
 - 0s - loss: 2.7886e-04
Epoch 36/50
 - 0s - loss: 2.6574e-04
Epoch 37/50
 - 0s - loss: 2.6187e-04
Epoch 38/50
 - 0s - loss: 2.3749e-04
Epoch 39/50
 - 0s - loss: 2.4857e-04
Epoch 40/50
 - 0s - loss: 2.4797e-04
Epoch 41/50
 - 0s - loss: 2.0134e-04
Epoch 42/50
 - 0s - loss: 2.0205e-04
Epoch 43/50
 - 0s - loss: 2.0219e-04
Epoch 44/50
 - 0s - loss: 1.9717e-04
Epoch 45/50
 - 0s - loss: 1.7174e-04
Epoch 46/50
 - 0s - loss: 1.7831e-04
Epoch 47/50
 - 0s - loss: 1.9075e-04
Epoch 48/50
 - 0s - loss: 1.6237e-04
Epoch 49/50
 - 0s - loss: 1.7307e-04
Epoch 50/50
 - 0s - loss: 1.6758e-04
The mean squared error (MSE) for the test data set is: 0.00019516230269800872