tensorflow中的變數管理

2021-08-26 23:42:58 字數 2794 閱讀 4324

import tensorflow as tf

# variable_scope()示例

"""tensorflow中通過變數名稱獲取變數的機制主要是通過tf.get_variable和tf.variable_scope函式實現的

tf提供tf.get_variable函式來建立或獲取變數;當tf.get_variable用於建立變數時,它和tf.variable的功能基本等價

"""# tf.get_variable函式呼叫時提供維度資訊和初始化方法,tf.get_variable函式與tf.variable函式最大區別是:tf.variable函式中變數名稱是乙個可選引數

# tf.get_variable函式中的變數名稱是乙個必填引數,tf.get_variable會根據這個名字去建立或獲取乙個變數

v = tf.get_variable("v", shape=[1], initializer=tf.constant_initializer(1.0))

v = tf.variable(tf.constant(1.0, shape=[1], name="v"))

# 為了解決出現變數復用造成的tf.get_variable錯誤,需要通過tf.variable_scope()函式生成乙個上下文管理器,並明確指出在這個上下文管理器中,

# tf.get_variable()將獲取已經生成的變數

with tf.variable_scope("foo"):

v = tf.get_variable("v", [1], initializer=tf.constant_initializer(1.0))

"""由於命名空間foo中已經存在名為v的變數,所以下面**將會報錯

with tf.variable_scope("foo"):

v = tf.get_variable("v", [1])

"""# 在生成上下文管理器時,將引數reuse設定為true,這樣tf.get_variable函式將直接獲取已經宣告的變數

with tf.variable_scope("foo", reuse=true):

v1 = tf.get_variable("v", [1])

print(v1 == v) # v,v1代表的是相同的tf中的變數

# 將reuse設定為true時,tf.variable_scope將只能獲取已經建立過的變數,因為在bar命名空間中,還沒有建立過變數v,所以報錯

"""with tf.variable_scope("bar", reuse=true):

v = tf.get_variable("v",[1])

"""# 如果tf.variable_scope()函式使用引數reuse=none,reuse=false建立上下文管理器時,tf.get_variable操作將會建立新的變數,如果同名變數已存在,將會報錯

# tf.variable_scope()函式是可以巢狀的

with tf.variable_scope("root"):

print(tf.get_variable_scope().reuse)

with tf.variable_scope("foo", reuse=true):

print(tf.get_variable_scope().reuse)

with tf.variable_scope("bar"):

print(tf.get_variable_scope().reuse)

print(tf.get_variable_scope().reuse)

# tf.variable_scope()函式除了可以控制tf.get_variable執行功能之外,也提供了乙個管理變數命名空間的方式

with tf.variable_scope("hi"):

v1 = tf.get_variable("v", [1])

print(v1.name)

print(tf.get_variable_scope().reuse)

with tf.variable_scope("hi"):

with tf.variable_scope("bar"):

v2 = tf.get_variable("v", [1])

print(v2.name)

v4 = tf.get_variable("v1", [1]) # 此處的變數名一定不能是v

print(v4.name)

with tf.variable_scope("", reuse=true):

v5 = tf.get_variable("hi/bar/v", [1])

print(v5 == v2)

v6 = tf.get_variable("hi/v1", [1])

print(v6 == v4)

# name_scope()在視覺化過程中,為變數劃分範圍,表示計算圖中的乙個層級,不影響get_variable建立的變數,只會影響variable()建立的變數

"""name_scope 對用get_variable()建立的變數的名字不會有任何影響,而 variable()建立的操作會被加上字首,並且會給操作加上名字字首

"""with tf.variable_scope("fooo"):

with tf.name_scope("bar"):

v = tf.get_variable("v", [1])

b = tf.variable(tf.zeros([1]), name="b")

x = 1.0 + v

print(v.name)

print(b.name)

print(x.op.name)

tensorflow中的變數管理

變數管理 通過名稱獲取變數 tf.get variable 和tf.variable scope 函式實現。tf.get variable 建立變數或者獲取變數,與tf.variable 基本等價 下面二個變數等價 v tf.get variable v shape 1 initializer tf...

83 Tensorflow中的變數管理

created on apr 21,2017 author p0079482 如何通過tf.variable scope函式來控制tf.ger variable函式獲取已經建立過的變數 在名字為foo的命名空間內建立名字為v的變數 import tensorflow as tf with tf.va...

Tensorflow中變數的重複使用

tf.get variables name,shape,initializer 和tf.variable name,shape,initializer 都是相同的,建立 初始化 乙個變數.但是tf.get variables 中name是必寫項,而tf.variable 是可選項。且tf.get v...