欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python人工智能tensorflow函數(shù)tf.assign使用方法

 更新時間:2022年05月05日 14:44:31   作者:Bubbliiiing  
這篇文章主要為大家介紹了python人工智能tensorflow函數(shù)tf.assign使用方法,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

參數(shù)數(shù)量及其作用

該函數(shù)共有五個參數(shù),分別是:

  • 被賦值的變量 ref
  • 要分配給變量的值 value、
  • 是否驗證形狀 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ù)的作用是將一個要分配給變量的值value賦予被賦值的變量ref,用于tensorflow各個參數(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)]

以上就是python人工智能tensorflow函數(shù)tf.assign使用方法的詳細(xì)內(nèi)容,更多關(guān)于tensorflow函數(shù)tf.assign的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Python寫的一個定時重跑獲取數(shù)據(jù)庫數(shù)據(jù)

    Python寫的一個定時重跑獲取數(shù)據(jù)庫數(shù)據(jù)

    本文給大家分享基于python寫的一個定時重跑獲取數(shù)據(jù)庫數(shù)據(jù)的方法,非常不錯,具有參考借鑒價值,需要的朋友參考下
    2016-12-12
  • python3訪問字典里的值實例方法

    python3訪問字典里的值實例方法

    在本篇內(nèi)容里小編給大家整理的是一篇關(guān)于python3訪問字典里的值實例方法,有興趣的朋友們可以學(xué)習(xí)參考下。
    2020-11-11
  • Python類屬性的延遲計算

    Python類屬性的延遲計算

    這篇文章主要為大家詳細(xì)介紹了Python類屬性的延遲計算,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-10-10
  • echarts動態(tài)獲取Django數(shù)據(jù)的實現(xiàn)示例

    echarts動態(tài)獲取Django數(shù)據(jù)的實現(xiàn)示例

    本文主要介紹了echarts動態(tài)獲取Django數(shù)據(jù)的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-08-08
  • 使用go和python遞歸刪除.ds store文件的方法

    使用go和python遞歸刪除.ds store文件的方法

    使用python和go遞歸刪除.DS_Store文件,.DS_Store (英文全稱 Desktop Services Store)是一種由蘋果公司的Mac OS X操作系統(tǒng)所創(chuàng)造的隱藏文件,目的在于存貯文件夾的自定義屬性
    2014-01-01
  • 使用Django框架中ORM系統(tǒng)實現(xiàn)對數(shù)據(jù)庫數(shù)據(jù)增刪改查

    使用Django框架中ORM系統(tǒng)實現(xiàn)對數(shù)據(jù)庫數(shù)據(jù)增刪改查

    這篇文章主要介紹了使用Django的ORM實現(xiàn)對數(shù)據(jù)庫數(shù)據(jù)增刪改查方法,文中附含詳細(xì)示例代碼以及過程詳解,有需要的朋友可以借鑒參考下
    2021-09-09
  • Python中實現(xiàn)一行拆多行和多行并一行的示例代碼

    Python中實現(xiàn)一行拆多行和多行并一行的示例代碼

    這篇文章主要介紹了Python中實現(xiàn)一行拆多行和多行并一行的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-09-09
  • python實現(xiàn)簡單聊天功能

    python實現(xiàn)簡單聊天功能

    這篇文章主要為大家詳細(xì)介紹了python實現(xiàn)簡單聊天功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-07-07
  • python爬蟲將js轉(zhuǎn)化成json實現(xiàn)示例

    python爬蟲將js轉(zhuǎn)化成json實現(xiàn)示例

    這篇文章主要為大家介紹了python爬蟲將js轉(zhuǎn)化成json實現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-05-05
  • python自動更新pom文件的方法

    python自動更新pom文件的方法

    這篇文章主要介紹了python自動更新pom文件的方法,本文通過圖文實例代碼相結(jié)合給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-09-09

最新評論