检查目标时出错:预期lambda_1具有1个维,但数组的形状为(60000,10)

时间:2018-07-26 17:14:42

标签: python tensorflow keras tensor

我试图创建一个可逆网络,当它向后移动时,权重矩阵就是权重矩阵在正向过程中的转置。所以我定义了一个自定义层

  class Backwardlayer(Dense):
        def __init__(self,output_dim,b_layer,activation=None,use_bias=True,kernel_initializer='glorot_uniform',bias_initializer='zeros',kernel_regularizer=None,bias_regularizer=None,activity_regularizer=None,kernel_constraint=None,bias_constraint=None,**kwargs):
            self.output_dim = output_dim
            self.b_layer = b_layer

            super(Backwardlayer, self).__init__(output_dim,**kwargs)

        def build(self, input_shape):
            assert len(input_shape) >= 2
            input_dim = input_shape[-1]

            self.kernel = K.transpose(self.b_layer.kernel)

            if self.use_bias:
                self.bias = self.add_weight(shape=(self.output_dim,),initializer=self.bias_initializer, name='bias', regularizer=self.bias_regularizer, constraint=self.bias_constraint)
            else:
                self.bias = None
            self.built = True

def direction_cosine(x):
    return K.sqrt(K.sum(x, axis=-1, keepdims=None))
def abs(x):
    return K.abs(x)




        input_img = Input(shape=(784,))
        layer_1 = Dense(512, activation=abs)
        layer_2 = Dense(512, activation=abs)
        layer_3 = Dense(256, activation=abs)
        layer_4 = Dense(128, activation=abs)
        layer_5 = Dense(10, activation=abs)

        encoder_layer_1 = layer_1(input_img)
        encoder_layer_2 = layer_2(encoder_layer_1)
        encoder_layer_3 = layer_3(encoder_layer_2)
        encoder_layer_4 = layer_4(encoder_layer_3)
        encoder_layer_5 = layer_5(encoder_layer_4)

    decoder_layer_1 = Backwardlayer(128,b_layer=encoder_layer_5,activation=abs,name='dl1')(encoder_layer_5)
decoder_layer_2 = Backwardlayer(256,b_layer=layer_4,activation=abs,name='dl2')(decoder_layer_1)
    decoder_layer_3 = Backwardlayer(512,b_layer=layer_3,activation=abs,name='dl3')(decoder_layer_2)
    decoder_layer_4 = Backwardlayer(512,b_layer=layer_2,activation=abs,name='dl4')(decoder_layer_3)
    reconstructed_img = Backwardlayer(784,b_layer=layer_1,activation=abs,name='dl5')
rms = RMSprop()
(x_train,y_train), (x_test,y_test) = mnist.load_data()

x_train = x_train.reshape(-1,784)
x_test = x_test.reshape(-1,784)  

x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train /= 255
x_test /= 255

AANN = Model(input=input_img, output=[reconstructed_img,normalised_output])

AANN.summary()

AANN.compile(optimizer=rms,loss=['mse','categorical_crossentropy'],loss_weights=[1,1])

history = AANN.fit(x_train,[x_train,y_train],epochs=3,batch_size=128,verbose=2,validation_data=(x_test,y_test))

这是错误:检查目标时出错:预期lambda_1具有1个维,但数组的形状为(60000,10)

回溯(最近通话最近一次):

 File "AANN.py", line 95, in <module>
    history = AANN.fit(x_train,[x_train,y_train],epochs=3,batch_size=128,verbose=2,validation_data=(x_test,y_test))
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/keras/engine/training.py", line 955, in fit
    batch_size=batch_size)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/keras/engine/training.py", line 792, in _standardize_user_data
    exception_prefix='target')
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/keras/engine/training_utils.py", line 126, in standardize_input_data
    'with shape ' + str(data_shape))
ValueError: Error when checking target: expected lambda_1 to have 1 dimensions, but got array with shape (60000, 10)

1 个答案:

答案 0 :(得分:1)

错误是自我解释:“张量没有内核”。

  

层有内核。

这是不正确的:

encoder_layer_1 = layer_1(input_img)
encoder_layer_2 = layer_2(encoder_layer_1)
encoder_layer_3 = layer_3(encoder_layer_2)
encoder_layer_4 = layer_4(encoder_layer_3)
encoder_layer_5 = layer_5(encoder_layer_4)

这是真的:

layer_1_output_tensor = layer_1(input_img)
layer_2_output_tensor = layer_2(layer_1_output_tensor)
layer_3_output_tensor = layer_3(layer_2_output_tensor)
layer_4_output_tensor = layer_4(layer_3_output_tensor)
layer_5_output_tensor = layer_5(layer_4_output_tensor) 

那么您需要:

decoder_layer_output_1 = Backwardlayer(128,b_layer=layer_5,activation=abs,name='dl1')(layer_5_output_TENSOR)

说实话,我认为您的方法无效。您需要“矩阵除法”,而不是矩阵乘法。

也许您是在寻找内核^(-1)而不是转置,并在应用偏差之前减去了偏差。...