2016-12-02 127 views
1

我試圖在Keras博客https://blog.keras.io/building-autoencoders-in-keras.html上找到的卷積自動編碼器運行以下代碼。不過,我收到一條錯誤消息:張量的錯誤形狀

input_img = Input(shape=(1, 28, 28)) 

x = Convolution2D(16, 3, 3, activation='relu', border_mode='same')(input_img) 
x = MaxPooling2D((2, 2), border_mode='same')(x) 
x = Convolution2D(8, 3, 3, activation='relu', border_mode='same')(x) 
x = MaxPooling2D((2, 2), border_mode='same')(x) 
x = Convolution2D(8, 3, 3, activation='relu', border_mode='same')(x) 
encoded = MaxPooling2D((2, 2), border_mode='same')(x) 

# at this point the representation is (8, 4, 4) i.e. 128-dimensional 

x = Convolution2D(8, 3, 3, activation='relu', border_mode='same')(encoded) 
x = UpSampling2D((2, 2))(x) 
x = Convolution2D(8, 3, 3, activation='relu', border_mode='same')(x) 
x = UpSampling2D((2, 2))(x) 
x = Convolution2D(16, 3, 3, activation='relu')(x) 
x = UpSampling2D((2, 2))(x) 
decoded = Convolution2D(1, 3, 3, activation='sigmoid', border_mode='same')(x) 
print(decoded.get_shape()) 

autoencoder = Model(input_img, decoded) 
autoencoder.compile(optimizer='adadelta', 
        loss='binary_crossentropy' 
        ) 

autoencoder.fit(x_train, x_train, 
       nb_epoch=50, 
       batch_size=128, 
       shuffle=True, 
       validation_data=(x_test, x_test) 
       ) 

--------------------------------------------------------------------------- 
Exception         Traceback (most recent call last) 
<ipython-input-25-5ca753acfdbb> in <module>() 
    28     batch_size=128, 
    29     shuffle=True, 
---> 30     validation_data=(x_test, x_test) 
    31    ) 

C:\Users\Alexander\Anaconda3\lib\site-packages\keras\engine\training.py in fit(self, x, y, batch_size, nb_epoch, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch) 
    1036               class_weight=class_weight, 
    1037               check_batch_dim=False, 
-> 1038               batch_size=batch_size) 
    1039   # prepare validation data 
    1040   if validation_data: 

C:\Users\Alexander\Anaconda3\lib\site-packages\keras\engine\training.py in _standardize_user_data(self, x, y, sample_weight, class_weight, check_batch_dim, batch_size) 
    965         output_shapes, 
    966         check_batch_dim=False, 
--> 967         exception_prefix='model target') 
    968   sample_weights = standardize_sample_weights(sample_weight, 
    969              self.output_names) 

C:\Users\Alexander\Anaconda3\lib\site-packages\keras\engine\training.py in standardize_input_data(data, names, shapes, check_batch_dim, exception_prefix) 
    109           ' to have shape ' + str(shapes[i]) + 
    110           ' but got array with shape ' + 
--> 111           str(array.shape)) 
    112  return arrays 
    113 

Exception: Error when checking model target: expected convolution2d_92 to have shape (None, 4, 28, 1) but got array with shape (60000, 1, 28, 28) 

的解碼,這我打印出來的尺寸,是(?, 4, 28, 1),這似乎是什麼模型預期,給出錯誤信息。謝謝您的幫助。

+0

你有沒有想過發生了什麼?我在運行他們的示例時遇到了同樣的錯誤。 – mdornfe1

回答

0

問題是我偶然使用了Theano後端,當時我應該使用了Tensorflow後端。在theano後端,第一個座標表示單個張量的形狀,但是它在張量流中是相反的。例如,對於input_img,在Theano中它的形狀爲(1, 28, 28),但在Tensorflow中爲(28, 28, 1)

相關問題