如何在 keras 中添加可训练的 hadamard 产品层?

Uda*_*r S 4 keras

我试图在训练样本中引入稀疏性。我的数据矩阵的大小为(比如说)NxP,我想通过一个层(keras 层)传递它,该层的权重与输入大小相同。即可训练权重矩阵 W 的形状为 NxP。我想对这一层进行输入矩阵的 hadamard 乘积(逐元素乘法)。W 将元素与输入相乘。在这种情况下如何获得 W 的可训练层?

编辑:顺便说一下,非常感谢您的快速回复。然而,我想要做的 hadamard 乘积是在两个矩阵之间,一个是输入,我们称之为 X,我的 X 是 NxP 的形状。而且我希望我在 hadamard 层中的内核与 X 的大小相同。因此内核也应该具有 NxP 的大小。两个矩阵的元素乘法是通过调用函数实现的。

但是当前的实现仅将内核大小指定为 P。另外,我尝试在构建中更改内核的形状,如下所示:

self.kernel = self.add_weight(name='kernel',
                                      shape=input_shape,
                                      initializer='uniform',
                                      trainable=True)
Run Code Online (Sandbox Code Playgroud)

但它给了我以下错误:

类型错误:无法将类型的对象转换为张量。内容:(无,16)。考虑将元素转换为支持的类型。

这里 P 是 16,我会在运行时得到 N,N 与训练样本的数量相似。

预先感谢您的帮助。

Jul*_*yes 5

文档为例创建一个层,在call函数中将其定义为x * self.kernel.

这是我的 POC:

from keras import backend as K
from keras.engine.topology import Layer
from keras.models import Sequential
from keras.layers import Dense, Activation
import numpy as np
np.random.seed(7)

class Hadamard(Layer):

    def __init__(self, **kwargs):
        super(Hadamard, self).__init__(**kwargs)

    def build(self, input_shape):
        # Create a trainable weight variable for this layer.
        self.kernel = self.add_weight(name='kernel', 
                                      shape=(1,) + input_shape[1:],
                                      initializer='uniform',
                                      trainable=True)
        super(Hadamard, self).build(input_shape)  # Be sure to call this somewhere!

    def call(self, x):
        print(x.shape, self.kernel.shape)
        return x * self.kernel

    def compute_output_shape(self, input_shape):
        print(input_shape)
        return input_shape

N = 10
P = 64

model = Sequential()
model.add(Dense(128, input_shape=(N, P), activation='relu'))
model.add(Dense(64))
model.add(Hadamard())
model.add(Activation('relu'))
model.add(Dense(32))
model.add(Dense(1))

model.compile(loss='mean_squared_error', optimizer='adam')

print(model.summary())

model.fit(np.ones((10, N, P)), np.ones((10, N, 1)))

print(model.predict(np.ones((20, N, P))))
Run Code Online (Sandbox Code Playgroud)

如果需要将其用作第一层,则应包含输入形状参数:

N = 10
P = 64

model = Sequential()
model.add(Hadamard(input_shape=(N, P)))

model.compile(loss='mean_squared_error', optimizer='adam')

print(model.summary())
Run Code Online (Sandbox Code Playgroud)

这导致:

_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
hadamard_1 (Hadamard)       (None, 10, 64)            640       
=================================================================
Total params: 640
Trainable params: 640
Non-trainable params: 0
Run Code Online (Sandbox Code Playgroud)