【问题标题】:ResourceExhaustedError when declaring Embeddings layer (Keras)声明嵌入层时出现 ResourceExhaustedError (Keras)
【发布时间】:2018-09-28 02:41:56
【问题描述】:

我正在为 NLP 创建一个 NN,从 Embedding 层开始(使用预训练的嵌入)。但是当我在 Keras(Tensorflow 后端)中声明 Embedding 层时,我有一个 ResourceExhaustedError

ResourceExhaustedError: OOM when allocating tensor with shape[137043,300] and type float on /job:localhost/replica:0/task:0/device:GPU:0 by allocator GPU_0_bfc
 [[{{node embedding_4/random_uniform/RandomUniform}} = RandomUniform[T=DT_INT32, dtype=DT_FLOAT, seed=87654321, seed2=9524682, _device="/job:localhost/replica:0/task:0/device:GPU:0"](embedding_4/random_uniform/shape)]]
 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.

我已经查过谷歌:大部分 ResourceExhaustedError 发生在训练时,是因为 GPU 的 RAM 不够大。它是通过减少批量大小来解决的。

但就我而言,我什至没有开始训练!这条线是问题所在:

q1 = Embedding(nb_words + 1, 
             param['embed_dim'].value, 
             weights=[word_embedding_matrix], 
             input_length=param['sentence_max_len'].value)(question1)

这里,word_embedding_matrix 是大小为(137043, 300) 的矩阵,即预训练嵌入。

据我所知,这不会占用大量内存(不像here):

137043 * 300 * 4 字节 = 53 kiB

这是使用的 GPU:

 +-----------------------------------------------------------------------------+
 | NVIDIA-SMI 396.26                 Driver Version: 396.26                    |
 |-------------------------------+----------------------+----------------------+
 | GPU  Name        Persistence-M| Bus-Id        Disp.A | Volatile Uncorr. ECC |
 | Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |
 |===============================+======================+======================|
 |   0  GeForce GTX 108...  Off  | 00000000:02:00.0 Off |                  N/A |
 | 23%   32C    P8    16W / 250W |   6956MiB / 11178MiB |      0%      Default |
 +-------------------------------+----------------------+----------------------+
 |   1  GeForce GTX 108...  Off  | 00000000:03:00.0 Off |                  N/A |
 | 23%   30C    P8    16W / 250W |    530MiB / 11178MiB |      0%      Default |
 +-------------------------------+----------------------+----------------------+
 |   2  GeForce GTX 108...  Off  | 00000000:82:00.0 Off |                  N/A |
 | 23%   34C    P8    16W / 250W |    333MiB / 11178MiB |      0%      Default |
 +-------------------------------+----------------------+----------------------+
 |   3  GeForce GTX 108...  Off  | 00000000:83:00.0 Off |                  N/A |
 | 24%   46C    P2    58W / 250W |   4090MiB / 11178MiB |     23%      Default |
 +-------------------------------+----------------------+----------------------+

 +-----------------------------------------------------------------------------+
 | Processes:                                                       GPU Memory |
 |  GPU       PID   Type   Process name                             Usage      |
 |=============================================================================|
 |    0      1087      C   uwsgi                                       1331MiB |
 |    0      1088      C   uwsgi                                       1331MiB |
 |    0      1089      C   uwsgi                                       1331MiB |
 |    0      1090      C   uwsgi                                       1331MiB |
 |    0      1091      C   uwsgi                                       1331MiB |
 |    0      4176      C   /usr/bin/python3                             289MiB |
 |    1      2631      C   ...e92/venvs/wordintent_venv/bin/python3.6   207MiB |
 |    1      4176      C   /usr/bin/python3                             313MiB |
 |    2      4176      C   /usr/bin/python3                             323MiB |
 |    3      4176      C   /usr/bin/python3                             347MiB |
 |    3     10113      C   python                                      1695MiB |
 |    3     13614      C   python3                                     1347MiB |
 |    3     14116      C   python                                       689MiB |
 +-----------------------------------------------------------------------------+

有人知道我为什么会遇到这个异常吗?

【问题讨论】:

  • 137043 * 300 * 4 字节 = 157 MB,而不是 53 kB。然后 Keras 将使用大约 15 倍,因此预计 2.3 GB。

标签: tensorflow machine-learning neural-network keras deep-learning


【解决方案1】:

来自this link,将 TensorFlow 配置为不直接分配最大 GPU 似乎可以解决问题。

在模型层的声明之前运行它可以解决问题:

config = tf.ConfigProto()
config.gpu_options.allow_growth = True
config.gpu_options.per_process_gpu_memory_fraction = 0.3
session = tf.Session(config=config)
K.set_session(session)

在接受我的答案之前,我会留出一些时间,看看其他答案。

【讨论】:

    猜你喜欢
    • 2020-06-12
    • 2019-09-07
    • 2018-08-12
    • 1970-01-01
    • 2022-12-31
    • 1970-01-01
    • 2018-04-07
    • 2017-05-22
    • 2018-01-11
    相关资源
    最近更新 更多