TF0002、变量Variable

import tensorflow as tf
import warnings
warnings.filterwarnings('ignore')
# 定义一个变量
a = tf.Variable([1,2])
print(a)

输出:

<tf.Variable 'Variable:0' shape=(2,) dtype=int32_ref>
# 定义一个变量
x = tf.Variable([1,2])
# 定义一个常量
a = tf.constant([3,3])
# 减法op
sub = tf.subtract(x, a)
# 加法op
add = tf.add(x,sub)

# 所有变量初始化
init = tf.global_variables_initializer()

with tf.Session() as sess:
    # 执行变量初始化,变量要初始化才能使用
    sess.run(init)
    print(x)
    print('sub:',sess.run(sub))
    print("add:",sess.run(add))

输出:

<tf.Variable 'Variable_7:0' shape=(2,) dtype=int32_ref>
sub: [-2 -1]
add: [-1  1]
发布了23 篇原创文章 · 获赞 1 · 访问量 3357

猜你喜欢

转载自blog.csdn.net/ABCDABCD321123/article/details/104290727