Auto Differentiation Tools
Project description
cs107-FinalProject, Group 16
Group members | |
---|---|
Connor Capitolo | connorcapitolo@g.harvard.edu |
Kexin Huang | kexinhuang@hsph.harvard.edu |
Haoxin Li | haoxin_li@hsph.harvard.edu |
Chen Lucy Zhang | chz522@g.harvard.edu |
Installation (we assume you are familiar with virtual environments and Git)
To install from pip:
pip install apollo_ad
To install from source:
# install virtualenv
pip install virtualenv
# create virtual environment
virtualenv apollo_ad_env
# activate virtual environment
source apollo_ad_env/bin/activate
# clone from GitHub
git clone https://github.com/West-Coast-Quaranteam/cs107-FinalProject.git
# get into the folder
cd cs107-FinalProject
# install requirements
pip install -r requirements.txt
Running Python Interactive Session From Command Line (we assume you have Python properly installed)
From your command line, please run a command similar to the one below to access the Python interactive session:
python
If done correctly, this is the prompt you should now see:
>>>
You're ready to use Apollo AD!
Elemental Functions and Examples
Here is a non-exhaustive list of some of the operations available in Apollo AD, and how to use them:
Elemental Function | Example |
---|---|
Addition | x + y |
Multiplication | x * y |
Subtraction | x - y |
Division | x / y |
Negation | -x |
Power | x ** 2 |
Log | log(x) |
Square root | sqrt(x) |
Exponential | exp(x) |
Tangent | tan(x) |
Cosine | cos(x) |
Sine | sin(x) |
Inverse Tangent | arctan(x) |
Inverse Cosine | arccos(x) |
Inverse Sin | arcsin(x) |
Hyperbolic Tangent | tanh(x) |
Hyperbolic Cosine | cosh(x) |
Hyperbolic Sin | sinh(x) |
We've also implemented the basic comparison operators for you to use.
Examples
UI
We also provide a nice interface, where you can specify the variable values and functions without any coding:
from apollo_ad import UI
UI()
Click here for an UI interaction example!
Welcome to Apollo AD Library!
Enter the number of variables:
2
Enter the number of functions:
3
Type the variable name of variable No. 1 (Please only input a name that CANNOT be cast as an integer or float):
a
Type the value of variable a (Please only input a float):
3
Type the derivative seed of variable a (Please only input a float; your default input should be 1):
1
Type the variable name of variable No. 2 (Please only input a name that CANNOT be cast as an integer or float):
b
Type the value of variable b (Please only input a float):
2
Type the derivative seed of variable b (Please only input a float; your default input should be 1):
1
Type function No. 1 :
a + b + sin(b)
Type function No. 2 :
sqrt(a) + log(b)
Type function No. 3 :
exp(a * b) + a ** 2
---- Summary ----
Variable(s):
{'a': '3', 'b': '2'}
Function(s):
a + b + sin(b)
sqrt(a) + log(b)
exp(a * b) + a ** 2
---- Computing Gradients ----
# of variables < # of functions ====> automatically use the forward mode!
---- Output ----
-- Values --
Function F1: 5.909297426825682
Function F2: 2.4251979881288226
Function F3: 412.4287934927351
-- Gradients --
Function F1: [1. 0.58385316]
Function F2: [0.28867513 0.5 ]
Function F3: [ 812.85758699 1210.28638048]
Programming Usage
apollo_ad
expects two inputs: a dictionary with variable name as the key and variable value as the value, as well as a list of strings where each string describes a function:
from apollo_ad import auto_diff
var = {'x': 0.5, 'y': 4}
fct = ['cos(x) + y ** 2',
'2 * log(y) - sqrt(x)/3',
'sqrt(x)/3',
'3 * sinh(x) - 4 * arcsin(x) + 5']
out = auto_diff(var, fct)
print(out)
# -- Values --
# Function F1: 16.87758256189037
# Function F2: 2.5368864618442655
# Function F3: 0.23570226039551587
# Function F4: 4.468890814088047
# -- Gradients --
# Function F1: [-0.47942554 8. ]
# Function F2: [-0.23570226 0.5 ]
# Function F3: [0.23570226 0. ]
# Function F4: [-1.23592426 -4.61880215]
apollo_ad
supports both forward and reverse mode in the backend, where the auto_diff
class automatically detects which is the best way to use, depending on the number of inputs and outputs. You can also directly use the Forward
and Reverse
mode class. Remember that both forward and reverse mode should produce the same results.
from apollo_ad import Forward, Reverse
var = {'x': 3, 'y': 4}
fct = ['cos(x) + y ** 2', '2 * log(y) - sqrt(x)/3']
out = Forward(var, fct)
print(out)
# -- Values --
# Function F1: 15.010007503399555
# Function F2: 3.2938507417182654
# -- Gradients --
# Function F1: [-0.14112001 8. ]
# Function F2: [0.23710829 0.5 ]
var = {'x': 1, 'y': 2}
fct = ['x * y + exp(x * y)', 'x + 3 * y']
out = Reverse(var, fct)
print(out)
# -- Values --
# Function F1: 9.38905609893065
# Function F2: 7.0
# -- Gradients --
# Function F1: [16.7781122 8.3890561]
# Function F2: [1. 3.]
You can also specify the seed. As the seed in forward mode is for each variable whereas in reverse mode it's for each function, we expect a dictionary for forward mode and a list for reverse mode. Here is an example:
var = {'x': 3, 'y': 4}
seed = {'x': 1, 'y': 2}
fct = ['cos(x) + y ** 2', '2 * log(y) - sqrt(x)/3']
z = auto_diff(var, fct, seed)
# -- Values --
# Function F1: 15.010007503399555
# Function F2: 2.1952384530501554
# -- Gradients --
# Function F1: [-0.14112001 16. ]
# Function F2: [-0.09622504 1. ]
You can also run the above examples by typing:
from apollo_ad import demo
demo()
Additional Notes
If you are trying to access the attributes of this package, please note that they will be lists, numpy arrays, or dictionaries.
Most importantly, have fun and provide us feedback!
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file apollo_ad-0.0.7.tar.gz
.
File metadata
- Download URL: apollo_ad-0.0.7.tar.gz
- Upload date:
- Size: 16.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/51.0.0 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.8.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | cea08da41071efd73c2760d2ac58980c85143ca5526cbee2bd980f1cbebedf16 |
|
MD5 | cc5ff23c5d534b6be5419d2caf8c00e4 |
|
BLAKE2b-256 | 2b9fd2c00a95bc008cb2c1c4edd49001fcfeb5b16f73adcbc1e71c7bc7607cde |
File details
Details for the file apollo_ad-0.0.7-py3-none-any.whl
.
File metadata
- Download URL: apollo_ad-0.0.7-py3-none-any.whl
- Upload date:
- Size: 14.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/51.0.0 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.8.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d844f1c7134c359b6b8fa9c87668a545a6ac21790188e87dee47d3f4d2b6e99b |
|
MD5 | f93a3ade94ecd974994b201781ad5787 |
|
BLAKE2b-256 | 5544aad583b2d5f2b2983392bbe4b734e0dfd20f69614505f8999a31d1f2e9db |