如何在Keras中实现矢量数据的一维卷积自动编码器?

时间:2018-03-15 03:03:33

标签: keras convolution autoencoder

我对自动编码器的输入向量大小为128.我总共有730个样本(730x128)。我正在尝试使用1D CNN自动编码器。我想稍后使用隐藏层作为我的新的低维表示。 我的代码现在运行,但我的解码输出甚至不接近原始输入。 这是代码:

input_sig = Input(batch_shape=(None,128,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)

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

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(128,activation = 'relu')(flat)
decoded = Reshape((128,1))(decoded)

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

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

然后 - >自动编码器的输入。 (730,128,1) 但是当我根据解码绘制原始信号时,它们是非常不同的! 感谢您对此的帮助。

1 个答案:

答案 0 :(得分:0)

您需要有一个具有“ Sigmoid”激活功能的单通道卷积层,才能重建解码图像。 看下面的例子。您可以使用loss ='mse'和optimizer ='adam'

进行编译
input_sig = Input(batch_shape=(1,128,1))
x = Conv1D(8,3, activation='relu', padding='same',dilation_rate=2)(input_sig)
x1 = MaxPooling1D(2)(x)
x2 = Conv1D(4,3, activation='relu', padding='same',dilation_rate=2)(x1)
x3 = MaxPooling1D(2)(x2)
x4 = AveragePooling1D()(x3)
flat = Flatten()(x4)
encoded = Dense(2)(flat)
d1 = Dense(64)(encoded)
d2 = Reshape((16,4))(d1)
d3 = Conv1D(4,1,strides=1, activation='relu', padding='same')(d2)
d4 = UpSampling1D(2)(d3)
d5 = Conv1D(8,1,strides=1, activation='relu', padding='same')(d4)
d6 = UpSampling1D(2)(d5)
d7 = UpSampling1D(2)(d6)
decoded = Conv1D(1,1,strides=1, activation='sigmoid', padding='same')(d7)
model= Model(input_sig, decoded)