激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

腳本之家,腳本語言編程技術及教程分享平臺!
分類導航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服務器之家 - 腳本之家 - Python - tensorflow 獲取變量&打印權值的實例講解

tensorflow 獲取變量&打印權值的實例講解

2021-03-05 00:03cassiePython Python

今天小編就為大家分享一篇tensorflow 獲取變量&打印權值的實例講解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

在使用tensorflow中,我們常常需要獲取某個變量的值,比如:打印某一層的權重,通常我們可以直接利用變量的name屬性來獲取,但是當我們利用一些第三方的庫來構造神經網絡的layer時,存在一種情況:就是我們自己無法定義該層的變量,因為是自動進行定義的。

比如用tensorflow的slim庫時:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<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>

我們希望獲取第一個卷積層的權重weight,該怎么辦呢??

在訓練時,這些可訓練的變量會被tensorflow保存在 tf.trainable_variables() 中,于是我們就可以通過打印 tf.trainable_variables() 來獲取該卷積層的名稱(或者你也可以自己根據scope來看出來該變量的name ),然后利用tf.get_default_grap().get_tensor_by_name 來獲取該變量。

舉個簡單的例子:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<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;"#簡單起見,這里沒有用第三方庫來說明,</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>

結果如下:

tensorflow 獲取變量&打印權值的實例講解

以上這篇tensorflow 獲取變量&打印權值的實例講解就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持服務器之家。

原文鏈接:https://blog.csdn.net/cassiePython/article/details/79179044

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国产精品视频海角社区88 | 国产午夜亚洲精品理论片大丰影院 | 久久国产精品网 | 海外中文字幕在线观看 | 91av在线免费视频 | 在线成人一区二区 | 成人免费视频视频在线观看 免费 | 高清久久久久 | 成人性视频在线 | 国产精品久久国产精品 | 午夜国产成人 | 欧美人一级淫片a免费播放 久久久久久久久91 国产99久久久久久免费看 | 国产亚洲高清视频 | 91精品国啪老师啪 | 久久精品视频7 | 欧美激情性色生活片在线观看 | 国产亚洲在 | 黄色网络免费看 | 国产精品国产三级国产在线观看 | 亚洲最新无码中文字幕久久 | 毛片在线免费视频 | 欧美亚洲综合在线 | 在线香蕉视频 | 亚洲成人精品久久 | 成熟女人特级毛片www免费 | 欧美一区黄色 | 久久伊 | 精品无码久久久久久国产 | 污视频在线免费播放 | 亚洲第一男人天堂 | xnxx 日本免费 | 狠狠干天天操 | 91免费国产视频 | 欧美一区二区三区久久综合 | 欧美黄一级 | 国产99久久精品一区二区 | 18被视频免费观看视频 | 欧美成人精品一区二区三区 | 在线播放视频一区二区 | 一级黄色免费电影 | 色视频91 |