如何解决运行BERT模型时Google Colab中的RAM波动
我正在google colab上运行一个简单的注释分类任务。我正在使用DistilBERT进行上下文嵌入。我仅使用4000个训练样本,导致笔记本不断崩溃。 当我运行单元以获取嵌入时,我会保持一个关于RAM利用率如何增加的标签。我看到它从3gb到8gb之间振荡。 它不应该只是增加吗?任何人都可以在较低的级别上解释它的工作原理。
这是我的代码,最后是我看到上述内容的单元格。
# For DistilBERT:
model_class,tokenizer_class,pretrained_weights = (ppb.DistilBertModel,ppb.DistilBertTokenizer,'distilbert-base-uncased')
## Want BERT instead of distilBERT? Uncomment the following line:
#model_class,pretrained_weights = (ppb.BertModel,ppb.BertTokenizer,'bert-base-uncased')
# Load pretrained model/tokenizer
tokenizer = tokenizer_class.from_pretrained(pretrained_weights)
model = model_class.from_pretrained(pretrained_weights)
max_len=80
tokenized = sample['comment_text'].apply((lambda x: tokenizer.encode(x,add_special_tokens=True,max_length= max_len)))
padded = np.array([i + [0]*(max_len-len(i)) for i in tokenized.values])
attention_mask = np.where(padded != 0,1,0)
attention_mask.shape
input_ids = torch.tensor(padded)
attention_mask = torch.tensor(attention_mask)
**with torch.no_grad():
last_hidden_states = model(input_ids,attention_mask=attention_mask)**
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。