---
title: 数据预处理中存在tf.Variable导致训练异常-昇腾社区
description: TensorFlow网络执行时，报如下错误：
keywords: 数据预处理;tf.Variable
url: https://www.hiascend.com/document/caselibrary/detail/tensorflow_faq_0005
section: (其他)
---

# 数据预处理中存在tf.Variable导致训练异常-昇腾社区

URL: https://www.hiascend.com/document/caselibrary/detail/tensorflow_faq_0005
描述: TensorFlow网络执行时，报如下错误：
关键词: 数据预处理;tf.Variable

昇腾文档 [了解详情](https://www.hiascend.com/document)

故障案例 [了解详情](https://www.hiascend.com/document/caselibrary)

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

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

2025/03/29

481

问题信息

| 问题来源 | 产品大类 | 产品子类 | 关键字 |
| --- | --- | --- | ---|
| 官方 | 模型训练 | 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)
```

本页内容

- 问题信息
- 问题现象描述
- 原因分析
- 解决方案
