简体   繁体   中英

Dimension out of range when applying l2 normalization in Pytorch

I'm getting a runtime error:

RuntimeError: Dimension out of range (expected to be in range of [-1, 0], but got 1)`

and can't figure out how to fix it.

The error appears to refer to the line:

i_enc = F.normalize(input =i_batch, p=2, dim=1, eps=1e-12)  # (batch, K, feat_dim)

I'm trying to encode image features (batch x 36 x 2038) by applying a L2 norm. Below is the full code for the section.

def forward(self, q_batch, i_batch):

    # batch size = 512
    # q -> 512(batch)x14(length)
    # i -> 512(batch)x36(K)x2048(f_dim)
    # one-hot -> glove
    emb = self.embed(q_batch)
    output, hn = self.gru(emb.permute(1, 0, 2))  
    q_enc = hn.view(-1,self.h_dim)

    # image encoding with l2 norm
    i_enc = F.normalize(input =i_batch, p=2, dim=1, eps=1e-12)  # (batch, K, feat_dim)


    q_enc_copy = q_enc.repeat(1, self.K).view(-1, self.K, self.h_dim)

    q_i_concat = torch.cat((i_enc, q_enc_copy), -1)
    q_i_concat = self.non_linear(q_i_concat, self.td_W, self.td_W2 )#512 x 36 x 512
    i_attention = self.att_w(q_i_concat)  #512x36x1
    i_attention = F.softmax(i_attention.squeeze(),1)
    #weighted sum
    i_enc = torch.bmm(i_attention.unsqueeze(1), i_enc).squeeze()  # (batch, feat_dim)

    # element-wise multiplication
    q = self.non_linear(q_enc, self.q_W, self.q_W2)
    i = self.non_linear(i_enc, self.i_W, self.i_W2)
    h = torch.mul(q, i)  # (batch, hid_dim)

    # output classifier
    # BCE with logitsloss
    score = self.c_Wo(self.non_linear(h, self.c_W, self.c_W2))

    return score

I would appreciate any help. Thanks

I would suggest to check the shape of i_batch (eg print(i_batch.shape) ), as I suspect i_batch has only 1 dimension (eg of shape [N] ).

This would explain why PyTorch is complaining you can normalize only over the dimension #0; while you are asking for the operation to be done over a dimension #1 (cf dim=1 ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM