python - 错误“AttributeError: 'NoneType' object has no attribute 'endswith'”是什么意思?
问题描述
def get_checkpoint_every_epoch():
checkpoint_every_epoch = 'model_checkpoints_every_epoch'
checkpoints = ModelCheckpoint(filepath=checkpoint_every_epoch,
frequency= 'epoch',
save_weights_only=True,
verbose=1)
return checkpoints
def get_checkpoint_best_only():
checkpoint_best_path = 'model_checkpoints_best_only/checkpoint'
checkpoint_best= ModelCheckpoint(filepath=checkpoint_best_path,
save_weights_only= True,
monitor= 'val_accuracy',
save_best_only= True,
verbose=1)
return checkpoint_best
def get_early_stopping():
early_stopping= tf.keras.callbacks.EarlyStopping(monitor='val_accuracy',
patience=3)
return early_stopping
checkpoint_every_epoch = get_checkpoint_every_epoch()
checkpoint_best_only = get_checkpoint_best_only()
early_stopping = get_early_stopping()
def get_model_last_epoch(model):
model_last_epoch_file = tf.train.latest_checkpoint("checkpoints_every_epoch")
model.load_weights(model_last_epoch_file)
return model
def get_model_best_epoch(model):
model_best_epoch_file = tf.train.latest_checkpoint("checkpoints_best_only")
model.load_weights(model_best_epoch_file)
return model
model_last_epoch = get_model_last_epoch(get_new_model(x_train[0].shape))
model_best_epoch = get_model_best_epoch(get_new_model(x_train[0].shape))
print('Model with last epoch weights:')
get_test_accuracy(model_last_epoch, x_test, y_test)
print('')
print('Model with best epoch weights:')
get_test_accuracy(model_best_epoch, x_test, y_test)
这是我的代码。但它返回了一个错误,上面写着:
AttributeError Traceback (most recent call last)
<ipython-input-18-b6d169507ca4> in <module>
3 # Verify that the second has a higher validation (testing) accuarcy.
4
----> 5 model_last_epoch = get_model_last_epoch(get_new_model(x_train[0].shape))
6 model_best_epoch = get_model_best_epoch(get_new_model(x_train[0].shape))
7 print('Model with last epoch weights:')
<ipython-input-17-4c8cba016afe> in get_model_last_epoch(model)
12 model_last_epoch_file = tf.train.latest_checkpoint("checkpoints_every_epoch")
13
---> 14 model.load_weights(model_last_epoch_file)
15
16 return model
/opt/conda/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/training.py in
load_weights(self, filepath, by_name)
179 raise ValueError('Load weights is not yet supported with TPUStrategy '
180 'with steps_per_run greater than 1.')
--> 181 return super(Model, self).load_weights(filepath, by_name)
182
183 @trackable.no_automatic_dependency_tracking
/opt/conda/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/network.py in
load_weights(self, filepath, by_name)
1137 format.
1138 """
-> 1139 if _is_hdf5_filepath(filepath):
1140 save_format = 'h5'
1141 else:
/opt/conda/lib/python3.7/site-packages/tensorflow_core/python/keras/engine/network.py in
_is_hdf5_filepath(filepath)
1447
1448 def _is_hdf5_filepath(filepath):
-> 1449 return (filepath.endswith('.h5') or filepath.endswith('.keras') or
1450 filepath.endswith('.hdf5'))
1451
AttributeError: 'NoneType' object has no attribute 'endswith'
我不明白错误消息框中的代码哪里出错了。这是什么意思
AttributeError:“NoneType”对象没有属性“endswith”
系统要指出哪个错误?
解决方案
推荐阅读
- django - 如何自定义 Django-Jazzmin 登录页面
- php - 我收到类似 mail.myserver.in 的 TLS/SSL 失败的通知:意外的 TCP 输入断开连接
- django - Django Ajax:响应呈现在另一个页面不同的页面
- android - 在 exoplayer 中播放动态 url
- python - AttributeError:模块“torchtext.datasets”没有属性“text_classification”
- java - 如何知道用户没有给我们的应用程序提供互联网访问权限?
- php - 将两个小时添加到从 MySQL 数据库中获取的数据
- node.js - 如何在 Nativescript 8 和 Angular 中生成组件
- python - django / ajax 表单提交收到 405 错误但有效吗?
- php - 将静态数据从一个 php 发送到另一个