Skip to content

autograd doesn't follow broadcasting #1787

@szagoruyko

Description

@szagoruyko

Gradient shape is not adjusted to follow broadcasting and autograd fails. Backwards of these two fail, for example:

  • torch.randn(1) * torch.randn(5,4)
  • torch.randn(4,1) * torch.randn(1,4)

grad/variable shape mismatch:

import torch
from torch.autograd import Variable

# no broadcasting

a = Variable(torch.randn(1), requires_grad=True)
b = Variable(torch.randn(5,4), requires_grad=True)

(a.expand_as(b) * b).sum().backward()

print 'a:', a.size(), a.grad.size()
print 'b:', b.size(), b.grad.size()


# broadcasting failure case

a = Variable(torch.randn(1), requires_grad=True)
b = Variable(torch.randn(5,4), requires_grad=True)

(a * b).sum().backward()

print 'a:', a.size(), a.grad.size() # <-- grad size is different with tensor here
print 'b:', b.size(), b.grad.size()

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions