Keras Sequential fit_generator 参数列表中validation_steps的含义 [英] Meaning of validation_steps in Keras Sequential fit_generator parameter list

查看:63
本文介绍了Keras Sequential fit_generator 参数列表中validation_steps的含义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Python 中使用带有 Tensorflow 后端的 Keras.更准确地说,tensorflow 1.2.1 及其内置的 contrib.keras 库.

I am using Keras with a Tensorflow backend in Python. To be more precise tensorflow 1.2.1 and its build-in contrib.keras lib.

我想使用 Sequential 模型对象的 fit_generator 方法,但我对应该作为方法参数传递的内容感到困惑.

I want to use the fit_generator method of a Sequential model object, but I am confused with what I should pass as the method-parameters.

通过阅读文档此处,我获得了以下信息:

From reading the doc here I got the following information:

  • generator :python 训练数据批量生成器;无限循环其训练数据
  • validation_data:-就我而言 - 一个 Python 验证数据批处理生成器;该文档没有提到对其验证数据的无限循环
  • steps_per_epoch : 训练批次数 = uniqueTrainingData/batchSize
  • 验证步骤 : ??? ;= uniqueValidationData/批量大小 ???
  • use_multiprocessing:布尔值;不要传递不可picklable的参数???
  • workers:最大使用进程数
  • generator : a python training data batch generator; endlessly looping over its training data
  • validation_data: -in my case - a python validation data batch generator; the doc doesn't mention endless looping over its validation data
  • steps_per_epoch : number of training batches = uniqueTrainingData / batchSize
  • validation steps : ??? ; = uniqueValidationData / batch size ???
  • use_multiprocessing : boolean; don't pass non picklable arguments ???
  • workers : max number of used processes

如上用 ???我真的不知道validation_steps 是什么意思.我知道上面链接的文档的定义(在每个时期结束时从验证生成器产生的步骤数),但这只会在给定的上下文中混淆我.从文档中我知道validation_data 生成器必须以(inputs, targets) 形式生成数据和标签元组.与上面的陈述相反,必须有多个在每个时期结束时从验证生成器产生的步骤",在这种情况下,这意味着在每个训练时期后将产生多个验证批次.

As indicated above with ??? I don't really know what validation_steps means. I know the definition of the above linked doc (Number of steps to yield from validation generator at the end of every epoch) but that only confuses my in the given context. From the doc i know that the validation_data generator has to yield data, label tuples in the form (inputs, targets). In contrast to that the above statement indicates that there have to be multiple "steps to yield from validation generator at the end of every epoch" which in this context would mean, that multiple validation batches would be yielded after each training epoch.

关于validation_steps的问题:

  • 这样真的有效吗?如果是这样:为什么?我认为在每个 epoch 之后,一个验证批次(理想情况下之前没有使用过)用于验证以确保训练得到验证,而不会冒着训练"模型在已经使用的验证集上表现更好的风险.
  • 在上一个问题的上下文中:为什么推荐的验证步骤数量是 uniqueValidationData/batches 而不是 uniqueValidationData/epochs?拥有例如不是更好吗?100 个 epoch 的 100 个验证批次,而不是 x 验证批次,其中 x 可能小于或大于指定的 epoch 数?或者:如果您的验证批次比 epoch 数量少得多,那么模型是否在没有验证的情况下对其余 epoch 进行了训练,或者验证集是否被重用/改组+重用?
  • 训练和验证批次具有相同的批次大小(红利trainingDataCount 和validationDataCount 的共享除数)是否重要?
  • Does it really work that way? If so: Why? I thought that after each epoch one validation batch, which ideally wasn't used before, is used for validation to ensure that the training gets validated without risking to "train" the model to perform better on already used validation sets.
  • In context of the previous question: Why is the recommended amount of validation steps uniqueValidationData / batches and not uniqueValidationData / epochs? Isn't it better to have e.g. 100 validation batches for 100 epochs instead of x validation batches where x could be less or more than the specified number of epochs? Alternatively: If you have much less validation batches than number of epoches, is the model trained without validation for the rest of the epochs or do validation sets get reused / reshuffled+reused?
  • Is it important that the training and validation batches have the same batch size (shared divisor of the dividends trainingDataCount and validationDataCount)?

关于 use_multiprocessing 的附加问题:

  • numpy 数组是否可以选择或者我是否必须将它们转换为多维列表?

推荐答案

验证生成器的工作原理与训练生成器完全一样.您可以定义每个 epoch 将使用的批次数量.

