NameError: name ‘tf‘ is not defined

NameError: name ‘tf’ is not defined

报错如下:

/opt/conda/lib/python3.6/site-packages/tensorflow/python/keras/layers/core.py in <lambda>(x)
    307       It defaults to the `image_data_format` value found in your
    308       Keras config file at `~/.keras/keras.json`.
--> 309       If you never set it, then it will be "channels_last".
    310 
    311   Call arguments:

NameError: name 'tf' is not defined

这个报错比较奇怪,定位到是执行到如下语句的报错:

tf.keras.models.load_model(MODEL_PATH)

之前遇到过几次,我试了下主要有两种解决方式是:

第一种解决方式是重新拉取MODEL_PATH的文件,可能在拉取过程中发生一些文件损坏。

第二种解决方式是,观察报错,提示 name 'tf' is not defined,说明内部没有将tensorflow这个库传入内部当中,用以下语句进行解决:

tf.keras.models.load_model(MODEL_PATH, custom_objects={'tf': tf})

文章出处登录后可见!

已经登录?立即刷新

共计人评分,平均

到目前为止还没有投票!成为第一位评论此文章。

(0)
乘风的头像乘风管理团队
上一篇 2023年11月13日
下一篇 2023年11月13日

相关推荐