简体   繁体   English

Keras 在第一个纪元结束时显示形状错误

[英]Keras shows shape error at the end of first epoch

I try to create an LSTM autoencoder with keras我尝试用 keras 创建一个 LSTM 自动编码器

While, it shows a value error at the end of first epoch同时,它在第一个纪元结束时显示值错误

ValueError: operands could not be broadcast together with shapes (32,20) (20,20) (32,20) 

The shape of model input is (sample_size,20,31), and following is the model模型输入的shape为(sample_size,20,31),下面是模型

The sampling function:采样函数:

def sampling(args):

    z_mean, z_log_var = args
    batch = K.shape(z_mean)[0]
    dim = K.int_shape(z_mean)[1]
    # by default, random_normal has mean=0 and std=1.0
    epsilon = K.random_normal(shape=(batch,dim))
    return z_mean + K.exp(0.5 * z_log_var) * epsilon 

The encoder part:编码器部分:

inputs = Input(shape=(lag,data.shape[1],), name='encoder_input')
x = LSTM(30,activation='relu',return_sequences=True) (inputs)
x = LSTM(60,activation='relu') (x)
z_mean = Dense(60, name='z_mean')(x)
z_log_var = Dense(60, name='z_log_var')(x)
z_temp = Lambda(sampling, output_shape=(60,), name='z')([z_mean, z_log_var])
z = RepeatVector(lag)(z_temp)
encoder = Model(inputs, [z_mean, z_log_var, z], name='encoder')

The decoder part:解码器部分:

latent_inputs = Input(shape=(lag,60), name='z_sampling')
x_2 = LSTM(60, activation='relu',return_sequences= True)(latent_inputs)
x_2 = LSTM(data.shape[1], activation='relu',return_sequences= True)(x_2)
decoder = Model(latent_inputs, x_2, name='decoder')
outputs = decoder(encoder(inputs)[2])
vae = Model(inputs, outputs)

And loss and fit part:以及损失和拟合部分:

outputs = decoder(encoder(inputs)[2])
vae = Model(inputs, outputs)
reconstruction_loss = mse(inputs, outputs)
kl_loss = 1 + z_log_var - K.square(z_mean) - K.exp(z_log_var)
kl_loss = K.mean(kl_loss)
kl_loss *= -0.1
vae_loss = reconstruction_loss + kl_loss
vae.add_loss(vae_loss) 
vae.compile(optimizer='adam')
vae.fit(train,epochs=100)

It will cause this error:它会导致这个错误:

Epoch 1/100
632256/632276 [============================>.] - ETA: 0s - loss: 0.0372
ValueError: operands could not be broadcast together with shapes (32,20) (20,20) (32,20) 

If there is a shape error, how dose model work at previous step.如果存在形状错误,剂量模型在上一步如何工作。 That's my main problem, thanks for your answer这是我的主要问题,谢谢你的回答

You are working with batch size of 32 , but at the very end your operand get a tensor with only 20 elements, because this amount remains after 632256 from 632276:您正在使用32的批量大小,但在最后您的操作数得到一个只有20 个元素的张量,因为这个数量在 632256 从 632276 之后仍然存在:

632276 - 632256 = 20

Basically this is the error message about and that is why the previous steps worked.基本上这是有关的错误消息,这就是前面步骤起作用的原因。

Most straightforward solution:最直接的解决方案:

use fit() method's steps_per_epoch option:使用fit()方法的steps_per_epoch选项:

steps_per_epoch: Integer or None. steps_per_epoch:整数或无。
Total number of steps (batches of samples) before declaring one epoch finished and starting the next epoch.在宣布一个纪元结束并开始下一个纪元之前的总步骤数(样本批次)。 When training with input tensors such as TensorFlow data tensors, the default None is equal to the number of samples in your dataset divided by the batch size, or 1 if that cannot be determined.使用 TensorFlow 数据张量等输入张量进行训练时,默认 None 等于数据集中的样本数除以批量大小,如果无法确定则为 1。

steps_per_epoch = total_samples // batch_size

In this case basically you drop the last 20 samples.在这种情况下,基本上您会丢弃最后 20 个样本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM