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

标签 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))

最佳答案

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

关于theano - 与 CNN 的输入相比,Keras 的输出层形状不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37955953/

相关文章:

machine-learning - Keras 模型预测数列

python - Scikit-Learn 与 Keras (Tensorflow) 用于多项逻辑回归

python - 从theano中给定的pmf中选择一个数字

opencl - 在AMD Radeon上尝试使用OpenCL + Theano时出现 "pygpu was configured but could not be imported"错误

python - nvcc 致命 : Value 'sm_61' is not defined for option 'gpu-architecture' error with theano

tensorflow - 如何从 GAN 训练生成器?

python - 将类标签附加到 Keras 模型

python - Keras:找出层数

ubuntu - 让 THEANO 与 GPU ubuntu 14.04 一起工作的问题

tensorflow - 不可训练参数的定义是什么?