Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
wangruicn opened this issue Jul 6, 2018 · 12 comments
Closed

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

wangruicn opened this issue Jul 6, 2018 · 12 comments

Comments

@wangruicn
Copy link

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
Copy link
Owner

jiesutd commented Jul 6, 2018

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

@wangruicn
Copy link
Author

我的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
Copy link
Owner

jiesutd commented Jul 6, 2018

Great!

@jiesutd jiesutd closed this as completed Jul 6, 2018
@xiao2mo
Copy link

xiao2mo commented Jul 17, 2018

Thank u

@xiao2mo
Copy link

xiao2mo commented Jul 17, 2018

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

@jiesutd
Copy link
Owner

jiesutd commented Jul 17, 2018

@xiao2mo show me your error

@zhaoyuanfang
Copy link

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

@mingxixixi
Copy link

我的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
Copy link

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

请问你解决了这个问题

@GongCQ
Copy link

GongCQ commented Oct 26, 2020

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

@liangbingtao
Copy link

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

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

@jiang587
Copy link

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

删掉后好像不起作用

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants