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

How to solve RuntimeError: For batched 3-D input, hx and cx should also be 3-D but got (2-D, 2-D) tensors #2

Open
augooh opened this issue Dec 19, 2023 · 2 comments

Comments

@augooh
Copy link

augooh commented Dec 19, 2023

No description provided.

@augooh augooh changed the title How to sloveRuntimeError: For batched 3-D input, hx and cx should also be 3-D but got (2-D, 2-D) tensors How to solve RuntimeError: For batched 3-D input, hx and cx should also be 3-D but got (2-D, 2-D) tensors Dec 19, 2023
@augooh
Copy link
Author

augooh commented Dec 19, 2023

I tried modify hidden = (h_.contiguous(), h_.contiguous()) to

        # cell_state = torch.zeros(self.num_layers, batch_size, self.hidden_size).to(self.device)
        # hidden_state = torch.randn(self.num_layers, batch_size, self.hidden_size).to(self.device)
        # cell_state = torch.randn(self.num_layers, batch_size, self.hidden_size).to(self.device)
        # hidden_state = h_.clone().detach().view(self.num_layers, batch_size, self.hidden_size).to(self.device)
        # cell_state = h_.clone().detach().view(self.num_layers, batch_size, self.hidden_size).to(self.device)

        # hidden = (hidden_state, cell_state)

There is no error, but the training results are poor.

@GabSabb
Copy link

GabSabb commented Mar 12, 2024

I got the same issue and replaced your code to

hidden_state = h_.contiguous().detach().clone().view(self.num_layers, batch_size, self.hidden_size).to(self.device)
cell_state = h_.contiguous().detach().clone().view(self.num_layers, batch_size, self.hidden_size).to(self.device) 
hidden = (hidden_state, cell_state)

The training is poor as well:
recon33

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

2 participants