NoneType object has no attribute endwith (Tensorflow)

Viewed 2050

I was attempting to create a RNN that would generate text from shakespearean literature, as taught by this tensorflow course: https://www.tensorflow.org/tutorials/text/text_generation

When I attempted to load the weights, my program would crash with the error message: AttributeError: 'NoneType' object has no attribute 'endswith'

Here is the line of code that crashes the program:

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

Here is the pastebin of my code: https://pastebin.com/KqmD0phL

Here is the full error message:

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'
2 Answers

I just ran into the same problem. Reason for this error message at my case was invalid path for the directory containing model training checkpoints. So I propose to check if this line

check_dir = './training_checkpoints'

On your code is right. You could at least try to change it to full path of the directory containing checkpoint data.

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

I had the same exact issue from a different tutorial. From what I can tell there appears to be disparity between Tensorflow specific calls and Tensorflow.Keras calls.

I found mention in another post about saving with Keras API and loading with Keras API which makes sense to me.

I hope this helps.

I used:

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()
]

then loaded the model explicitly with:

#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))
Related