我正在尝试创建一个自定义的keras图层来执行特定任务
我输入了shape =(batch_size,M,N,p) 我希望我的输出为shape =(batch_size,M,N,f)
所以, 我设置了形状为(M,N,p,f)
的可训练conv_weight下面是我的代码
class convLayer(Layer):
"""
Self defined convolutional layer
"""
def __init__(self, filter_no, **kwargs):
self.filter_no = filter_no
super(convLayer, self).__init__(**kwargs)
def build(self, input_shape):
self.conv_weights = self.add_weight(name='weight',
shape=(input_shape[1], input_shape[2],
input_shape[3], self.filter_no),
initializer='uniform',
trainable=True)
super(convLayer, self).build(input_shape)
def call(self, inputs):
outputs = K.placeholder(shape=(inputs.shape[0], inputs.shape[1],
inputs.shape[2], self.filter_no),
dtype=tf.float32)
for i in range(self.filter_no):
weight = self.conv_weights[:,:,:, i]
val = tf.math.multiply(inputs, weight)
for j in range(val.shape[3]):
if i==0:
outputs[:,:,:,i].assign(val[:,:,:,j])
else:
outputs[:,:,:,i].assign(tf.math.add(outputs[:,:,:,i], val[:,:,:,j]))
return outputs
def compute_output_shape(self, input_shape):
return (input_shape[0], input_shape[1], input_shape[2], self.filter_no)
对于每个f,我的输出应为shape =(batch_size,M,N,f),输入和conv_weight中轴p的所有元素都应相乘并加在一起。
我一直在尝试并遇到一些错误。我对创建自定义图层还比较陌生。请帮助。谢谢。
错误消息: 切片分配仅支持变量。