-
Notifications
You must be signed in to change notification settings - Fork 2
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
13 changed files
with
546 additions
and
196 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
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,112 @@ | ||
use crate::{wrap_vec_func_scalar, wrap_vec_func_vec}; | ||
use numpy::{IntoPyArray, PyArray1, PyReadonlyArray1, PyReadonlyArray2}; | ||
use pyo3::{conversion::FromPyObjectBound, exceptions::PyRuntimeError, intern, prelude::*}; | ||
use tuutal::{steepest_descent, SteepestDescentParameter, TuutalError, VecType}; | ||
|
||
macro_rules! first_order_method { | ||
($method:ident, $name:ident) => { | ||
#[pyfunction] | ||
pub fn $method<'py>( | ||
py: Python<'py>, | ||
f: PyObject, | ||
g: PyObject, | ||
x0: PyReadonlyArray1<f64>, | ||
gamma: f64, | ||
beta: f64, | ||
gtol: f64, | ||
maxiter: Option<usize>, | ||
) -> PyResult<Bound<'py, PyArray1<f64>>> { | ||
match steepest_descent( | ||
wrap_vec_func_scalar!(py, f), | ||
wrap_vec_func_vec!(py, g), | ||
&x0.as_array().to_owned(), | ||
&SteepestDescentParameter::$name(gamma, beta), | ||
gtol, | ||
maxiter.unwrap_or(x0.len().unwrap() * 1000), | ||
) { | ||
Ok(value) => Ok(value.into_pyarray_bound(py)), | ||
Err(error) => match error { | ||
TuutalError::Convergence { | ||
iterate: x, | ||
maxiter: _, | ||
} => { | ||
println!("Maximum number of iterations reached before convergence"); | ||
Ok(x.into_pyarray_bound(py)) | ||
} | ||
err => Err(PyRuntimeError::new_err(err.to_string())), // Should never come this far. | ||
}, | ||
} | ||
} | ||
}; | ||
} | ||
|
||
first_order_method!(armijo, new_armijo); | ||
first_order_method!(powell_wolfe, new_powell_wolfe); | ||
first_order_method!(adagrad, new_adagrad); | ||
first_order_method!(adadelta, new_adadelta); | ||
|
||
// #[pyfunction] | ||
// pub fn armijo<'py>( | ||
// py: Python<'py>, | ||
// f: PyObject, | ||
// g: PyObject, | ||
// x0: PyReadonlyArray1<f64>, | ||
// gamma: f64, | ||
// beta: f64, | ||
// gtol: f64, | ||
// maxiter: Option<usize>, | ||
// ) -> PyResult<Bound<'py, PyArray1<f64>>> { | ||
// match steepest_descent( | ||
// wrap_vec_func_scalar!(py, f), | ||
// wrap_vec_func_vec!(py, g), | ||
// &x0.as_array().to_owned(), | ||
// &SteepestDescentParameter::new_armijo(gamma, beta), | ||
// gtol, | ||
// maxiter.unwrap_or(x0.len().unwrap() * 1000), | ||
// ) { | ||
// Ok(value) => Ok(value.into_pyarray_bound(py)), | ||
// Err(error) => match error { | ||
// TuutalError::Convergence { | ||
// iterate: x, | ||
// maxiter: _, | ||
// } => { | ||
// println!("Maximum number of iterations reached before convergence"); | ||
// Ok(x.into_pyarray_bound(py)) | ||
// } | ||
// err => Err(PyRuntimeError::new_err(err.to_string())), // Should never come this far. | ||
// }, | ||
// } | ||
// } | ||
|
||
// #[pyfunction] | ||
// pub fn adagrad<'py>( | ||
// py: Python<'py>, | ||
// f: PyObject, | ||
// g: PyObject, | ||
// x0: PyReadonlyArray1<f64>, | ||
// gamma: f64, | ||
// beta: f64, | ||
// gtol: f64, | ||
// maxiter: Option<usize>, | ||
// ) -> PyResult<Bound<'py, PyArray1<f64>>> { | ||
// match steepest_descent( | ||
// wrap_vec_func_scalar!(py, f), | ||
// wrap_vec_func_vec!(py, g), | ||
// &x0.as_array().to_owned(), | ||
// &SteepestDescentParameter::new_adagr(gamma, beta), | ||
// gtol, | ||
// maxiter.unwrap_or(x0.len().unwrap() * 1000), | ||
// ) { | ||
// Ok(value) => Ok(value.into_pyarray_bound(py)), | ||
// Err(error) => match error { | ||
// TuutalError::Convergence { | ||
// iterate: x, | ||
// maxiter: _, | ||
// } => { | ||
// println!("Maximum number of iterations reached before convergence"); | ||
// Ok(x.into_pyarray_bound(py)) | ||
// } | ||
// err => Err(PyRuntimeError::new_err(err.to_string())), // Should never come this far. | ||
// }, | ||
// } | ||
// } |
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,41 @@ | ||
macro_rules! wrap_scalar_func_scalar { | ||
($py:expr, $py_func:expr) => { | ||
|x: f64| { | ||
$py_func | ||
.call1($py, (x,)) | ||
.expect("python objective function failed.") | ||
.extract::<f64>($py) | ||
.expect("python function should return a float-pointing number") | ||
} | ||
}; | ||
} | ||
|
||
macro_rules! wrap_vec_func_scalar { | ||
($py:expr, $py_func:expr) => { | ||
|x: &VecType<f64>| { | ||
$py_func | ||
.call1($py, (x.clone().into_pyarray_bound($py),)) | ||
.expect("python objective function failed.") | ||
.extract::<f64>($py) | ||
.expect("python function should return a float-pointing number") | ||
} | ||
}; | ||
} | ||
|
||
macro_rules! wrap_vec_func_vec { | ||
($py:expr, $py_func:expr) => { | ||
|x: &VecType<f64>| { | ||
$py_func | ||
.call1($py, (x.clone().into_pyarray_bound($py),)) | ||
.expect("python objective function failed.") | ||
.extract::<PyReadonlyArray1<f64>>($py) | ||
.expect("python function should return a python numpy.ndarray") | ||
.as_array() | ||
.to_owned() | ||
} | ||
}; | ||
} | ||
|
||
pub(crate) use wrap_scalar_func_scalar; | ||
pub(crate) use wrap_vec_func_scalar; | ||
pub(crate) use wrap_vec_func_vec; |
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
Oops, something went wrong.