how to change the hidden size? "Expected hidden size (1, 512, 64), got [1, 128, 64]"

34 views Asked by At

I'm slowly getting desperate with this problem. I'm trying to write a neural network for time series, and I just can't figure out this error:

x, _ = self.gru(x, h0)
  File "C:\ProgramData\Anaconda3\envs\tf_gpu\lib\site-packages\torch\nn\modules\module.py", line 1518, in _wrapped_call_impl
    return self._call_impl(*args, **kwargs)
  File "C:\ProgramData\Anaconda3\envs\tf_gpu\lib\site-packages\torch\nn\modules\module.py", line 1527, in _call_impl
    return forward_call(*args, **kwargs)
  File "C:\ProgramData\Anaconda3\envs\tf_gpu\lib\site-packages\torch\nn\modules\rnn.py", line 1100, in forward
    self.check_forward_args(input, hx, batch_sizes)
  File "C:\ProgramData\Anaconda3\envs\tf_gpu\lib\site-packages\torch\nn\modules\rnn.py", line 273, 
in check_forward_args
    self.check_hidden_size(hidden, expected_hidden_size)
  File "C:\ProgramData\Anaconda3\envs\tf_gpu\lib\site-packages\torch\nn\modules\rnn.py", line 256, 
in check_hidden_size
    raise RuntimeError(msg.format(expected_hidden_size, list(hx.size())))
RuntimeError: Expected hidden size (1, 512, 64), got [1, 128, 64]
PS C:\Users\Claudius\Documents\Traden\OrderbookTrading\lob-deep-learning-main> 

the necessary Code:



 class Lobster(nn.Module):
    def __init__(self, lighten):
        super().__init__()
        self.device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
        self.name = 'lobster'
        if lighten:
            self.name += '-lighten'       
        # gru layers
        self.dropout = nn.Dropout(p=0.1)
        self.gru = nn.GRU(input_size=192, hidden_size=64, num_layers=1, batch_first=True)
        self.fc1 = nn.Linear(64, 3)

  
 def forward(self, x):
        #initialzustand ist ein tensor mit nullen und der dimension 1,128,64
        #64 = hidden size
        #128 = batchsize
        print("----------x.size",x.size(0))
        #h0 = torch.zeros(1, x.size(0), 64).to(self.device)#original
        h0 = torch.zeros(1, x.size(0), 64).to(self.device)

        x = self.conv1(x)
        x = self.conv2(x)
        x = self.conv3(x)

        x_inp1 = self.inp1(x)
        x_inp2 = self.inp2(x)
        x_inp3 = self.inp3(x)

        x = torch.cat((x_inp1, x_inp2, x_inp3), dim=1)

        x = x.permute(0, 2, 1, 3)
        x = torch.reshape(x, (-1, x.shape[1], x.shape[2]))
        x = self.dropout(x)
        x, _ = self.gru(x, h0)
        x = x[:, -1, :]

        x = self.fc1(x)
        forecast_y = torch.softmax(x, dim=1)

        return forecast_y`


ChatGpt always said to change the "x.size(0)". also it said i should change the hidden_size of the gru, but this doesnt help any further.

if i follow gpt´s advice. Then the error looks like this:

in check_hidden_size raise RuntimeError(msg.format(expected_hidden_size, list(hx.size()))) RuntimeError: Expected hidden size (1, 1, 64), got [1, 512, 64]

I would be very, very grateful for your help! Best regards, Claudius

0

There are 0 answers