1.TensorFlow 借助于with批量初始化变量
第一种方式:
import tensorflow as tf
x = tf.Variable(3, name="x")
y = tf.Variable(4, name="y")
z = tf.Variable(4, name="z")
w = tf.Variable(4, name="w")
f = x * y * z + 3 - w
//单个初始化,变量增多,还真是个事
with tf.Session() as sess:
x.initializer.run()
y.initializer.run()
z.initializer.run()
w.initializer.run()
print(x)
print(y)
print(z)
print(w)
//f fuction eval 方式和js的eval一样,作为方法函数执行
result = f.eval()
print(result)
第二种:
import tensorflow as tf
x = tf.Variable(3, name="x")
y = tf.Variable(4, name="y")
z = tf.Variable(4, name="z")
w = tf.Variable(4, name="w")
f = x * y * z + 3 - w
//全局设置初始化值
init_all_variable = tf.global_variables_initializer()
with tf.Session() as sess:
init_all_variable.run()
print(x)
print(y)
print(w)
print(z)
result = f.eval()
print(result)
两种的结果都是一样:47
网友评论