机器学习

深度学习-tensorflow-变量和常量

2018-07-23  本文已影响64人  老生住长亭

1.tensorflow 申明变量方法:tensorflow.Variable(3)

2.tensorflow 申明常量方法:tensorflow.constant(3)

在初始化时变量会放入cache,常量不会

import tensorflow as tf

# w not in cache , but tf.Variable() in cache
w = tf.constant(10)

x = w + 2

y = x + 5

z = y * 4

j = y + 10

# create a tree : w -> x -> y -> z; y-> j
with tf.Session() as sess:
    print(y.eval())
    # Variable is cycle is session close , variable life cycle is end ,but constant used is end
    # z值依赖y的值, y值依赖x ,z依赖w,所有计算z的值从新计算x,y,w的值
    print(z.eval())


with tf.Session() as sess:
    # create array, element is y,z j, but y value will be in cache.
    #  while compute j and z ,y value in cache
    #  将y值放入数组中,计算z和j的值,直接从数组取
    k_val, v_val, j_val = sess.run([y, z, j])
    print(k_val)
    print(v_val)
    print(j_val)

上一篇下一篇

猜你喜欢

热点阅读