> For the complete documentation index, see [llms.txt](https://baozoulin.gitbook.io/tensorflow/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://baozoulin.gitbook.io/tensorflow/chapter1/variable-bian-liang.md).

# Variable 变量

定义语法： **state = tf.Variable()**

```python
import tensorflow as tf

state = tf.Variable(0, name='counter')

# 定义加法步骤 (注: 此步并没有直接计算)
new_value = tf.add(state,1)

# 将 State 更新成 new_value
update = tf.assign(state, new_value)

# 使用 Session
with tf.Session() as sess:
    sess.run(tf.global_variables_initializer()  

    for _ in range(3):
        sess.run(update)
        print(sess.run(state))
```

一定要把 **sess** 的指针指向 **state** 再进行 **print** 才能得到想要的结果


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://baozoulin.gitbook.io/tensorflow/chapter1/variable-bian-liang.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
