tf.estimator.Estimator 在逐个时期与所有时期进行试验时给出不同的测试精度

tf.estimator.Estimator gives different test accuracy when trianed epoch by epoch vs over all epochs

我已经为 tf.estimator.Estimator 定义了一个简单的 CNN 作为我的 model_fn 并用这个 input_fn:

喂养它
def input_fn(features, labels, batch_size, epochs):
    dataset = tf.data.Dataset.from_tensor_slices((features))
    dataset = dataset.map(lambda x: tf.cond(tf.random_uniform([], 0, 1) > 0.5, lambda: dataset_augment(x), lambda: x),
                          num_parallel_calls=16).cache()
    dataset_labels = tf.data.Dataset.from_tensor_slices((labels))
    dataset = dataset.zip((dataset, dataset_labels))
    dataset = dataset.shuffle(30000)
    dataset = dataset.repeat(epochs)
    dataset = dataset.batch(batch_size)
    dataset = dataset.prefetch(-1)
    return dataset

当我以这种方式训练估算器时,在 10 个时期后我获得了 43% 的测试准确率:

steps_per_epoch = data_train.shape[0] // batch_size
for epoch in range(1, epochs + 1):
    cifar100_classifier.train(lambda: input_fn(data_train, labels_train, batch_size, epochs=1), steps=steps_per_epoch)

但是当我以这种方式训练它时,我在 10 个时期后获得了 32% 的测试准确率:

steps_per_epoch = data_train.shape[0] // batch_size
max_steps = epochs * steps_per_epoch
cifar100_classifier.train(steps=max_steps,
                              input_fn=lambda: input_fn(data_train, labels_train, batch_size, epochs=epochs))

我只是不明白为什么这两种方法会产生不同的结果。谁能解释一下?

你的模型的权重是随机初始化的吗?这可能是个案。

由于您在第一个示例中多次调用 input_fn ,因此您似乎会通过 dataset_augment(x) 生成更多增强数据,因为您正在为 coin-toss 进行增强每个 x 每个时代。

在第二个示例中,您只执行这些 coin-tosses 一次,然后在同一数据上训练多个时期。所以这里你的训练集实际上是“更小”的。

在第一个示例中,.cache() 并没有真正帮助您解决这个问题。