机器学习

深度学习-TensorFlow 如何赋值方式

2018-07-21  本文已影响306人  老生住长亭

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

上一篇下一篇

猜你喜欢

热点阅读