简体   繁体   English

如何计算训练所需的RAM内存?

[英]How to calculate RAM memory needed for training?

How can I calculate RAM memory needed for training a keras model? 如何计算训练keras模型所需的RAM内存? I want to calculate this because I sometimes encounter the exceeds system memory error when training models. 我要进行计算,因为在训练模型时有时会遇到超出系统内存的错误。 Here is my model, for example: 例如,这是我的模型:

Layer (type)                 Output Shape              Param #
=================================================================
conv2d_1 (Conv2D)            (None, 30, 30, 32)        320
_________________________________________________________________
conv2d_2 (Conv2D)            (None, 28, 28, 32)        9248
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 14, 14, 32)        0
_________________________________________________________________
dropout_1 (Dropout)          (None, 14, 14, 32)        0
_________________________________________________________________
conv2d_3 (Conv2D)            (None, 12, 12, 64)        18496
_________________________________________________________________
conv2d_4 (Conv2D)            (None, 10, 10, 64)        36928
_________________________________________________________________
max_pooling2d_2 (MaxPooling2 (None, 5, 5, 64)          0
_________________________________________________________________
dropout_2 (Dropout)          (None, 5, 5, 64)          0
_________________________________________________________________
flatten_1 (Flatten)          (None, 1600)              0
_________________________________________________________________
dense_1 (Dense)              (None, 128)               204928
_________________________________________________________________
dropout_3 (Dropout)          (None, 128)               0
_________________________________________________________________
dense_2 (Dense)              (None, 10)                1290
=================================================================
Total params: 271,210
Trainable params: 271,210
Non-trainable params: 0

Assuming each parameter is a 32-bit datatype (single-precision floating point, 4 bytes). 假设每个参数都是32位数据类型(单精度浮点数,4个字节)。 Your memory usage should be somewhere around: (# of params) * 4B 您的内存使用量应该在以下范围内:(参数数量)* 4B

In this case: 271,210 * 4B = 1084840B =~ 1MB 在这种情况下:271,210 * 4B = 1084840B =〜1MB

However, there is an important consideration to keep in mind. 但是,要记住一个重要的考虑因素。 This is assuming a batch size of 1, ie you're loading in 1 input at a time. 假设批量大小为1,即您一次加载1个输入。 If you are using minibatch (typical batch size of 32 or 64) then you'll have to multiply that memory calculation by the size of the batch. 如果您使用最小批量(通常批量大小为32或64),则必须将该内存计算乘以批量大小。 If you are using batch gradient descent , then you may be using your entire dataset on each batch. 如果您正在使用批次梯度下降 ,则可能在每个批次上使用整个数据集。 In this case, your memory requirements could be enormous. 在这种情况下,您的内存需求可能非常巨大。

This analysis inspired by: https://datascience.stackexchange.com/questions/17286/cnn-memory-consumption 该分析的灵感来自: https : //datascience.stackexchange.com/questions/17286/cnn-memory-consumption

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM