部署NLP模型遇FileNotFoundError,pickle与.h5保存均报错求助
NLP模型部署FileNotFoundError问题解决
报错信息
FileNotFoundError: Unsuccessful TensorSliceReader constructor: Failed to find any matching files for ram://a603e930-4fda-4105-8554-7af5e5fc02f5/variables/variables
You may be trying to load on a different device from the computational device. Consider setting theexperimental_io_deviceoption intf.saved_model.LoadOptionsto the io_device such as '/job:localhost'
问题背景
先后使用pickle、.h5格式保存NLP模型,加载时均触发上述错误。
解决方法
- 加载模型时指定
experimental_io_device参数,强制使用本地设备读取文件:import tensorflow as tf load_options = tf.saved_model.LoadOptions(experimental_io_device='/job:localhost') model = tf.keras.models.load_model('你的模型文件路径.h5', options=load_options) - 重新完整保存模型:确保模型保存过程未中断,执行以下代码重新生成.h5文件:
model.save('完整的模型保存路径.h5', save_format='h5') - 明确指定加载设备:若训练与加载设备不一致(如训练用GPU,加载用CPU),添加设备上下文:
import tensorflow as tf load_options = tf.saved_model.LoadOptions(experimental_io_device='/job:localhost') with tf.device('/CPU:0'): model = tf.keras.models.load_model('你的模型文件路径.h5', options=load_options)
内容的提问来源于stack exchange,提问作者Karthik Bhandary
相关产品推荐
相关产品推荐

