python - 使用 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 行。知道如何解决这个问题吗?
解决方案
推荐阅读
- spring-boot - Spring Boot 静态内容在本地工作正常,但当我推送到 VPS 服务器时却不行
- angular - “这个”如何在 _.each 循环中工作?/ 你如何调用 _.each 循环的返回值?
- keras - 如何强制 LSTM 学习单调序列?
- php - 调用未定义的方法 app\models\etc.. Yii2
- ms-access - Microsoft Access - 包括上次修改者
- prometheus-alertmanager - Prometheus Alertmanager 通过 UI 使警报静音不会阻止警报首次出现以触发
- android - 让我的应用收到电话通知
- java - 主方法内变量的修饰符
- c++ - 用户定义数组大小
- python - Python 和 Julia 以不同的方式计算函数?