Aar*_*and 11 python neural-network keras tensorflow
我一直在尝试使用python中的Keras和Tensorflow构建和比较神经网络,但是当我希望绘制模型进行比较时,我收到了一个错误:
TypeError: 'History' object is not subscriptable
Run Code Online (Sandbox Code Playgroud)
这是我对这三个模型的代码:
############################## 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()
Run Code Online (Sandbox Code Playgroud)
运行任何模型,获取预测概率,绘制ROC曲线或绘制PR曲线都没有问题。但是,当我尝试将三个曲线绘制在一起时,在此代码区域出现错误:
model1.history['val_loss']
TypeError: 'History' object is not subscriptable
Run Code Online (Sandbox Code Playgroud)
是否有人对这种类型的错误有经验,可以导致我做错了什么?
先感谢您。
kri*_*hna 13
调用以model.fit()返回History具有成员的对象,该成员history的类型为dict。
因此,您可以替换:
model2.fit(X, y, validation_split=0.33, epochs=30, callbacks=
[early_stopping_monitor], verbose=False)
Run Code Online (Sandbox Code Playgroud)
与
history2 = model2.fit(X, y, validation_split=0.33, epochs=30, callbacks=
[early_stopping_monitor], verbose=False)
Run Code Online (Sandbox Code Playgroud)
其他型号也一样。
然后您可以使用:
plt.plot(history1.history['val_loss'], 'r', history2.history['val_loss'], 'b',
history3.history['val_loss'], 'g')
Run Code Online (Sandbox Code Playgroud)
接受的答案很棒。但是,如果有人试图访问历史记录而不在拟合期间存储它,请尝试以下操作:
由于val_loss不是History对象上的属性,也不是可以用来索引的键,因此您编写它的方式将不起作用。但是,您可以尝试访问对象history中的属性History,该属性是一个应该包含val_loss作为键的字典。
所以,替换:
plt.plot(model1.history['val_loss'], 'r', model2.history['val_loss'], 'b',
model3.history['val_loss'], 'g')
Run Code Online (Sandbox Code Playgroud)
和
plt.plot(model1.history.history['val_loss'], 'r', model2.history.history['val_loss'], 'b',
model3.history.history['val_loss'], 'g')
Run Code Online (Sandbox Code Playgroud)