python人工智能tensorflow函数tf.assign使用方法

 更新时间:2022年05月05日 14:44:31   作者:Bubbliiiing  
这篇文章主要为大家介绍了python人工智能tensorflow函数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)]

以上就是python人工智能tensorflow函数tf.assign使用方法的详细内容,更多关于tensorflow函数tf.assign的资料请关注脚本之家其它相关文章!

相关文章

  • Python写的一个定时重跑获取数据库数据

    Python写的一个定时重跑获取数据库数据

    本文给大家分享基于python写的一个定时重跑获取数据库数据的方法,非常不错,具有参考借鉴价值,需要的朋友参考下
    2016-12-12
  • python3访问字典里的值实例方法

    python3访问字典里的值实例方法

    在本篇内容里小编给大家整理的是一篇关于python3访问字典里的值实例方法,有兴趣的朋友们可以学习参考下。
    2020-11-11
  • Python类属性的延迟计算

    Python类属性的延迟计算

    这篇文章主要为大家详细介绍了Python类属性的延迟计算,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-10-10
  • echarts动态获取Django数据的实现示例

    echarts动态获取Django数据的实现示例

    本文主要介绍了echarts动态获取Django数据的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-08-08
  • 使用go和python递归删除.ds store文件的方法

    使用go和python递归删除.ds store文件的方法

    使用python和go递归删除.DS_Store文件,.DS_Store (英文全称 Desktop Services Store)是一种由苹果公司的Mac OS X操作系统所创造的隐藏文件,目的在于存贮文件夹的自定义属性
    2014-01-01
  • 使用Django框架中ORM系统实现对数据库数据增删改查

    使用Django框架中ORM系统实现对数据库数据增删改查

    这篇文章主要介绍了使用Django的ORM实现对数据库数据增删改查方法,文中附含详细示例代码以及过程详解,有需要的朋友可以借鉴参考下
    2021-09-09
  • Python中实现一行拆多行和多行并一行的示例代码

    Python中实现一行拆多行和多行并一行的示例代码

    这篇文章主要介绍了Python中实现一行拆多行和多行并一行的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-09-09
  • python实现简单聊天功能

    python实现简单聊天功能

    这篇文章主要为大家详细介绍了python实现简单聊天功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-07-07
  • python爬虫将js转化成json实现示例

    python爬虫将js转化成json实现示例

    这篇文章主要为大家介绍了python爬虫将js转化成json实现示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-05-05
  • python自动更新pom文件的方法

    python自动更新pom文件的方法

    这篇文章主要介绍了python自动更新pom文件的方法,本文通过图文实例代码相结合给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-09-09

最新评论