From 9d8781c32c64a41efb2bbc76c55acb4687f00453 Mon Sep 17 00:00:00 2001 From: Russ Tedrake Date: Fri, 11 Jun 2021 19:49:32 -0400 Subject: [PATCH] add virtual work, update solutions --- .../hopfield_network/hopfield_network.ipynb | 230 +++++++----------- .../sysid/linear_sysid/linear_sysid.ipynb | 1 + index.html | 2 +- multibody.html | 34 ++- sysid.html | 116 ++++----- 5 files changed, 169 insertions(+), 214 deletions(-) diff --git a/exercises/pend/hopfield_network/hopfield_network.ipynb b/exercises/pend/hopfield_network/hopfield_network.ipynb index e8b988fe..8a6ebdba 100644 --- a/exercises/pend/hopfield_network/hopfield_network.ipynb +++ b/exercises/pend/hopfield_network/hopfield_network.ipynb @@ -1,34 +1,4 @@ { - "nbformat": 4, - "nbformat_minor": 0, - "metadata": { - "@webio": { - "lastCommId": null, - "lastKernelId": null - }, - "colab": { - "name": "hopfield_network.ipynb", - "provenance": [], - "collapsed_sections": [] - }, - "kernelspec": { - "display_name": "Python 3", - "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.4" - } - }, "cells": [ { "cell_type": "markdown", @@ -51,9 +21,11 @@ }, { "cell_type": "code", + "execution_count": null, "metadata": { "id": "PBhjzZEOLubw" }, + "outputs": [], "source": [ "import importlib\n", "import sys\n", @@ -82,9 +54,7 @@ "# underactuated imports\n", "from underactuated import plot_2d_phase_portrait, FindResource\n", "from underactuated.jupyter import running_as_notebook" - ], - "execution_count": null, - "outputs": [] + ] }, { "cell_type": "markdown", @@ -109,9 +79,11 @@ }, { "cell_type": "code", + "execution_count": null, "metadata": { "id": "ousWBWX73GDh" }, + "outputs": [], "source": [ "# We provide two helper functions: softmax and matrix-vector multiplication\n", "def softmax(x, beta = 1):\n", @@ -135,44 +107,7 @@ " return x # you need to modify this\n", "\n", " ###################################################################\n" - ], - "execution_count": null, - "outputs": [] - }, - { - "cell_type": "code", - "metadata": { - "id": "UMVRCnB6uRdE" - }, - "source": [ - "# We provide two helper functions: softmax and matrix-vector multiplication\n", - "def softmax(x, beta = 1):\n", - " e_x = np.array([np.exp(beta*x[i]) for i in range(len(x))])\n", - " return e_x / e_x.sum(axis = 0)\n", - "\n", - "def matrix_vector_multiplication(A, x):\n", - " return [sum(a*b for a,b in zip(A_row,x)) for A_row in A]\n", - "\n", - "def dynamics(x, A, beta):\n", - " \"\"\"outputs the right hand side of differential equation in Hopfield dynamical system.\n", - " use helper functions above for matrix vector multiplication and softmax function.\n", - "\n", - " ARGUMENTS: x: list (length n) of numpy array\n", - " A: numpy array of size m by n\n", - " beta: scalar\n", - " RETURNS: numpy array of size n\n", - " \"\"\"\n", - " ################### Your solution goes here #######################\n", - "\n", - " Ax = [sum(a*b for a,b in zip(A_row,x)) for A_row in A]\n", - " sftmx = softmax(Ax, beta)\n", - " AT_sftmx = [sum(a*b for a,b in zip(A_row,sftmx)) for A_row in A.T]\n", - " return AT_sftmx - np.array(x)\n", - "\n", - " ###################################################################\n" - ], - "execution_count": null, - "outputs": [] + ] }, { "cell_type": "markdown", @@ -186,9 +121,11 @@ }, { "cell_type": "code", + "execution_count": null, "metadata": { "id": "URTCCqT63cPJ" }, + "outputs": [], "source": [ "A = np.array([[1,0],[0,1],[-1,-1]])\n", "beta = 5\n", @@ -198,9 +135,7 @@ "\n", "# plot the phase portrait of the 2d system\n", "plot_2d_phase_portrait((lambda x: dynamics(x, A, beta)), x1lim=[-2, 2], x2lim=[-2, 2], linewidth=1, density=2)" - ], - "execution_count": null, - "outputs": [] + ] }, { "cell_type": "markdown", @@ -218,9 +153,11 @@ }, { "cell_type": "code", + "execution_count": null, "metadata": { "id": "XhImd8aVZtv1" }, + "outputs": [], "source": [ "# This data was obtained by taking images (0, 1, 4, 6) and (8, 34, 5),\n", "# respectively from the torchvision MNIST dataset:\n", @@ -251,9 +188,7 @@ "\n", "training_data = np.asarray(training_data)\n", "print(training_data.shape)" - ], - "execution_count": null, - "outputs": [] + ] }, { "cell_type": "markdown", @@ -273,24 +208,26 @@ }, { "cell_type": "code", + "execution_count": null, "metadata": { "id": "gsDaCZAqyUrH" }, + "outputs": [], "source": [ "x_image = training_data[0]\n", "x_state = training_data[0].reshape((-1))\n", "\n", "print(\"Training image has the shape \" + str(x_image.shape) + '.')\n", "print(\"The image can be represented as a vector of size \" + str(x_state.shape) + '.')" - ], - "execution_count": null, - "outputs": [] + ] }, { "cell_type": "code", + "execution_count": null, "metadata": { "id": "s0h37TyVbyDH" }, + "outputs": [], "source": [ "\n", "################### Your solution goes here ############################\n", @@ -308,37 +245,7 @@ "\n", "A = calculate_A(training_data)\n", "beta = 1e-5\n" - ], - "execution_count": null, - "outputs": [] - }, - { - "cell_type": "code", - "metadata": { - "id": "nk06PC_buZm_" - }, - "source": [ - "\n", - "################### Your solution goes here ############################\n", - "\n", - "def calculate_A(training_data):\n", - " \"\"\"outputs the right hand side of differential equation in Hopfield dynamical system.\n", - "\n", - " ARGUMENTS: training_data: a list of numpy array of arbitrary size (e.g., 28 by 28 for MNIST problem)\n", - " RETURNS: numpy array of size m by n (e.g., 4 by 784 for MNIST problem)\n", - " \"\"\"\n", - " A = []\n", - " for item in training_data:\n", - " A += [item.reshape((-1))]\n", - " return np.array(A)\n", - "\n", - "#########################################################################\n", - "\n", - "A = calculate_A(training_data)\n", - "beta = 1e-5\n" - ], - "execution_count": null, - "outputs": [] + ] }, { "cell_type": "markdown", @@ -356,9 +263,11 @@ }, { "cell_type": "code", + "execution_count": null, "metadata": { "id": "hLJLnMXyesD9" }, + "outputs": [], "source": [ "class HopfieldNet(VectorSystem):\n", " def __init__(self, A, beta):\n", @@ -388,9 +297,7 @@ "\n", "# finalize diagram\n", "diagram = builder.Build()" - ], - "execution_count": null, - "outputs": [] + ] }, { "cell_type": "markdown", @@ -405,9 +312,11 @@ }, { "cell_type": "code", + "execution_count": null, "metadata": { "id": "NrPFuRiyeyLy" }, + "outputs": [], "source": [ "# function that given the initial state\n", "# and a simulation time returns the system trajectory\n", @@ -429,9 +338,7 @@ "\n", " # return the output (here = state) trajectory\n", " return logger.data()" - ], - "execution_count": null, - "outputs": [] + ] }, { "cell_type": "markdown", @@ -444,9 +351,11 @@ }, { "cell_type": "code", + "execution_count": null, "metadata": { "id": "oMfs_WL1e4Bz" }, + "outputs": [], "source": [ "# Visualize the simulated trajectories for corrupted image 1.\n", "if running_as_notebook:\n", @@ -469,15 +378,15 @@ "\n", "plt.close()\n", "myAnimation" - ], - "execution_count": null, - "outputs": [] + ] }, { "cell_type": "code", + "execution_count": null, "metadata": { "id": "nRDln7VXjFeh" }, + "outputs": [], "source": [ "# Visualize the simulated trajectories for corrupted image 2.\n", "\n", @@ -496,15 +405,15 @@ "\n", "plt.close()\n", "myAnimation" - ], - "execution_count": null, - "outputs": [] + ] }, { "cell_type": "code", + "execution_count": null, "metadata": { "id": "I3tPCtvmPsql" }, + "outputs": [], "source": [ "# Visualize the simulated trajectories for corrupted image 3.\n", "\n", @@ -523,9 +432,7 @@ "\n", "plt.close()\n", "myAnimation" - ], - "execution_count": null, - "outputs": [] + ] }, { "cell_type": "markdown", @@ -545,9 +452,11 @@ }, { "cell_type": "code", + "execution_count": null, "metadata": { "id": "hhphJgjHuEu5" }, + "outputs": [], "source": [ "urls = [\"https://upload.wikimedia.org/wikipedia/en/5/59/Pok%C3%A9mon_Squirtle_art.png\",\n", " \"https://upload.wikimedia.org/wikipedia/en/a/a5/Pok%C3%A9mon_Charmander_art.png\",\n", @@ -580,15 +489,15 @@ "for i, item in enumerate(query_data):\n", " ax[i].imshow(item)\n", "plt.show()" - ], - "execution_count": null, - "outputs": [] + ] }, { "cell_type": "code", + "execution_count": null, "metadata": { "id": "VzAMYp_tvI3f" }, + "outputs": [], "source": [ "A_img = []\n", "for item in training_data:\n", @@ -611,15 +520,15 @@ "\n", "# finalize diagram\n", "diagram = builder.Build()" - ], - "execution_count": null, - "outputs": [] + ] }, { "cell_type": "code", + "execution_count": null, "metadata": { "id": "RPPnIaKxA9c4" }, + "outputs": [], "source": [ "# function that given the initial state\n", "# and a simulation time returns the system trajectory\n", @@ -641,15 +550,15 @@ "\n", " # return the output (here = state) trajectory\n", " return logger.data()" - ], - "execution_count": null, - "outputs": [] + ] }, { "cell_type": "code", + "execution_count": null, "metadata": { "id": "VarWTJr4DyFV" }, + "outputs": [], "source": [ "# Visualize the simulated trajectories.\n", "x_init = query_data[0].reshape((-1))\n", @@ -667,9 +576,7 @@ "\n", "plt.close()\n", "myAnimation" - ], - "execution_count": null, - "outputs": [] + ] }, { "cell_type": "markdown", @@ -683,28 +590,57 @@ }, { "cell_type": "code", + "execution_count": null, "metadata": { "id": "bzbgQIGc0Efn" }, + "outputs": [], "source": [ "from underactuated.exercises.pend.hopfield_network.test_hopfield_network import TestHopfield\n", "from underactuated.exercises.grader import Grader\n", "Grader.grade_output([TestHopfield], [locals()], 'results.json')\n", "Grader.print_test_results('results.json')" - ], - "execution_count": null, - "outputs": [] + ] }, { "cell_type": "code", + "execution_count": null, "metadata": { "id": "wCHOihWN3oNI" }, - "source": [ - "" - ], - "execution_count": null, - "outputs": [] + "outputs": [], + "source": [] + } + ], + "metadata": { + "@webio": { + "lastCommId": null, + "lastKernelId": null + }, + "celltoolbar": "Tags", + "colab": { + "collapsed_sections": [], + "name": "hopfield_network.ipynb", + "provenance": [] + }, + "kernelspec": { + "display_name": "Python 3", + "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.4" } - ] + }, + "nbformat": 4, + "nbformat_minor": 1 } \ No newline at end of file diff --git a/exercises/sysid/linear_sysid/linear_sysid.ipynb b/exercises/sysid/linear_sysid/linear_sysid.ipynb index ae7b6500..1a287c28 100644 --- a/exercises/sysid/linear_sysid/linear_sysid.ipynb +++ b/exercises/sysid/linear_sysid/linear_sysid.ipynb @@ -463,6 +463,7 @@ "lastCommId": null, "lastKernelId": null }, + "celltoolbar": "Tags", "colab": { "collapsed_sections": [], "name": "linear_sysid.ipynb", diff --git a/index.html b/index.html index b477515c..9355dee3 100644 --- a/index.html +++ b/index.html @@ -527,7 +527,7 @@

Table of Contents

  • Appendix B: Multi-Body Dynamics