【问题标题】:Keras: test, cross validation and accuracy while processing batched data with train_on_batchKeras:在使用 train_on_batch 处理批处理数据时进行测试、交叉验证和准确性
【发布时间】:2017-10-28 18:38:05
【问题描述】:

有人能指出一个完整的例子吗?

  • 使用train_on_batch() 将批处理(和腌制)数据放入循环中
  • 为验证目的保留每批次的数据
  • 在处理完所有个批次后留出测试数据以进行准确性评估(请参阅下面我示例的最后一行)。

我在互联网上找到了很多 1 - 5 行代码 sn-ps,说明如何调用 train_on_batch()fit_generator(),但到目前为止,还没有任何内容能清楚说明如何分离和处理验证和测试数据在使用train_on_batch()时。

F。 Chollet 的出色示例 Cifar10_cnn (https://github.com/fchollet/keras/blob/master/examples/cifar10_cnn.py) 并未说明我上面列出的所有要点。

您可以说,“嘿,处理测试数据是您的问题。手动操作。”美好的!但我不明白这些例程做得好到什么程度,甚至不知道这是否有必要。它们大多是黑匣子,据我所知,它们在后台自动处理验证和测试数据。我希望更完整的示例能够消除混淆。

例如,在下面的示例中,我从 pickle 文件中迭代地读取批次,我将如何修改对 train_on_batch 的调用以处理 validation_data?以及如何留出测试数据(test_xtest_y)以便在算法结束时评估准确性?

while 1:
    try:
        batch = np.array(pickle.load(fvecs))
        polarities = np.array(pickle.load(fpols)) 

        # Divide a batch of 1000 documents (movie reviews) into:
        # 800 rows of training data, and
        # 200 rows of test (validation?) data
        train_x, val_x, train_y, val_y = train_test_split(batch, polarities, test_size=0.2)

        doc_size = 30
        x_batch = pad_sequences(train_x, maxlen=doc_size)
        y_batch = train_y

        # Fit the model 
        model.train_on_batch(x_batch, y_batch)
        # model.fit(train_x, train_y, validation_data=(val_x, val_y), epochs=2, batch_size=800, verbose=2)

    except EOFError:
        print("EOF detected.")
        break

# Final evaluation of the model
scores = model.evaluate(test_x, test_y, verbose=0)
print("Accuracy: %.2f%%" % (scores[1] * 100))

【问题讨论】:

    标签: deep-learning keras batch-processing metrics


    【解决方案1】:

    我无法为您提供完整的示例,但正如您所见 here 您同时拥有 train_on_batch 和 test_on_batch 这应该表明“train_on_batch”函数应该只训练模型而不是测试它。

    为了更加确定您可以在代码 itself 中看到该函数使用整个批次进行训练,并且没有用于测试/验证。

    为方便起见,我在下面引用相关代码:

    def train_on_batch(self, x, y,
                       sample_weight=None,
                       class_weight=None):
        """Runs a single gradient update on a single batch of data.
        # Arguments
            x: Numpy array of training data,
                or list of Numpy arrays if the model has multiple inputs.
                If all inputs in the model are named,
                you can also pass a dictionary
                mapping input names to Numpy arrays.
            y: Numpy array of target data,
                or list of Numpy arrays if the model has multiple outputs.
                If all outputs in the model are named,
                you can also pass a dictionary
                mapping output names to Numpy arrays.
            sample_weight: Optional array of the same length as x, containing
                weights to apply to the model's loss for each sample.
                In the case of temporal data, you can pass a 2D array
                with shape (samples, sequence_length),
                to apply a different weight to every timestep of every sample.
                In this case you should make sure to specify
                sample_weight_mode="temporal" in compile().
            class_weight: Optional dictionary mapping
                class indices (integers) to
                a weight (float) to apply to the model's loss for the samples
                from this class during training.
                This can be useful to tell the model to "pay more attention" to
                samples from an under-represented class.
        # Returns
            Scalar training loss
            (if the model has a single output and no metrics)
            or list of scalars (if the model has multiple outputs
            and/or metrics). The attribute `model.metrics_names` will give you
            the display labels for the scalar outputs.
        """
        x, y, sample_weights = self._standardize_user_data(
            x, y,
            sample_weight=sample_weight,
            class_weight=class_weight,
            check_batch_axis=True)
        if self.uses_learning_phase and not isinstance(K.learning_phase(), int):
            ins = x + y + sample_weights + [1.]
        else:
            ins = x + y + sample_weights
        self._make_train_function()
        outputs = self.train_function(ins)
        if len(outputs) == 1:
            return outputs[0]
        return outputs
    

    【讨论】:

    • 感谢您抽出宝贵时间。你对test_on_batch 的评价是有道理的。 (交叉)验证怎么样?这通常与训练和测试分开进行。此外,您如何在批次级别对准确性进行有意义的测试?测试一个模型的准确性是没有意义的,比如说,半训练的,因为准确性指标的全部目的是确定训练是否有效。换句话说,如何在从庞大数据集中一点一点读取数据的算法中实现准确度指标?
    • 通常您会希望跟踪您的测试准确性以检查过度/欠拟合和其他相关问题。在中期这样做肯定很奇怪,你通常会在每个纪元结束时这样做 - 如果你的测试集也很大,你可以使用 test_on_batch 。在某些情况下(如果您的 epoch 很长,某些 GAN 算法或其他边缘情况),您可能想在中期测试您的模型,但我不记得曾经见过它完成。
    • @ginge 你能解释一下我们如何在验证集也很大的情况下使用 test_on_batch 在每个 epoch 结束时测试准确性吗?换句话说,如何使用小批量实现准确度指标?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-21
    • 2019-03-11
    • 2018-08-17
    • 2020-11-07
    • 2017-05-04
    相关资源
    最近更新 更多