0

I am beginner of using pytorch. I would like to classify 2d binary array (17 * 20 ) to 8 classes, I am using cross entropy as loss function . I have 512 batch size . the input is 512 batches of size (17 * 20 )and the final outpu 512 batches of size 8. I applied the following model , I would like to get the final output to be only list of length 8. like [512,8] but I got that dim [512,680,8] (I printed the dimensions i git from the model after the code). How can I get [512,8] from that network as final output.

 def __init__(self, M=1):
        super(PPS, self).__init__()
        #input layer
        self.layer1 = nn.Sequential(
             nn.Conv2d(17, 680,  kernel_size=1, stride=1, padding=0),
             nn.ReLU())
        self.drop1 = nn.Sequential(nn.Dropout())
        self.batch1 = nn.BatchNorm2d(680)
        self.lstm1=nn.Sequential(nn.LSTM(
        input_size=20,
        hidden_size=16,
        num_layers=1,
        bidirectional=True,
        batch_first= True))
        self.gru = nn.Sequential(nn.GRU(
            input_size=16*2,
            hidden_size=16,
            num_layers=2,
            bidirectional=True,
            batch_first=True))
        self.fc1 = nn.Linear(16*2,8)

    def forward(self, x):
     
        out = self.layer1(x)
        out = self.drop1(out)
        out = self.batch1(out)
        out = out.squeeze()
        out,_ = self.lstm1(out)
        out,_ = self.gru(out)
        out = self.fc1(out)
        return out
cov2d torch.Size([512, 680, 20, 1])
drop torch.Size([512, 680, 20, 1])
batch torch.Size([512, 680, 20])
lstm1 torch.Size([512, 680, 32])
lstm2 torch.Size([512, 680, 32])
linear1 torch.Size([512, 680, 8])
No Na
  • 27
  • 2
  • 7

1 Answers1

0

If you want the output to be (512, 8) then you would have to change your last linear layer to something like this:

def __init__(self, M=1):
    ...
    self.gru = nn.Sequential(nn.GRU(
            input_size=16*2,
            hidden_size=16,
            num_layers=2,
            bidirectional=True,
            batch_first=True))
    self.fc1 = nn.Linear(680 * 16*2, 8)

    def forward (self, x):
        ...
        out, _ = self.gru(out)
        out = self.fc1(out.reshape(-1, 680 * 16*2))
        return out

The goal is to reduce the number of feature from 680 * 16 * 2 to 8. You can (and probably should) add more final linear layers that will do this reduction for you.

Nerveless_child
  • 1,366
  • 2
  • 15
  • 19
  • Thank you , I tried that solution , but it gave me an Error out = self.fc1(out.view(-1, 680 * 16*2)) RuntimeError: view size is not compatible with input tensor's size and stride (at least one dimension spans across two contiguous subspaces). Use .reshape(...) instead – No Na Feb 15 '21 at 10:26
  • You can do `.reshape()` like the error message suggested. I have edited my answer, check that. – Nerveless_child Feb 15 '21 at 14:03