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

RuntimeError: 1D target tensor expects 2D input tensors, but found inputs with sizes [46, 16, 50] and [46, 16, 50]. #6

Open
JunLiangZ opened this issue May 30, 2023 · 4 comments

Comments

@JunLiangZ
Copy link

How to solve this problem?

@MC-wither
Copy link

How to solve this problem?

Hey, I've met the same problem. Did you solve it?

@MC-wither
Copy link

How to solve this problem?

Hey, I've met the same problem. Did you solve it?

It seems the input dimensional dismatch of the call function of class nn.CosineEmbeddingLoss().

In the file trains/singleTask/DMD.py, replace the code

cosine_similarity_s_c_l = self.cosine(output['s_l'], output['c_l'], torch.tensor([-1]).cuda()).mean(0)
cosine_similarity_s_c_v = self.cosine(output['s_v'], output['c_v'], torch.tensor([-1]).cuda()).mean(0)
cosine_similarity_s_c_a = self.cosine(output['s_a'], output['c_a'], torch.tensor([-1]).cuda()).mean(0)

with

cosine_similarity_s_c_l = self.cosine(output['s_l'].reshape(-1, 50), output['c_l'].reshape(-1, 50), torch.tensor([-1]).cuda())
cosine_similarity_s_c_v = self.cosine(output['s_v'].reshape(-1, 50), output['c_v'].reshape(-1, 50), torch.tensor([-1]).cuda())
cosine_similarity_s_c_a = self.cosine(output['s_a'].reshape(-1, 50), output['c_a'].reshape(-1, 50), torch.tensor([-1]).cuda())

@ThousandLayerCake
Copy link

ThousandLayerCake commented Nov 23, 2023

How to solve this problem?

Hey, I've met the same problem. Did you solve it?

It seems the input dimensional dismatch of the call function of class nn.CosineEmbeddingLoss().

In the file trains/singleTask/DMD.py, replace the code

cosine_similarity_s_c_l = self.cosine(output['s_l'], output['c_l'], torch.tensor([-1]).cuda()).mean(0)
cosine_similarity_s_c_v = self.cosine(output['s_v'], output['c_v'], torch.tensor([-1]).cuda()).mean(0)
cosine_similarity_s_c_a = self.cosine(output['s_a'], output['c_a'], torch.tensor([-1]).cuda()).mean(0)

with

cosine_similarity_s_c_l = self.cosine(output['s_l'].reshape(-1, 50), output['c_l'].reshape(-1, 50), torch.tensor([-1]).cuda())
cosine_similarity_s_c_v = self.cosine(output['s_v'].reshape(-1, 50), output['c_v'].reshape(-1, 50), torch.tensor([-1]).cuda())
cosine_similarity_s_c_a = self.cosine(output['s_a'].reshape(-1, 50), output['c_a'].reshape(-1, 50), torch.tensor([-1]).cuda())

Hey, Is your solution to do cosine similarity on word Embeddings?
If my code looks like this, is cosine similarity calculated at the sentence level?

                    cosine_similarity_s_c_l = self.cosine(output['s_l'].permute(1,0,2).contiguous().view(output['s_l'].size(1), -1),
                                                          output['c_l'].permute(1,0,2).contiguous().view(output['c_l'].size(1), -1),
                                                          torch.tensor([-1]).cuda()).mean(0)
                    cosine_similarity_s_c_v = self.cosine(output['s_v'].permute(1,0,2).contiguous().view(output['s_l'].size(1), -1),
                                                          output['c_v'].permute(1,0,2).contiguous().view(output['c_l'].size(1), -1),
                                                          torch.tensor([-1]).cuda()).mean(0)
                    cosine_similarity_s_c_a = self.cosine(output['s_a'].permute(1,0,2).contiguous().view(output['s_l'].size(1), -1),
                                                          output['c_a'].permute(1,0,2).contiguous().view(output['c_l'].size(1), -1),
                                                          torch.tensor([-1]).cuda()).mean(0)

@FAFUxrhuang
Copy link

我是在文件trains/singleTask/DMD.py中,替换代码
cosine_similarity_s_c_l = self.cosine(output['s_l'], output['c_l'], torch.tensor([-1]).cuda()).mean(0)
cosine_similarity_s_c_v = self.cosine(output['s_v'], output['c_v'], torch.tensor([-1]).cuda()).mean(0)
cosine_similarity_s_c_a = self.cosine(output['s_a'], output['c_a'], torch.tensor([-1]).cuda()).mean(0)

cosine_similarity_s_c_l = self.cosine(output['s_l'].contiguous().view(labels.size(0),-1), output['c_l'].contiguous().view(labels.size(0),-1), torch.tensor([-1]).cuda()).mean(0)
cosine_similarity_s_c_v = self.cosine(output['s_v'].contiguous().view(labels.size(0),-1), output['c_v'].contiguous().view(labels.size(0),-1), torch.tensor([-1]).cuda()).mean(0)
cosine_similarity_s_c_a = self.cosine(output['s_a'].contiguous().view(labels.size(0),-1), output['c_a'].contiguous().view(labels.size(0),-1), torch.tensor([-1]).cuda()).mean(0)

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

4 participants