Python 在 Keras 中拟合模型时,批量大小和时代数应该有多大?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35050753/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 15:55:07  来源:igfitidea点击:

How big should batch size and number of epochs be when fitting a model in Keras?

pythonmachine-learningkerasdata-science

提问by pr338

I am training on 970 samples and validating on 243 samples.

我正在训练 970 个样本并验证 243 个样本。

How big should batch size and number of epochs be when fitting a model in Keras to optimize the val_acc? Is there any sort of rule of thumb to use based on data input size?

在 Keras 中拟合模型以优化 val_acc 时,批量大小和时期数应该有多大?是否有任何基于数据输入大小的经验法则?

回答by Lucas Ramadan

Since you have a pretty small dataset (~ 1000 samples), you would probably be safe using a batch size of 32, which is pretty standard. It won't make a huge difference for your problem unless you're training on hundreds of thousands or millions of observations.

由于您有一个非常小的数据集(约 1000 个样本),因此使用 32 的批量大小可能是安全的,这是非常标准的。除非您对数十万或数百万次观察进行训练,否则它不会对您的问题产生巨大影响。

To answer your questions on Batch Size and Epochs:

要回答有关批次大小和时期的问题:

In general: Larger batch sizes result in faster progress in training, but don't always converge as fast. Smaller batch sizes train slower, but canconverge faster. It's definitely problem dependent.

一般而言:较大的批量会导致训练进度更快,但并不总是收敛得那么快。小批量训练速度较慢,但收敛速度更快。这绝对是问题相关的。

In general, the models improve with more epochs of training, to a point. They'll start to plateau in accuracy as they converge. Try something like 50 and plot number of epochs (x axis) vs. accuracy (y axis). You'll see where it levels out.

一般来说,模型会随着更多的训练时期而改进。当它们收敛时,它们的准确度将开始趋于稳定。尝试类似 50 并绘制时期数(x 轴)与准确度(y 轴)的关系图。你会看到它的水平。

What is the type and/or shape of your data? Are these images, or just tabular data? This is an important detail.

您的数据的类型和/或形状是什么?这些是图像,还是表格数据?这是一个重要的细节。

回答by tauseef_CuriousGuy

I use Keras to perform non-linear regression on speech data. Each of my speech files gives me features that are 25000 rows in a text file, with each row containing 257 real valued numbers. I use a batch size of 100, epoch 50 to train Sequentialmodel in Keras with 1 hidden layer. After 50 epochs of training, it converges quite well to a low val_loss.

我使用 Keras 对语音数据进行非线性回归。我的每个语音文件都为我提供了文本文件中 25000 行的特征,每行包含 257 个实数值。我使用批量大小为 100、epoch 50Sequential在具有 1 个隐藏层的 Keras 中训练 模型。经过 50 个 epoch 的训练后,它非常好地收敛到一个低val_loss.

回答by fnatic9

I used Keras to perform non linear regression for market mix modelling. I got best results with a batch size of 32 and epochs = 100 while training a Sequential model in Keras with 3 hidden layers. Generally batch size of 32 or 25 is good, with epochs = 100 unless you have large dataset. in case of large dataset you can go with batch size of 10 with epochs b/w 50 to 100. Again the above mentioned figures have worked fine for me.

我使用 Keras 为市场组合建模执行非线性回归。在使用 3 个隐藏层的 Keras 中训练 Sequential 模型时,我在批量大小为 32 和 epochs = 100 的情况下获得了最佳结果。通常批量大小为 32 或 25 是好的,除非您有大型数据集,否则 epochs = 100。在大型数据集的情况下,您可以使用 10 的批次大小,黑白 50 到 100 的纪元。同样,上述数字对我来说效果很好。

回答by Beltino Goncalves

Great answers above. Everyone gave good inputs.

上面的答案很好。每个人都给出了很好的意见。

Ideally, this is the sequence of the batch sizes that should be used:

理想情况下,这是应该使用的批量大小的顺序:

{1, 2, 4, 8, 16} - slow 

{ [32, 64],[ 128, 256] }- Good starters

[32, 64] - CPU

[128, 256] - GPU for more boost

回答by Anurag Gupta

Epochs is up to your wish, depending upon when validation loss stops improving further. This much should be batch size:

Epochs 取决于您的意愿,具体取决于验证损失何时停止进一步改善。这应该是批量大小:


# To define function to find batch size for training the model
# use this function to find out the batch size

    def FindBatchSize(model):
        """#model: model architecture, that is yet to be trained"""
        import os, sys, psutil, gc, tensorflow, keras
        import numpy as np
        from keras import backend as K
        BatchFound= 16

        try:
            total_params= int(model.count_params());    GCPU= "CPU"
            #find whether gpu is available
            try:
                if K.tensorflow_backend._get_available_gpus()== []:
                    GCPU= "CPU";    #CPU and Cuda9GPU
                else:
                    GCPU= "GPU"
            except:
                from tensorflow.python.client import device_lib;    #Cuda8GPU
                def get_available_gpus():
                    local_device_protos= device_lib.list_local_devices()
                    return [x.name for x in local_device_protos if x.device_type == 'GPU']
                if "gpu" not in str(get_available_gpus()).lower():
                    GCPU= "CPU"
                else:
                    GCPU= "GPU"

            #decide batch size on the basis of GPU availability and model complexity
            if (GCPU== "GPU") and (os.cpu_count() >15) and (total_params <1000000):
                BatchFound= 64    
            if (os.cpu_count() <16) and (total_params <500000):
                BatchFound= 64  
            if (GCPU== "GPU") and (os.cpu_count() >15) and (total_params <2000000) and (total_params >=1000000):
                BatchFound= 32      
            if (GCPU== "GPU") and (os.cpu_count() >15) and (total_params >=2000000) and (total_params <10000000):
                BatchFound= 16  
            if (GCPU== "GPU") and (os.cpu_count() >15) and (total_params >=10000000):
                BatchFound= 8       
            if (os.cpu_count() <16) and (total_params >5000000):
                BatchFound= 8    
            if total_params >100000000:
                BatchFound= 1

        except:
            pass
        try:

            #find percentage of memory used
            memoryused= psutil.virtual_memory()
            memoryused= float(str(memoryused).replace(" ", "").split("percent=")[1].split(",")[0])
            if memoryused >75.0:
                BatchFound= 8
            if memoryused >85.0:
                BatchFound= 4
            if memoryused >90.0:
                BatchFound= 2
            if total_params >100000000:
                BatchFound= 1
            print("Batch Size:  "+ str(BatchFound));    gc.collect()
        except:
            pass

        memoryused= [];    total_params= [];    GCPU= "";
        del memoryused, total_params, GCPU;    gc.collect()
        return BatchFound