インスタンスの勾配を取得するために allennlp.predictors.Predictor.get_gradients を使用しようとしました。
allennlp==0.8.5
メインコード:
for instance in targeted_dev_data:
#model is locally trained on GPU
#instance is from allennlp.data.dataset_readers.reader
predictor = Predictor(model,
StanfordSentimentTreeBankDatasetReader(granularity="2-class",
token_indexers={"tokens": single_id_indexer},
use_subtrees=True))
input_gradients = predictor.get_gradients([instance])
しかし、次のエラーが発生しました。
File "sst_sememe.py", line 436, in main
input_gradients = predictor.get_gradients([instance])
File "/home/rui.ye/universal-triggers-master/myenv/lib/python3.7/site-packages/allennlp/predictors/predictor.py", line 110, in get_gradients
outputs = self._model.decode(self._model.forward(**dataset.as_tensor_dict()))
File "sst_sememe.py", line 59, in forward
embeddings = self.word_embeddings(tokens)
File "/home/rui.ye/universal-triggers-master/myenv/lib/python3.7/site-packages/torch/nn/modules/module.py", line 493, in __call__
result = self.forward(*input, **kwargs)
File "/home/rui.ye/universal-triggers-master/myenv/lib/python3.7/site-packages/allennlp/modules/text_field_embedders/basic_text_field_embedder.py", line 131, in forward
token_vectors = embedder(*tensors, **forward_params_values)
File "/home/rui.ye/universal-triggers-master/myenv/lib/python3.7/site-packages/torch/nn/modules/module.py", line 493, in __call__
result = self.forward(*input, **kwargs)
File "/home/rui.ye/universal-triggers-master/myenv/lib/python3.7/site-packages/allennlp/modules/token_embedders/embedding.py", line 144, in forward
sparse=self.sparse)
File "/home/rui.ye/universal-triggers-master/myenv/lib/python3.7/site-packages/torch/nn/functional.py", line 1506, in embedding
return torch.embedding(weight, input, padding_idx, scale_grad_by_freq, sparse)
RuntimeError: Expected object of backend CUDA but got backend CPU for argument #3 'index'
ただし、次のコードで Predictor.predict_instance テストに合格できます
for instance in targeted_dev_data:
#model is locally trained on GPU
#instance is from allennlp.data.dataset_readers.reader
predictor = Predictor(model,
StanfordSentimentTreeBankDatasetReader(granularity="2-class",
token_indexers={"tokens": single_id_indexer},
use_subtrees=True))
input_preds = predictor.predict_instance(instance)