網(wǎng)站首頁 編程語言 正文
參數(shù)數(shù)量及其作用
該函數(shù)共有五個(gè)參數(shù),分別是:
- 被賦值的變量 ref
- 要分配給變量的值 value、
- 是否驗(yàn)證形狀 validate_shape
- 是否進(jìn)行鎖定保護(hù) 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.
該函數(shù)的作用是將一個(gè)要分配給變量的值value賦予被賦值的變量ref,用于tensorflow各個(gè)參數(shù)的變量賦值。
例子
該例子將舉例如何進(jìn)行變量之間的數(shù)據(jù)賦值和如何進(jìn)行集合間的數(shù)據(jù)賦值。
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)) #顯示賦值后的結(jié)果
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)) #顯示賦值后的結(jié)果
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
相關(guān)推薦
- 2022-04-28 C#使用BackgroundWorker控件_C#教程
- 2022-08-27 C#過濾sql特殊字符串的方法_C#教程
- 2022-12-11 Dart多個(gè)future隊(duì)列完成加入順序關(guān)系及原子性論證_Dart
- 2022-05-07 LINQ教程之使用Lambda表達(dá)式_實(shí)用技巧
- 2022-10-31 R語言中set.seed()函數(shù)的作用詳解_R語言
- 2022-11-21 Qt實(shí)現(xiàn)小功能之復(fù)雜抽屜效果詳解_C 語言
- 2022-06-19 SQL?Server?Agent?服務(wù)啟動(dòng)后又停止問題_MsSql
- 2022-05-18 python?中賦值,深拷貝,淺拷貝的區(qū)別_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支