1

I am learning about neural networks with Kaggle tutorials. I have made a neural net to predict concrete strength and I want to display the MSE (for starters) metric after fitting the model. I have failed both with print(metrics) and plotting the metrics (displays an empty graph).

df = concrete.copy()

df_train = df.sample(frac=0.7, random_state=0)
df_valid = df.drop(df_train.index)

X_train = df_train.drop('CompressiveStrength', axis=1)
X_valid = df_valid.drop('CompressiveStrength', axis=1)
y_train = df_train['CompressiveStrength']
y_valid = df_valid['CompressiveStrength']
model = keras.Sequential([
    layers.BatchNormalization(),
    layers.Dense(512, activation='relu', input_shape=input_shape),
    layers.BatchNormalization(),
    layers.Dense(512, activation='relu'),
    layers.Dropout(rate=0.3), # apply 30% dropout to the next layer
    layers.Dense(512, activation='relu'),
    layers.BatchNormalization(),
    layers.Dense(512, activation='relu'),
    layers.BatchNormalization(),
    layers.Dense(1),
])
model.compile(
    optimizer='sgd', # SGD is more sensitive to differences of scale
    loss='mse',
    metrics=[tf.keras.metrics.MeanSquaredError()]
)
history = model.fit(
    X_train, y_train,
    validation_data=(X_valid, y_valid),
    batch_size=64,
    epochs=100,
    verbose=0,
    callbacks=[early_stopping],
)
print(history)
pyplot.plot(history.history['mean_squared_error'])
  • hi and welcome to stackoverflow! Can you please post any error you are seeing? (or the output you have vs the output you expect) – Galletti_Lance Jan 05 '22 at 20:23
  • """--------------------------------------------------------------------------- KeyError Traceback (most recent call last) in 33 #print(metrics) 34 #model.evaluate() ---> 35 pyplot.plot(history.history['MeanSquaredError']) KeyError: 'MeanSquaredError' """ – WaipahuMalasada Jan 05 '22 at 21:07
  • Hello, What I'd like (assuming I did am doing my model.fit properly) is to return the MSE – WaipahuMalasada Jan 05 '22 at 21:14
  • Have you looked at this post: https://stackoverflow.com/questions/41908379/keras-plot-training-validation-and-test-set-accuracy – Galletti_Lance Jan 05 '22 at 21:17
  • Hello @Galleti_Lance , I am looking at it now. I tried running 'code' plt.plot(history.history['acc']) plt.plot(history.history['val_acc']) plt.title('model accuracy') plt.ylabel('accuracy') plt.xlabel('epoch') plt.legend(['train', 'val'], loc='upper left') plt.show() 'code' and I got 'code' KeyError: 'acc' 'code' – WaipahuMalasada Jan 05 '22 at 21:31
  • use `loss` instead – Galletti_Lance Jan 05 '22 at 22:23
  • @Galletti_Lance could you elaborate? I don't understand. – WaipahuMalasada Jan 06 '22 at 18:01
  • you're not using accuracy as a metric so the `acc` key won't be there. You're using MSE as a loss function so if you want to see the MSE, plot the `loss` instead. – Galletti_Lance Jan 06 '22 at 19:07

0 Answers0