Skip to content

krysros/hy_autograd

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

hy_autograd

Efficiently computes derivatives with Hy

Installation

pip install -r requirements.txt

Run

hy tanh.hy

Example 1

(import autograd.numpy :as np)
(import autograd [elementwise_grad :as egrad])
(import matplotlib.pyplot :as plt)

(defn tanh [x] (/ (- 1.0 (np.exp (- x))) (+ 1.0 (np.exp (- x)))))

(setv x (np.linspace -7 7 200))

(plt.plot
  x (tanh x)
  x ((egrad tanh) x)                                      ; first derivative
  x ((egrad(egrad tanh)) x)                               ; second derivative
  x ((egrad(egrad(egrad tanh))) x)                        ; third derivative
  x ((egrad(egrad(egrad(egrad tanh)))) x)                 ; fourth derivative
  x ((egrad(egrad(egrad(egrad(egrad tanh))))) x)          ; fifth derivative
  x ((egrad(egrad(egrad(egrad(egrad(egrad tanh)))))) x))  ; sixth derivative

(plt.savefig "tanh.png")

tanh

See the tanh example file for the original code.

Example 2

Example of calculating partial derivatives:

(import autograd [grad :as ∂])
(defn f [x y] (+ (** x 2) (** y 3)))
(setv  dx 0  dy 1)
(print (( f dx) 1. 2.))
(print (( f dy) 1. 2.))

Releases

No releases published

Packages

No packages published

Languages