【问题标题】:6 GB RAM Fails in Vectorizing text using Word2Vec6 GB RAM 无法使用 Word2Vec 对文本进行矢量化
【发布时间】:2020-10-22 21:23:25
【问题描述】:

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

# --------------- 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) 形状的列表,每个索引由预处理的推文标记组成。 感谢您的时间和提前回答,当然我很高兴听到处理大型数据集的更好方法

【问题讨论】:

    标签: python machine-learning bigdata sentiment-analysis


    【解决方案1】:

    如果我理解正确,它适用于 100 万条推文,但对于 160 万条推文则失败?所以你知道代码是正确的。

    如果 GPU 在您认为不应该出现内存不足的情况下出现内存不足,则它可能正在保留上一个进程。使用nvidia-smi 检查哪些进程正在使用 GPU,以及有多少内存。如果(在运行代码之前)你发现 python 进程在其中持有一个大块,它可能是一个崩溃的进程,或者一个 Jupyter 窗口仍然打开,等等。

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

    【讨论】:

    • 问题是我尽可能多地释放内存,并且在运行代码之前使用的 RAM 大约是 2GB,但是当我的代码达到 tweet2vec 功能时,我的笔记本电脑绝对没用,你不能用它做任何事情我尝试过使用另一台具有 8GB RAM 的笔记本电脑,但它在缩放部分返回内存错误。我的笔记本电脑死机之前的确切数据量是 (1373567)。至于代码我不知道它可能有什么问题,因为它适用于较低的数据!!!
    • 我应该更正我的评论,直到第 1373567 个数据(在 tweet2vec 函数中)之前一切都很好,并且数据不应该有问题,因为每次代码运行时它都会被打乱,如果你的意思是模型的话属性我应该说不,因为模型只是给出给定单词的向量
    【解决方案2】:

    当我将代码(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))]
    

    )

    我不知道为什么!!!!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-25
      • 1970-01-01
      • 1970-01-01
      • 2023-03-21
      • 1970-01-01
      相关资源
      最近更新 更多