tf.assign的使用方法

tf.assign的使用方法

参数数量及其作用

该函数共有五个参数,分别是:被赋值的变量ref、要分配给变量的值value、是否验证形状validate_shape、是否进行锁定保护use_locking、名称name。

def assign(ref, value, validate_shape=None, use_locking=None, name=None)
Update 'ref' by assigning 'value' to it.

This operation outputs a Tensor that holds the new value of 'ref' after 
the value has been assigned. This makes it easier to chain operations  
that need to use the reset value.  
Args:

  ref: A mutable `Tensor`.  
	Should be from a `Variable` node. May be uninitialized.  
  value: A `Tensor`. Must have the same type as `ref`.  
    The value to be assigned to the variable.  
  validate_shape: An optional `bool`. Defaults to `True`.  
    If true, the operation will validate that the shape  
    of 'value' matches the shape of the Tensor being assigned to.  If false,  
    'ref' will take on the shape of 'value'.  
  use_locking: An optional `bool`. Defaults to `True`.  
    If True, the assignment will be protected by a lock;  
    otherwise the behavior is undefined, but may exhibit less contention.  
  name: A name for the operation (optional).  
Returns:
  A `Tensor` that will hold the new value of 'ref' after  
  the assignment has completed.    

该函数的作用是将一个要分配给变量的值value赋予被赋值的变量ref,用于tensorflow各个参数的变量赋值。

例子

该例子将举例如何进行变量之间的数据赋值和如何进行集合间的数据赋值。

import tensorflow as tf;  
import numpy as np;  

c1 = ['c1', tf.GraphKeys.GLOBAL_VARIABLES]
c2 = ['c2', tf.GraphKeys.GLOBAL_VARIABLES]
#常量初始化器
v1_cons = tf.get_variable('v1_cons',dtype = tf.float32,shape=[1,4], initializer=tf.constant_initializer(), collections = c1)
v2_cons = tf.get_variable('v2_cons',dtype = tf.float32,shape=[1,4], initializer=tf.constant_initializer(9), collections = c1)

#正太分布初始化器
v1_nor = tf.get_variable('v1_nor',dtype = tf.float32, shape=[1,4], initializer=tf.random_normal_initializer(mean=0, stddev=5), collections = c2)
v2_nor = tf.get_variable('v2_nor',dtype = tf.float32, shape=[1,4], initializer=tf.random_normal_initializer(mean=0, stddev=5), collections = c2)

assign1 = tf.assign(v1_cons,v2_cons)    #将v2_cons赋予v1_cons
c1_get = tf.get_collection('c1')        #获得c1集合
c2_get = tf.get_collection('c2')        #获得c2集合
assign2 = [tf.assign(cg1,cg2) for cg1,cg2 in zip(c1_get,c2_get) ]   #将c2赋予c1

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    print("v1_cons:",sess.run(v1_cons))
    print("v2_cons:",sess.run(v2_cons))
    print(sess.run(assign1))            #显示赋值后的结果
    print("将v2_cons赋予v1_cons:",sess.run(v1_cons))

    print("c1_get_collection:",sess.run(c1_get))
    print("c2_get_collection:",sess.run(c2_get))
    print(sess.run(assign2))            #显示赋值后的结果
    print("将c2赋予c1:",sess.run(c1_get))

其输出为:

v1_cons: [[0. 0. 0. 0.]]
v2_cons: [[9. 9. 9. 9.]]
[[9. 9. 9. 9.]]
将v2_cons赋予v1_cons: [[9. 9. 9. 9.]]
c1_get_collection: [array([[9., 9., 9., 9.]], dtype=float32), array([[9., 9., 9., 9.]], dtype=float32)]
c2_get_collection: [array([[-3.9746916, -7.5332146,  2.4480317, -1.3282107]], dtype=float32), array([[10.687443 ,  3.6653206,  1.7079141, -4.524155 ]], dtype=float32)]
[array([[-3.9746916, -7.5332146,  2.4480317, -1.3282107]], dtype=float32), array([[10.687443 ,  3.6653206,  1.7079141, -4.524155 ]], dtype=float32)]
将c2赋予c1: [array([[-3.9746916, -7.5332146,  2.4480317, -1.3282107]], dtype=float32), array([[10.687443 ,  3.6653206,  1.7079141, -4.524155 ]], dtype=float32)]
  • 5
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Bubbliiiing

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值