Python:神经网络-类型错误:';历史';对象不可下标
我一直在练习使用python中的Keras和Tensorflow构建和比较神经网络,但当我试图绘制用于比较的模型时,我收到了一个错误:Python:神经网络-类型错误:';历史';对象不可下标,python,tensorflow,neural-network,keras,Python,Tensorflow,Neural Network,Keras,我一直在练习使用python中的Keras和Tensorflow构建和比较神经网络,但当我试图绘制用于比较的模型时,我收到了一个错误: TypeError: 'History' object is not subscriptable 以下是我对这三种型号的代码: ############################## Initiate model 1 ############################### # Model 1 has no hidden layers from k
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()
返回一个历史记录
对象,该对象具有成员历史记录
,类型为dict
因此,您可以替换:
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')
这是另一个解决方案,必须包括
。历史记录
在模型拟合的末尾
公认的答案非常好。但是,如果有人试图在fit期间访问历史而不存储它,请尝试以下操作:
由于val_loss
不是History
对象上的属性,也不是可用于索引的键,因此您编写它的方式将不起作用。但是,您可以尝试访问history
对象中的属性history
,这是一个应该包含val\u loss
作为键的dict
因此,替换:
plt.plot(模型1.历史['val\u loss'],'r',模型2.历史['val\u loss'],'b',
模型3.历史记录['val_loss'],'g')
与
plt.plot(模型1.history.history['val\u loss'],'r',模型2.history.history['val\u loss'],'b',
模型3.history.history['val_loss'],'g')
试试model1.history.history['val_loss']
和其他类似的方法。@krishna,效果很好。谢谢你工作得很有魅力。谢谢你的洞察力!
plt.plot(history1.history['val_loss'], 'r', history2.history['val_loss'], 'b',
history3.history['val_loss'], 'g')
history = model.fit(trainX, trainy, batch_size=50, epochs=200, validation_split=0.3,callbacks=[tensorboard]).history