首页 > 解决方案 > 错误“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”

系统要指出哪个错误?

标签: pythontensorflowtensorflow2.0

解决方案


推荐阅读