Keras 在第一个纪元结束时显示形状错误
Keras shows shape error at the end of first epoch
我尝试用 keras 创建一个 LSTM 自动编码器
同时,它在第一个纪元结束时显示值错误
ValueError: operands could not be broadcast together with shapes (32,20) (20,20) (32,20)
模型输入的shape为(sample_size,20,31),下面是模型
采样函数:
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
编码器部分:
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')
解码器部分:
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)
以及损失和拟合部分:
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)
会导致这个错误:
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)
如果存在形状错误,剂量模型在上一步中是如何工作的。这是我的主要问题,感谢您的回答
您正在使用 32 的批量大小,但在最后您的操作数得到一个只有 20 个元素的张量,因为此金额从 632276 保留到 632256 之后:
632276 - 632256 = 20
基本上这是有关的错误消息,这就是前面步骤起作用的原因。
最直接的解决方案:
使用fit()方法的steps_per_epoch
选项:
steps_per_epoch: Integer or None.
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.
steps_per_epoch = total_samples // batch_size
在这种情况下,基本上您丢弃了最后 20 个样本。
我尝试用 keras 创建一个 LSTM 自动编码器
同时,它在第一个纪元结束时显示值错误
ValueError: operands could not be broadcast together with shapes (32,20) (20,20) (32,20)
模型输入的shape为(sample_size,20,31),下面是模型
采样函数:
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
编码器部分:
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')
解码器部分:
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)
以及损失和拟合部分:
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)
会导致这个错误:
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)
如果存在形状错误,剂量模型在上一步中是如何工作的。这是我的主要问题,感谢您的回答
您正在使用 32 的批量大小,但在最后您的操作数得到一个只有 20 个元素的张量,因为此金额从 632276 保留到 632256 之后:
632276 - 632256 = 20
基本上这是有关的错误消息,这就是前面步骤起作用的原因。
最直接的解决方案:
使用fit()方法的steps_per_epoch
选项:
steps_per_epoch: Integer or None.
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.
steps_per_epoch = total_samples // batch_size
在这种情况下,基本上您丢弃了最后 20 个样本。