Skip to content

Commit 9121e66

Browse files
authored
Initial commit
0 parents  commit 9121e66

17 files changed

+753
-0
lines changed

.flake8

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#########################
2+
# Flake8 Configuration #
3+
# (.flake8) #
4+
#########################
5+
[flake8]
6+
ignore =
7+
# asserts are ok when testing.
8+
S101
9+
# pickle
10+
S301
11+
# pickle
12+
S403
13+
S404
14+
S603
15+
# Line break before binary operator (flake8 is wrong)
16+
W503
17+
# Ignore the spaces black puts before columns.
18+
E203
19+
# allow path extensions for testing.
20+
E402
21+
DAR101
22+
DAR201
23+
# flake and pylance disagree on linebreaks in strings.
24+
N400
25+
exclude =
26+
.tox,
27+
.git,
28+
__pycache__,
29+
docs/source/conf.py,
30+
build,
31+
dist,
32+
tests/fixtures/*,
33+
*.pyc,
34+
*.bib,
35+
*.egg-info,
36+
.cache,
37+
.eggs,
38+
data.
39+
max-line-length = 120
40+
max-complexity = 20
41+
import-order-style = pycharm
42+
application-import-names =
43+
seleqt
44+
tests

.github/workflows/test.yml

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
name: Tests
2+
3+
on: [ push, pull_request ]
4+
5+
jobs:
6+
tests:
7+
name: Tests
8+
runs-on: ${{ matrix.os }}
9+
strategy:
10+
matrix:
11+
os: [ ubuntu-latest ]
12+
python-version: [3.11.0]
13+
steps:
14+
- uses: actions/checkout@v2
15+
- name: Set up Python ${{ matrix.python-version }}
16+
uses: actions/setup-python@v2
17+
with:
18+
python-version: ${{ matrix.python-version }}
19+
- name: Install dependencies
20+
run: pip install nox
21+
- name: Test with pytest
22+
run:
23+
nox -s test
24+
lint:
25+
name: Lint
26+
runs-on: ubuntu-latest
27+
strategy:
28+
matrix:
29+
python-version: [3.11.0]
30+
steps:
31+
- uses: actions/checkout@v2
32+
- name: Set up Python ${{ matrix.python-version }}
33+
uses: actions/setup-python@v2
34+
with:
35+
python-version: ${{ matrix.python-version }}
36+
- name: Install dependencies
37+
run: pip install nox
38+
- name: Run flake8
39+
run: nox -s lint
40+
- name: Run mypy
41+
run: nox -s typing

.gitignore

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
.vscode/
2+
3+
# Byte-compiled / optimized / DLL files
4+
__pycache__/
5+
*.py[cod]
6+
*$py.class
7+
8+
# C extensions
9+
*.so
10+
11+
# Distribution / packaging
12+
.Python
13+
build/
14+
develop-eggs/
15+
dist/
16+
downloads/
17+
eggs/
18+
.eggs/
19+
lib/
20+
lib64/
21+
parts/
22+
sdist/
23+
var/
24+
wheels/
25+
share/python-wheels/
26+
*.egg-info/
27+
.installed.cfg
28+
*.egg
29+
MANIFEST
30+
31+
# PyInstaller
32+
# Usually these files are written by a python script from a template
33+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
34+
*.manifest
35+
*.spec
36+
37+
# Installer logs
38+
pip-log.txt
39+
pip-delete-this-directory.txt
40+
41+
# Unit test / coverage reports
42+
htmlcov/
43+
.tox/
44+
.nox/
45+
.coverage
46+
.coverage.*
47+
.cache
48+
nosetests.xml
49+
coverage.xml
50+
*.cover
51+
*.py,cover
52+
.hypothesis/
53+
.pytest_cache/
54+
cover/
55+
56+
# Translations
57+
*.mo
58+
*.pot
59+
60+
# Django stuff:
61+
*.log
62+
local_settings.py
63+
db.sqlite3
64+
db.sqlite3-journal
65+
66+
# Flask stuff:
67+
instance/
68+
.webassets-cache
69+
70+
# Scrapy stuff:
71+
.scrapy
72+
73+
# Sphinx documentation
74+
docs/_build/
75+
76+
# PyBuilder
77+
.pybuilder/
78+
target/
79+
80+
# Jupyter Notebook
81+
.ipynb_checkpoints
82+
83+
# IPython
84+
profile_default/
85+
ipython_config.py
86+
87+
# pyenv
88+
# For a library or package, you might want to ignore these files since the code is
89+
# intended to run in multiple environments; otherwise, check them in:
90+
# .python-version
91+
92+
# pipenv
93+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
94+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
95+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
96+
# install all needed dependencies.
97+
#Pipfile.lock
98+
99+
# poetry
100+
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
101+
# This is especially recommended for binary packages to ensure reproducibility, and is more
102+
# commonly ignored for libraries.
103+
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
104+
#poetry.lock
105+
106+
# pdm
107+
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
108+
#pdm.lock
109+
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
110+
# in version control.
111+
# https://pdm.fming.dev/#use-with-ide
112+
.pdm.toml
113+
114+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
115+
__pypackages__/
116+
117+
# Celery stuff
118+
celerybeat-schedule
119+
celerybeat.pid
120+
121+
# SageMath parsed files
122+
*.sage.py
123+
124+
# Environments
125+
.env
126+
.venv
127+
env/
128+
venv/
129+
ENV/
130+
env.bak/
131+
venv.bak/
132+
133+
# Spyder project settings
134+
.spyderproject
135+
.spyproject
136+
137+
# Rope project settings
138+
.ropeproject
139+
140+
# mkdocs documentation
141+
/site
142+
143+
# mypy
144+
.mypy_cache/
145+
.dmypy.json
146+
dmypy.json
147+
148+
# Pyre type checker
149+
.pyre/
150+
151+
# pytype static type analyzer
152+
.pytype/
153+
154+
# Cython debug symbols
155+
cython_debug/
156+
157+
# PyCharm
158+
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
159+
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
160+
# and can be added to the global gitignore or merged into this file. For a more nuclear
161+
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
162+
#.idea/

README.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Optimization Exercise
2+
During this exercise, you will implement gradient descent to solve optimization problems.
3+
For all activities, the task is to find the lowest point on a function surface or in
4+
mathematical language
5+
6+
$$ \min_{x}f(x) .$$
7+
8+
All modules in `src` require your attention.
9+
- To get started, take a look at `src/optimize_1d.py`.
10+
Use the gradient to find the minumum of the parabola starting from five, or in other words
11+
12+
$$ \min_{x} x^2, \text{ with } x_0 = 5 .$$
13+
14+
The problem is illustrated below:
15+
16+
![parabola_task](./figures/parabola_task.png)
17+
18+
19+
- Next we consider a paraboloid, $\cdot$ denotes the scalar product,
20+
21+
$$ \min_{\mathbf{x}} \mathbf{x} \cdot \mathbf{x}, \text{ with } \mathbf{x_0} = (2.9, -2.9) .$$
22+
23+
The paraboloid is already implemented in `src/optimize_2d.py`.
24+
Your task is to solve this problem using two-dimensional gradient descent.
25+
Once more the problem is illustrated below:
26+
27+
![paraboloid_task](./figures/paraboloid_task.png)
28+
29+
30+
- Additionally we consider a bumpy paraboloid, $\cdot$ denotes the scalar product,
31+
32+
$$ \min_{\mathbf{x}} \mathbf{x} \cdot \mathbf{x} + \cos(2 \pi x_0) + \sin(2 \pi x_1 ), \text{ with } \mathbf{x_0} = (2.9, -2.9) .$$
33+
34+
The addtional sin and cosine terms will require momentum for convergence.
35+
The bumpy paraboloid is already implemented in `src/optimize_2d_momentum_bumpy.py`.
36+
Your task is to solve this problem using two-dimensional gradient descent with momentum.
37+
Once more the problem is illustrated below:
38+
39+
![bumpy_paraboloid_task](./figures/bumpy_paraboloid_task.png)
40+
41+
42+
- Finally, to explore the automatic differentiation functionality we consider the problem,
43+
44+
$$ \min_{\mathbf{x}} \mathbf{x} \cdot \mathbf{x} + \cos(2 \pi x_0 ) + \sin(2 \pi x_1) + 0.5 \cdot \text{relu}(x_0) + 10 \cdot \tanh( \|\mathbf{x} \| ), \text{ with } \mathbf{x_0} = (2.9, -2.9) .$$
45+
46+
The function is already defined in `src/optimize_2d_momentum_bumpy_torch.py`. We dont have to find the gradient by hand!
47+
Use `torch.func.grad` [(torch-documentation)](https://pytorch.org/docs/stable/generated/torch.func.grad.html) to compute the gradient automatically. Use the result to find the minimum using momentum.
48+
49+
While coding use `nox -s test`, `nox -s lint`, and `nox -s typing` to check your code.
50+
Autoformatting help is available via `nox -s format`.
51+
Feel free to read mode about nox at https://nox.thea.codes/en/stable/ .

figures/bumpy_paraboloid_task.png

20.7 KB
Loading

figures/parabola_task.png

21.9 KB
Loading

figures/paraboloid_task.png

16.2 KB
Loading

noxfile.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
"""This module implements our CI function calls."""
2+
3+
import nox
4+
5+
6+
@nox.session(name="test")
7+
def run_test(session):
8+
"""Run pytest."""
9+
session.install("-r", "requirements.txt")
10+
session.install("pytest")
11+
session.run("pytest")
12+
13+
14+
@nox.session(name="lint")
15+
def lint(session):
16+
"""Check code conventions."""
17+
session.install("flake8==4.0.1")
18+
session.install(
19+
"flake8-black",
20+
"flake8-docstrings",
21+
"flake8-bugbear",
22+
"flake8-broken-line",
23+
"pep8-naming",
24+
"pydocstyle",
25+
"darglint",
26+
)
27+
session.run("flake8", "src", "tests", "noxfile.py")
28+
29+
30+
@nox.session(name="typing")
31+
def mypy(session):
32+
"""Check type hints."""
33+
session.install("-r", "requirements.txt")
34+
session.install("mypy")
35+
session.run(
36+
"mypy",
37+
"--install-types",
38+
"--non-interactive",
39+
"--ignore-missing-imports",
40+
"--no-strict-optional",
41+
"--no-warn-return-any",
42+
"--implicit-reexport",
43+
"--allow-untyped-calls",
44+
"src",
45+
)
46+
47+
48+
@nox.session(name="format")
49+
def format(session):
50+
"""Fix common convention problems automatically."""
51+
session.install("black")
52+
session.install("isort")
53+
session.run("isort", "src", "tests", "noxfile.py")
54+
session.run("black", "src", "tests", "noxfile.py")

pytest.ini

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[pytest]
2+
markers =
3+
slow: this test is slow and should only run locally.
4+
pythonpath = .

requirements.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
nox
2+
numpy
3+
matplotlib
4+
torch

0 commit comments

Comments
 (0)