我正在尝试将批量矩阵乘法作为 Keras 层进行,但无法弄清楚。
我尝试使用
tf.einsum('cij,cjk->cik', inputs, self.dense_weights) + self.dense_biases
c
是并行批次
怎么办?
这就是我初始化权重的方式
self.dense_weights = self.add_weight(
shape=(num_layers, inputs, output_units),
initializer='random_normal',
trainable=True,
name='dense_weights'
)
self.dense_biases = self.add_weight(
shape=(num_layers, 1, output_units),
initializer='zeros',
trainable=True,
name='dense_biases'
)
class ParallelDense(Layer):
def __init__(self, units, **kwargs):
super().__init__(**kwargs)
self.units = units
def build(self, input_shape):
super().build(input_shape)
self.kernel = self.add_weight(shape=[input_shape[1], input_shape[2], self.units], trainnable=True, initializer='glorot_uniform')
self.bias = self.add_weight(shape=[input_shape[1], self.units], trainnable=True, initializer='glorot_uniform')
def call(self, inputs):
return tf.einsum("bml, mlk -> bmk", inputs, self.kernel) + self.bias