9

在我使用 load_learner 加载模型后,我正在使用 googlecolab (gpu) 上的 fastai train 进行文本分类 (NLP) 模型,但当我更改 cpu 使用情况时,我收到错误“RuntimeError: _th_index_select 在 CPUType 上不支持一半”有什么方法可以预测cpu使用结果吗?

from fastai import *
from fastai.text import *
from sklearn.metrics import f1_score
defaults.device = torch.device('cpu')
@np_func
def f1(inp,targ): return f1_score(targ, np.argmax(inp, axis=-1))
path = Path('/content/drive/My Drive/Test_fast_ai')
learn = load_learner(path)
learn.predict("so sad")

RuntimeError                              Traceback (most recent call last)
<ipython-input-13-3775eb2bfe91> in <module>()
----> 1 learn.predict("so sad")

11 frames
/usr/local/lib/python3.6/dist-packages/torch/nn/functional.py in embedding(input, weight, padding_idx, max_norm, norm_type, scale_grad_by_freq, sparse)
   1504         # remove once script supports set_grad_enabled
   1505         _no_grad_embedding_renorm_(weight, input, max_norm, norm_type)
-> 1506     return torch.embedding(weight, input, padding_idx, scale_grad_by_freq, sparse)
   1507 
   1508 

RuntimeError: _th_index_select not supported on CPUType for Half
4

1 回答 1

1

我遇到了同样的问题。您是否使用参数训练模型to_fp16()?我通过从学习者中删除此参数来解决此问题。例如,当我使用以下命令行进行训练时,在 cpu 环境中使用模型进行预测时出现 RuntimeError。

learn_c = text_classifier_learner(data_clas, AWD_LSTM, drop_mult=0.5, metrics=[accuracy]).to_fp16()

要修复,我只需删除后缀.to_fp16(),一切顺利。

于 2019-11-26T13:16:08.103 回答