如何修复Conv1D自动编码器“期望具有3维...”

时间:2019-04-12 08:22:33

标签: keras deep-learning conv-neural-network autoencoder

我想将此代码用作自动编码器:

# ENCODER
input_sig = Input(batch_shape=(None,1389,1))
x = Conv1D(64,3, activation='relu', padding='valid')(input_sig)
x1 = MaxPooling1D(2)(x)
x2 = Conv1D(32,3, activation='relu', padding='valid')(x1)
x3 = MaxPooling1D(2)(x2)
flat = Flatten()(x3)
encoded = Dense(32,activation = 'relu')(flat)
#encoded = Reshape((32,1))(encoded)

print("shape of encoded {}".format(K.int_shape(encoded)))

# DECODER 
x2_ = Conv1D(32, 3, activation='relu', padding='valid')(x3)
x1_ = UpSampling1D(2)(x2_)
x_ = Conv1D(64, 3, activation='relu', padding='valid')(x1_)
upsamp = UpSampling1D(2)(x_)
flat = Flatten()(upsamp)
decoded = Dense(1389,activation = 'relu')(flat)
decoded = Reshape((1389,))(decoded)

print("shape of decoded {}".format(K.int_shape(decoded)))

autoencoder = Model(input_sig, decoded)
autoencoder.compile(optimizer='adam', loss='mse', metrics=['accuracy'])    

如您所见,编码的形状是(?,32)和解码的形状(?,1389)。

我的training_data的形状为(141,1389)。

通过执行以下代码

autoencoder.fit(X_train, X_train,
                epochs=150,
                batch_size=256,
                shuffle=True,
                validation_data=(X_test, X_test))

我得到了错误:ValueError:检查输入时出错:预期input_15具有3个维,但是数组的形状为(141,1389)

您能解决这个问题吗?

1 个答案:

答案 0 :(得分:1)

使用:

X_train = np.expand_dims(X_train, axis=2)

将训练数据扩展到第三维。

希望有帮助!