This repository has been archived by the owner on Jul 19, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
89 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,16 @@ | ||
from optimatic.grad_desc import Optimiser | ||
from optimatic.optimisers.grad_desc import Optimiser | ||
import numpy as np | ||
|
||
minimum = np.random.normal(scale=5) | ||
print("Actual minimum is: {}".format(minimum)) | ||
|
||
def f(x): | ||
return (x - 5.4) ** 2 | ||
return (x - minimum) ** 2 | ||
|
||
def df(x): | ||
return 2 * (x - 5.4) | ||
return 2 * (x - minimum) | ||
|
||
opt = Optimiser(f, df, np.random.normal(scale=5)) | ||
x = opt.optimise() | ||
|
||
opt = Optimiser(f, df, 0.0) | ||
print("Calculated minimum is: {}".format(x)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
optimatic.optimisers package | ||
============================ | ||
|
||
Submodules | ||
---------- | ||
|
||
optimatic.optimisers.grad_desc module | ||
------------------------------------- | ||
|
||
.. automodule:: optimatic.optimisers.grad_desc | ||
:members: | ||
:undoc-members: | ||
:show-inheritance: | ||
|
||
optimatic.optimisers.optimiser module | ||
------------------------------------- | ||
|
||
.. automodule:: optimatic.optimisers.optimiser | ||
:members: | ||
:undoc-members: | ||
:show-inheritance: | ||
|
||
|
||
Module contents | ||
--------------- | ||
|
||
.. automodule:: optimatic.optimisers | ||
:members: | ||
:undoc-members: | ||
:show-inheritance: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
""" | ||
Optimiser base class | ||
All optimiser classes should inherit from this class | ||
""" | ||
from abc import ABCMeta, abstractmethod | ||
import numpy as np | ||
|
||
class Optimiser(object): | ||
""" | ||
:param y: The function to optimise | ||
:param x0: The starting position for the algorithm | ||
:param precision: The precision to calculate the minimum to | ||
:param steps: The max number of iterations of the algorithm to run | ||
""" | ||
__metaclass__ = ABCMeta | ||
|
||
def __init__(self, y, x0, precision=1e-4, steps=10000): | ||
self.y = y | ||
self.xn = x0 | ||
self.xn_1 = x0 | ||
self.precision = precision | ||
self.steps = steps | ||
|
||
@abstractmethod | ||
def step(self): | ||
"""Runs one iteration of the algorithm""" | ||
return | ||
|
||
def optimise(self): | ||
"""Runs :func:`step` the specified number of times""" | ||
i = 0 | ||
self.step() | ||
step_size = np.linalg.norm(self.xn - self.xn_1) | ||
while step_size > self.precision and i < self.steps: | ||
self.step() | ||
step_size = np.linalg.norm(self.xn - self.xn_1) | ||
i += 1 | ||
return self.xn |