我使用AllenAI 的这个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, 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, batch_size, num_tags)
8 tags = torch.tensor([[0.0, 1.0], [1.0, 1.0], [0.0, 1.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, tags, 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
任何帮助或提示将不胜感激。