神剑山庄资源网 Design By www.hcban.com
在使用tensorflow中,我们常常需要获取某个变量的值,比如:打印某一层的权重,通常我们可以直接利用变量的name属性来获取,但是当我们利用一些第三方的库来构造神经网络的layer时,存在一种情况:就是我们自己无法定义该层的变量,因为是自动进行定义的。
比如用tensorflow的slim库时:
<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 来获取该变量。
举个简单的例子:
<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,打印权值
神剑山庄资源网 Design By www.hcban.com
神剑山庄资源网
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
神剑山庄资源网 Design By www.hcban.com
暂无tensorflow 获取变量&打印权值的实例讲解的评论...
更新日志
2024年10月02日
2024年10月02日
- 孙悦.1996-伙伴【正大国际】【WAV+CUE】
- 纪钧瀚《钢琴阅读时光 雨中书店聆听轻音乐》[FLAC/分轨][399.62MB]
- 证声音乐图书馆《走向自然 疗心爵士乐》[320K/MP3][87.4MB]
- 证声音乐图书馆《走向自然 疗心爵士乐》[FLAC/分轨][184.94MB]
- 陈慧娴.2018-Priscilla-Ism演唱会3CD(2024环球红馆40复刻系列)【环球】【WAV+CUE】
- 郑秀文.1999-我应该得到(国)【华纳】【WAV+CUE】
- 陈家慧.2011-钢琴酒吧2CD【龙吟唱片】【WAV+CUE】
- 证声音乐图书馆《雨季 蓝调吉他 Rainy Blues》[320K/MP3][45.01MB]
- 证声音乐图书馆《雨季 蓝调吉他 Rainy Blues》[FLAC/分轨][109.13MB]
- 赞多《序章》[320K/MP3][45.54MB]
- 许巍.2004-每一刻都是崭新的【步升大风】【WAV+CUE】
- 群星.2024-四方馆影视原声带【韶愔音乐】【FLAC分轨】
- 陈雷.1997-安锁咧【金圆唱片】【WAV+CUE】
- 关淑怡.2013-MY.FAVORITE.SK.3CD【环球】【WAV+CUE】
- Sweety.2006-花言乔语【丰华】【WAV+CUE】