Python:神经网络 - TypeError: 'History' 对象不可订阅

标签 python tensorflow neural-network keras

我一直在 python 中使用 Keras 和 Tensorflow 练习构建和比较神经网络,但是当我想要绘制模型进行比较时,我收到了一个错误:

TypeError: 'History' object is not subscriptable

这是我的三个模型的代码:

############################## 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()

我在运行任何模型、获取预测概率、绘制 ROC 曲线或绘制 PR 曲线方面都没有问题。但是,当我尝试将这三条曲线绘制在一起时,我的代码区域出现错误:

model1.history['val_loss']

TypeError: 'History' object is not subscriptable

有没有人遇到过这种类型的错误,可以告诉我我做错了什么?

提前谢谢你。

最佳答案

调用 model.fit() 返回一个 History 对象,它有一个成员 history,类型为 dict

所以你可以替换:

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

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

与其他模型类似。

然后你可以使用:

plt.plot(history1.history['val_loss'], 'r', history2.history['val_loss'], 'b', 
history3.history['val_loss'], 'g')

关于Python:神经网络 - TypeError: 'History' 对象不可订阅,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51731207/

相关文章:

python - python : missing whitespace after ',' [closed]中的语法错误

python - 如何导致 Python 3.5 崩溃?

opencv - TypeError (“Tensor is unhashable if Tensor equality is enabled. ” K.learning_phase():0

python - Tensorflow Adam 优化器 vs Keras Adam 优化器

python - TensorFlow 数据不适用于多输入 keras 模型

Matlab SOM 工具箱 U 矩阵可视化

python - MLP分类器: "ValueError: Unknown label type"

python - 如何加快将数据帧导入 pandas 的速度

具有客户端身份验证的 Python 嵌入式 Web 服务器

machine-learning - 输入 channel 数与 Keras 中过滤器的相应维度不匹配