繁体   English   中英

6 GB RAM 无法使用 Word2Vec 对文本进行矢量化

[英]6 GB RAM Fails in Vectorizing text using Word2Vec

我正在尝试使用 word2vec 和 tfidf-score 对包含 160 万条推文的数据集进行基本的推文情绪分析,但我的 6 GB Gforce-Nvidia 未能这样做。 由于这是我第一个与机器学习相关的实践项目,我想知道我做错了什么,因为数据集都是文本,它不应该占用这么多内存,这会使我的笔记本电脑在 tweet2vec 函数中冻结或在缩放部分出现内存错误。 下面是我的代码的一部分,一切都崩溃了。 最后一件事是我已经尝试了多达 100 万的数据并且它奏效了! 所以我很好奇是什么导致了这个问题

# --------------- calculating word weight for using later in word2vec model & bringing words together ---------------
def word_weight(data):
    vectorizer = TfidfVectorizer(sublinear_tf=True, use_idf=True)
    d = dict()
    for index in tqdm(data, total=len(data), desc='Assigning weight to words'):
        # --------- try except caches the empty indexes ----------
        try:
            matrix = vectorizer.fit_transform([w for w in index])
            tfidf = dict(zip(vectorizer.get_feature_names(), vectorizer.idf_))
            d.update(tfidf)
        except ValueError:
            continue
    print("every word has weight now\n"
          "--------------------------------------")
    return d


# ------------------- bringing tokens with weight to recreate tweets ----------------
def tweet2vec(tokens, size, tfidf):
    count = 0
    for index in tqdm(tokens, total=len(tokens), desc='creating sentence vectors'):
        # ---------- size is the dimension of word2vec model (200) ---------------
        vec = np.zeros(size)
        for word in index:
            try:
                vec += model[word] * tfidf[word]
            except KeyError:
                continue
        tokens[count] = vec.tolist()
        count += 1
    print("tweet vectors are ready for scaling for ML algorithm\n"
          "-------------------------------------------------")
    return tokens


dataset = read_dataset('training.csv', ['target', 't_id', 'created_at', 'query', 'user', 'text'])
dataset = delete_unwanted_col(dataset, ['t_id', 'created_at', 'query', 'user'])
dataset_token = [pre_process(t) for t in tqdm(map(lambda t: t, dataset['text']),
                                              desc='cleaning text', total=len(dataset['text']))]

print('pre_process completed, list of tweet tokens is returned\n'
      '--------------------------------------------------------')
X = np.array(tweet2vec(dataset_token, 200, word_weight(dataset_token)))
print('scaling vectors ...')
X_scaled = scale(X)
print('features scaled!')

提供给 word_weight 函数的数据是一个 (1599999, 200) 形状的列表,其中每个索引都由预处理的推文标记组成。 感谢您的时间和提前回答,当然我很高兴听到处理大数据集的更好方法

如果我理解正确,它可以处理 100 万条推文,但无法处理 160 万条推文? 所以你知道代码是正确的。

如果 GPU 在您认为不应该耗尽内存的情况下耗尽了内存,则它可能会继续执行先前的进程。 使用nvidia-smi检查哪些进程正在使用 GPU,以及多少内存。 如果(在运行代码之前)您发现 python 进程在其中包含一个大块,则可能是一个崩溃的进程,或者一个 Jupyter 窗口仍然打开,等等。

我发现watch nvidia-smi (不确定是否有 Windows 等效项)很有用,以了解 GPU 内存如何随着训练的进行而变化。 通常在开始时保留一个块,然后它保持相当恒定。 如果您看到它呈线性上升,则代码可能有问题(您是否在每次迭代时重新加载模型,诸如此类?)。

当我将代码(tweet2vec 函数)更改为此(w 是字重)时,我的问题解决了

def tweet2vec(tokens, size, tfidf):
    # ------------- size is the dimension of word2vec model (200) ---------------
    vec = np.zeros(size).reshape(1, size)
    count = 0
    for word in tokens:
        try:
            vec += model[word] * tfidf[word]
            count += 1
        except KeyError:
            continue
    if count != 0:
        vec /= count
    return vec

X = np.concatenate([tweet2vec(token, 200, w) for token in tqdm(map(lambda token: token, dataset_token),
                                                               desc='creating tweet vectors',
                                                               total=len(dataset_token))]

)

我不知道为什么!!!!

暂无
暂无

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

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