keras NoneType对象没有属性endwith(Tensorflow)

g52tjvyc  于 2022-12-13  发布在  其他
关注(0)|答案(3)|浏览(160)

我试图创建一个RNN,它可以从莎士比亚的文学作品中生成文本,正如tensorflow 课程所教授的那样:https://www.tensorflow.org/tutorials/text/text_generation
当我试图加载砝码时,程序会崩溃并显示错误消息:属性错误:'NoneType'对象没有'endswith'属性
以下是导致程序崩溃的代码行:

model.load_weights(tf.train.latest_checkpoint(check_dir))

下面是我代码的粘贴框:https://pastebin.com/KqmD0phL
以下是完整的错误消息:

Traceback (most recent call last):
  File "D:/Python/PycharmProjects/untitled/Shakespeare.py", line 118, in <module>
    main()
  File "D:/Python/PycharmProjects/untitled/Shakespeare.py", line 108, in main
    model.load_weights(tf.train.latest_checkpoint(check_dir))
  File "C:\Users\marco\venv\lib\site-packages\tensorflow_core\python\keras\engine\training.py", line 182, in load_weights
    return super(Model, self).load_weights(filepath, by_name)
  File "C:\Users\marco\venv\lib\site-packages\tensorflow_core\python\keras\engine\network.py", line 1335, in load_weights
    if _is_hdf5_filepath(filepath):
  File "C:\Users\marco\venv\lib\site-packages\tensorflow_core\python\keras\engine\network.py", line 1645, in _is_hdf5_filepath
    return (filepath.endswith('.h5') or filepath.endswith('.keras') or
AttributeError: 'NoneType' object has no attribute 'endswith'
wdebmtf2

wdebmtf21#

我刚刚遇到了同样的问题。在我的情况下,这个错误消息的原因是包含模型训练检查点的目录的路径无效。所以我建议检查此行是否

check_dir = './training_checkpoints'

您的代码是正确的。您至少可以尝试将其更改为包含检查点数据的目录的完整路径。

check_dir = '/full/path/to/training_checkpoints'
lmvvr0a8

lmvvr0a82#

我在不同的教程中遇到了同样的问题。从我所能告诉的来看,Tensorflow特定调用和Tensorflow.Keras调用之间似乎存在差异。
我发现在另一个帖子中提到了用Keras API保存和用Keras API加载,这对我来说很有意义。
我希望这能帮上忙。
我用了:

callbacks = [
    tf.keras.callbacks.TensorBoard(log_dir='.'+os.sep+'logs',
                                   histogram_freq=0,  
                                   embeddings_freq=0, 
                                   update_freq='epoch',
                                   profile_batch=0),  
                                   #added this (which doesn't profile) to get the example to to work

    #When saving a model's weights, tf.keras defaults to the checkpoint format. 
    #Pass save_format='h5' to use HDF5 (or pass a filename that ends in .h5).
    tf.keras.callbacks.ModelCheckpoint(filepath=checkpoint_prefix,
                                       #save_weights_only=True,
                                       verbose=1),
    PrintLR()
]

然后显式加载模型:

#tutorials indicates to save weights only but I found this to be a problem / concern between 
#tensorflow and keras calls, so save the whole model (who cares anyway)
#model.load_weights(tf.train.latest_checkpoint(checkpoint_dir))

#load the specific model name
model=tf.keras.models.load_model(checkpoint_dir+os.sep+'ckpt_12.h5')

eval_loss, eval_acc = model.evaluate(eval_dataset)

print('Eval loss: {}, Eval Accuracy: {}'.format(eval_loss, eval_acc))
jjjwad0x

jjjwad0x3#

我也面临着同样的问题。你可以在加载砝码时尝试这个:

latest = tf.train.latest_checkpoint(checkpoint_dir)

latest

model.load_weights(latest)

这将使用最新检查点的权重来训练模型。

相关问题