我正在尝试使用Keras对时间序列进行重建的LSTM-VAE模型。
我参考了https://github.com/twairball/keras_lstm_vae/blob/master/lstm_vae/vae.py和https://machinelearningmastery.com/lstm-autoencoders/来创建LSTM-VAE架构。
我在训练网络时遇到了麻烦,我在eager执行模式下训练时得到以下错误:
InvalidArgumentError: Incompatible shapes: [8,1] vs. [32,1] [Op:Mul]
输入形状为(7752,30,1)
,其中30个时间步长和1个特征。
模型编码器:
# encoder
latent_dim = 1
inter_dim = 32
#sample,timesteps, features
input_x = keras.layers.Input(shape= (X_train.shape[1], X_train.shape[2]))
#intermediate dimension
h = keras.layers.LSTM(inter_dim)(input_x)
#z_layer
z_mean = keras.layers.Dense(latent_dim)(h)
z_log_sigma = keras.layers.Dense(latent_dim)(h)
z = Lambda(sampling)([z_mean, z_log_sigma])
模型解码器:
# Reconstruction decoder
decoder1 = RepeatVector(X_train.shape[1])(z)
decoder1 = keras.layers.LSTM(100, activation='relu', return_sequences=True)(decoder1)
decoder1 = keras.layers.TimeDistributed(Dense(1))(decoder1)
抽样函数:
batch_size = 32
def sampling(args):
z_mean, z_log_sigma = args
epsilon = K.random_normal(shape=(batch_size, latent_dim),mean=0., stddev=1.)
return z_mean + z_log_sigma * epsilon
VAE损失函数:
def vae_loss2(input_x, decoder1):
""" Calculate loss = reconstruction loss + KL loss for each data in minibatch """
# E[log P(X|z)]
recon = K.sum(K.binary_crossentropy(input_x, decoder1), axis=1)
# D_KL(Q(z|X) || P(z|X)); calculate in closed form as both dist. are Gaussian
kl = 0.5 * K.sum(K.exp(z_log_sigma) + K.square(z_mean) - 1. - z_log_sigma, axis=1)
return recon + kl
有没有建议可以使模型工作得更好?