python - 如何修改 train_step 以便在调用 model.fit() 时支持验证集?

标签 python keras tensorflow2.0

我正在学习这个 Keras 教程,该教程解释了如何编写您的自定义 train_step() 函数,同时仍然能够调用 model.fit() 来训练您的模型:

https://keras.io/guides/customizing_what_happens_in_fit/

model.fit() 应该能够支持 validation_data 但我不知道在哪里编写代码来计算 validation_data 的自定义指标和自定义损失。 我决定编写一个自定义循环,但我想使用 fit。

有什么想法吗?

最佳答案

我完全错过了指南中提到函数 test_step() 的段落:

def test_step(self, data):
    # Unpack the data
    x, y = data
    # Compute predictions
    y_pred = self(x, training=False)
    # Updates the metrics tracking the loss
    self.compiled_loss(y, y_pred, regularization_losses=self.losses)
    # Update the metrics.
    self.compiled_metrics.update_state(y, y_pred)
    # Return a dict mapping metric names to current value.
    # Note that it will include the loss (tracked in self.metrics).
    return {m.name: m.result() for m in self.metrics}

关于python - 如何修改 train_step 以便在调用 model.fit() 时支持验证集?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63508441/

相关文章:

python - 无法在 Python 图像库(枕头)上加载字体

python - 从瓶颈特征训练密集层与卡住除最后一层以外的所有层——应该是一样的,但它们的行为不同

youtube - 使用 Keras 对 Youtube 评论使用 LSTM 进行情绪分析

python - 将 TensorFlow 张量转换为 Numpy 数组

python - 分解 Pandas 数据框的列

python - 有没有办法更改 AWS lambda 函数的 'scratch' (/tmp) 空间位置?

amazon-web-services - Sagemaker 模型训练中设备上没有剩余空间

python - 值错误 : Cannot reshape a tensor (BERT - transfer learning)

python - 如何在 TensorFlow 中获取对数概率?

python - 没有模块名称pyspark错误