与CNN的输入相比,Keras的输出层形状不匹配

时间:2016-06-21 23:01:25

标签: theano keras

我在Keras中遇到错误,其中输出的维度与输入的维度不同。我不明白20来自哪里。我的所有维度似乎都显示为18.另外,convolution2d_70的输出形状只是表示多个'所以我不确定这意味着什么。有什么想法吗?

异常:检查模型目标时出错:预期卷积2d_70具有形状(无,1,36L,20L)但是具有形状的阵列(49L,1L,36L,18L) < /强>

from keras.layers import Dense, Input, Convolution2D, MaxPooling2D, UpSampling2D
from keras.models import Model

from os import listdir
import os.path
import numpy as np
import re

versions = !pip freeze
for line in versions:
    if re.search('Keras', line):
          print line


samples = 100
x = np.ndarray([samples,36,18])
i=0

for i in range(samples):
    x[i] = np.random.randint(15, size=(36, 18))
    i+=1

#TODO: CREATE A REAL TEST SET
x_train = x[:49]
x_test = x[50:]
print x_train.shape
print x_test.shape

#INPUT LAYER
input_img = Input(shape=(1,x_train.shape[1],x_train.shape[2]))

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)

#MODEL
autoencoder = Model(input=input_img, output=decoded)

#SEPERATE ENCODER MODEL
encoder = Model(input=input_img, output=encoded)

# create a placeholder for an encoded (32-dimensional) input
encoded_input = Input(shape=(8, 4, 4))

# retrieve the last layer of the autoencoder model
decoder_layer1 = autoencoder.layers[-3]
decoder_layer2 = autoencoder.layers[-2]
decoder_layer3 = autoencoder.layers[-1]

print decoder_layer3.get_config()

# create the decoder model
decoder = Model(input=encoded_input, output=decoder_layer3(decoder_layer2(decoder_layer1(encoded_input))))

#COMPILER
autoencoder.compile(optimizer='adadelta', loss='binary_crossentropy')

autoencoder.summary()

x_train = x_train.astype('float32') / 15.
x_test = x_test.astype('float32') / 15.
x_test = np.reshape(x_test, (len(x_test), 1, x_test.shape[1], x_test.shape[2]))
x_train = np.reshape(x_train, (len(x_train), 1, x_train.shape[1], x_train.shape[2]))

autoencoder.fit(x_train,
               x_train,
                nb_epoch=5,
                batch_size=1,
                verbose=True,
                shuffle=True,
                validation_data=(x_test,x_test))

1 个答案:

答案 0 :(得分:1)

请注意,在解码器中的第三个卷积层中,您缺少border_mode='same'。这会使您的维度不匹配。