Tensorflow 和 Keras 中的多重损失
Multiple losses in Tensorflow and Keras
在我的简单变分自动编码器代码中,我想在模型 运行ning 时查看重建和 KL 散度损失值。我使用了 Keras 文档页面中的 Example of VAE on MNIST dataset using MLP,并将损失部分修改如下:
reconstruction_loss = original_dim*binary_crossentropy(inputs,outputs)
kl_loss = -0.5*K.sum(1 + z_log_var - K.square(z_mean) - K.exp(z_log_var), axis=-1)
vae_loss = K.mean(reconstruction_loss + kl_loss)
losses = { 'recon_loss': reconstruction_loss,
'latent_loss': kl_loss,
'total_loss': vae_loss,}
vae.add_loss(losses)
vae.compile(optimizer='adam')
history = vae.fit(x_train, epochs=epochs, batch_size=batch_size,
validation_data=(x_test, None))
但是当我运行模型时,编译命令returns这个错误信息:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-21-b668ab0f8437> in <module>
----> 1 vae.compile(optimizer='adam')
2
~\Anaconda3\lib\site-packages\keras\engine\training.py in compile(self, optimizer, loss, metrics, loss_weights, sample_weight_mode, weighted_metrics, target_tensors, **kwargs)
349 total_loss += loss_weight * output_loss
350 if total_loss is None:
--> 351 if not self.losses:
352 raise ValueError('The model cannot be compiled '
353 'because it has no loss to optimize.')
~\Anaconda3\lib\site-packages\keras\engine\network.py in losses(self)
423
424 unique_tensors = list(
--> 425 set(x for x in losses if not isinstance(x, (float, int))))
426 non_tensors = [x for x in losses if isinstance(x, (float, int))]
427 return unique_tensors + non_tensors
TypeError: unhashable type: 'dict'
我看到了一些关于 TypeError: unhashable type: 'dict' 的其他问题,但我无法解决我的问题问题。有没有想法在 Keras 中采用 Compile 命令并处理多个损失?
请在您的损失字典中检查每项损失的类型。
print (type(losses['recon_loss']))
我修改了损失函数并在编译函数中使用了指标。
def recon_loss(inputs,outputs):
reconstruction_loss = original_dim*binary_crossentropy(inputs,outputs)
return K.mean(reconstruction_loss)
def latent_loss(inputs,outputs):
kl_loss = -0.5*K.sum(1 + z_log_var - K.square(z_mean) - K.exp(z_log_var), axis=-1)
return K.mean(kl_loss)
def total_loss(inputs,outputs):
reconstruction_loss = original_dim*binary_crossentropy(inputs,outputs)
kl_loss = -0.5*K.sum(1 + z_log_var - K.square(z_mean) - K.exp(z_log_var), axis=-1)
return K.mean(reconstruction_loss + kl_loss)
vae.compile(optimizer='adam',loss=total_loss,metrics=[recon_loss, latent_loss])
现在,模型 returns 训练和验证数据集的重建、潜在损失和总损失。
在我的简单变分自动编码器代码中,我想在模型 运行ning 时查看重建和 KL 散度损失值。我使用了 Keras 文档页面中的 Example of VAE on MNIST dataset using MLP,并将损失部分修改如下:
reconstruction_loss = original_dim*binary_crossentropy(inputs,outputs)
kl_loss = -0.5*K.sum(1 + z_log_var - K.square(z_mean) - K.exp(z_log_var), axis=-1)
vae_loss = K.mean(reconstruction_loss + kl_loss)
losses = { 'recon_loss': reconstruction_loss,
'latent_loss': kl_loss,
'total_loss': vae_loss,}
vae.add_loss(losses)
vae.compile(optimizer='adam')
history = vae.fit(x_train, epochs=epochs, batch_size=batch_size,
validation_data=(x_test, None))
但是当我运行模型时,编译命令returns这个错误信息:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-21-b668ab0f8437> in <module>
----> 1 vae.compile(optimizer='adam')
2
~\Anaconda3\lib\site-packages\keras\engine\training.py in compile(self, optimizer, loss, metrics, loss_weights, sample_weight_mode, weighted_metrics, target_tensors, **kwargs)
349 total_loss += loss_weight * output_loss
350 if total_loss is None:
--> 351 if not self.losses:
352 raise ValueError('The model cannot be compiled '
353 'because it has no loss to optimize.')
~\Anaconda3\lib\site-packages\keras\engine\network.py in losses(self)
423
424 unique_tensors = list(
--> 425 set(x for x in losses if not isinstance(x, (float, int))))
426 non_tensors = [x for x in losses if isinstance(x, (float, int))]
427 return unique_tensors + non_tensors
TypeError: unhashable type: 'dict'
我看到了一些关于 TypeError: unhashable type: 'dict' 的其他问题,但我无法解决我的问题问题。有没有想法在 Keras 中采用 Compile 命令并处理多个损失?
请在您的损失字典中检查每项损失的类型。
print (type(losses['recon_loss']))
我修改了损失函数并在编译函数中使用了指标。
def recon_loss(inputs,outputs):
reconstruction_loss = original_dim*binary_crossentropy(inputs,outputs)
return K.mean(reconstruction_loss)
def latent_loss(inputs,outputs):
kl_loss = -0.5*K.sum(1 + z_log_var - K.square(z_mean) - K.exp(z_log_var), axis=-1)
return K.mean(kl_loss)
def total_loss(inputs,outputs):
reconstruction_loss = original_dim*binary_crossentropy(inputs,outputs)
kl_loss = -0.5*K.sum(1 + z_log_var - K.square(z_mean) - K.exp(z_log_var), axis=-1)
return K.mean(reconstruction_loss + kl_loss)
vae.compile(optimizer='adam',loss=total_loss,metrics=[recon_loss, latent_loss])
现在,模型 returns 训练和验证数据集的重建、潜在损失和总损失。