tensorflow 獲取變量&打印權(quán)值的實(shí)例講解
在使用tensorflow中,我們常常需要獲取某個(gè)變量的值,比如:打印某一層的權(quán)重,通常我們可以直接利用變量的name屬性來獲取,但是當(dāng)我們利用一些第三方的庫來構(gòu)造神經(jīng)網(wǎng)絡(luò)的layer時(shí),存在一種情況:就是我們自己無法定義該層的變量,因?yàn)槭亲詣?dòng)進(jìn)行定義的。
比如用tensorflow的slim庫時(shí):
<span style="font-size:14px;">def resnet_stack(images, output_shape, hparams, scope=None):</span>
<span style="font-size:14px;"> """Create a resnet style transfer block.</span>
<span style="font-size:14px;"></span>
<span style="font-size:14px;"> Args:</span>
<span style="font-size:14px;"> images: [batch-size, height, width, channels] image tensor to feed as input</span>
<span style="font-size:14px;"> output_shape: output image shape in form [height, width, channels]</span>
<span style="font-size:14px;"> hparams: hparams objects</span>
<span style="font-size:14px;"> scope: Variable scope</span>
<span style="font-size:14px;"></span>
<span style="font-size:14px;"> Returns:</span>
<span style="font-size:14px;"> Images after processing with resnet blocks.</span>
<span style="font-size:14px;"> """</span>
<span style="font-size:14px;"> end_points = {}</span>
<span style="font-size:14px;"> if hparams.noise_channel:</span>
<span style="font-size:14px;"> # separate the noise for visualization</span>
<span style="font-size:14px;"> end_points['noise'] = images[:, :, :, -1]</span>
<span style="font-size:14px;"> assert images.shape.as_list()[1:3] == output_shape[0:2]</span>
<span style="font-size:14px;"></span>
<span style="font-size:14px;"> with tf.variable_scope(scope, 'resnet_style_transfer', [images]):</span>
<span style="font-size:14px;"> with slim.arg_scope(</span>
<span style="font-size:14px;"> [slim.conv2d],</span>
<span style="font-size:14px;"> normalizer_fn=slim.batch_norm,</span>
<span style="font-size:14px;"> kernel_size=[hparams.generator_kernel_size] * 2,</span>
<span style="font-size:14px;"> stride=1):</span>
<span style="font-size:14px;"> net = slim.conv2d(</span>
<span style="font-size:14px;"> images,</span>
<span style="font-size:14px;"> hparams.resnet_filters,</span>
<span style="font-size:14px;"> normalizer_fn=None,</span>
<span style="font-size:14px;"> activation_fn=tf.nn.relu)</span>
<span style="font-size:14px;"> for block in range(hparams.resnet_blocks):</span>
<span style="font-size:14px;"> net = resnet_block(net, hparams)</span>
<span style="font-size:14px;"> end_points['resnet_block_{}'.format(block)] = net</span>
<span style="font-size:14px;"></span>
<span style="font-size:14px;"> net = slim.conv2d(</span>
<span style="font-size:14px;"> net,</span>
<span style="font-size:14px;"> output_shape[-1],</span>
<span style="font-size:14px;"> kernel_size=[1, 1],</span>
<span style="font-size:14px;"> normalizer_fn=None,</span>
<span style="font-size:14px;"> activation_fn=tf.nn.tanh,</span>
<span style="font-size:14px;"> scope='conv_out')</span>
<span style="font-size:14px;"> end_points['transferred_images'] = net</span>
<span style="font-size:14px;"> return net, end_points</span>
我們希望獲取第一個(gè)卷積層的權(quán)重weight,該怎么辦呢??
在訓(xùn)練時(shí),這些可訓(xùn)練的變量會(huì)被tensorflow保存在 tf.trainable_variables() 中,于是我們就可以通過打印 tf.trainable_variables() 來獲取該卷積層的名稱(或者你也可以自己根據(jù)scope來看出來該變量的name ),然后利用tf.get_default_grap().get_tensor_by_name 來獲取該變量。
舉個(gè)簡(jiǎn)單的例子:
<span style="font-size:14px;">import tensorflow as tf</span>
<span style="font-size:14px;">with tf.variable_scope("generate"):</span>
<span style="font-size:14px;"> with tf.variable_scope("resnet_stack"):</span>
<span style="font-size:14px;"> #簡(jiǎn)單起見,這里沒有用第三方庫來說明,</span>
<span style="font-size:14px;"> bias = tf.Variable(0.0,name="bias")</span>
<span style="font-size:14px;"> weight = tf.Variable(0.0,name="weight")</span>
<span style="font-size:14px;"></span>
<span style="font-size:14px;">for tv in tf.trainable_variables():</span>
<span style="font-size:14px;"> print (tv.name)</span>
<span style="font-size:14px;"></span>
<span style="font-size:14px;">b = tf.get_default_graph().get_tensor_by_name("generate/resnet_stack/bias:0")</span>
<span style="font-size:14px;">w = tf.get_default_graph().get_tensor_by_name("generate/resnet_stack/weight:0")</span>
<span style="font-size:14px;"></span>
<span style="font-size:14px;">with tf.Session() as sess:</span>
<span style="font-size:14px;"> tf.global_variables_initializer().run()</span>
<span style="font-size:14px;"> print(sess.run(b))</span>
<span style="font-size:14px;"> print(sess.run(w))
</span>
結(jié)果如下:

