用于时间序列异常检测的 Keras LSTM-VAE(变分自动编码器)
Keras LSTM-VAE (Variational Autoencoder) for time-series anamoly detection
我正在尝试使用 Keras 为时间序列重建建模 LSTM-VAE。
我曾参考 https://github.com/twairball/keras_lstm_vae/blob/master/lstm_vae/vae.py and https://machinelearningmastery.com/lstm-autoencoders/ 创建 LSTM-VAE 架构。
我在训练网络时遇到问题,在急切执行模式下训练时出现以下错误:
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
LSTM-VAE model architecture
有什么建议可以使模型正常工作吗?
你需要推断采样函数中的 batch_dim 并且你需要注意你的损失......你的损失函数使用前几层的输出所以你需要注意这一点。我使用 model.add_loss(...)
实现这个
# encoder
latent_dim = 1
inter_dim = 32
timesteps, features = 100, 1
def sampling(args):
z_mean, z_log_sigma = args
batch_size = tf.shape(z_mean)[0] # <================
epsilon = K.random_normal(shape=(batch_size, latent_dim), mean=0., stddev=1.)
return z_mean + z_log_sigma * epsilon
# timesteps, features
input_x = Input(shape= (timesteps, features))
#intermediate dimension
h = LSTM(inter_dim, activation='relu')(input_x)
#z_layer
z_mean = Dense(latent_dim)(h)
z_log_sigma = Dense(latent_dim)(h)
z = Lambda(sampling)([z_mean, z_log_sigma])
# Reconstruction decoder
decoder1 = RepeatVector(timesteps)(z)
decoder1 = LSTM(inter_dim, activation='relu', return_sequences=True)(decoder1)
decoder1 = TimeDistributed(Dense(features))(decoder1)
def vae_loss2(input_x, decoder1, z_log_sigma, z_mean):
""" 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))
# 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)
return recon + kl
m = Model(input_x, decoder1)
m.add_loss(vae_loss2(input_x, decoder1, z_log_sigma, z_mean)) #<===========
m.compile(loss=None, optimizer='adam')
我正在尝试使用 Keras 为时间序列重建建模 LSTM-VAE。
我曾参考 https://github.com/twairball/keras_lstm_vae/blob/master/lstm_vae/vae.py and https://machinelearningmastery.com/lstm-autoencoders/ 创建 LSTM-VAE 架构。
我在训练网络时遇到问题,在急切执行模式下训练时出现以下错误:
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
LSTM-VAE model architecture
有什么建议可以使模型正常工作吗?
你需要推断采样函数中的 batch_dim 并且你需要注意你的损失......你的损失函数使用前几层的输出所以你需要注意这一点。我使用 model.add_loss(...)
# encoder
latent_dim = 1
inter_dim = 32
timesteps, features = 100, 1
def sampling(args):
z_mean, z_log_sigma = args
batch_size = tf.shape(z_mean)[0] # <================
epsilon = K.random_normal(shape=(batch_size, latent_dim), mean=0., stddev=1.)
return z_mean + z_log_sigma * epsilon
# timesteps, features
input_x = Input(shape= (timesteps, features))
#intermediate dimension
h = LSTM(inter_dim, activation='relu')(input_x)
#z_layer
z_mean = Dense(latent_dim)(h)
z_log_sigma = Dense(latent_dim)(h)
z = Lambda(sampling)([z_mean, z_log_sigma])
# Reconstruction decoder
decoder1 = RepeatVector(timesteps)(z)
decoder1 = LSTM(inter_dim, activation='relu', return_sequences=True)(decoder1)
decoder1 = TimeDistributed(Dense(features))(decoder1)
def vae_loss2(input_x, decoder1, z_log_sigma, z_mean):
""" 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))
# 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)
return recon + kl
m = Model(input_x, decoder1)
m.add_loss(vae_loss2(input_x, decoder1, z_log_sigma, z_mean)) #<===========
m.compile(loss=None, optimizer='adam')