113 TensorFlow變數集合

2022-01-22 16:39:16 字數 1135 閱讀 1059

#

乙個tensorflow程式斷開的部分可能要建立變數

#如果有一種方法來訪問所有的變數是非常有用的

#因為這個原因tensorflow提供了集合,是一些張量的集合

#或者是其他的物件,就像tf.variable 例項一樣

#預設情況下 tf.variable 物件被放置在下面的兩個集合中

#tf.graphkeys.global_variables

#變數可以在多個裝置之間被分享

#tf.graphkeys.trainable_variables

#tensorflow會自動對上面集中的變數進行計算,列如自動求導

#如果你不想讓變數被自動訓練

#可以將它新增到 tf.graphkeys.local_variables集合中

#下面的**塊解釋了如何將my_local變數新增到這個集合中

import

tensorflow as tf

my_local = tf.get_variable("

my_local

", shape=(), collections=[tf.graphkeys.local_variables])

#alternatively you can specify trainable=false as an argument to tf.get_variable

my_non_trainable = tf.get_variable("

my_non_trainable

", shape=(), trainable=false)

#you can also use your own collections, any string is a valid collection name

tf.add_to_collection("

my_collection_name

",my_local)

#and to retrieve a list of all the variables (or other objects) you've placed in a collection you can use

tf.get_collection("

my_collection_name

")

tensorflow 1 13原始碼編譯

bazel和tensorflow有嚴格的對應版本,對應關係如下 編譯tensorflow之前先要安裝bazel 編譯tensorflow git clone cd tensorflow git checkout r1.13 r1.9,r1.10,etc.這裡選用的 r1.13 configure 在...

TensorFlow建立變數

一 使用tf.variable函式建立變數 tf.variable initial value none,trainable true,collections none,validate shape true,caching device none,name none,variable def no...

tensorflow 共享變數

import tensorflow as tf 設定隨機種子,使得每次隨機初始化都一樣 tf.set random seed 1234 這是我們要共享的變數函式 def share variable input weight tf.get variable weight 2,2 return wei...