tf.Variable

__init__(
    initial_value=None,
    trainable=True,
    collections=None,
    validate_shape=True,
    caching_device=None,
    name=None,
    variable_def=None,
    dtype=None,
    expected_shape=None,
    import_scope=None
)
参数名类型说明
initial_value张量Variable 类的初始值,这个变量必须指定 shape 信息,否则后面 validate_shape 需设为 False
trainableBoolean是否把变量添加到 collection GraphKeys.TRAINABLE_VARIABLES 中(collection 是一种全局存储,不受变量名生存空间影响,一处保存,到处可取)
collectionsGraph collections全局存储,默认是 GraphKeys.GLOBAL_VARIABLES
validate_shapeBoolean是否允许被未知维度的 initial_value 初始化
caching_devicestring指明哪个 device 用来缓存变量
namestring变量名
dtypedtype如果被设置,初始化的值就会按照这个类型初始化
expected_shapeTensorShape要是设置了,那么初始的值会是这种维度

 示例:

import tensorflow as tf

initial = tf.truncated_normal(shape=[10,10],mean=0,stddev=1)
W = tf.Variable(initial)
list = [[1.,1.],[2.,2.]]
X = tf.Variable(list,dtype=tf.float32)
init_op = tf.global_variables_initializer()
with tf.Session() as sess:
    sess.run(init_op)
    print('####################(1)#################')
    print(sess.run(W))
    print('####################(2)#################')
    print(sess.run(W[:2,:2]))
    op = W[:2,:2].assign(22.*tf.ones((2,2)))
    print('####################(3)#################')
    print(sess.run(op))
    print('####################(4)#################')
    print(W.eval(sess))  #computes and returns the value of this variable
    print('####################(5)#################')
    print(W.eval())  #Usage with the default session
    print('####################(6)#################')
    print(W.dtype)
    print(sess.run(W.initial_value))
    print(sess.run(W.op))
    print(W.shape)
    print('####################(7)#################')
    print(sess.run(X))

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值