Keras-重用上一层的权重-转换为keras张量 [英] Keras - Reuse weights from a previous layer - converting to keras tensor
本文介绍了Keras-重用上一层的权重-转换为keras张量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试重用上一层的权重矩阵.作为一个玩具示例,我想做这样的事情:
I am trying to reuse the weight matrix from a previous layer. As a toy example I want to do something like this:
import numpy as np
from keras.layers import Dense, Input
from keras.layers import merge
from keras import backend as K
from keras.models import Model
inputs = Input(shape=(4,))
inputs2 = Input(shape=(4,))
dense_layer = Dense(10, input_shape=(4,))
dense1 = dense_layer(inputs)
def my_fun(my_inputs):
w = my_inputs[0]
x = my_inputs[1]
return K.dot(w, x)
merge1 = merge([dense_layer.W, inputs2], mode=my_fun)
问题在于dense_layer.W
不是keras张量.所以我收到以下错误:
The problem is that dense_layer.W
is not a keras tensor. So I get the following error:
Exception: Output tensors to a Model must be Keras tensors. Found: dot.0
关于如何将dense_layer.W
转换为Keras张量的任何想法吗?
Any idea on how to convert dense_layer.W
to a Keras tensor?
谢谢
推荐答案
似乎您想在图层之间共享权重. 我认为您可以将密集层用作输入和输入2的共享层.
It seems that you want to share weights between layers. I think You can use denselayer as shared layer for inputs and inputs2.
merge1=dense_layer(inputs2)
请在 https://keras处检查共享层. io/getting-started/functional-api-guide/#shared-layers
这篇关于Keras-重用上一层的权重-转换为keras张量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文