Tensorflow Variable

tensorflow变量定义和赋值没有python那么简单,需要在session中run才能拿到结果

import tensorflow as tf

w = tf.Variable([[1.0,2.0]])
print(w)#<tf.Variable 'Variable:0' shape=(1, 2) dtype=float32_ref>
x = tf.Variable([[1.0],[0.5]])
print(x)#<tf.Variable 'Variable_1:0' shape=(2, 1) dtype=float32_ref>

y = tf.matmul(w,x)
print(y)#Tensor("MatMul:0", shape=(1, 1), dtype=float32)

#全局变量初始化
init_op = tf.global_variables_initializer()
with tf.Session() as sess:
    sess.run(init_op)
    print(y.eval())#[[2.]]
    res = sess.run(y)
    print(res)#[[2.]]

猜你喜欢

转载自blog.51cto.com/5669384/2415624