When the custom layer has multiple outputs, you need to wrap
the returned result with [] in the calculation of compute_output_shape and call .
For example: return output, indices will be encapsulated into (output, indices), so there will be a'tuple' object has no attribute'_keras_shape' error.
For example, in compute_output_shape, the results of return shape1, shape2 will be automatically added (shape1, shape2) and finally encapsulated into [(shape1, shape2)], so that the dimensions are changed, and there will be a list out-of-bounds error.
method:
改成return [value1, value2] , return [shape1, shape2]
def call(self, inputs):
res_es1 = inputs
res_es1 = K.tanh(K.dot(res_es1, self.Ws1))
res_att = K.softmax(K.dot(res_es1, self.Ws2))
res_es2 = K.batch_dot(res_att,inputs, axes=(1,1)) #shape(batch, r, hidden)
#计算l2 loss,使得每个分量所表示的尽量不同
l2_loss = K.batch_dot(res_es2, res_es2, axes=(2,2))
one_diago = K.ones_like(l2_loss)
loss_value = K.sum(K.square(l2_loss - one_diago))
return [res_es2, loss_value] #(res_es2, loss_value)表示tuple
def compute_output_shape(self, input_shape):
return [(None,self.r, input_shape[2]) , (1,)]