tensorflow练习02-矩阵与变量

import tensorflow as tf
# matrix1 = tf.constant([[3, 3]])
# matrix2 = tf.constant([[2], [2]])
# product = tf.matmul(matrix1, matrix2)  #操作:矩阵相乘
#
# #method 1
# with tf.Session() as sess:
#     result=sess.run(product)
#     print(result)

####定义变量#######
state = tf.Variable(0,name='counter')
one = tf.constant(1)
new_value = tf.add(state, one)
update = tf.assign(state, new_value)   #操作:将new_value赋值更新到state
init = tf.global_variables_initializer()
with tf.Session() as sess:
    sess.run(init)
    for _ in range(3):
        sess.run(update)
        print(sess.run(state))

你可能感兴趣的:(tensorflow练习02-矩阵与变量)