为什么使用 Lime 和 BERT 会出现内存错误

问题描述 投票:0回答:1

我正在使用 LIME 来可视化我的微调 BERT 模型。我不知道为什么它占用了太多内存并被系统杀死了。这是我的代码:

tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')

model = BertForSequenceClassification.from_pretrained(f'{BASE_PATH}results/{MODEL}/', num_labels=4)

def _proba(texts):
    encodings = tokenizer(texts, truncation=True, padding=True, max_length=250, return_tensors='pt')
    pred = model(**encodings)
    softmax = Softmax(dim = 1)
    prob = softmax(pred.logits).detach().numpy()
    return prob
    
    
explainer = LimeTextExplainer(class_names=['A', 'B', 'C', 'D'])


idx = 0
exp = explainer.explain_instance(test_texts[idx], _proba, num_features=4)

exp.save_to_file('/lime_vis.html')

我在 RAM 为 64GB 的服务器中运行此代码。我很好奇它怎么仍然会出现内存错误。

我尝试在 colab、kaggle 上运行它,它只占用单个示例的所有内存。

python deep-learning pytorch bert-language-model lime
1个回答
0
投票

对于其他正在寻找解决方案的人。我已根据此更改了实现https://github.com/marcotcr/lime/issues/356。效果很好。

© www.soinside.com 2019 - 2024. All rights reserved.