Reusing layer weights in Tensorflow

半城伤御伤魂 提交于 2019-12-21 04:51:26

问题


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).

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: 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.


回答1:


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

Transposition is simple:

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

will swap the last two axes.

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



来源:https://stackoverflow.com/questions/42699897/reusing-layer-weights-in-tensorflow

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!