torch_gradient.py

View page source

Calculate Gradient Using PyTorch

Syntax

      grad cmpad.torch.gradient ( algo )
      grad . setup ( option )
      g = grad ( x )

Purpose

This uses PyTorch to implement a py_fun_obj that computes the gradient of the last component of values computed by algo .

algo

This is a py_fun_obj where the input and output vectors have type torch.tensor with float elements . The last range space component, computed by algo , defines the scalar function that the gradient is for.

grad

This is a py_fun_obj where the input and output vectors have elements of type float .

x

This is a numpy vector of float with length option [ 'n_arg' ] . It is the argument value at which to compute the gradient.

g

This is a numpy vector of float with length option [ 'n_arg' ] . It is the value of the gradient ad x .

Example

The file xam_grad_torch.py contains an example and test using this class.

Source Code

#
# imports
import torch
#
# gradient
class gradient :
   #
   def __init__(self, algo) :
      self.algo   = algo
      self.option = None
   #
   def option(self) :
      return self.optiion
   #
   def domain(self) :
      return self.option['n_arg']
   #
   def range(self) :
      return self.option['n_arg']
   #
   #
   def setup(self, option) :
      assert type(option) == dict
      assert 'n_arg' in option
      #
      # self.option
      self.option = option
      #
      # self.algo
      self.algo.setup(option)
      #
      # self.n_arg
      self.n_arg = self.algo.domain()
      assert self.n_arg == option['n_arg']
      #
   #
   # call
   def __call__(self, x) :
      # See https://discuss.torch.org/t/
      #     how-does-one-reuse-the-autograd-computational-graph/190447/2
      assert len(x) == self.n_arg
      ax = torch.tensor(x, requires_grad = True)
      az = self.algo(ax)
      az[-1].backward()
      return ax.grad