在Tensorflow中重用图层权重 [英] Reusing layer weights in Tensorflow

查看:232
本文介绍了在Tensorflow中重用图层权重的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用tf.slim来实现自动编码器.我完全了解以下架构:

I am using tf.slim to implement an autoencoder. I's fully convolutional with the following architecture:

[conv, outputs = 1] => [conv, outputs = 15] => [conv, outputs = 25] =>
=> [conv_transpose, outputs = 25] => [conv_transpose, outputs = 15] => 
[conv_transpose, outputs = 1]

它必须是完全卷积的,我不能做池化(较大问题的局限性).我想用绑重物,所以

It has to be fully convolutional and I cannot do pooling (limitations of the larger problem). I want to use tied weights, so

encoder_W_3 = decoder_W_1_Transposed 

(因此第一个解码器层的权重是最后一个编码器层的权重).

(so the weights of the first decoder layer are the ones of the last encoder layer, transposed).

如果我以常规方式重用权重,则tfslim允许您重用权重,即reuse = True,然后仅提供要重用的层的作用域名称,就会出现大小问题:

If I reuse weights the regular way tfslim lets you reuse them, i.e. reuse = True and then just provide the scope name of the layer you want to reuse, I get size issue:

ValueError: Trying to share variable cnn_block_3/weights, but specified shape (21, 11, 25, 25) and found shape (21, 11, 15, 25).

如果不转移先前模型的权重,则这很有意义.有人对我如何调换这些砝码有任何想法吗?

This makes sense, if you do not transpose the weights of the previous model. Does anyone have an idea on how I can transpose those weights?

PS:我知道这是非常抽象和手工的,但是我正在tfslim之上使用自定义api,因此我无法在此处发布代码示例.

PS: I know this is very abstract and hand-waving, but I am working with a custom api, on top of tfslim, so I can't post code examples here.

推荐答案

有人对我如何调换这些砝码有想法吗?

Does anyone have an idea on how I can transpose those weights?

换位很简单:

new_weights = tf.transpose(weights, perm=[0, 1, 3, 2])

将交换最后两个轴.

但是,正如@Seven提到的那样,这不足以解决错误,因为权重总数发生了变化.

However, as @Seven mentioned, that wouldn't be enough to address the error, as the total number of weights changed.

这篇关于在Tensorflow中重用图层权重的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