以上這篇tensorflow 獲取變量&打印權(quán)值的實(shí)例講解就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
根據(jù)DataFrame某一列的值來選擇具體的某一行方法
今天小編就為大家分享一篇根據(jù)DataFrame某一列的值來選擇具體的某一行方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-07-07
Django中URL視圖函數(shù)的一些高級(jí)概念介紹
這篇文章主要介紹了Django中URL視圖函數(shù)的一些高級(jí)概念,Django是Python重多人氣框架中最為著名的一個(gè),需要的朋友可以參考下2015-07-07
python實(shí)現(xiàn)網(wǎng)頁鏈接提取的方法分享
這篇文章主要介紹了python實(shí)現(xiàn)的網(wǎng)頁鏈接提取的方法,需要的朋友可以參考下2014-02-02
python 集合 并集、交集 Series list set 轉(zhuǎn)換的實(shí)例
今天小編就為大家分享一篇python 集合 并集、交集 Series list set 轉(zhuǎn)換的實(shí)例。具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-05-05
在PyCharm環(huán)境中使用Jupyter Notebook的兩種方法總結(jié)
今天小編就為大家分享一篇在PyCharm環(huán)境中使用Jupyter Notebook的兩種方法總結(jié),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-05-05
Python PyQt5實(shí)戰(zhàn)項(xiàng)目之查詢器的實(shí)現(xiàn)流程詳解
PyQt5以一套Python模塊的形式來實(shí)現(xiàn)功能。它包含了超過620個(gè)類,600個(gè)方法和函數(shù)。它是一個(gè)多平臺(tái)的工具套件,它可以運(yùn)行在所有的主流操作系統(tǒng)中,包含Unix,Windows和Mac OS。PyQt5采用雙重許可模式。開發(fā)者可以在GPL和社區(qū)授權(quán)之間選擇2021-11-11
Python報(bào)錯(cuò)ImportError: No module named ‘mi
在 Python 開發(fā)過程中,報(bào)錯(cuò)是常有的事,而當(dāng)遇到“ImportError: No module named ‘missing_module’”這樣的報(bào)錯(cuò)時(shí),可能會(huì)讓開發(fā)者感到困惑和苦惱,本文將深入探討這個(gè)報(bào)錯(cuò)的原因和解決方法,幫助開發(fā)者快速解決這個(gè)問題,需要的朋友可以參考下2024-10-10

