如何解决RuntimeError:预期设备cpu和dtype浮动,但设备cpu和dtype布尔
我使用来自AllenAI的this python脚本将CRF模块(有条件的随机字段)层集成到我的ML系统中...
并执行以下示例以测试CRF模块。
import torch
from allennlp.modules import ConditionalRandomField
num_tags = 2
model = ConditionalRandomField(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.0,1.0],[1.0,[0.0,1.0]],dtype=torch.long) # (seq_length,batch_size)
model(emissions,tags)
执行后,出现以下错误。
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
<ipython-input-3-415939853a93> in <module>
7 emissions = torch.randn(seq_length,num_tags)
8 tags = torch.tensor([[0.0,dtype=torch.long) #
(seq_length,batch_size)
----> 9 model(emissions,tags)
~/anaconda3/envs/torch/lib/python3.6/site-packages/torch/nn/modules/module.py in
__call__(self,*input,**kwargs)
545 result = (result,)
546 input = result
--> 547 if torch._C._get_tracing_state():
548 result = self._slow_forward(*input,**kwargs)
549 else:
<ipython-input-2-2d984bd97cf1> in forward(self,inputs,tags,mask)
329 mask = mask.to(torch.bool)
330
--> 331 log_denominator = self._input_likelihood(inputs,mask)
332 log_numerator = self._joint_likelihood(inputs,mask)
333
<ipython-input-2-2d984bd97cf1> in _input_likelihood(self,logits,mask)
249 # In valid positions (mask == True) we want to take the logsumexp over the
current_tag dimension
250 # of `inner`. Otherwise (mask == False) we want to retain the previous
alpha.
--> 251 alpha = util.logsumexp(inner,1) * mask[i].view(batch_size,1) + alpha *
(
252 ~mask[i]
253 ).view(batch_size,1)
RuntimeError: expected device cpu and dtype Float but got device cpu and dtype Bool
我确实知道这是PyTorch版本与allennlp软件包中使用的功能不兼容的问题,但是我不确定它期望的是什么版本的Torch和Torchvision。 这些是我机器上安装的PyTorch的版本。
torch 1.5.1
torchvision 0.4.2
allennlp 1.0.0
任何帮助或提示将不胜感激。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。