jiesutd / LatticeLSTM

Chinese NER using Lattice LSTM. Code for ACL 2018 paper.
1.8k stars 453 forks source link

Viterbi解码的cat操作中,tensor维度不一致 #8

Closed wangruicn closed 6 years ago

wangruicn commented 6 years ago

Traceback (most recent call last): File "/home/rui/workspace/lattice-lstm/LatticeLSTM-master/main.py", line 459, in train(data, save_model_dir, seg) File "/home/rui/workspace/lattice-lstm/LatticeLSTM-master/main.py", line 286, in train batch_charlen, batch_charrecover, batch_label, mask) File "/home/rui/workspace/lattice-lstm/LatticeLSTM-master/model/bilstmcrf.py", line 32, in neg_log_likelihood_loss scores, tag_seq = self.crf._viterbi_decode(outs, mask) File "/home/rui/workspace/lattice-lstm/LatticeLSTM-master/model/crf.py", line 159, in _viterbi_decode partition_history = torch.cat(partition_history,0).view(seq_len, batch_size,-1).transpose(1,0).contiguous() ## (batch_size, seq_len. tag_size) RuntimeError: invalid argument 0: Tensors must have same number of dimensions: got 2 and 3 at /pytorch/torch/lib/THC/generic/THCTensorMath.cu:102

在对partition_history执行cat操作时,输入的tensor list维度不一致。

partition_history中,第一个tensor是 [batch_size, tag_size, 1]:

partition = inivalues[:, START_TAG, :].clone().view(batch_size, tag_size, 1)  # bat_size * to_target_size     
partition_history.append(partition)

而在for中,torch.max返回的partition形状为 [batch_size,tag_size],与第一个tensor维度不一致,导致cat操作失败

cur_values = cur_values + partition.contiguous().view(batch_size, tag_size, 1).expand(batch_size, tag_size, tag_size)
partition, cur_bp = torch.max(cur_values, 1)
partition_history.append(partition)

请问如何修改

jiesutd commented 6 years ago

You need to confirm your PyTorch version. Currently it only support the PyTorch 0.3.0

wangruicn commented 6 years ago

我的PyTorch版本是0.3.1。可能在0.3的子版本间有一些细微差别。 将

partition = inivalues[:, START_TAG, :].clone().view(batch_size, tag_size, 1)

修改为

partition = inivalues[:, START_TAG, :].clone().view(batch_size, tag_size)

后看起来似乎正常了

jiesutd commented 6 years ago

Great!

xiao2mo commented 6 years ago

Thank u

xiao2mo commented 6 years ago

i have changed 2 parts as u mentioned above. but it still cannot work as expected. oops.

jiesutd commented 6 years ago

@xiao2mo show me your error

zhaoyuanfang commented 5 years ago

现在到了pytorch0.4.1了=。=mul() received an invalid combination of arguments - got(list), but expected one of:"Tensor""Number"

mingxixixi commented 5 years ago

我的PyTorch版本是0.3.1。可能在0.3的子版本间有一些细微差别。 将

partition = inivalues[:, START_TAG, :].clone().view(batch_size, tag_size, 1)

修改为

partition = inivalues[:, START_TAG, :].clone().view(batch_size, tag_size)

后看起来似乎正常了

你好,我改了之后为什么还有这样的错? RuntimeError: invalid argument 1: the number of sizes provided must be greater or equal to the number of dimensions in the tensor at /pytorch/torch/lib/THC/generic/THCTensor.c:326

marcma7358 commented 4 years ago

现在到了pytorch0.4.1了=。=mul() received an invalid combination of arguments - got(list), but expected one of:"Tensor""Number"

请问你解决了这个问题

GongCQ commented 4 years ago

删掉crf.py的第75行、第96行、第143行里的参数“1”,就能正常运行了

liangbingtao commented 3 years ago

删掉crf.py的第75行、第96行、第143行里的参数“1”,就能正常运行了

你好,最近刚开始研究这份代码,请问能加个联系方式请教交流下吗? 提前感谢!!我的qq是1607247596,谢谢!

jiang587 commented 3 years ago

删掉crf.py的第75行、第96行、第143行里的参数“1”,就能正常运行了

删掉后好像不起作用