kmkurn / pytorch-crf

(Linear-chain) Conditional random field in PyTorch.
https://pytorch-crf.readthedocs.io
MIT License
943 stars 152 forks source link

RuntimeError: tensors used as indices must be long or byte tensors #38

Closed yuzhiguo07 closed 3 years ago

yuzhiguo07 commented 5 years ago

I just follow the example in the document, but I got this error: Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/opt/conda/lib/python3.6/site-packages/torch/nn/modules/module.py", line 491, in __call__ result = self.forward(*input, **kwargs) File "/opt/conda/lib/python3.6/site-packages/torchcrf/__init__.py", line 102, in forward numerator = self._compute_score(emissions, tags, mask) File "/opt/conda/lib/python3.6/site-packages/torchcrf/__init__.py", line 187, in _compute_score score += emissions[0, torch.arange(batch_size), tags[0]] RuntimeError: tensors used as indices must be long or byte tensors

code:

import torch from torchcrf import CRF num_tags = 5 model = CRF(num_tags) seq_length = 3 # maximum sequence length in a batch batch_size = 2 # number of samples in the batch emissions = torch.randn(seq_length, batch_size, num_tags) tags = torch.tensor([ ... [0, 1], [2, 4], [3, 1] ... ], dtype=torch.long) # (seq_length, batch_size) model(emissions, tags)

kmkurn commented 5 years ago

Hi, I tried on my machine and it worked just fine. What version of PyTorch are you using?

iamxinxin commented 3 years ago

I got the same error with pytorch==1.6.0

kmkurn commented 3 years ago

It works just fine for me with PyTorch 1.6.0. Are you sure your tags is of dtype long?

iamxinxin commented 3 years ago

Thanks! Float tensor found. It works after I changed tags' dtype to long

kmkurn commented 3 years ago

Great! Glad that you got it working. I'm closing this issue.