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

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

问题描述

我在Python中将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.

我想使用顺序模型对象的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:

  • 生成器:python培训数据批处理生成器;无休止地循环浏览其训练数据
  • validation_data :-在我的情况下 -一个python验证数据批处理生成器;该文档没有提到对其验证数据进行无休止的循环
  • steps_per_epoch :number of training batches = uniqueTrainingData / batchSize
  • 验证步骤:???; = uniqueValidationData/批量大小???
  • use_multiprocessing :布尔值;不要传递非可挑选参数???
  • 工人:已使用的最大进程数
  • 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是什么意思. 我知道上面链接的文档(Number of steps to yield from validation generator at the end of every epoch)的定义,但这只会使我在给定的上下文中感到困惑.从文档中,我知道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的问题:

  • 它真的可以那样工作吗?如果是这样:为什么?我认为,在每个时期之后,最好使用一个验证批处理(最好以前没有使用过)进行验证,以确保对训练进行了验证,而没有冒着训练"模型在已使用的验证集上更好地表现的风险.
  • 在上一个问题的上下文中:为什么建议的验证步骤数量是uniqueValidationData / batches,而不是uniqueValidationData / epochs?有没有更好100个验证周期的100个验证批次,而不是x可能小于或大于指定的验证周期的x个验证批次?或者:如果您的验证批数少于历次,那么是否对模型进行了训练而不对其余时期进行验证,或者验证集是否可以重用/重新组合+重用?
  • 训练和验证批次具有相同的批次大小(红利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数组,还是必须将它们转换为多维列表?

推荐答案

验证生成器的工作原理与训练生成器完全相同.您定义每个时期将使用多少批次.

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

  • 训练生成器将产生steps_per_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
  • steps_per_epoch = TotalTrainingSamples / TrainingBatchSize
  • validation_steps = TotalvalidationSamples / ValidationBatchSize

基本上,这两个变量是:每个纪元将产生多少个批次.
这样可以确保在每个纪元:

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即可.生成器不会在每个时期之后重置,因此第二个时期将占用第二批,依此类推,直到再次循环到第一批为止.

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.

我更喜欢训练每个时期的全部数据,如果时间太长,我会使用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顺序fit_generator参数列表中validation_steps的含义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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