解决Indexerror: dimension out of range (expected to be in range of [-1, 0], but got 1)

Problem Description

When reproducing the code, adjust the batch_size to 1, and softmax reports the following error:

# Super Sketch Network links a RNN and CNN together with an attention layer in the last layer.
class SSN(nn.Module):
    
    def __init__(self, cnn_model_name,rnn_model_name, d_frozen = True,num_classes=40):
        pass
                
        
    def forward(self, images,strokes):
        cnn_output,cnn_f = self.cnn(images)
        rnn_output,rnn_f = self.rnn(strokes,None)
        
        #Attention Layer linking RNN and CNN together.
        output = torch.stack([cnn_output,rnn_output],dim = 1)
        
        #Get the center feature
        ssn_feat = torch.cat((cnn_f,rnn_f),dim = 1)
        att_score = torch.matmul(output, self.attention).squeeze()
        att_score = F.softmax(att_score,dim = 1).view(output.size(0), output.size(1), 1)
        score = output * att_score

        score = torch.sum(score, dim=1)
        
        return score,ssn_feat

Indexerror: dimension out of range (expected to be in range of [-1, 0], but got 1)

solution

When using the softmax function, it is necessary to ensure that the matrix is ​​two-dimensional, but when batch_size=1, the dimension of the entire output matrix is 

[batch_size =1,maxlen,1],

If the dimension of output.squeeze is not specified, the dimension of [maxlen] will be obtained and an error will be reported.

The solution is to manually increase the dimension of the tensor. In PyTorch, torch.unsqueezetensors can be increased in dimension using functions. This function adds a dimension at the specified position (the last dimension by default), increasing the dimension of the tensor by 1. For example, for a (3, 4)tensor of shape , using torch.unsqueeze(input, dim=0)will yield a (1, 3, 4)tensor of shape . The specific usage is as follows:

import torch

# 假设输入的张量为tensor,shape为(n,)
tensor = torch.tensor([1, 2, 3])

# 增加一个维度,变成(1, n)
tensor = torch.unsqueeze(tensor, dim=0)

Applied to this code is:

# Super Sketch Network links a RNN and CNN together with an attention layer in the last layer.
class SSN(nn.Module):
    
    def __init__(self, cnn_model_name,rnn_model_name, d_frozen = True,num_classes=40):
        pass
                
        
    def forward(self, images,strokes):
        cnn_output,cnn_f = self.cnn(images)
        rnn_output,rnn_f = self.rnn(strokes,None)
        
        #Attention Layer linking RNN and CNN together.
        output = torch.stack([cnn_output,rnn_output],dim = 1)
        
        #Get the center feature
        ssn_feat = torch.cat((cnn_f,rnn_f),dim = 1)
        att_score = torch.matmul(output, self.attention).squeeze()
        att_score = torch.unsqueeze(att_score,dim=0)
        att_score = F.softmax(att_score,dim = 1).view(output.size(0), output.size(1), 1)
        score = output * att_score

        score = torch.sum(score, dim=1)
        
        return score,ssn_feat

Guess you like

Origin blog.csdn.net/qq_54708219/article/details/130232096