The validation generator works exactly like the training generator. You define how many batches it will wield per epoch.

  • 训练生成器将生成 steps_per_epoch 批次.
  • 当 epoch 结束时,验证生成器将生成 validation_steps 批次.
  • The training generator will yield steps_per_epoch batches.
  • When the epoch ends, the validation generator will yield validation_steps batches.

但是验证数据与训练数据完全没有关系.没有必要根据训练批次分开验证批次(我什至会说这样做没有意义,除非你有非常具体的意图).此外,训练数据中的样本总数与测试数据中的样本总数无关.

But validation data has absolutely no relation to training data. There is no need to separate validation batches according to training batches (I would even say that there is no point in doing that, unless you have a very specific intention). Also, the total number of samples in training data is not related to the total number of samples in test data.

多批次的目的只是为了节省计算机的内存,因此您可以一次测试一个较小的包.您可能会找到适合您的记忆或预期训练时间的批量大小并使用该大小.

The point of having many batches is just to spare your computer's memory, so you test smaller packs one at a time. Probably, you find a batch size that will fit your memory or expected training time and use that size.

也就是说,Keras 为您提供了一种完全免费的方法,因此您可以根据需要确定训练和验证批次.

That said, Keras gives you a totally free method, so you can determine the training and the validation batches as you wish.

理想情况下,您可以一次性使用所有验证数据.如果您仅使用部分验证数据,您将获得每个批次的不同指标,可能会让您认为模型变得更糟或更好,而实际上并没有,您只是测量了不同的验证集.

Ideally, you use all your validation data at once. If you use only part of your validation data, you will get different metrics for each batch, what may make you think that your model got worse or better when it actually didn't, you just measured different validation sets.

这就是为什么他们建议 validation_steps = total_validation_samples//validation_batch_size.
从理论上讲,您在每个时期测试整个数据,因为理论上您还应该在每个时期训练您的整个数据.

That's why they suggest validation_steps = total_validation_samples // validation_batch_size.
Theoretically, you test your entire data every epoch, as you theoretically should also train your entire data every epoch.

因此,理论上每个时期都会产生:

So, theorethycally each epoch yields:

  • steps_per_epoch = TotalTrainingSamples/TrainingBatchSize
  • validation_steps = TotalvalidationSamples/ValidationBatchSize

基本上,这两个变量是:每个 epoch 将产生多少批次.
这确保在每个时期:

Basically, the two vars are: how many batches per epoch you will yield.
This makes sure that at each epoch:

  • 你完全训练你的整个训练集
  • 您完全验证了整个验证集

尽管如此,如何分离训练和验证数据完全取决于您.

Nevertheless, it's totally up to you how you separate your training and validation data.

如果您确实希望每个时期有一个不同的批次(时期使用的数据少于整个数据),没关系,只需通过 steps_per_epoch=1validation_steps=1, 例如.生成器不会在每个 epoch 之后重置,因此第二个 epoch 将采用第二批,依此类推,直到它再次循环到第一批.

If you do want to have one different batch per epoch (epochs using less than your entire data), it's ok, just pass steps_per_epoch=1 or validation_steps=1, for instance. The generator is not resetted after each epoch, so the second epoch will take the second batch, and so on, until it loops again to the first batch.

我更喜欢在每个 epoch 训练整个数据,如果时间太长,我会使用 callback 在每批结束时显示日志:

I prefer training the entire data per epoch, and if the time is too long, I use a callback that shows the logs at the end of each batch:

from keras.callbacks import LambdaCallback

callbacks = callbacks=[LambdaCallback(on_batch_end=lambda batch,logs:print(logs))]

多处理

我永远无法使用 use_multiprocessing=True,它在第一个纪元开始时冻结.

Multiprocessing

I was never able to use use_multiprocessing=True, it freezes at the start of the first epoch.

我注意到 workers 与从生成器预加载的批次数量有关.如果您定义了 max_queue_size=1,您将拥有恰好 workers 预加载的批次数量.

I've noticed the workers are related to how many batches are preloaded from the generator. If you define max_queue_size=1, you will have exactly workers amount of batches preloaded.

他们建议您在多处理时使用 keras 序列.这些序列几乎就像一个生成器一样工作,但它会跟踪每个批次的顺序/位置.

They suggest you use keras Sequences when multiprocessing. The sequences work pretty much as a generator, but it keeps track of the order/position of each batch.

这篇关于Keras Sequential fit_generator 参数列表中validation_steps的含义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