【问题标题】:How to fix "ResourceExhaustedError: OOM when allocating tensor"如何修复“ResourceExhaustedError:分配张量时出现 OOM”
【发布时间】:2019-12-18 15:12:04
【问题描述】:

我想制作一个具有多个输入的模型。所以,我尝试构建一个这样的模型。

# define two sets of inputs
inputA = Input(shape=(32,64,1))
inputB = Input(shape=(32,1024))
 
# CNN
x = layers.Conv2D(32, kernel_size = (3, 3), activation = 'relu')(inputA)
x = layers.Conv2D(32, (3,3), activation='relu')(x)
x = layers.MaxPooling2D(pool_size=(2,2))(x)
x = layers.Dropout(0.2)(x)
x = layers.Flatten()(x)
x = layers.Dense(500, activation = 'relu')(x)
x = layers.Dropout(0.5)(x)
x = layers.Dense(500, activation='relu')(x)
x = Model(inputs=inputA, outputs=x)
 
# DNN
y = layers.Flatten()(inputB)
y = Dense(64, activation="relu")(y)
y = Dense(250, activation="relu")(y)
y = Dense(500, activation="relu")(y)
y = Model(inputs=inputB, outputs=y)
 
# Combine the output of the two models
combined = concatenate([x.output, y.output])
 

# combined outputs
z = Dense(300, activation="relu")(combined)
z = Dense(100, activation="relu")(combined)
z = Dense(1, activation="softmax")(combined)

model = Model(inputs=[x.input, y.input], outputs=z)

model.summary()

opt = Adam(lr=1e-3, decay=1e-3 / 200)
model.compile(loss = 'sparse_categorical_crossentropy', optimizer = opt,
    metrics = ['accuracy'])

和总结 : _

但是,当我尝试训练这个模型时,

history = model.fit([trainimage, train_product_embd],train_label,
    validation_data=([validimage,valid_product_embd],valid_label), epochs=10, 
    steps_per_epoch=100, validation_steps=10)

问题发生了...... :

 ResourceExhaustedError                    Traceback (most recent call
 last) <ipython-input-18-2b79f16d63c0> in <module>()
 ----> 1 history = model.fit([trainimage, train_product_embd],train_label,
 validation_data=([validimage,valid_product_embd],valid_label),
 epochs=10, steps_per_epoch=100, validation_steps=10)

 4 frames
 /usr/local/lib/python3.6/dist-packages/tensorflow_core/python/client/session.py
 in __call__(self, *args, **kwargs)    1470         ret =
 tf_session.TF_SessionRunCallable(self._session._session,    1471      
 self._handle, args,
 -> 1472                                                run_metadata_ptr)    1473         if run_metadata:    1474          
 proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)
 
 ResourceExhaustedError: 2 root error(s) found.   (0) Resource
 exhausted: OOM when allocating tensor with shape[800000,32,30,62] and
 type float on /job:localhost/replica:0/task:0/device:GPU:0 by
 allocator GPU_0_bfc     [[{{node conv2d_1/convolution}}]] Hint: If you
 want to see a list of allocated tensors when OOM happens, add
 report_tensor_allocations_upon_oom to RunOptions for current
 allocation info.
 
     [[metrics/acc/Mean_1/_185]] Hint: If you want to see a list of
 allocated tensors when OOM happens, add
 report_tensor_allocations_upon_oom to RunOptions for current
 allocation info.
 
   (1) Resource exhausted: OOM when allocating tensor with
 shape[800000,32,30,62] and type float on
 /job:localhost/replica:0/task:0/device:GPU:0 by allocator GPU_0_bfc    
 [[{{node conv2d_1/convolution}}]] Hint: If you want to see a list of
 allocated tensors when OOM happens, add
 report_tensor_allocations_upon_oom to RunOptions for current
 allocation info.
 
 0 successful operations. 0 derived errors ignored.

感谢您的阅读并希望对我有所帮助:)

【问题讨论】:

  • 我使用的是批量大小 = 1024。更改为 768 后解决了我的问题。

标签: python tensorflow machine-learning keras deep-learning


【解决方案1】:

OOM 代表“内存不足”。您的 GPU 内存不足,因此无法为该张量分配内存。您可以做一些事情:

  • 减少DenseConv2D 层中的过滤器数量
  • 使用较小的batch_size(或增加steps_per_epochvalidation_steps
  • 使用灰度图(可以使用tf.image.rgb_to_grayscale
  • 减少层数
  • 在卷积层之后使用MaxPooling2D
  • 缩小图片大小(您可以使用tf.image.resize
  • 为您的输入使用更小的float 精度,即np.float32
  • 如果您使用的是预训练模型,请冻结第一层(如 this

有更多关于这个错误的有用信息:

OOM when allocating tensor with shape[800000,32,30,62]

这是一个奇怪的形状。如果您使用图像,通常应该有 3 或 1 个通道。最重要的是,您似乎一次传递了整个数据集;你应该分批传递它。

【讨论】:

  • 这有点奇怪,我得到同样的错误(在切换 cpu 为 gpu 版本之后)只是通过执行顺序和添加层,甚至不适合或任何东西。形状[173056,4096] 并输入浮点数。当您甚至没有通过批次时出现分配错误是否正常?
  • 是的,因为神经网络只是一个巨大的浮点值矩阵,就像输入批次一样。
【解决方案2】:

[800000,32,30,62]看来,您的模型将所有数据放在了一批中。

尝试指定的批量大小,如

history = model.fit([trainimage, train_product_embd],train_label, validation_data=([validimage,valid_product_embd],valid_label), epochs=10, steps_per_epoch=100, validation_steps=10, batch_size=32)

如果仍然 OOM,请尝试减少 batch_size

【讨论】:

  • OP 指定 steps_per_epoch=100 所以我认为情况并非如此。 batch_size 应自动设置为 sample_size/steps_per_epoch
  • 我首先尝试了“batch_size”。但是,有一个错误:“ValueError:如果你的数据是符号张量的形式,你应该指定steps_per_epoch参数(而不是batch_size参数,因为符号张量预计会产生批量输入数据) 。”顺便谢谢你的意见:)
【解决方案3】:

我也遇到过。

您可以尝试使用某种形式的迁移学习来减少可训练参数 - 尝试冻结最初的几层并使用较小的批量大小。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-08
    • 1970-01-01
    • 2021-12-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多