updated HIG code example
This commit is contained in:
1 parent
0e3cd32658
commit
aa4f67eec7
1 file changed
+367
-204
+367
-204
@@ -2,70 +2,116 @@
|
|||||||
"cells": [
|
"cells": [
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {},
|
"metadata": {
|
||||||
"source": [
|
"id": "2tT-NooLyyGW"
|
||||||
"# Comparing Half-Inverse Gradients to Other Methods\n",
|
|
||||||
"\n",
|
|
||||||
"In this notebook, we demonstrate three different ways to optimize neural networks with physical loss functions. We'll compare: \n",
|
|
||||||
"\n",
|
|
||||||
"1. Adam; a standard network optimizer\n",
|
|
||||||
"2. Physical Gradients; an algorithm fully inverting the physics\n",
|
|
||||||
"3. Half-Inverse Gradients; an algorithm locally inverting physics and network.\n",
|
|
||||||
"\n",
|
|
||||||
"The learning task is to find the control function steering the dynamics of a coupled oscillator system. More concretely, we consider a set of different phyiscal inputs ($x_i$). Using a corresponding control function ($u_i$), we can influence the time evolution ($T$) and receive an output state ($y_i$).\n",
|
|
||||||
"\n",
|
|
||||||
"$y_i=T(x_i;u_i)$\n",
|
|
||||||
"\n",
|
|
||||||
"If we want to evolve a given initial state($x_i$) into a given target state ($\\hat{y_i}$), we receive an inverse problem for the control function $(u_i)$. The quality of between desired target state ($\\hat{y}_i$) und received target state ($y_i$) is measured by a loss function ($L$).\n",
|
|
||||||
"\n",
|
|
||||||
"$\\min_{u_i}\\; L(\\hat{y}_i,T(x_i;u_i))$\n",
|
|
||||||
"\n",
|
|
||||||
"If we use a neural network ($N$ parameterized by $\\theta$) to learn the control function over a set of input/output pairs ($x_i,\\hat{y}_i$), we transform the above physics optimization task into a learning problem.\n",
|
|
||||||
"\n",
|
|
||||||
"$\\min_{\\theta}\\; \\sum_i L(\\hat{y}_i,T(x_i;N(x_i,y_i,\\theta)))$\n",
|
|
||||||
"\n",
|
|
||||||
"We begin by setting up the physics solver ($T$), the network ($N$), and the loss ($L$)."
|
|
||||||
]
|
|
||||||
},
|
},
|
||||||
{
|
|
||||||
"cell_type": "code",
|
|
||||||
"execution_count": 77,
|
|
||||||
"metadata": {},
|
|
||||||
"outputs": [],
|
|
||||||
"source": [
|
"source": [
|
||||||
"import numpy as np\n",
|
"# Coupled Oscillators with Half-Inverse Gradients\n",
|
||||||
"import tensorflow as tf\n",
|
|
||||||
"import time\n",
|
|
||||||
"import os\n",
|
|
||||||
"\n",
|
"\n",
|
||||||
"os.environ[\"CUDA_VISIBLE_DEVICES\"]='' # set GPUs\n",
|
"In this notebook, we'll turn to a practical example comparing the half-inverse gradients (HIGs) to other methods for training neural networks with physical loss functions. Specifically, we'll compare:\n",
|
||||||
"#tf.config.run_functions_eagerly(True) # deactivate Tensorflow Tracing"
|
"\n",
|
||||||
|
"1. Adam: as a standard gradient-descent (GD) based network optimizer,\n",
|
||||||
|
"2. Scale-Invariant Physics: the previously described algorithm that fully inverts the physics,\n",
|
||||||
|
"3. Half-Inverse Gradients: which locally and jointly inverting physics and network.\n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"## Inverse problem setup\n",
|
||||||
|
"\n",
|
||||||
|
"The learning task is to find the control function steering the dynamics of a coupled oscillator system. \n",
|
||||||
|
"This is a classical problem in physics, and a good case to evaluate the HIGs due to it's smaller size. We're using two mass points, and thus we'll only have four degrees of freedom for position and velocity of both points (compared to, e.g., the $32\\times32\\times2$ unknowns we'd get even for \"only\" a small fluid simulation with 32 cells along x and y). \n",
|
||||||
|
"\n",
|
||||||
|
"Nonetheless, the oscillators are a highly-non trivial case: we aim for applying a control such that the initial state is reached again after a chosen time interval. We'll use 24 steps of a fourth-order Runge-Kutta scheme, and hence the NN has to learn how to best \"nudge\" the two mass points over the course of all time steps, so that they end up at the desired position with the right velocity at the right time.\n",
|
||||||
|
"\n",
|
||||||
|
"A system of $N$ coupled oscillators is described by the following Hamiltonian:\n",
|
||||||
|
"\n",
|
||||||
|
"$$\n",
|
||||||
|
" \\mathcal{H}(x_i,p_i,t)=\\sum_i \\bigg( \\frac{x_i^2}{2}+ \\frac{p_i^2}{2} + \\alpha \\cdot (x_i-x_{i+1})^4+u(t) \\cdot x_i \\cdot c_i\\bigg),\n",
|
||||||
|
"$$\n",
|
||||||
|
"\n",
|
||||||
|
"which provides the basis for the RK4 time integration below.\n",
|
||||||
|
"\n"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {},
|
|
||||||
"source": [
|
"source": [
|
||||||
"## Physics Model\n",
|
|
||||||
"\n",
|
"\n",
|
||||||
"Differential equation: Coupled Linear Oscillators with Control term\n",
|
"## Problem statement\n",
|
||||||
"\n",
|
"\n",
|
||||||
"Solver: Runge-Kutta-4 \n",
|
"More concretely, we consider a set of different phyiscal inputs ($x_i$). Using a corresponding control function ($u_i$), we can influence the time evolution of our physical system $\\mathcal P$ and receive an output state ($y_i$).\n",
|
||||||
"\n",
|
"\n",
|
||||||
"Nx: Number of oscillators,\n",
|
"$$y_i=\\mathcal P(x_i;u_i)$$\n",
|
||||||
"Nt: Number of time evolution steps,\n",
|
"\n",
|
||||||
"dt: Length of one time step"
|
"If we want to evolve a given initial state($x_i$) into a given target state ($\\hat{y_i}$), we receive an inverse problem for the control function $(u_i)$. The quality of between desired target state ($\\hat{y}_i$) und received target state ($y_i$) is measured by a loss function ($L$).\n",
|
||||||
|
"\n",
|
||||||
|
"$$\\text{arg min}_{u_i}\\; \\mathcal P(\\hat{y}_i,T(x_i;u_i))$$\n",
|
||||||
|
"\n",
|
||||||
|
"If we use a neural network ($f$ parameterized by $\\theta$) to learn the control function over a set of input/output pairs ($x_i,\\hat{y}_i$), we transform the above physics optimization task into a learning problem.\n",
|
||||||
|
"\n",
|
||||||
|
"$$\\text{arg min}_{\\theta}\\; \\sum_i L\\big(\\hat{y}_i, \\mathcal P(x_i;f(x_i,y_i; \\theta)) \\big)$$"
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"id": "5NOyhZ-MOnoZ"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"source": [
|
||||||
|
"Before we begin by setting up the physics solver $\\mathcal P$, we import the necessary libraries (this example uses TensorFlow), and define the main global variable `MODE`, that switches between _Adam_ (`'GD'`), _Scale-invariant physics_ (`'SIP'`), and _Half-inverse gradients_ (`'HIG'`)."
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"id": "We6eG_ZWOspS"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 95,
|
||||||
|
"metadata": {
|
||||||
|
"id": "A0cyHl7tyyGg"
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"import numpy as np\n",
|
||||||
|
"import tensorflow as tf\n",
|
||||||
|
"import time, os\n",
|
||||||
|
"\n",
|
||||||
|
"os.environ[\"CUDA_VISIBLE_DEVICES\"]='' # set GPUs\n",
|
||||||
|
"#tf.config.run_functions_eagerly(True) # deactivate Tensorflow Tracing\n",
|
||||||
|
"\n",
|
||||||
|
"# main switch for the three methods:\n",
|
||||||
|
"MODE = 'HIG' # HIG | SIP | GD"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {
|
||||||
|
"id": "yCN4F_AjyyGk"
|
||||||
|
},
|
||||||
|
"source": [
|
||||||
|
"## Coupled linear oscillator simulation\n",
|
||||||
|
"\n",
|
||||||
|
"For the physics simulation, we'll solve a differential equation for a system of coupled linear oscillators with a control term. The time integration, a fourth-order Runge-Kutta scheme is used.\n",
|
||||||
|
"\n",
|
||||||
|
"Below, we're first defining a few global constants:\n",
|
||||||
|
"`Nx`: the number of oscillators,\n",
|
||||||
|
"`Nt`: the number of time evolution steps, and\n",
|
||||||
|
"`DT`: the length of one time step. \n",
|
||||||
|
"\n",
|
||||||
|
"We'll then define a helper function to set up a Laplacian stencil, the `coupled_oscillators_batch()` function which computes the simulation for a whole mini batch of values, and finally the `solver()` function, which runs the desired number of time steps with a given control signal."
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 78,
|
"execution_count": 96,
|
||||||
"metadata": {},
|
"metadata": {
|
||||||
|
"id": "hevmSaVvyyGl"
|
||||||
|
},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"Nx = 2\n",
|
"Nx = 2\n",
|
||||||
"Nt = 24\n",
|
"Nt = 24\n",
|
||||||
"dt = 0.5\n",
|
"DT = 0.5\n",
|
||||||
"\n",
|
"\n",
|
||||||
"def build_laplace(n,boundary='0'):\n",
|
"def build_laplace(n,boundary='0'):\n",
|
||||||
" if n==1:\n",
|
" if n==1:\n",
|
||||||
@@ -83,14 +129,13 @@
|
|||||||
"\n",
|
"\n",
|
||||||
"@tf.function\n",
|
"@tf.function\n",
|
||||||
"def coupled_oscillators_batch( x, control):\n",
|
"def coupled_oscillators_batch( x, control):\n",
|
||||||
" print('coup harm osc tracing')\n",
|
|
||||||
" '''\n",
|
" '''\n",
|
||||||
" ODE of type: x' = f(x)\n",
|
" ODE of type: x' = f(x)\n",
|
||||||
" :param x_in: shape: (batch, 2 * number of osc)\n",
|
" :param x_in: position and velocities, shape: (batch, 2 * number of osc) order second index: x_i , v_i\n",
|
||||||
" order second index: x_i , v_i\n",
|
" :param control: control function, shape: (batch,)\n",
|
||||||
" :param control: shape:(batch,)\n",
|
|
||||||
" :return:\n",
|
" :return:\n",
|
||||||
" '''\n",
|
" '''\n",
|
||||||
|
" #print('coupled_oscillators_batch')\n",
|
||||||
" n_osc = x.shape[1]//2\n",
|
" n_osc = x.shape[1]//2\n",
|
||||||
"\n",
|
"\n",
|
||||||
" # natural time evo\n",
|
" # natural time evo\n",
|
||||||
@@ -139,21 +184,27 @@
|
|||||||
"def solver(x0, control):\n",
|
"def solver(x0, control):\n",
|
||||||
" x = x0\n",
|
" x = x0\n",
|
||||||
" for i in range(Nt):\n",
|
" for i in range(Nt):\n",
|
||||||
" x = runge_kutta_4_batch(x, dt, control[:,i], coupled_oscillators_batch)\n",
|
" x = runge_kutta_4_batch(x, DT, control[:,i], coupled_oscillators_batch)\n",
|
||||||
" return x\n"
|
" return x\n"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {},
|
"metadata": {
|
||||||
|
"id": "VtY4-wbXyyGn"
|
||||||
|
},
|
||||||
"source": [
|
"source": [
|
||||||
"## Network Model"
|
"## Training setup\n",
|
||||||
|
"\n",
|
||||||
|
"The neural network itself is quite simple: it consists of four dense layers (the intermediate ones with 20 neurons each), and `tanh` activation functions."
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 79,
|
"execution_count": 97,
|
||||||
"metadata": {},
|
"metadata": {
|
||||||
|
"id": "et1j8lYRyyGp"
|
||||||
|
},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"act = tf.keras.activations.tanh\n",
|
"act = tf.keras.activations.tanh\n",
|
||||||
@@ -168,15 +219,19 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {},
|
"metadata": {
|
||||||
|
"id": "zilE67gYyyGq"
|
||||||
|
},
|
||||||
"source": [
|
"source": [
|
||||||
"## Loss Function"
|
"As loss function, we'll use an $L^2$ loss:"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 80,
|
"execution_count": 98,
|
||||||
"metadata": {},
|
"metadata": {
|
||||||
|
"id": "km83dkWgyyGq"
|
||||||
|
},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"@tf.function\n",
|
"@tf.function\n",
|
||||||
@@ -189,95 +244,104 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {},
|
"metadata": {
|
||||||
|
"id": "ZyS31DHdyyGr"
|
||||||
|
},
|
||||||
"source": [
|
"source": [
|
||||||
"## Data set\n",
|
"And as data set for training we simply create 4k of random position values which the oscillators start with (`X_TRAIN`), and which they should return to at the end of the simulation (`Y_TRAIN`). As they should return to their initial states, we have `X_TRAIN=Y_TRAIN`.\n",
|
||||||
"\n"
|
"\n"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 81,
|
"execution_count": 99,
|
||||||
"metadata": {},
|
"metadata": {
|
||||||
|
"id": "_zz7VSIpyyGs"
|
||||||
|
},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"N = 2**12\n",
|
"N = 2**12\n",
|
||||||
"x_train = np.random.rand(N, 2 * Nx).astype(np.float32)\n",
|
"X_TRAIN = np.random.rand(N, 2 * Nx).astype(np.float32)\n",
|
||||||
"y_train = x_train"
|
"Y_TRAIN = X_TRAIN # the target states are identical"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {},
|
"metadata": {
|
||||||
|
"id": "1KChFddqyyGs"
|
||||||
|
},
|
||||||
"source": [
|
"source": [
|
||||||
"## Optimization\n",
|
"## Training\n",
|
||||||
"For the optimization procedure, we need to set up some parameters. \n",
|
"For the optimization procedure of the neural network training, we need to set up some global parameters. The next cell initializes some suitable values tailored to each of the three methods. These were determined heuristically to work best for each. If we try to use the same settings for all, this would inevitably make the comparison unfair for some of them.\n",
|
||||||
"1. Adam: The truncation parameter has no meaning here.\n",
|
|
||||||
"2. PG: The specified optimizer is the one used for network optimization. The physics inversion is done via Gauss-Newton and corresponds to an exact inversion since the physical optimization landscape is quadratic. For the Jacobian inversion in Gauss-Newton, we can specify a truncation parameter.\n",
|
|
||||||
"3. HIG: To receive the HIG algorithm, the optimizer has to be set to SGD. For the Jacobian half-inversion, we can specify a truncation parameter. Optimal batch sizes are typically lower. Learning rates can usually be chosen around 1.\n",
|
|
||||||
"\n",
|
"\n",
|
||||||
"Specify the maximal simulation time in seconds via max_number."
|
"1. _Adam_: This is the most widely used NN optimizer, and we're using it here as a representative of the GD family. Note that the truncation parameter has no meaning for Adam.\n",
|
||||||
|
"\n",
|
||||||
|
"2. _SIP_: The specified optimizer is the one used for network optimization. The physics inversion is done via Gauss-Newton and corresponds to an exact inversion since the physical optimization landscape is quadratic. For the Jacobian inversion in Gauss-Newton, we can specify a truncation parameter.\n",
|
||||||
|
"\n",
|
||||||
|
"3. _HIG_: To obtain the HIG algorithm, the optimizer has to be set to SGD. For the Jacobian half-inversion, we can specify a truncation parameter. Optimal batch sizes are typically lower than for the other two, and a learning rate of 1 typically works very well.\n",
|
||||||
|
"\n",
|
||||||
|
"The maximal training time in seconds is set via `MAX_TIME` below."
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 82,
|
"execution_count": 100,
|
||||||
"metadata": {},
|
"metadata": {
|
||||||
"outputs": [],
|
"colab": {
|
||||||
"source": [
|
"base_uri": "https://localhost:8080/"
|
||||||
"# Adam Training\n",
|
|
||||||
"if 0:\n",
|
|
||||||
" mode = 'GD' \n",
|
|
||||||
" optimizer = tf.keras.optimizers.Adam\n",
|
|
||||||
" batch_size = 256\n",
|
|
||||||
" learning_rate = 0.001\n",
|
|
||||||
" max_number = 30#*60\n",
|
|
||||||
" trunc = 10**-10 "
|
|
||||||
]
|
|
||||||
},
|
},
|
||||||
{
|
"id": "PYjx7Sk8yyGz",
|
||||||
"cell_type": "code",
|
"outputId": "9ee462ad-499c-4f7c-d3a3-0fb11ab36abe"
|
||||||
"execution_count": 83,
|
|
||||||
"metadata": {},
|
|
||||||
"outputs": [],
|
|
||||||
"source": [
|
|
||||||
"# PG training\n",
|
|
||||||
"if 0:\n",
|
|
||||||
" mode = 'PG' \n",
|
|
||||||
" optimizer = tf.keras.optimizers.Adam\n",
|
|
||||||
" batch_size = 256\n",
|
|
||||||
" learning_rate = 0.001\n",
|
|
||||||
" max_number = 30#*60\n",
|
|
||||||
" trunc = 10**-10"
|
|
||||||
]
|
|
||||||
},
|
},
|
||||||
|
"outputs": [
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"output_type": "stream",
|
||||||
"execution_count": 84,
|
"name": "stdout",
|
||||||
"metadata": {},
|
"text": [
|
||||||
"outputs": [],
|
"Running variant: HIG\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
"source": [
|
"source": [
|
||||||
"# HIG training\n",
|
"if MODE=='HIG': # HIG training\n",
|
||||||
"if 1:\n",
|
" OPTIMIZER = tf.keras.optimizers.SGD\n",
|
||||||
" mode = 'HIG' # GD, PG, HIG\n",
|
" BATCH_SIZE = 32 # larger batches make HIGs unnecessariy slow...\n",
|
||||||
" optimizer = tf.keras.optimizers.SGD\n",
|
" LR = 1.0\n",
|
||||||
" batch_size = 32\n",
|
" TRUNC = 10**-10\n",
|
||||||
" learning_rate = 1.0\n",
|
"\n",
|
||||||
" max_number = 30#*60\n",
|
"elif MODE=='SIP': # SIP training\n",
|
||||||
" trunc = 10**-10"
|
" OPTIMIZER = tf.keras.optimizers.Adam\n",
|
||||||
|
" BATCH_SIZE = 256\n",
|
||||||
|
" LR = 0.001 # for the internal step with Adam\n",
|
||||||
|
" TRUNC = 0 # not used\n",
|
||||||
|
"\n",
|
||||||
|
"else: # Adam Training (as GD representative)\n",
|
||||||
|
" MODE = 'GD' \n",
|
||||||
|
" OPTIMIZER = tf.keras.optimizers.Adam\n",
|
||||||
|
" BATCH_SIZE = 256\n",
|
||||||
|
" LR = 0.001\n",
|
||||||
|
" TRUNC = 0 # not used\n",
|
||||||
|
"\n",
|
||||||
|
"# global parameters for all three methods\n",
|
||||||
|
"MAX_TIME = 100 # [s]\n",
|
||||||
|
"print(\"Running variant: \"+MODE)"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {},
|
"metadata": {
|
||||||
|
"id": "4VFJ4pmEyyG0"
|
||||||
|
},
|
||||||
"source": [
|
"source": [
|
||||||
"Function to construct the half-inverse of matrix for HIGs"
|
"The next function, `HIG_pinv()`, is a crucial one: it constructs the half-inverse of a given matrix for HIGs. It computes an SVD, takes the square-root of the singular values, and then re-assembles the matrix. "
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 85,
|
"execution_count": 101,
|
||||||
"metadata": {},
|
"metadata": {
|
||||||
|
"id": "c3B4qtUPyyG1"
|
||||||
|
},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"from tensorflow.python.framework import ops\n",
|
"from tensorflow.python.framework import ops\n",
|
||||||
@@ -288,9 +352,11 @@
|
|||||||
"from tensorflow.python.util.tf_export import tf_export\n",
|
"from tensorflow.python.util.tf_export import tf_export\n",
|
||||||
"from tensorflow.python.ops.linalg.linalg_impl import _maybe_validate_matrix,svd\n",
|
"from tensorflow.python.ops.linalg.linalg_impl import _maybe_validate_matrix,svd\n",
|
||||||
"\n",
|
"\n",
|
||||||
"@tf_export('linalg.gpinv')\n",
|
"# partial inversion of the Jacobian via SVD:\n",
|
||||||
|
"# this function is adopted from tensorflow's SVD function, and published here under its license: Apache-2.0 License , https://github.com/tensorflow/tensorflow/blob/master/LICENSE\n",
|
||||||
|
"@tf_export('linalg.HIG_pinv')\n",
|
||||||
"@dispatch.add_dispatch_support\n",
|
"@dispatch.add_dispatch_support\n",
|
||||||
"def gpinv(a, rcond=None,beta=0.5, validate_args=False, name=None):\n",
|
"def HIG_pinv(a, rcond=None,beta=0.5, validate_args=False, name=None):\n",
|
||||||
"\n",
|
"\n",
|
||||||
" with ops.name_scope(name or 'pinv'):\n",
|
" with ops.name_scope(name or 'pinv'):\n",
|
||||||
" a = ops.convert_to_tensor(a, name='a')\n",
|
" a = ops.convert_to_tensor(a, name='a')\n",
|
||||||
@@ -321,25 +387,14 @@
|
|||||||
"\n",
|
"\n",
|
||||||
" rcond = ops.convert_to_tensor(rcond, dtype=dtype, name='rcond')\n",
|
" rcond = ops.convert_to_tensor(rcond, dtype=dtype, name='rcond')\n",
|
||||||
"\n",
|
"\n",
|
||||||
" # Calculate pseudo inverse via SVD.\n",
|
" [ singular_values, left_singular_vectors, right_singular_vectors, ] = svd(\n",
|
||||||
" # Note: if a is Hermitian then u == v. (We might observe additional\n",
|
|
||||||
" # performance by explicitly setting `v = u` in such cases.)\n",
|
|
||||||
" [\n",
|
|
||||||
" singular_values, # Sigma\n",
|
|
||||||
" left_singular_vectors, # U\n",
|
|
||||||
" right_singular_vectors, # V\n",
|
|
||||||
" ] = svd(\n",
|
|
||||||
" a, full_matrices=False, compute_uv=True)\n",
|
" a, full_matrices=False, compute_uv=True)\n",
|
||||||
"\n",
|
"\n",
|
||||||
" # Saturate small singular values to inf. This has the effect of make\n",
|
|
||||||
" # `1. / s = 0.` while not resulting in `NaN` gradients.\n",
|
|
||||||
" cutoff = rcond * math_ops.reduce_max(singular_values, axis=-1)\n",
|
" cutoff = rcond * math_ops.reduce_max(singular_values, axis=-1)\n",
|
||||||
" singular_values = array_ops.where_v2(\n",
|
" singular_values = array_ops.where_v2(\n",
|
||||||
" singular_values > array_ops.expand_dims_v2(cutoff, -1), singular_values**beta,\n",
|
" singular_values > array_ops.expand_dims_v2(cutoff, -1), singular_values**beta,\n",
|
||||||
" np.array(np.inf, dtype))\n",
|
" np.array(np.inf, dtype))\n",
|
||||||
"\n",
|
"\n",
|
||||||
" # By the definition of the SVD, `a == u @ s @ v^H`, and the pseudo-inverse\n",
|
|
||||||
" # is defined as `pinv(a) == v @ inv(s) @ u^H`.\n",
|
|
||||||
" a_pinv = math_ops.matmul(\n",
|
" a_pinv = math_ops.matmul(\n",
|
||||||
" right_singular_vectors / array_ops.expand_dims_v2(singular_values, -2),\n",
|
" right_singular_vectors / array_ops.expand_dims_v2(singular_values, -2),\n",
|
||||||
" left_singular_vectors,\n",
|
" left_singular_vectors,\n",
|
||||||
@@ -353,18 +408,26 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {},
|
"metadata": {
|
||||||
|
"id": "Bj90LjpyyyG2"
|
||||||
|
},
|
||||||
"source": [
|
"source": [
|
||||||
"We define a Python class to organize the optimization algorithm. Using physics solver, network model, loss function and a data set. We compute loss values after each epoch. Each epoch consists of several mini batch updates. Depending on the optimization method, the mini batch update differs:\n",
|
"Now we have all pieces in place to run the training. The next cell defines a Python class to organize the neural network optimization. It receives the physics solver, network model, loss function and a data set, and runs as many epochs as possible within the given time limit `MAX_TIME`.\n",
|
||||||
"1. Adam: Compute loss gradient, then apply the Adam\n",
|
"\n",
|
||||||
"2. PG: Compute loss gradient und physics Jacobian, invert them data-point-wise, define a modified loss to compute network updates\n",
|
"Depending on the chosen optimization method, the mini batch updates differ:\n",
|
||||||
"3. HIG: Compute loss gradient and network-physics Jacobian, then compute half-inversion"
|
"1. Adam: Compute loss gradient, then apply the Adam update.\n",
|
||||||
|
"2. PG: Compute loss gradient und physics Jacobian, invert them data-point-wise, and compute network updates via the proxy loss and Adam.\n",
|
||||||
|
"3. HIG: Compute loss gradient and network-physics Jacobian, then jointly compute the half-inversion, and update the network parameters with the resulting step.\n",
|
||||||
|
"\n",
|
||||||
|
"The `mini_batch_update()` method of the optimizer class realizes these three variants."
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 86,
|
"execution_count": 102,
|
||||||
"metadata": {},
|
"metadata": {
|
||||||
|
"id": "peqXxMNpyyG5"
|
||||||
|
},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"class Optimization():\n",
|
"class Optimization():\n",
|
||||||
@@ -377,9 +440,9 @@
|
|||||||
" self.y_dim = y_train.shape[1]\n",
|
" self.y_dim = y_train.shape[1]\n",
|
||||||
" self.weight_shapes = [weight_tensor.shape for weight_tensor in self.model.trainable_weights]\n",
|
" self.weight_shapes = [weight_tensor.shape for weight_tensor in self.model.trainable_weights]\n",
|
||||||
"\n",
|
"\n",
|
||||||
" def set_params(self,batch_size,learning_rate,optimizer,max_number,mode,trunc):\n",
|
" def set_params(self,batch_size,learning_rate,optimizer,max_time,mode,trunc):\n",
|
||||||
" self.number_of_batches = N // batch_size\n",
|
" self.number_of_batches = N // batch_size\n",
|
||||||
" self.max_number = max_number\n",
|
" self.max_time = max_time\n",
|
||||||
" self.batch_size = batch_size\n",
|
" self.batch_size = batch_size\n",
|
||||||
" self.learning_rate = learning_rate\n",
|
" self.learning_rate = learning_rate\n",
|
||||||
" self.optimizer = optimizer(learning_rate)\n",
|
" self.optimizer = optimizer(learning_rate)\n",
|
||||||
@@ -406,7 +469,7 @@
|
|||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
" @tf.function\n",
|
" @tf.function\n",
|
||||||
" def pg_get_physics_derivatives(self,x_batch, y_batch): #physical grads\n",
|
" def pg_get_physics_derivatives(self,x_batch, y_batch): # physics gradient for SIP\n",
|
||||||
"\n",
|
"\n",
|
||||||
" with tf.GradientTape(persistent=True) as tape:\n",
|
" with tf.GradientTape(persistent=True) as tape:\n",
|
||||||
" control_batch = self.model(y_batch)\n",
|
" control_batch = self.model(y_batch)\n",
|
||||||
@@ -456,7 +519,7 @@
|
|||||||
" grad = self.gd_get_derivatives(x_batch, y_batch)\n",
|
" grad = self.gd_get_derivatives(x_batch, y_batch)\n",
|
||||||
" self.optimizer.apply_gradients(zip(grad, self.model.trainable_weights))\n",
|
" self.optimizer.apply_gradients(zip(grad, self.model.trainable_weights))\n",
|
||||||
"\n",
|
"\n",
|
||||||
" elif self.mode==\"PG\":\n",
|
" elif self.mode==\"SIP\":\n",
|
||||||
" jacy,grad,control_batch = self.pg_get_physics_derivatives(x_batch, y_batch)\n",
|
" jacy,grad,control_batch = self.pg_get_physics_derivatives(x_batch, y_batch)\n",
|
||||||
" grad_e = tf.expand_dims(grad,-1)\n",
|
" grad_e = tf.expand_dims(grad,-1)\n",
|
||||||
" pinv = tf.linalg.pinv(jacy,rcond=10**-5)\n",
|
" pinv = tf.linalg.pinv(jacy,rcond=10**-5)\n",
|
||||||
@@ -470,7 +533,7 @@
|
|||||||
" flat_jacy_list = [tf.reshape(jac, (self.batch_size * self.y_dim, -1)) for jac in jacy]\n",
|
" flat_jacy_list = [tf.reshape(jac, (self.batch_size * self.y_dim, -1)) for jac in jacy]\n",
|
||||||
" flat_jacy = tf.concat(flat_jacy_list, axis=1)\n",
|
" flat_jacy = tf.concat(flat_jacy_list, axis=1)\n",
|
||||||
" flat_grad = tf.reshape(grad, (-1,))\n",
|
" flat_grad = tf.reshape(grad, (-1,))\n",
|
||||||
" inv = gpinv(flat_jacy, rcond=self.trunc)\n",
|
" inv = HIG_pinv(flat_jacy, rcond=self.trunc)\n",
|
||||||
" processed_derivatives = tf.tensordot(inv, flat_grad, axes=(1, 0))\n",
|
" processed_derivatives = tf.tensordot(inv, flat_grad, axes=(1, 0))\n",
|
||||||
" #processed_derivatives = self.linear_solve(flat_jacy, flat_grad)\n",
|
" #processed_derivatives = self.linear_solve(flat_jacy, flat_grad)\n",
|
||||||
" update_list = []\n",
|
" update_list = []\n",
|
||||||
@@ -486,18 +549,17 @@
|
|||||||
"\n",
|
"\n",
|
||||||
" def epoch_update(self):\n",
|
" def epoch_update(self):\n",
|
||||||
" for batch_index in range(self.number_of_batches):\n",
|
" for batch_index in range(self.number_of_batches):\n",
|
||||||
" position = batch_index * batch_size\n",
|
" position = batch_index * self.batch_size\n",
|
||||||
" x_batch = self.x_train[position:position + batch_size]\n",
|
" x_batch = self.x_train[position:position + self.batch_size]\n",
|
||||||
" y_batch = self.y_train[position:position + batch_size]\n",
|
" y_batch = self.y_train[position:position + self.batch_size]\n",
|
||||||
" self.mini_batch_update(x_batch, y_batch)\n",
|
" self.mini_batch_update(x_batch, y_batch)\n",
|
||||||
"\n",
|
"\n",
|
||||||
" def eval(self,epoch,wc_time,ep_dur):\n",
|
" def eval(self,epoch,wc_time,ep_dur):\n",
|
||||||
"\n",
|
|
||||||
" print('Epoch: ', epoch,' WallClockTime: ',wc_time,' EpochDuration: ',ep_dur )\n",
|
|
||||||
"\n",
|
|
||||||
" train_loss = self.computation(self.x_train,self.y_train)\n",
|
" train_loss = self.computation(self.x_train,self.y_train)\n",
|
||||||
" train_loss_per_dp = train_loss / N\n",
|
" train_loss_per_dp = train_loss / N\n",
|
||||||
" print('TrainLoss:', train_loss_per_dp)\n",
|
" if epoch<5 or epoch%20==0: print('Epoch: ', epoch,', wall clock time: ',wc_time,', loss: ', float(train_loss_per_dp) )\n",
|
||||||
|
" #print('TrainLoss:', train_loss_per_dp)\n",
|
||||||
|
" #print('Epoch: ', epoch,' WallClockTime: ',wc_time,' EpochDuration: ',ep_dur )\n",
|
||||||
" return train_loss_per_dp\n",
|
" return train_loss_per_dp\n",
|
||||||
"\n",
|
"\n",
|
||||||
" def start_training(self):\n",
|
" def start_training(self):\n",
|
||||||
@@ -509,7 +571,7 @@
|
|||||||
" epoch=0\n",
|
" epoch=0\n",
|
||||||
" wc_time = 0\n",
|
" wc_time = 0\n",
|
||||||
"\n",
|
"\n",
|
||||||
" while wc_time<max_number:\n",
|
" while wc_time<self.max_time:\n",
|
||||||
" \n",
|
" \n",
|
||||||
" duration = time.time()\n",
|
" duration = time.time()\n",
|
||||||
" self.epoch_update()\n",
|
" self.epoch_update()\n",
|
||||||
@@ -530,104 +592,200 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {},
|
"metadata": {
|
||||||
|
"id": "3hCSV9KcyyG7"
|
||||||
|
},
|
||||||
"source": [
|
"source": [
|
||||||
"Start the simulation."
|
"All that's left to do is to start the training with the chosen global parameters, and collect the results in `time_list`, and `loss_list`."
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 87,
|
"execution_count": 103,
|
||||||
"metadata": {},
|
"metadata": {
|
||||||
|
"colab": {
|
||||||
|
"base_uri": "https://localhost:8080/"
|
||||||
|
},
|
||||||
|
"id": "6BazTjB5yyG8",
|
||||||
|
"outputId": "f4cf85e6-e7d3-4ed1-8c67-b442e1653bfe"
|
||||||
|
},
|
||||||
"outputs": [
|
"outputs": [
|
||||||
{
|
{
|
||||||
"name": "stdout",
|
|
||||||
"output_type": "stream",
|
"output_type": "stream",
|
||||||
|
"name": "stdout",
|
||||||
"text": [
|
"text": [
|
||||||
"Epoch: 0 WallClockTime: 0 EpochDuration: 0\n",
|
"Epoch: 0 , wall clock time: 0 , loss: 1.4401766061782837\n",
|
||||||
"coup harm osc tracing\n",
|
"Epoch: 1 , wall clock time: 28.06755018234253 , loss: 5.44398972124327e-05\n",
|
||||||
"TrainLoss: tf.Tensor(1.0081012, shape=(), dtype=float32)\n",
|
"Epoch: 2 , wall clock time: 31.38792371749878 , loss: 1.064436037268024e-05\n",
|
||||||
"coup harm osc tracing\n",
|
"Epoch: 3 , wall clock time: 34.690271854400635 , loss: 3.163525434501935e-06\n",
|
||||||
"Epoch: 1 WallClockTime: 27.670811891555786 EpochDuration: 27.670811891555786\n",
|
"Epoch: 4 , wall clock time: 37.9914448261261 , loss: 1.1857609933940694e-06\n"
|
||||||
"TrainLoss: tf.Tensor(6.0117705e-05, shape=(), dtype=float32)\n",
|
|
||||||
"Epoch: 2 WallClockTime: 30.26737689971924 EpochDuration: 2.596565008163452\n",
|
|
||||||
"TrainLoss: tf.Tensor(1.1014066e-05, shape=(), dtype=float32)\n"
|
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"source": [
|
"source": [
|
||||||
"opt = Optimization(model,solver,loss_function,x_train,y_train)\n",
|
"opt = Optimization(model, solver, loss_function, X_TRAIN, Y_TRAIN)\n",
|
||||||
"opt.set_params(batch_size,learning_rate,optimizer,max_number,mode,trunc)\n",
|
"opt.set_params(BATCH_SIZE, LR, OPTIMIZER, MAX_TIME, MODE, TRUNC)\n",
|
||||||
"time_list, loss_list = opt.start_training()"
|
"time_list, loss_list = opt.start_training()"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"metadata": {},
|
"metadata": {
|
||||||
|
"id": "dPAJ6CGiyyG9"
|
||||||
|
},
|
||||||
"source": [
|
"source": [
|
||||||
"Plot and save data"
|
"\n",
|
||||||
|
"## Evaluation\n",
|
||||||
|
"\n",
|
||||||
|
"Now we can evaluate how our training converged over time. The following graph shows the loss evolution over time in seconds."
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 88,
|
"execution_count": 104,
|
||||||
"metadata": {},
|
"metadata": {
|
||||||
|
"colab": {
|
||||||
|
"base_uri": "https://localhost:8080/",
|
||||||
|
"height": 316
|
||||||
|
},
|
||||||
|
"id": "YzHtLIqgyyG9",
|
||||||
|
"outputId": "0388cf17-63e8-4587-a168-362f903e19d5"
|
||||||
|
},
|
||||||
"outputs": [
|
"outputs": [
|
||||||
{
|
{
|
||||||
"name": "stdout",
|
"output_type": "display_data",
|
||||||
"output_type": "stream",
|
|
||||||
"text": [
|
|
||||||
"dict_keys(['HIG'])\n"
|
|
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vJJuQ5IZt2C92jwikiMq+FGmQpkSPHNlSz65oz3lS8Vx5/s/cN2b3/Prlr1BRxORCKeCH6XOrn0qY+/uwN96NmX+bzu48LmpDP56GfvTMoKOJiIRqsAKvpnVM7M3zOzjgtpmYVcsNoYbO9Rl0v1JXNyiKi9OXs75zyUzcfHGoKOJSATKUcE3szfNbJOZLcy2vLuZLTOz5Wb24LHW4e4r3f3m3ISVI6tcNp7nft+Kkf3aUSoullvemcvNI+awZtu+oKOJSATJ6RH+CKB71gVmFgsMA3oATYG+ZtbUzJqb2RfZvirnaWo5onb1KvDlPZ34y0WNmbVyK92GJDN0UgoHDqnNIyI5LPjuPhXYlm1xG2B5+Mg9DRgJ9HL3Be5+SbavTTkNZGb9zGyumc3dvHlzjndEQorHxtCvc30mDUyiW9MqDPnmF7o/P5XkX/R3KVLU5aaHXx1Yk+VxanjZEZlZBTN7FTjTzB462uvcfbi7J7p7YqVKlXIRr2irmlCSYVefxbs3tyHGjOvf/J473pvHuh37g44mIgEpsJO27r7V3W939/ru/tSxXluU7paZ3zo1rMT4ezvxpwsb8e2yTZw3OJlXpqwgLT0z6GgiUsByU/DXAjWzPK4RXpZr7j7W3fslJCTkxeqKvBLFYrnr3AZ8MyCJTg0r8s+vlnLR0GnMXLEl6GgiUoByU/DnAA3NrK6ZxQF9gDF5EUpH+PmLine truncated
|
|
||||||
"text/plain": [
|
"text/plain": [
|
||||||
"<Figure size 432x288 with 1 Axes>"
|
"<Figure size 432x288 with 1 Axes>"
|
||||||
]
|
],
|
||||||
|
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAYoAAAEWCAYAAAB42tAoAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4yLjIsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+WH4yJAAAgAElEQVR4nO3dd5xU9bnH8c+znS3ssvSySJVexd6wRNFw1cQGmqq5x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zGJHjcYrRKR5KSgSQFFhNredPYK5q8q45YWPwy5HRFoZBUWCOHlIF75zRC/+8tZyps1fG3Y5ItKKKCgSyE9PHcjQ7m259rE5FG/eHnY5ItJKKCgSSGZaKpMmjqbO4bKps6iurQu7JBFpBRQUCaZXhxxuOXMYs1aUcvtLi8IuR0RaAQVFAho/vBvnHdqTP762jFcXrQ+7HBFJcgqKBPXL8YMZ2CWPqx+dw9qyyrDLEZEkpqBIUFnpqUw6bzSV1bVcPnUWNRqvEJE4UVAksH6dcvn1GUN5f/km7v7PJ2GXIyJJSkGR4L4+ugdnHdSDe15dwpufbAy7HBFJQgqKJPCr04fQt2MuVz4ym/VbNV4hIk1LQZEEsjPSmHzeaLZWVvOjR2ZTW+dhlyQiSURBkSQGdMnjxLine truncated
|
||||||
},
|
},
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"needs_background": "light"
|
"needs_background": "light"
|
||||||
},
|
}
|
||||||
"output_type": "display_data"
|
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"source": [
|
"source": [
|
||||||
"import matplotlib.pyplot as plt\n",
|
"import matplotlib.pyplot as plt\n",
|
||||||
"\n",
|
"\n",
|
||||||
"wct_times = {}\n",
|
"plt.plot(np.cumsum(time_list),loss_list)\n",
|
||||||
"wct_times[mode] = wct_time\n",
|
|
||||||
"print(format(wct_times.keys()))\n",
|
|
||||||
"\n",
|
|
||||||
"wct_time = np.cumsum(time_list)\n",
|
|
||||||
"plt.plot(wct_time,loss_list)\n",
|
|
||||||
"plt.yscale('log')\n",
|
"plt.yscale('log')\n",
|
||||||
"plt.xlabel('Wall clock time')\n",
|
"plt.xlabel('Wall clock time [s]'); plt.ylabel('Loss')\n",
|
||||||
"plt.xlabel('Loss')\n",
|
"plt.title('Linear chain - '+MODE+' with '+str(OPTIMIZER.__name__))\n",
|
||||||
"plt.title('Linear Chain - '+mode+'_'+str(optimizer.__name__))\n",
|
"plt.show()"
|
||||||
"plt.show()\n"
|
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"source": [
|
||||||
|
"For all three methods, you'll see a big linear step right at the start. As we're -- for fairness -- measuring the whole runtime, this first step includes all TensorFlow initialization steps, which are significantly more involved for HIG and SIP. Adam is much faster in terms of initialization, and likewise faster per training iteration.\n",
|
||||||
|
"\n",
|
||||||
|
"All three methods by themselves manage to bring down the loss. What's more interesting is to see how they compare. For this, the next cell stores the training evolution, and this notebook needs to be run one time with each of the three methods to produce the final comparison graph."
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"id": "xnr9BvlDU8H6"
|
||||||
|
}
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 89,
|
"execution_count": 105,
|
||||||
"metadata": {},
|
"metadata": {
|
||||||
|
"id": "bZMVQaJiyyG-"
|
||||||
|
},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"path = './'\n",
|
"path = '/home/'\n",
|
||||||
"name1 = 'time'+mode\n",
|
"namet = 'time'\n",
|
||||||
"name2 = 'loss'+mode\n",
|
"namel = 'loss'\n",
|
||||||
"np.savetxt(path+name1+'.txt',time_list)\n",
|
"np.savetxt(path+MODE+namet+'.txt',time_list)\n",
|
||||||
"np.savetxt(path+name2+'.txt',loss_list)"
|
"np.savetxt(path+MODE+namel+'.txt',loss_list)"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"source": [
|
||||||
|
"After runs with each of the methods, we can show them side by side:"
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"id": "KS1lL5LsV9JV"
|
||||||
|
}
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": null,
|
"source": [
|
||||||
"metadata": {},
|
"from os.path import exists\n",
|
||||||
"outputs": [],
|
"\n",
|
||||||
"source": []
|
"# if previous runs are available, compare all 3\n",
|
||||||
|
"if exists(path+'HIG'+namet+'.txt') and exists(path+'HIG'+namel+'.txt') and exists(path+'SIP'+namet+'.txt') and exists(path+'SIP'+namel+'.txt') and exists(path+'GD'+namet+'.txt') and exists(path+'GD'+namel+'.txt'):\n",
|
||||||
|
" lt_hig = np.loadtxt(path+'HIG'+namet+'.txt')\n",
|
||||||
|
" ll_hig = np.loadtxt(path+'HIG'+namel+'.txt')\n",
|
||||||
|
"\n",
|
||||||
|
" lt_sip = np.loadtxt(path+'SIP'+namet+'.txt')\n",
|
||||||
|
" ll_sip = np.loadtxt(path+'SIP'+namel+'.txt')\n",
|
||||||
|
"\n",
|
||||||
|
" lt_gd = np.loadtxt(path+'GD'+namet+'.txt')\n",
|
||||||
|
" ll_gd = np.loadtxt(path+'GD'+namel+'.txt')\n",
|
||||||
|
"\n",
|
||||||
|
" plt.plot(np.cumsum(lt_gd),ll_gd, label=\"GD\")\n",
|
||||||
|
" plt.plot(np.cumsum(lt_sip),ll_sip, label=\"SIP\")\n",
|
||||||
|
" plt.plot(np.cumsum(lt_hig),ll_hig, label=\"HIG\")\n",
|
||||||
|
" plt.yscale('log')\n",
|
||||||
|
" plt.xlabel('Wall clock time [s]'); plt.ylabel('Training loss'); plt.legend()\n",
|
||||||
|
" plt.title('Linear chain comparison ')\n",
|
||||||
|
" plt.show()\n",
|
||||||
|
"else:\n",
|
||||||
|
" print(\"Run this notebook three times with MODE='HIG|SIP|GD' to produce the final graph\")\n"
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"colab": {
|
||||||
|
"base_uri": "https://localhost:8080/",
|
||||||
|
"height": 316
|
||||||
|
},
|
||||||
|
"id": "OsKQ0MyX6Zmx",
|
||||||
|
"outputId": "c76d6002-24e3-4f7d-e3a2-042ce15f9478"
|
||||||
|
},
|
||||||
|
"execution_count": 106,
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"output_type": "display_data",
|
||||||
|
"data": {
|
||||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9egOEjXjbmOFRkruHPxnQyJGUJSWFLtsl6xofznnDEs35HDffPWcdP7q+gVE8KEvrE2Vmwjh9M6RDc8ARo6qMsYKMnZ/9Dg7x6EnT9pUKgubcmSJcybN48VK1YQGBhIVlYWFRUVB6z33XffERcXx2233cYDDzzAE0884bUatJP4IAU4A5gzeQ7GGG5ceCOVdc9vAMb0iuHJs0cBsCOn/lPulZsIhMZCwnAYOB3GXmR1eZXm2F2ZUrbas2cPcXFxBAYGAhAXF0dSUlKD60+ePJnNmzd7tQZtUbRCz4ie3DXpLm78/kaeXPEk14+9/oB1YsOsq81mFx34F4BqQkgM5O2yuwqlLJ/fAumrvbvNhOFw/EONrnLcccdx7733MnDgQI455hhmzZrFlClTGlx/3rx5DB8+3KtlaouilWb0nsGZA8/k5TUvszB14QHLQwL8CPJ3kFNcXs+7VaNCYuo/RFepLiQsLIzly5fz3HPPER8fz6xZs3jllVcOWO+oo45i5MiRFBQUcOutt3q1Bm1ReMFN42/i18xfuX3R7bx30nskhO4/iBQbGqgtioMRHGN1PRmj52Uo+zXxl39bcjqdTJ06lalTpzJ8+HBeffXVA9apGaNoC9qi8IJAZyBzpsyhvLqcmxfeTFWdQ0G7RQSyt7DMpuo6sZBYqCqDSh3fUV3XhLine truncated
|
||||||
|
},
|
||||||
|
"metadata": {
|
||||||
|
"needs_background": "light"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"source": [
|
||||||
|
"This graph makes the significant differences in terms of convergence very clear: Adam (the blue `GD` curve), performs a large number of updates, but its rough approximation of the Hessian is not enough to converge to high accuracies. It stagnates at a high loss level.\n",
|
||||||
|
"\n",
|
||||||
|
"The SIP updates don't outperform Adam in this scenario. This is caused by the relatively simple phyiscs (the _linear_ oscillators), and the higher runtime cost of SIP. If you run this example longer, SIP will acutally overtake Adam, but start suffering from numerical issues with the full inversion.\n",
|
||||||
|
"\n",
|
||||||
|
"The HIGs perform much better than the two others: despite being fairly slow per iteration, the half-inversion produces a very good update, that makes the training converge to very low loss values very quickly. The HIGs reach an accuracy that is around four order of magnitudes better than the other two methods."
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"id": "XifTlo4xWPDb"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"source": [
|
||||||
|
"## Next steps\n",
|
||||||
|
"\n",
|
||||||
|
"There's a variety of interesting avenues for further tests and modifications with this notebook:\n",
|
||||||
|
"\n",
|
||||||
|
"* Most importantly, we've acutally only looked at training performance so far! This keeps the notebook reasonably short, but it's admittedly bad practice. While we can claim that HIGs likewise work on _real_ test data, this is a great next step: allocate proper test samples, and re-run the evaluation for all three methods on the test data.\n",
|
||||||
|
"* Also, you can vary the physics behavior: use more oscillators for longer or shorter time spans, or even include a non-linear force (as employed in the HIG paper). Be warned: for the latter, the SIP version will require a new inverse solver, though."
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"id": "UiaLlPVz-clP"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"metadata": {
|
"metadata": {
|
||||||
@@ -635,8 +793,7 @@
|
|||||||
"hash": "60a46218545510a486eb79ffac0761a327b44b02462576ac242e6c15f2abc4b7"
|
"hash": "60a46218545510a486eb79ffac0761a327b44b02462576ac242e6c15f2abc4b7"
|
||||||
},
|
},
|
||||||
"kernelspec": {
|
"kernelspec": {
|
||||||
"display_name": "Python 3",
|
"display_name": "Python 3.8.10 64-bit ('tf250': conda)",
|
||||||
"language": "python",
|
|
||||||
"name": "python3"
|
"name": "python3"
|
||||||
},
|
},
|
||||||
"language_info": {
|
"language_info": {
|
||||||
@@ -649,9 +806,15 @@
|
|||||||
"name": "python",
|
"name": "python",
|
||||||
"nbconvert_exporter": "python",
|
"nbconvert_exporter": "python",
|
||||||
"pygments_lexer": "ipython3",
|
"pygments_lexer": "ipython3",
|
||||||
"version": "3.8.5"
|
"version": "3.8.10"
|
||||||
|
},
|
||||||
|
"orig_nbformat": 4,
|
||||||
|
"colab": {
|
||||||
|
"name": "LinearChain_GD_PG_HIG-fromSlack-colab.ipynb",
|
||||||
|
"provenance": [],
|
||||||
|
"collapsed_sections": []
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"nbformat": 4,
|
"nbformat": 4,
|
||||||
"nbformat_minor": 2
|
"nbformat_minor": 0
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user