我正在尝试在Keras中实现一个层,它为每个输入添加元素 . 因此,输入,重量和输出具有完全相同的形状 . 然而,我正在努力实现这一点,我没有找到任何自动图层的例子,不会改变输入形状 .
来自keras.engine.topology导入层导入keras.backend为K.
class SumationLayer(Layer):
def __init__(self, **kwargs):
self.output_dim = K.placeholder(None)
super(SumationLayer, self).__init__(**kwargs)
def build(self, input_shape):
# Create a trainable weight variable for this layer.
self.kernel = self.add_weight(name='kernel',
shape=(input_shape[1], self.output_dim),
initializer='uniform',
trainable=True)
super(SumationLayer, self).build(input_shape) # Be sure to call this somewhere!
self.output_dim = (input_shape[0], self.output_dim)
def call(self, x):
return x + self.kernel
def compute_output_shape(self, input_shape):
return (input_shape[0], self.output_dim)
这会输出以下错误:
TypeError: Value passed to parameter 'shape' has DataType float32 not in list of allowed values: int32, int64
如果我像Keras示例那样实现该层,那么我必须在初始化时输入输出形状,这会产生不希望的行为(通过完全连接输入来平坦化输出) .
1 回答
玩代码我得到它的工作:但这仅适用于二维张量 . 如果你需要一个三维张量,你还需要包含input_shape [3] .