Skip to main content
 首页 » 编程设计

python之TensorFlow 在执行 embedding_lookup 时内存不足 (ResourceExhaustedError)

2025年01月19日27虾米姐

我正在使用预置向量来创建这样的嵌入

import numpy 
import gensim 
import tensorflow 
ft_model=gensim.models.KeyedVectors.load_word2vec_format("ft_model.vec") 
vocabulary=ft_model.vocab 
embeddings=numpy.array([ft_model.word_vec(x) for x in vocabulary.keys()]) 
 
vocabulary_size=len(vocabulary) 
embedding_size=embeddings.shape[1] 
 
W=tensorflow.Variable( 
    tensorflow.constant(0.0, shape=[vocabulary_size, embedding_size]), 
    trainable=False, 
    name="W" 
) 
embedding_placeholder=tensorflow.placeholder( 
    tensorflow.float32,[vocabulary_size,embedding_size], 
    name="fasttext_vector" 
) 
embedding_init=W.assign(embedding_placeholder) 
data_placeholder=tensorflow.placeholder(tensorflow.int32,shape=[None, max_length]) 
embedding_layer=tensorflow.nn.embedding_lookup(W, data_placeholder) 

在它短暂地运行 1 或两个训练批处理并且代码完全崩溃后,我得到了一个错误!

ResourceExhaustedError (see above for traceback): OOM when allocating tensor with shape[5000,14621,100] 

堆栈跟踪清楚地表明这是由 embedding_layer=tensorflow.nn.embedding_lookup(W, data_placeholder) 行引起的。 知道是什么原因造成的吗? 100 是嵌入大小,但其他数字(5000、14621)相当奇怪,比我预期的要大,而且似乎导致 TensorFlow 完全耗尽所有 GPU 内存! 嵌入查找看起来很常见,我合并的 .vec 文件非常小。

请您参考如下方法:

可能是您的计算机内存 (RAM) 不足。在启动模型之前先查看任务管理器。 我有 16 GB 和 79%,所以它用完了。在准备好数据后,使用 jupyter notebook 查看剩余的 Ram 数量可能会有所帮助