下载
中文
注册
数据预处理中存在tf.Variable导致训练异常

数据预处理中存在tf.Variable导致训练异常

2025/03/29

18

暂无评分
我要评分

问题信息

问题来源产品大类产品子类关键字
官方模型训练TensorFlow数据预处理、tf.Variable

问题现象描述

TensorFlow网络执行时,报如下错误:

tensorflow.python.framework.errors_impl.FailedPreconditionError: Error while reading resource variable inference/embed_continuous from Container: localhost.  This could mean that the variable was uninitialized. Not found: Resource localhost/inference/embed_continuous/N10tensorflow3VarE does not exist.

原因分析

此问题是由于数据预处理脚本中存在tf.Variable变量。训练脚本在昇腾平台运行时,tf.Variable变量在Host侧执行,而tf.Variable变量的初始化在Device侧执行,变量执行和变量初始化不在同一设备执行,导致训练异常。

使用了tf.Variable的训练脚本代码示例如下:

batch_size = tf.Variable(
    tf.placeholder(tf.int64, [], 'batch_size'),
    trainable= False, collections=[]
)
train_dataset = train_dataset.batch(batch_size, drop_remainder=True)

解决方案

修改训练脚本,将tf.Variable修改成常量,修改示例如下:

batch_size = 64
train_dataset = train_dataset.batch(batch_size, drop_remainder=True)

本页内容