python人工智能tensorflow函数tf.assign使用方法
作者:Bubbliiiing 发布时间:2023-04-08 00:59:03
标签: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)]
来源:https://blog.csdn.net/weixin_44791964/article/details/96145026


猜你喜欢
- vue3使用computed获取vuex里数据不再是vue2.0里什么mapGetter,mapState那些复杂的获取方式,vue3.0里
- 在pycharm使用过程中,对于每次新建的python文件的时候,关于代码编写者的一些个人信息快捷填写,使用模板的方式比较方便。方法如下:1
- Instr函数与InstrRev函数大家都应该很熟悉,但是如果你看过《ASP * 站开发实践教程》,你应该注意一下。该书中介绍它们时是很有迷
- 这一次将使用pymysql来进行一次对MySQL的增删改查的全部操作,相当于对前五次的总结:先查阅数据库:现在编写源码进行增删改查操作,源码
- 实现思路需求需要将本数据库的数据进行处理(添加前缀),然后导入主数据库。但是当前数据库记录的create_time、update_time
- 构建运动模糊模型现假定相机不动,图像f(x,y)在图像面上移动并且图像f(x,y)除移动外不随时间变化。令x0(t)和y0(t)分别代表位移
- django配置mysql数据库:1.首先更改django项目文件中的settings.py的数据库配置DATABASES = { &nbs
- 要找到最早的活动事务,可以使用DBCC OPENTRAN命令。详细用法见MSDN:http://msdn.microsoft.com/zh-
- 最近在看流畅的python,在看第14章节的itertools模块,对其itertools中的相关函数实现的逻辑的实现其中在zip_long
- 先给大家说下我的项目需求:用户扫一扫二维码会产生一个链接,该链接会向后端发送个请求,返回一个 apk 的下载地址,用户点击下载按钮可以下载此
- python制作超级玛丽游戏,供大家参考,具体内容如下这篇文章,我们优先介绍超级玛丽游戏中的多状态跳跃,和行走地图拖动的原理,然后实现。并实
- 环境 MySQL 5.1 + 命令行工具 问题 MySQL表字段设置默认值 解决 --SQL: CREATE TABLE test( i_a
- 本文实例讲述了go语言map字典删除操作的方法。分享给大家供大家参考。具体分析如下:这里先构造了一点map,添加了青岛、济南、烟台三地的拼音
- 泛型,如果你学过Java ,应该对它不陌生吧。但你可能不知道在 Python 中(3.4+ ),也可以实现简单的泛型函数。在Python中只
- 原理:利用复化梯形公式,复化Simpson公式,计算积分。步骤:import math"""测试函数"
- 如果是后台上传文件:setting配置:STATIC_URL = '/static/'STATICFILES_DIRS =
- 如何使整个页面内容居中,如何使高度适应内容自动伸缩。这是学习CSS布局最常见的问题。下面就给出一个实际的例子,并详细解释。(本文的经验和是蓝
- mysql中全连接full join...on...用法大部分开发者可能会困惑,为什么我的sql语句怎么写都是错的。很简单因为full jo
- 在SQL SERVER中如何通过SQL语句获取服务器硬件和系统信息呢?下面介绍一下如何通过SQL语句获取处理器(CPU)、内存(Memory
- 最近自己在搭建一个基于webpack的react项目,遇到关于output.publicPath和webpack-dev-server中pu