由于未知原因在keras中获取ValueError

时间:2019-04-15 00:20:06

标签: python keras

我正在运行以下keras模型

input_profile = Input(shape=(23, 23, 1)) 
x = Conv2D(16, (3, 3), activation='relu', padding='same')(input_profile)
x = MaxPooling2D((2, 2), padding='same')(x)
x = Conv2D(8, (3, 3), activation='relu', padding='same')(x)
x = MaxPooling2D((2, 2), padding='same')(x)
x = Conv2D(8, (3, 3), activation='relu', padding='same')(x)
encoded = MaxPooling2D((2, 2), padding='same')(x)

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

x = Conv2D(8, (3, 3), activation='relu', padding='same')(encoded)
x = UpSampling2D((2, 2))(x)
x = Conv2D(8, (3, 3), activation='relu', padding='same')(x)
x = UpSampling2D((2, 2))(x)
x = Conv2D(16, (3, 3), activation='relu')(x)
x = UpSampling2D((2, 2))(x)
decoded = Conv2D(1, (3, 3), activation='sigmoid', padding='same')(x)

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

X_GTEx = np.load('GTEx_X_float64.npy')
x_train = X_GTEx
x_train = np.reshape(x_train, (5207, 23, 23, 1))
from keras.callbacks import TensorBoard

autoencoder.fit(x_train, x_train,\
                epochs=50, batch_size=127,\
                shuffle=True, validation_data=(x_train, x_train),\
                callbacks=[TensorBoard(log_dir='/tmp/autoencoder')])

运行它会给我以下错误:

ValueError: Error when checking target: expected conv2d_7 to have shape (20, 20, 1) but got array with shape (23, 23, 1)

很显然,我没有设置任何形状为(20,20,1)的东西。我的程序怎么了?

1 个答案:

答案 0 :(得分:0)

每个输入维的长度为奇数。这使得MaxPooling2D层使用floor运算符对张量进行下采样。

在具有Input(shape=(23, 23, 1))的模型中,每个MaxPooling2D层之后的张量的尺寸变化将为23 to 11 11 to 5

然后,两个UpSampling层将(5, 5, 1)张量上采样到(20, 20, 1),但是模型需要与输入形状相同的张量。

相关问题