获取keras中的ValueError,原因未知

2024-06-23 19:42:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在运行以下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)

很明显,我没有对shape(20,20,1)设置任何内容。我的程序怎么了


Tags: inputtrainprofileactivationkerasrelusameshape
1条回答
网友
1楼 · 发布于 2024-06-23 19:42:29

输入的每个维度的长度都是奇数。这使得MaxPooling2D层使用floor操作符对张量进行向下采样

在使用Input(shape=(23, 23, 1))的模型中,每个MaxPooling2D层之后张量的维数变化将为23 to 11{}

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

相关问题 更多 >

    热门问题