【发布时间】:2020-11-18 09:27:29
【问题描述】:
我想加载一个模型并将其显示在绘图中。不幸的是,我收到了一个错误。
为什么此时会出现这个错误(通常他应该知道?),我该如何解决?
我已经调查过这个错误并寻找类似Python Math - TypeError: 'NoneType' object is not subscriptable的问题
为什么history.history['loss'] 会出现此错误?通常他应该知道!
错误:
training_loss = history.history['loss']
TypeError: 'NoneType' object is not subscriptable
代码:
def load_model():
history = tf.keras.models.load_model(path)
return model
def get_loss(history):
# Get training and test loss histories
training_loss = history.history['loss'] # here is the error
test_loss = history.history['val_loss']
# Create count of the number of epochs
epoch_count = range(1, len(training_loss) + 1)
# Visualize loss history
plt.plot(epoch_count, training_loss, 'r--')
plt.plot(epoch_count, test_loss, 'b-')
plt.legend(['Training Loss', 'Test Loss'])
plt.xlabel('Epoch')
plt.ylabel('Loss')
plt.show();
get_loss(load_model)
编辑:
history = model.fit(...)
model.save(model_file, overwrite=True)
【问题讨论】:
-
"为什么会这样,我该如何解决?"如果您问这个问题是关于一个如此普通的错误,并且自己无法进行任何调查,那么您需要退后一步并彻底研究语言基础知识,然后再尝试弄清楚机器学习。这就像在你可以爬行之前飞行。
-
我当然做了一些研究。我可以排除它是以前的。一定和这里的代码有关……
-
一般来说,该错误意味着您试图索引一个没有该功能的对象。
'NoneType' object is not subscriptable是当您使用方括号表示法 object[key] 对象未定义方法时由 python 抛出的。 -
好的,你认为它告诉你它具体是一个
'NoneType' object有这个问题? -
@Marko 没用。
标签: python tensorflow keras