tensorflow建立變數以及根據名稱尋找變數,tensorflow變數

來源:互聯網
上載者:User

tensorflow建立變數以及根據名稱尋找變數,tensorflow變數

環境:Ubuntu14.04,tensorflow=1.4(bazel源碼安裝),Anaconda python=3.6

聲明變數主要有兩種方法:tf.Variabletf.get_variable,二者的最大區別是:

(1) tf.Variable是一個類,內建很多屬性函數;而 tf.get_variable是一個函數;
(2) tf.Variable只能產生獨一無二的變數,即如果給出的name已經存在,則會自動修改產生新的變數name;
(3) tf.get_variable可以用於產生共用變數。預設情況下,該函數會進行變數名檢查,如果有重複則會報錯。當在指定變數域中聲明可

以變數共用時,可以重複使用該變數(例如RNN中的參數共用)。
下面給出簡單的的樣本程式:

import tensorflow as tfwith tf.variable_scope('scope1',reuse=tf.AUTO_REUSE) as scope1:  x1 = tf.Variable(tf.ones([1]),name='x1')  x2 = tf.Variable(tf.zeros([1]),name='x1')  y1 = tf.get_variable('y1',initializer=1.0)  y2 = tf.get_variable('y1',initializer=0.0)  init = tf.global_variables_initializer()  with tf.Session() as sess:    sess.run(init)    print(x1.name,x1.eval())    print(x2.name,x2.eval())    print(y1.name,y1.eval())    print(y2.name,y2.eval())

輸出結果為:

scope1/x1:0 [ 1.]scope1/x1_1:0 [ 0.]scope1/y1:0 1.0scope1/y1:0 1.0

1. tf.Variable(…)

tf.Variable(…)使用給定初始值來建立一個新變數,該變數會預設添加到 graph collections listed in collections, which defaults to [GraphKeys.GLOBAL_VARIABLES]。

如果trainable屬性被設定為True,該變數同時也會被添加到graph collection GraphKeys.TRAINABLE_VARIABLES.

# tf.Variable__init__(  initial_value=None,  trainable=True,  collections=None,  validate_shape=True,  caching_device=None,  name=None,  variable_def=None,  dtype=None,  expected_shape=None,  import_scope=None,  constraint=None)

2. tf.get_variable(…)

tf.get_variable(…)的傳回值有兩種情形:

使用指定的initializer來建立一個新變數;
當變數重用時,根據變數名搜尋返回一個由tf.get_variable建立的已經存在的變數;

get_variable(  name,  shape=None,  dtype=None,  initializer=None,  regularizer=None,  trainable=True,  collections=None,  caching_device=None,  partitioner=None,  validate_shape=True,  use_resource=None,  custom_getter=None,  constraint=None)

3. 根據名稱尋找變數

在建立變數時,即使我們不指定變數名稱,程式也會自動進行命名。於是,我們可以很方便的根據名稱來尋找變數,這在抓取參數、finetune模型等很多時候都很有用。

樣本1:

通過在tf.global_variables()變數列表中,根據變數名進行匹配搜尋尋找。 該種搜尋方式,可以同時找到由tf.Variable或者tf.get_variable建立的變數。

import tensorflow as tfx = tf.Variable(1,name='x')y = tf.get_variable(name='y',shape=[1,2])for var in tf.global_variables():  if var.name == 'x:0':    print(var)

樣本2:

利用get_tensor_by_name()同樣可以獲得由tf.Variable或者tf.get_variable建立的變數。
需要注意的是,此時獲得的是Tensor, 而不是Variable,因此 x不等於x1.

import tensorflow as tfx = tf.Variable(1,name='x')y = tf.get_variable(name='y',shape=[1,2])graph = tf.get_default_graph()x1 = graph.get_tensor_by_name("x:0")y1 = graph.get_tensor_by_name("y:0")

樣本3:

針對tf.get_variable建立的變數,可以利用變數重用來直接擷取已經存在的變數。

with tf.variable_scope("foo"):  bar1 = tf.get_variable("bar", (2,3)) # createwith tf.variable_scope("foo", reuse=True):  bar2 = tf.get_variable("bar") # reusewith tf.variable_scope("", reuse=True): # root variable scope  bar3 = tf.get_variable("foo/bar") # reuse (equivalent to the above)print((bar1 is bar2) and (bar2 is bar3))

以上就是本文的全部內容,希望對大家的學習有所協助,也希望大家多多支援幫客之家。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.