python - 如何在 Tensorflow 训练之间进行评估

标签 python tensorflow machine-learning tensorboard

根据我对机器学习的理解(虽然我对此很陌生),模型的评估需要在训练过程中进行。这将避免过度拟合或减少错误预测的可能性。

我尝试修改 Tensorflow 官方网站提供的鲍鱼示例以适合我的项目,但我发现代码仅在模型训练完成后进行一次评估。

这对我来说很奇怪,因为只有一次评估似乎使“评估阶段”毫无用处。换句话说,如果训练已经完成了,评估还有什么用呢?它无助于建立一个更好的模型,不是吗?

这是我的代码的一部分:

 nn = tf.estimator.Estimator(model_fn=model_fn, params=model_params, model_dir='/tmp/nmos_self_define')

  train_input_fn = tf.estimator.inputs.numpy_input_fn(
      x={"x": train_features_numpy},
      y=train_labels_numpy,
      batch_size = 1,
      num_epochs= 1,
      shuffle=True)

  # Train
  nn.train(input_fn=train_input_fn)

  test_input_fn = tf.estimator.inputs.numpy_input_fn(
      x={"x": test_features_numpy},
      y=test_labels_numpy,
      batch_size = 1,
      num_epochs= 1,
      shuffle=False)

  ev = nn.evaluate(input_fn=test_input_fn)
  print("Loss: %s" % ev["loss"])
  print("Root Mean Squared Error: %s" % ev["rmse"])    

而通过Tensorboard可视化的训练结果为: enter image description here

如您所见,训练结束时仅进行了一次评估(蓝点)

虽然我不确定损失没有减少是否是因为缺乏评估,但我想知道如何操作代码以便在训练期间执行评估过程。

感谢您花时间阅读这个问题,我很乐意在概念上和代码方面对此进行讨论

最佳答案

Evaluation of a model needs to be done during the training process. This would avoid overfitting or reduce the possibility of bad predictions.

就其本身而言,训练时评估不会执行任何操作,但它允许运算符(operator)跟踪模型在以前从未见过的数据上的性能。然后您可以相应地调整超参数(例如学习率或正则化因子)。

I found out that the code only do evaluation ONCE after model training is done.

您提供的代码片段仅在训练一个周期后就运行评估。您应该对模型进行多个时期的训练,以获得更好的性能。

顺便说一句,您应该创建我们所说的“验证集”,它是训练数据的一小部分,算法不会在其上进行训练来进行训练评估。使用您当前的方法,您可能会过度拟合您的测试集。测试集只应很少使用来评估模型的真实泛化能力。

关于python - 如何在 Tensorflow 训练之间进行评估,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51108486/

相关文章:

python - 区分 `colorbar` 中的截取值

python - 损失函数 Keras out_dim > 1

python - TensorFlow 中 LSTMCell 单元中的权重是如何初始化的

machine-learning - 不确定tensorflow-gpu是否真的使用GPU

machine-learning - 如何将数据集拆分为训练集和验证集

python - 为什么我无法获得 Python 迭代器的倒数第二个结果?

python - 在多处理或多线程应用程序中保留 cpu 时间

python - 如何将 MultiIndex 转换并 reshape 为 3D Numpy 数组?

python - Keras 中 sigmoid 激活函数的使用

python-3.x - 尝试使用 Keras 中的 model.predict() 时尺寸错误