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
0
投稿
猜你喜欢
- RegExp就是建立正则的对像。如:Set regEx = New RegExp regE
- 【1】 以XML 返回 (1)未定义属性的 select logisticsId,logisticsName from LogisticsC
- 题目: 一个环形单链表,从头结点开始向后,指针每移动一个结点,就计数加1,当数到第m个节点时,就把该结点删除,然后继续从下一个节点开始从1计
- 仿豆瓣分页原型(Javascript版)写了个分页的样式。自我感觉,这样的分页前后兼顾,对于用户的体验是蛮好使的Javascript分页代码
- 首先需要安装itchat库,可以pip install itchat安装,也可以在pycharm里安装# -*- coding:utf-8
- 一、解析网站1.1 获取音频地址在喜马拉雅网站上,随便点开一个音频,打开“开发者工具”,再点击播放按钮,可以看到出现了多个请求:经过排查,发
- python中安装包的方式有很多种:源码包:python setup.py install在线安装:pip install 包名(linux
- 适配器模式Adapter Pattern是什么适配器模式是一种结构型模式,它可以将一个类的接口转换成客户端所期望的接口,从而使原本不兼容的类
- 1、跳过迭代对象的开头string_from_file = """ // Wooden: ...
- 有一道题: 比较两个列表范围,如果包含的话,返回TRUE,否则FALSE。 详细题目如下:Create a function, this f
- 如何做一个树状展开视图来显示自己的记录结构?在SQL中,如何做一个可收起和展开树状结构图?就是资源管理器左栏的那种效果。这要用到Data s
- ASP正则表达式,RegExp对象提供简单的正则表达式支持功能。RegExp对象的用法: Function RegExpTest(
- 下面就是简单的例子,这里提供2中方法:test.htm &
- 方法一.Image { max-width:600px;height:
- 最近在网上经常看到朋友们聊到UEO,我就想哈UEO是啥东西啊,我去找啦些资料看,他们都说将来UEO发展一定会比较好,我也说这是肯定的.我为什
- 一看,C盘只有不到2M可用空间,一查原因,sqlserver安装路径下的log目录文件占了好大,5G多, 于是上网搜了下,解决了: 把与sq
- 1、设置web.config文件。以下为引用的内容:<system.web> ...... <globalization
- 本文实例讲述了Python实现的爬取百度文库功能。分享给大家供大家参考,具体如下:# -*- coding: utf-8 -*-from s
- by cpglkg <script>var arr=[5,10,15];function rect(arr,width){&nb
- 常用的四种SQL命令:1.查询数据记录(Select)语法:Select 字段串行 From table Where 字段=内容例