美文网首页机器学习
深度学习-tensorflow-变量和常量

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

作者: 老生住长亭 | 来源:发表于2018-07-23 22:49 被阅读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)

相关文章

网友评论

本文标题:深度学习-tensorflow-变量和常量

本文链接:https://www.haomeiwen.com/subject/mnesmftx.html