diff --git a/ncg_optimizer/__init__.py b/ncg_optimizer/__init__.py index c8654f5..18c30c9 100644 --- a/ncg_optimizer/__init__.py +++ b/ncg_optimizer/__init__.py @@ -16,7 +16,7 @@ __all__ = ( 'LCG', ) -__version__ = '0.0.1b0' +__version__ = '0.0.1b1' _package_opts = [ diff --git a/ncg_optimizer/lcg.py b/ncg_optimizer/lcg.py index 48f230f..04f53ea 100644 --- a/ncg_optimizer/lcg.py +++ b/ncg_optimizer/lcg.py @@ -60,7 +60,7 @@ def step(self, closure=None): continue d_p = p.grad - print(p) + n=len(d_p) state = self.state[p] diff --git a/tests/test_basic.ipynb b/tests/test_basic.ipynb new file mode 100644 index 0000000..b668f5f --- /dev/null +++ b/tests/test_basic.ipynb @@ -0,0 +1,627 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "import torch\n", + "\n", + "import ncg_optimizer as optim" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "def quadratic(tensor):\n", + " x, y = tensor\n", + " a = 2.0\n", + " b = 1.0\n", + " return (x ** 2) / a + (y ** 2) / b + 2 * x * y" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def rosenbrock(tensor):\n", + " x, y = tensor\n", + " return (1 - x) ** 2 + 1 * (y - x ** 2) ** 2" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def beale(tensor):\n", + " x, y = tensor\n", + " f = (\n", + " (1.5 - x + x * y) ** 2\n", + " + (2.25 - x + x * y ** 2) ** 2\n", + " + (2.625 - x + x * y ** 3) ** 2\n", + " )\n", + " return f" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [], + "source": [ + "initial_state = (1.5, 1.5)\n", + "min_loc = (0, 0)\n", + "x = torch.Tensor(initial_state).requires_grad_(True)\n", + "x_min = torch.Tensor(min_loc)\n", + "iterations = 500" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "tensor([1.5000, 1.5000], requires_grad=True)\n", + "tensor([ 0.2360, -0.1854], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n", + "tensor([3.2783e-07, 4.3213e-07], requires_grad=True)\n" + ] + }, + { + "data": { + "text/plain": [ + "True" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "optimizer = optim.LCG([x], eps=1e-3)\n", + "for _ in range(iterations):\n", + " optimizer.zero_grad()\n", + " f = quadratic(x)\n", + " f.backward(retain_graph=True, create_graph=True)\n", + " optimizer.step()\n", + "torch.allclose(x, x_min, atol=0.001)" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "UUE", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.7.13 (default, Mar 28 2022, 08:03:21) [MSC v.1916 64 bit (AMD64)]" + }, + "orig_nbformat": 4, + "vscode": { + "interpreter": { + "hash": "0326a7f9ed817c17642f61d66b02c6fdb6b9844b168a64b2475a75c5fdc150c7" + } + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/tests/test_basic.py b/tests/test_basic.py deleted file mode 100644 index 51ba530..0000000 --- a/tests/test_basic.py +++ /dev/null @@ -1,64 +0,0 @@ -import pytest -import torch - -import ncg_optimizer as optim - - -def rosenbrock(tensor): - x, y = tensor - return (1 - x) ** 2 + 1 * (y - x ** 2) ** 2 - - -def quadratic(tensor): - x, y = tensor - a = 1.0 - b = 1.0 - return (x ** 2) / a + (y ** 2) / b - - -def beale(tensor): - x, y = tensor - f = ( - (1.5 - x + x * y) ** 2 - + (2.25 - x + x * y ** 2) ** 2 - + (2.625 - x + x * y ** 3) ** 2 - ) - return f - - -cases = [ -# (rosenbrock, (1.5, 1.5), (1, 1)), - (quadratic, (1.5, 1.5), (0, 0)), -# (beale, (1.5, 1.5), (3, 0.5)), -] - - -def ids(v): - n = '{} {}'.format(v[0].__name__, v[1:]) - return n - - -optimizers = [ - (optim.lcg, {'eps': 1e-5}, 500), -] - - -@pytest.mark.parametrize('case', cases, ids=ids) -@pytest.mark.parametrize('optimizer_config', optimizers, ids=ids) -def test_benchmark_function(case, optimizer_config): - func, initial_state, min_loc = case - optimizer_class, config, iterations = optimizer_config - x = torch.Tensor(initial_state).requires_grad_(True) - print([x]) - x_min = torch.Tensor(min_loc) - print(iterations) - optimizer = optimizer_class([x], **config) - for _ in range(iterations): - optimizer.zero_grad() - f = func(x) - f.backward(retain_graph=True, create_graph=True) - optimizer.step() - assert torch.allclose(x, x_min, atol=0.001) - - name = optimizer.__class__.__name__ - assert name in optimizer.__repr__() \ No newline at end of file