TensorFlow變數管理詳解

2022-10-04 20:18:23 字數 3160 閱讀 1769

一、tensorflow變數管理

1. tensorflow還提供了tf.get_variable函式來建立或者獲取變數,tf.variable用於建立變數時,其功能和tf.variable基本是等價的。tf.get_variable中的初始化方法(initializer)的引數和tf.variable的初始化過程也類似,initializer函式和tf.variable的初始化方法是一一對應的,詳見下表。

tf.get_variable和tf.variable最大的區別就在於指定變數名稱的引數。對於tf.variable函式,變數名稱是乙個可選的引數,通過name=」v」的形式給出,對於tf.get_variable函式,變數名稱是乙個必填的引數,tf.get_variable會根據這個名稱去建立或者獲取變數。

2. 通過tf.variable_scope函式可以控制tf.get_variable函式的語義。當tf.variable_scope函式的引數reuse=true生成上下文管理器時,該上下文管理器內的所有的tf.get_variable函式會直接獲取已經建立的變數,如果變數不存在則報錯;當tf.variable_scope函式的引數reuse=false或者none時建立的上下文管理器中,tf.get_vaxomaxriable函式則直接建立新的變數,若同名的變數已經存在則報錯。

3. 另tf.variable_scope函式是可以巢狀使用的。巢狀的時候,若某層上下文管理器未宣告reuse引數,則該層上下文管理器的reuse引數與其外層保持一致。

4.tf.variable_scope函式提供了乙個管理變數命名空間的方式。在tf.variable_scope中建立的變數,名稱.name中名稱前面會加入命名空間的名稱,並通過「/」來分隔命名空間的名稱和變數的名稱。tf.get_variable("foou/baru/u", [1]),可以通過帶命名空間名稱的變數名來獲取其命名空間下的變數。

二、tensorflow程式設計演示

import tensorflow as tf

# 在名字為foo的命名空間內建立名字為v的變數

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])

# valueerror: variable程式設計客棧 foo/v already exists, disallowed.

# did you mean to set reuse=true in varscope&

'''

# 將引數reuse引數設定為true,則tf.get_variable可直接獲取已宣告的變數

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

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

print(v == v1) # true

'''''

# 當reuse=true時,tf.get_variable只能獲取指定命名空間內的已建立的變數

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

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

# valueerror: variable bar/v does not exist, or was not created with

# tf.get_variable(). did you mean to set reuse=none in varscope?

'''

with tf.variable_scope("root"):

# 通過tf.get_variable_scope().reuse函式獲取當前上下文管理器內的reuse引數取值

print(tf.get_variable_scope().reuse) # false

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

print(tf.get_variable_scope().reuse) # true

with tf.variable_scope("bar1"):

# 巢狀在上下文管理器foo1內的bar1內未指定reuse引數,則保持與外層一致

print(tf.get_variable_scope().reuse) # true

print(tf.get_variable_scope().reuse) # false

# tf.variable_scope函式提供了乙個管理變數命名空間的方式

u1 = tf.get_variable("u", [1])

print(u1.name) # u:0

with tf.variable_scope("foou"):

u2 = tf.get_variable("u", [1])

print(u2.name) # foou/u:0

with tf.variable_scope("foou"):

with tf.variable_scope("baru"):

u3 = tf.get_variable("u", [1])

print(u3.name) # foou/baru/u:0

u4 = tf.get_variable("u1", [1])

print(u4.name) # foou/u1:0

# 可直接通程式設計客棧過帶命名空間名稱的變數名來獲取其命名空間下的變數

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

u5 = tf.get_variable("foou/baru/u", [1])

print(u5.name) # foou/baru/u:0

print(u5 == u3) # true

u6 = tf.get_variable("foou/u1", [1])

print(u6.name) # foou/u1:0

print(u6 == u4) # true

本文標題: tensorflow變數管理詳解

本文位址:

tensorflow中的變數管理

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

tensorflow中的變數管理

import tensorflow as tf variable scope 示例 tensorflow中通過變數名稱獲取變數的機制主要是通過tf.get variable和tf.variable scope函式實現的 tf提供tf.get variable函式來建立或獲取變數 當tf.get va...

Tensorflow學習筆記 變數管理

一 深度學習變數的建立方法有兩種tf.variable 和tf.get variable 但兩種是有區別的 1 tf.variable 每次呼叫都是乙個新的變數,沒指定名稱時,會預設加上名字,指定了名字,且連續調了兩次,會自動給後面建立的變數的名字後面加字尾 2 tf.get variables 建...