twitter-archive / torch-autograd

Autograd automatically differentiates native Torch code
Apache License 2.0
560 stars 114 forks source link

gradfuns.lua dimension problem on line 130 #176

Open HeylenJonas opened 7 years ago

HeylenJonas commented 7 years ago

Hi, Still new to all of this so I'm not sure if this problem is caused by some underlying problem in my framework. On line 130 of gradfuns.lua: return torch.narrow(g, dim, torch.size(x, dim) + 1, torch.size(y, dim)) torch.size(x,dim) errors at some point. The function is called multiple times. First x is a tesor of size 3000x10, later 3000x9 and so on... and dim is 2. So far no problem. Finally x is a vector of 3000 and dim is 2 -> error. I fixed it for me by adding a few lines of code:

    if (torch.nDimension(x)>1) then
        size_x_dim = torch.size(x, dim)
    else
        size_x_dim = 1
    end
    local size_y_dim
    if (torch.nDimension(y)>1) then
        size_y_dim = torch.size(y, dim)
    else
        size_y_dim = 1
    end
    return torch.narrow(g, dim, size_x_dim + 1, size_y_dim)

Not the nicest way, but working. I'm not sure why the code is not giving x as a tensor of size 3000x1 instead of a vector of size 3000?

Thanks, Jonas