Python: Neural Network – TypeError: 'History' object is not subscriptable

Question:

I have been practicing building and comparing neural networks using Keras and Tensorflow in python, but when I look to plot the models for comparisons I am receiving an error:

TypeError: 'History' object is not subscriptable

Here is my code for the three models:

############################## Initiate model 1 ###############################
# Model 1 has no hidden layers
from keras.models import Sequential
model1 = Sequential()

# Get layers
from keras.layers import Dense
# Add first layer
n_cols = len(X.columns)
model1.add(Dense(units=n_cols, activation='relu', input_shape=(n_cols,)))
# Add output layer
model1.add(Dense(units=2, activation='softmax'))

# Compile the model
model1.compile(loss='categorical_crossentropy', optimizer='adam', metrics= 
['accuracy']) 

# Define early_stopping_monitor
from keras.callbacks import EarlyStopping
early_stopping_monitor = EarlyStopping(patience=2)

# Fit model
model1.fit(X, y, validation_split=0.33, epochs=30, callbacks= 
[early_stopping_monitor], verbose=False)


############################## Initiate model 2 ###############################
# Model 2 has 1 hidden layer that has the mean number of nodes of input and output layer
model2 = Sequential()

# Add first layer
model2.add(Dense(units=n_cols, activation='relu', input_shape=(n_cols,)))
# Add hidden layer
import math
model2.add(Dense(units=math.ceil((n_cols+2)/2), activation='relu'))
# Add output layer
model2.add(Dense(units=2, activation='softmax'))

# Compile the model
model2.compile(loss='categorical_crossentropy', optimizer='adam', metrics= 
['accuracy']) 

# Fit model
model2.fit(X, y, validation_split=0.33, epochs=30, callbacks= 
[early_stopping_monitor], verbose=False)

############################## Initiate model 3 ###############################
# Model 3 has 1 hidden layer that is 2/3 the size of the input layer plus the size of the output layer
model3 = Sequential()

# Add first layer
model3.add(Dense(units=n_cols, activation='relu', input_shape=(n_cols,)))
# Add hidden layer
model3.add(Dense(units=math.ceil((n_cols*(2/3))+2), activation='relu'))
# Add output layer
model3.add(Dense(units=2, activation='softmax'))

# Compile the model
model3.compile(loss='categorical_crossentropy', optimizer='adam', metrics= 
['accuracy']) 

# Fit model
model3.fit(X, y, validation_split=0.33, epochs=30, callbacks= 
[early_stopping_monitor], verbose=False)


# Plot the models
plt.plot(model1.history['val_loss'], 'r', model2.history['val_loss'], 'b', 
model3.history['val_loss'], 'g')
plt.xlabel('Epochs')
plt.ylabel('Validation score')
plt.show()

I have no problems with running any of my models, getting predicted probabilities, plotting ROC curves, or plotting PR curves. However, when I attempt to plot the three curves together I am getting an error from this area of my code:

model1.history['val_loss']

TypeError: 'History' object is not subscriptable

Does anyone have experience with this type of error and can lead me to what I am doing wrong?

Thank you in advance.

Asked By: Aaron England

||

Answers:

Call to model.fit() returns a History object that has a member history, which is of type dict.

So you can replace :

model2.fit(X, y, validation_split=0.33, epochs=30, callbacks= 
[early_stopping_monitor], verbose=False)

with

history2 = model2.fit(X, y, validation_split=0.33, epochs=30, callbacks= 
[early_stopping_monitor], verbose=False)

Similarly for other models.

and then you can use :

plt.plot(history1.history['val_loss'], 'r', history2.history['val_loss'], 'b', 
history3.history['val_loss'], 'g')
Answered By: Krishna
history =  model.fit(trainX, trainy, batch_size=50, epochs=200, validation_split=0.3,callbacks=[tensorboard]).history

This is another solution have to include .history at the end of the model fit

Answered By: Suleiman

The accepted answer is great. However, in case anyone is trying to access history without storing it during fit, try the following:

Since val_loss is not an attribute on the History object and not a key that you can index with, the way you wrote it won’t work. However, what you can try is to access the attribute history in the History object, which is a dict that should contain val_loss as a key.

so, replace:

plt.plot(model1.history['val_loss'], 'r', model2.history['val_loss'], 'b', 
model3.history['val_loss'], 'g')

with

plt.plot(model1.history.history['val_loss'], 'r', model2.history.history['val_loss'], 'b', 
model3.history.history['val_loss'], 'g')

I used some of the answers and the next code works for me:`

#Training the ANN
history=classifier.fit(X_train, y_train, batch_size=10, epochs=1000).history
print("Trained finished")

#the graphic
import matplotlib.pyplot as plt
plt.xlabel("#Epoch")
plt.ylabel("Maagnitud de perdida")
plt.plot(history["loss"])

`

wish it help you

Answered By: Samuel López