Questions tagged [automatic-differentiation]

Also known as algorithmic differentiation, short AD. Techniques that take a procedure evaluating a numerical function and transform it into a procedure that additionally evaluates directional derivatives, gradients, higher order derivatives.

Also known as algorithmic differentiation, short AD. Techniques that take a procedure evaluating a numerical function and transform it into a procedure that additionally evaluates directional derivatives, gradients, higher order derivatives.

Techniques include operator

  • overloading for dual numbers,
  • operator overloading to extract the operations sequence as a tape,
  • code analysis and transformation.

For a function with input of dimension n and output of dimension n, requiring L elementary operations for its evaluation, one directional derivative or one gradient can be computed with 3*L operations.

The accuracy of the derivative is, automatically, nearly as good as the accuracy of the function evaluation.

Other differentiation method are

  • symbolic differentiation, where the expanded expression for the derivatives is obtained first, which can be large depending on the implementation, and
  • numerical differentiation by divided differences, which provides less accuracy with comparable effort, or comparable accuracy with a higher effort.

See wikipedia and autodiff.org

192 questions
3
votes
1 answer

Partial Derivative using Autograd

I have a function that takes in a multivariate argument x. Here x = [x1,x2,x3]. Let's say my function looks like: f(x,T) = np.dot(x,T) + np.exp(np.dot(x,T) where T is a constant. I am interested in finding df/dx1, df/dx2 and df/dx3 functions. I have…
3
votes
2 answers

Why is a function type required to be "wrapped" for the type checker to be satisfied?

The following program type-checks: {-# LANGUAGE RankNTypes #-} import Numeric.AD (grad) newtype Fun = Fun (forall a. Num a => [a] -> a) test1 [u, v] = (v - (u * u * u)) test2 [u, v] = ((u * u) + (v * v) - 1) main = print $ fmap (\(Fun f) -> grad…
3
votes
1 answer

how does the pytorch autograd work?

I submitted this as an issue to cycleGAN pytorch implementation, but since nobody replied me there, i will ask again here. I'm mainly puzzled by the fact that multiple forward passes was called before one single backward pass, see the following in…
yfi
  • 152
  • 1
  • 12
3
votes
1 answer

Numeric.AD - type variable escaping its scope

I'm trying to use automatic differentiation in Haskell for a nonlinear control problem, but have some problems getting it to work. I basically have a cost function, which should be optimized given an initial state. The types are: data Reference a =…
bzn
  • 2,362
  • 1
  • 17
  • 20
3
votes
1 answer

Avoid sorting args in Python module Sympy

I am currently developing a differential operator for sympy that can be placed in matricial form. In this case the order of the args list when creating a Mul object is very important to guarantee that the differentiation is performed where it is…
Saullo G. P. Castro
  • 56,802
  • 26
  • 179
  • 234
2
votes
0 answers

How do I manually set partial derivatives for a multi-input function in pytorch?

I am writing a machine learning program for my PhD which finds poles of a rational function which approximates the solution to a given differential equation. To calculate the loss, I need to calculate this estimate which is given as a function with:…
2
votes
0 answers

Computing Gradients of Weighted State Averages in PyTorch Models

I have two trained models with the same architecture and different performances. I want to construct a new model by taking the weighted average of their states (s1, s2, s1+s2=1) and calculate the gradients of the new model's loss with respect to s1…
2
votes
2 answers

How does automatic differentiation with respect to the input work?

I've been trying to understand how automatic differentiation (autodiff) works. There are several implementations of this that can be found in Tensorflow, PyTorch and other programs. There are three aspects of automatic differentiation that currently…
2
votes
0 answers

Using Reversediff in Julia throws a weird type error that I don't understand

I have the following function error = prior_error(data,s_vals,ones(20)/20) This executes fine. I now package the arguments up as follows inputs = (data,s_vals,ones(20)/20) And then try and get the gradient test =…
Pablo
  • 220
  • 2
  • 13
2
votes
1 answer

TypeError in Julia/Turing when sampling for a forced differential equation

I am new to Julia and Turing and am trying to fit a forced 0-D box ODE to data, but I get type error when doing sampling. Following this page (solve system of ODEs with read in external forcing), I added an interpolation handle of the forcing as a…
2
votes
1 answer

Calculating the Hessian Vector Product of a Flax NN output wrt to the inputs

I am trying to get the second derivative of the output w.r.t the input of a neural network built using Flax. The network is structured as follows: import numpy as np import jax import jax.numpy as jnp import flax.linen as nn import optax from flax…
Vignesh Gopakumar
  • 143
  • 1
  • 3
  • 7
2
votes
1 answer

Is there a PyTorch equivalent of tf.custom_gradient()?

I am new to PyTorch but have a lot of experience with TensorFlow. I would like to modify the gradient of just a tiny piece of the graph: just the derivative of activation function of a single layer. This can be easily done in Tensorflow using…
2
votes
2 answers

Differentiation of an improper integral using JAX and SciPy

I provide a simple code example of a failed attempt to use JAX to automatically differentiate through an improper integral function making use of SciPy's quad() method. The function I consider is with gradient given by The following code is able…
PDRX
  • 1,003
  • 1
  • 11
  • 15
2
votes
1 answer

What is the alternative of elementwise_grad of autograd in JAX?

I want to solve a second order differential equation with neural network. For automatic differentiation I am using JAX library. To compute first order and second order derivative of my target variable 'u' i.e to compute du/dx and d2u/dx2…
2
votes
1 answer

Julia Roots find_zero with ForwardDiff.Dual type?

I'm trying to apply automatic differentiation (ForwardDiff) to a function that contains an instance of find_zero (Roots) and am encountering an error that seems to relate to find_zero not accepting the ForwardDiff.Dual type. Here's a (contrived)…
A. Harris
  • 117
  • 3