NoneType对象没有以(Tensorflow)结尾的属性

问题描述 投票:0回答:1

我正在尝试创建一个RNN,以从莎士比亚的文学作品中生成文本,如本张量流课程所教导:https://www.tensorflow.org/tutorials/text/text_generation

[当我尝试加载权重时,我的程序将崩溃并显示错误消息:AttributeError:'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'
python tensorflow keras recurrent-neural-network nonetype
1个回答
0
投票

我在其他教程中也遇到了同样的问题。据我所知,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))
© www.soinside.com 2019 - 2024. All rights reserved.