首页 > 解决方案 > 使用 HuggingFace 从文本中提取特征的生成器

问题描述

我正在尝试从推文中建立一个模型来进行情绪分析。我正在使用 HuggingFace 特征提取管道来提取特征以输入另一个模型。但是,当我尝试使用管道从文本列表中提取特征时,我的 CoLab 实例(默认 GPU 运行时)由于 RAM 不足而崩溃。

作为参考,这是我的代码中的一些片段。我正在使用 HuggingFace 的特征提取管道

#Feature extraction pipeline
import numpy as np
from transformers import AutoTokenizer, AutoModel, pipeline


model = AutoModel.from_pretrained('distilbert-base-uncased')
tokenizer = AutoTokenizer.from_pretrained('distilbert-base-uncased', add_special_tokens = 'true', padding = 'longest')
nlp = pipeline('feature-extraction', model=model, tokenizer=tokenizer, device = 0)
#Generator function
def get_tweets(file_name):
    with open(file_name, "r", encoding="utf") as tweet_records:
        for tweet_record in csv.reader(tweet_records):
            tweet_label = tweet_record[3]
            tweet_record = tweet_record[2]#remove first two features
            #tweet_record = #preprocessing step/s here

            tweet_record = nlp(tweet_record) #get extracted features. 
            #nlp(features) is used to extract features using the pipeline
            yield tweet_record, tweet_label
#Testing
filename = '/content/drive/My Drive/Twitter Files/combined-0-1 (2).csv'
store_list = list(iter(get_tweets(filename)))
next(iter_tweet)  # Skipping the column names

即使已经实现了生成器,由于缺少 RAM,这仍然会崩溃。CSV 中有 236K 行。知道如何解决这个问题吗?

标签: pythonhuggingface-transformers

解决方案


推荐阅读