Properly make autograd.Function with scalar variable
<SCODE>class mul_scalar(torch.autograd.Function):
  """
  Customized autograd.Function of
  f(T,s) = s * T,
  where T is a fixed Tensor and s is a Variable
  """

  def forward(self, T, s_var):
    self.save_for_backward(T, s_var)
    return T.mul(s_var[0])

  def backward(self, grad_output):
    T, s_var = self.saved_tensors
    return grad_output.mul(s_var[0]), grad_output.dot(T)
<ECODE> <SCODE>class Net(nn.Module):
  def __init__(self, var=1):
    super(Net, self).__init__()
    self.ms = mul_scalar()
    
  def forward(self, x):
    ...
    self.ms(x, w)
    ...
<ECODE> <SCODE>  def forward(self, x):
    c = Variable(torch.FloatTensor([1]), requires_grad = True)
    ms = mul_scalar()
    z1 = ms(x, c)
<ECODE>
Ja-Keoung_Koo
<SCODE>def mul_scalar(T, s_var):
    # supposes that T is 1D var
    # need to unsqueeze s_var if not
    return T * s_var.expand_as(T)
<ECODE>
fmassa
apaszke
Thanks for kind replies. Is it safe to use functions like expand_as, unsqueeze and so on? I wonder that when backprogating, grad_output is automatically resized.
Ja-Keoung_Koo
Yes, it is safe to use these functions, and they were unit tested so they should be fine.
fmassa