Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Partial derivative of a function with numpy.array input

I have a function like:

def fun(A, B, C):
    return numpy.sum(numpy.dot(A, B)) + numpy.sum(C)

Where A, B, C are 2-dimensional numpy.array, and the return value is a float value

How can I get the partial derivative of fun(A,B,C) with regard to A, B, or c? (and the partial derivatives will also be numpy.array)

I'm ready to use libaries like numpy and scipy, but not symbolic libraries.

like image 423
theincluder Avatar asked Dec 11 '25 10:12

theincluder


1 Answers

For numerical differentiation, you can use numdifftools.

import numpy as np
import numdifftools

def fun(A, B, C):
    return numpy.sum(numpy.dot(A, B)) + numpy.sum(C)

def fun_A(A, B, C):
    J = numdifftools.Jacobian(lambda z: fun(z.reshape(A.shape), B, C).ravel())
    return J(A.ravel()).reshape(A.shape)


np.random.seed(1234)
A = np.random.rand(30,30)
B = np.random.rand(30,30)
C = np.random.rand(30,30)
print fun_A(A,B,C)[3,5]
# -> 14.9081790839

# Verify result manually
Ap = A.copy()
Ap[3,5] += 1e-6
print (fun(Ap,B,C) - fun(A,B,C)) / 1e-6
# -> 14.908178855

You can also easily cook up your own naive numerical differention routine using the latter approach just by incrementing each matrix element at a time by a small amount.

like image 152
pv. Avatar answered Dec 12 '25 23:12

pv.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!