Tensorflow--变量

B站:

http://www.bilibili.com/video/av20542427?share_medium=android&share_source=copy_link&bbid=8099B994-2CEF-4DB4-A914-9DC322B1E3A531040infoc&ts=1532330002470

import tensorflow as tf

#增加一个变量
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(sess.run(sub))
    print(sess.run(add))
    
"""
result:
[-2 -1]
[-1  1]
"""




#变量自增
#创建一个变量初始化为0
state = tf.Variable(0,name='counter')
#创建一个op,作用是使state加1
new_value = tf.add(state,1)
#赋值op,tensorflow不能直接赋值,使用assign将new_value赋值state
update = tf.assign(state,new_value)

init = tf.global_variables_initializer()

with tf.Session() as sess:
    sess.run(init)
    print(sess.run(state))
    for _ in range(5):
        sess.run(update)
        print(sess.run(state))

"""
result:
0
1
2
3
4
5
"""

猜你喜欢

转载自blog.csdn.net/qq_38173003/article/details/81168120