训练神经网络时如何打印出两种不同的损失?

时间:2020-04-01 02:24:06

标签: python tensorflow keras deep-learning neural-network

我有两个不同的损失。一个是reconstruction_loss,另一个是kl_loss

vae_loss是合并损失reconstruction_loss + kl_loss

训练模型时如何打印出两个损失(reconstruction_losskl_loss)?

我的代码在这里

reconstruction_loss = mse(inputs, outputs)
kl_loss = 1 + z_log_var_encoded - K.square(z_mean_encoded) - K.exp(z_log_var_encoded)
kl_loss = K.sum(kl_loss, axis=-1)
kl_loss *= -0.5
kl_loss_metric = kl_loss
kl_loss *= beta
vae_loss = K.mean(reconstruction_loss + kl_loss)

vae.add_loss(vae_loss)

opt = tf.keras.optimizers.Adam(lr=0.001)
vae.compile(optimizer=opt, metrics=['mse'])
history = vae.fit(x_trn, epochs=epochs, batch_size=batch_size, validation_data=(x_val, None))

我所能看到的就是

Epoch 1/1000
15348/15348 [==============================] - 0s 26us/step - loss: 40.5305 - val_loss: 5.1290
Epoch 2/1000
15348/15348 [==============================] - 0s 20us/step - loss: 4.4478 - val_loss: 3.7231
Epoch 3/1000
15348/15348 [==============================] - 0s 19us/step - loss: 3.3014 - val_loss: 2.9859
Epoch 4/1000
15348/15348 [==============================] - 0s 19us/step - loss: 2.8866 - val_loss: 2.6053
Epoch 5/1000
15348/15348 [==============================] - 0s 20us/step - loss: 2.7549 - val_loss: 2.4140
Epoch 6/1000
15348/15348 [==============================] - 0s 20us/step - loss: 2.2844 - val_loss: 2.2147

0 个答案:

没有答案