Files
udlbook/Notebooks/Chap16/16_3_Contraction_Mappings.ipynb
2023-10-06 16:57:48 +01:00

294 lines
7.7 KiB
Plaintext

{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": [],
"authorship_tag": "ABX9TyNeCWINUqqUGKMcxsqPFTAh",
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/github/udlbook/udlbook/blob/main/Notebooks/Chap16/16_3_Contraction_Mappings.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "markdown",
"source": [
"# **Notebook 16.3: Contraction mappings**\n",
"\n",
"This notebook investigates a 1D normalizing flows example similar to that illustrated in figure 16.9 in the book.\n",
"\n",
"Work through the cells below, running each cell in turn. In various places you will see the words \"TO DO\". Follow the instructions at these places and make predictions about what is going to happen or write code to complete the functions.\n",
"\n",
"Contact me at udlbookmail@gmail.com if you find any mistakes or have any suggestions."
],
"metadata": {
"id": "t9vk9Elugvmi"
}
},
{
"cell_type": "code",
"source": [
"import numpy as np\n",
"import matplotlib.pyplot as plt"
],
"metadata": {
"id": "OLComQyvCIJ7"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"# Define a function that is a contraction mapping\n",
"def f(z):\n",
" return 0.3 + 0.5 *z + 0.02 * np.sin(z*15)"
],
"metadata": {
"id": "4Pfz2KSghdVI"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"def draw_function(f, fixed_point=None):\n",
" z = np.arange(0,1,0.01)\n",
" z_prime = f(z)\n",
"\n",
" # Draw this function\n",
" fig, ax = plt.subplots()\n",
" ax.plot(z, z_prime,'c-')\n",
" ax.plot([0,1],[0,1],'k--')\n",
" if fixed_point!=None:\n",
" ax.plot(fixed_point, fixed_point, 'ro')\n",
" ax.set_xlim(0,1)\n",
" ax.set_ylim(0,1)\n",
" ax.set_xlabel('Input, $z$')\n",
" ax.set_ylabel('Output, f$[z]$')\n",
" plt.show()"
],
"metadata": {
"id": "zEwCbIx0hpAI"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"draw_function(f)"
],
"metadata": {
"id": "k4e5Yu0fl8bz"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"Now let's find where $\\mbox{f}[z]=z$ using fixed point iteration"
],
"metadata": {
"id": "DfgKrpCAjnol"
}
},
{
"cell_type": "code",
"source": [
"# Takes a function f and a starting point z\n",
"def fixed_point_iteration(f, z0):\n",
" # TODO -- write this function\n",
" # Print out the iterations as you go, so you can see the progress\n",
" # Set the maximum number of iterations to 20\n",
" # Replace this line\n",
" z_out = 0.5;\n",
"\n",
"\n",
"\n",
" return z_out"
],
"metadata": {
"id": "bAOBvZT-j3lv"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"Now let's test that and plot the solution"
],
"metadata": {
"id": "CAS0lgIomAa0"
}
},
{
"cell_type": "code",
"source": [
"# Now let's test that\n",
"z = fixed_point_iteration(f, 0.2)\n",
"draw_function(f, z)"
],
"metadata": {
"id": "EYQZJdNPk8Lg"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"# Let's define another function\n",
"def f2(z):\n",
" return 0.7 + -0.6 *z + 0.03 * np.sin(z*15)\n",
"draw_function(f2)"
],
"metadata": {
"id": "4DipPiqVlnwJ"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"# Now let's test that\n",
"# TODO Before running this code, predict what you think will happen\n",
"z = fixed_point_iteration(f2, 0.9)\n",
"draw_function(f2, z)"
],
"metadata": {
"id": "tYOdbWcomdEE"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"# Let's define another function\n",
"# Define a function that is a contraction mapping\n",
"def f3(z):\n",
" return -0.2 + 1.5 *z + 0.1 * np.sin(z*15)\n",
"draw_function(f3)"
],
"metadata": {
"id": "Mni37RUpmrIu"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"# Now let's test that\n",
"# TODO Before running this code, predict what you think will happen\n",
"z = fixed_point_iteration(f3, 0.7)\n",
"draw_function(f3, z)"
],
"metadata": {
"id": "agt5mfJrnM1O"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"Finally, let's invert a problem of the form $y = z+ f[z]$ for a given value of $y$. What is the $z$ that maps to it?"
],
"metadata": {
"id": "n6GI46-ZoQz6"
}
},
{
"cell_type": "code",
"source": [
"def f4(z):\n",
" return -0.3 + 0.5 *z + 0.02 * np.sin(z*15)"
],
"metadata": {
"id": "dy6r3jr9rjPf"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"def fixed_point_iteration_z_plus_f(f, y, z0):\n",
" # TODO -- write this function\n",
" # Replace this line\n",
" z_out = 1\n",
"\n",
" return z_out"
],
"metadata": {
"id": "GMX64Iz0nl-B"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"def draw_function2(f, y, fixed_point=None):\n",
" z = np.arange(0,1,0.01)\n",
" z_prime = z+f(z)\n",
"\n",
" # Draw this function\n",
" fig, ax = plt.subplots()\n",
" ax.plot(z, z_prime,'c-')\n",
" ax.plot(z, y-f(z),'r-')\n",
" ax.plot([0,1],[0,1],'k--')\n",
" if fixed_point!=None:\n",
" ax.plot(fixed_point, y, 'ro')\n",
" ax.set_xlim(0,1)\n",
" ax.set_ylim(0,1)\n",
" ax.set_xlabel('Input, $z$')\n",
" ax.set_ylabel('Output, z+f$[z]$')\n",
" plt.show()"
],
"metadata": {
"id": "uXxKHad5qT8Y"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"# Test this out and draw\n",
"y = 0.8\n",
"z = fixed_point_iteration_z_plus_f(f4,y,0.2)\n",
"draw_function2(f4,y,z)\n",
"# If you have done this correctly, the red dot should be\n",
"# where the cyan curve has a y value of 0.8"
],
"metadata": {
"id": "mNEBXC3Aqd_1"
},
"execution_count": null,
"outputs": []
}
]
}