Skip to content

Commit

Permalink
Update course book
Browse files Browse the repository at this point in the history
  • Loading branch information
actions-user committed Aug 20, 2024
1 parent 385a016 commit 5d62e44
Show file tree
Hide file tree
Showing 181 changed files with 29,163 additions and 29,266 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@
" - title (str): title of the plot.\n",
" \"\"\"\n",
" with plt.xkcd():\n",
" plt.plot(x_range, sims)\n",
" plt.plot(x_range, sim_mat)\n",
" plt.xlabel('x')\n",
" plt.ylabel('Similarity')\n",
" plt.title(title)"
Expand Down Expand Up @@ -382,6 +382,100 @@
"set_seed(seed = 42)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Helper functions\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"cellView": "form",
"execution": {},
"tags": [
"hide-input"
]
},
"outputs": [],
"source": [
"# @title Helper functions\n",
"\n",
"# mainly contains solutions to exercises for correct plot output; please don't take a look!\n",
"set_seed(42)\n",
"\n",
"vector_length = 1024\n",
"symbol_names = ['circle','square','triangle']\n",
"discrete_space = sspspace.DiscreteSPSpace(symbol_names, ssp_dim=vector_length, optimize = False)\n",
"\n",
"circle = discrete_space.encode('circle')\n",
"square = discrete_space.encode('square')\n",
"triangle = discrete_space.encode('triangle')\n",
"\n",
"shape = (circle + square + triangle).normalize()\n",
"\n",
"shape_sim_mat = np.zeros((4,4))\n",
"\n",
"shape_sim_mat[0,0] = (circle | circle).item()\n",
"shape_sim_mat[1,1] = (square | square).item()\n",
"shape_sim_mat[2,2] = (triangle | triangle).item()\n",
"shape_sim_mat[3,3] = (shape | shape).item()\n",
"\n",
"shape_sim_mat[0,1] = shape_sim_mat[1,0] = (circle | square).item()\n",
"shape_sim_mat[0,2] = shape_sim_mat[2,0] = (circle | triangle).item()\n",
"shape_sim_mat[0,3] = shape_sim_mat[3,0] = (circle | shape).item()\n",
"\n",
"shape_sim_mat[1,2] = shape_sim_mat[2,1] = (square | triangle).item()\n",
"shape_sim_mat[1,3] = shape_sim_mat[3,1] = (square | shape).item()\n",
"\n",
"shape_sim_mat[2,3] = shape_sim_mat[3,2] = (triangle | shape).item()\n",
"\n",
"new_symbol_names = ['circle','square','triangle', 'red', 'blue', 'green']\n",
"new_discrete_space = sspspace.DiscreteSPSpace(new_symbol_names, ssp_dim=vector_length, optimize=False)\n",
"\n",
"objs = {n:new_discrete_space.encode(np.array([n])) for n in new_symbol_names}\n",
"\n",
"objs['red*circle'] = objs['red'] * objs['circle']\n",
"objs['blue*triangle'] = objs['blue'] * objs['triangle']\n",
"objs['green*square'] = objs['green'] * objs['square']\n",
"\n",
"new_object_names = ['red','red^','red*circle','circle','circle^']\n",
"new_objs = objs.copy()\n",
"\n",
"new_objs['red^'] = new_objs['red*circle'] * ~new_objs['circle']\n",
"new_objs['circle^'] = new_objs['red*circle'] * ~new_objs['red']\n",
"\n",
"axis_vectors = ['one']\n",
"\n",
"encoder = sspspace.DiscreteSPSpace(axis_vectors, ssp_dim=1024, optimize=False)\n",
"\n",
"vocab = {w:encoder.encode(w) for w in axis_vectors}\n",
"\n",
"integers = [vocab['one']]\n",
"\n",
"max_int = 5\n",
"for i in range(2, max_int + 1):\n",
" integers.append(integers[-1] * vocab['one'])\n",
"\n",
"integers = np.array(integers).squeeze()\n",
"integer_sims = integers @ integers.T\n",
"\n",
"five_unbind_two = sspspace.SSP(integers[4]) * ~sspspace.SSP(integers[1])\n",
"five_unbind_two_sims = five_unbind_two @ integers.T\n",
"\n",
"new_encoder = sspspace.RandomSSPSpace(domain_dim=1, ssp_dim=1024)\n",
"\n",
"xs = np.linspace(-4,4,401)[:,None]\n",
"phis = new_encoder.encode(xs)\n",
"\n",
"real_line_sims = phis[200, :] @ phis.T\n",
"\n",
"phi_shifted = phis[200,:][None,:] * new_encoder.encode([[np.pi/2]])\n",
"shifted_real_line_sims = phi_shifted.flatten() @ phis.T"
]
},
{
"cell_type": "markdown",
"metadata": {
Expand Down Expand Up @@ -587,17 +681,17 @@
},
"outputs": [],
"source": [
"sim_mat = np.zeros((3,3))\n",
"concepts_sim_mat = np.zeros((3,3))\n",
"\n",
"sim_mat[0,0] = (circle | circle).item()\n",
"sim_mat[1,1] = (square | square).item()\n",
"sim_mat[2,2] = (triangle | triangle).item()\n",
"concepts_sim_mat[0,0] = (circle | circle).item()\n",
"concepts_sim_mat[1,1] = (square | square).item()\n",
"concepts_sim_mat[2,2] = (triangle | triangle).item()\n",
"\n",
"sim_mat[0,1] = sim_mat[1,0] = (circle | square).item()\n",
"sim_mat[0,2] = sim_mat[2,0] = (circle | triangle).item()\n",
"sim_mat[1,2] = sim_mat[2,1] = (square | triangle).item()\n",
"concepts_sim_mat[0,1] = concepts_sim_mat[1,0] = (circle | square).item()\n",
"concepts_sim_mat[0,2] = concepts_sim_mat[2,0] = (circle | triangle).item()\n",
"concepts_sim_mat[1,2] = concepts_sim_mat[2,1] = (square | triangle).item()\n",
"\n",
"plot_similarity_matrix(sim_mat, symbol_names)"
"plot_similarity_matrix(concepts_sim_mat, symbol_names)"
]
},
{
Expand Down Expand Up @@ -780,21 +874,21 @@
"raise NotImplementedError(\"Student exercise: complete calcualtion of similarity matrix.\")\n",
"###################################################################\n",
"\n",
"sim_mat = np.zeros((4,4))\n",
"shape_sim_mat = np.zeros((4,4))\n",
"\n",
"sim_mat[0,0] = (circle | circle).item()\n",
"sim_mat[1,1] = (square | square).item()\n",
"sim_mat[2,2] = (triangle | ...).item()\n",
"sim_mat[3,3] = (shape | ...).item()\n",
"shape_sim_mat[0,0] = (circle | circle).item()\n",
"shape_sim_mat[1,1] = (square | square).item()\n",
"shape_sim_mat[2,2] = (triangle | ...).item()\n",
"shape_sim_mat[3,3] = (shape | ...).item()\n",
"\n",
"sim_mat[0,1] = sim_mat[1,0] = (circle | square).item()\n",
"sim_mat[0,2] = sim_mat[2,0] = (circle | triangle).item()\n",
"sim_mat[0,3] = sim_mat[3,0] = (circle | shape).item()\n",
"shape_sim_mat[0,1] = shape_sim_mat[1,0] = (circle | square).item()\n",
"shape_sim_mat[0,2] = shape_sim_mat[2,0] = (circle | triangle).item()\n",
"shape_sim_mat[0,3] = shape_sim_mat[3,0] = (circle | shape).item()\n",
"\n",
"sim_mat[1,2] = sim_mat[2,1] = (square | triangle).item()\n",
"sim_mat[1,3] = sim_mat[3,1] = (square | shape).item()\n",
"shape_sim_mat[1,2] = shape_sim_mat[2,1] = (square | triangle).item()\n",
"shape_sim_mat[1,3] = shape_sim_mat[3,1] = (square | shape).item()\n",
"\n",
"sim_mat[2,3] = sim_mat[3,2] = (... | ...).item()"
"shape_sim_mat[2,3] = shape_sim_mat[3,2] = (... | ...).item()"
]
},
{
Expand All @@ -804,7 +898,7 @@
"execution": {}
},
"source": [
"[*Click for solution*](https://github.com/neuromatch/NeuroAI_Course/tree/main/tutorials/W2D2_NeuroSymbolicMethods/solutions/W2D2_Tutorial1_Solution_8d8fa911.py)\n",
"[*Click for solution*](https://github.com/neuromatch/NeuroAI_Course/tree/main/tutorials/W2D2_NeuroSymbolicMethods/solutions/W2D2_Tutorial1_Solution_708701bb.py)\n",
"\n"
]
},
Expand All @@ -816,7 +910,7 @@
},
"outputs": [],
"source": [
"plot_similarity_matrix(sim_mat, symbol_names + [\"shape\"], values = True)"
"plot_similarity_matrix(shape_sim_mat, symbol_names + [\"shape\"], values = True)"
]
},
{
Expand Down Expand Up @@ -998,10 +1092,10 @@
"source": [
"set_seed(42)\n",
"\n",
"symbol_names = ['circle','square','triangle', 'red', 'blue', 'green']\n",
"discrete_space = sspspace.DiscreteSPSpace(symbol_names, ssp_dim=vector_length, optimize=False)\n",
"new_symbol_names = ['circle','square','triangle', 'red', 'blue', 'green']\n",
"new_discrete_space = sspspace.DiscreteSPSpace(new_symbol_names, ssp_dim=vector_length, optimize=False)\n",
"\n",
"objs = {n:discrete_space.encode(np.array([n])) for n in symbol_names}"
"objs = {n:new_discrete_space.encode(np.array([n])) for n in new_symbol_names}"
]
},
{
Expand Down Expand Up @@ -1082,13 +1176,13 @@
"outputs": [],
"source": [
"object_names = list(objs.keys())\n",
"sims = np.zeros((len(object_names), len(object_names)))\n",
"obj_sims = np.zeros((len(object_names), len(object_names)))\n",
"\n",
"for name_idx, name in enumerate(object_names):\n",
" for other_idx in range(name_idx, len(object_names)):\n",
" sims[name_idx, other_idx] = sims[other_idx, name_idx] = (objs[name] | objs[object_names[other_idx]]).item()\n",
" obj_sims[name_idx, other_idx] = obj_sims[other_idx, name_idx] = (objs[name] | objs[object_names[other_idx]]).item()\n",
"\n",
"plot_similarity_matrix(sims, object_names)"
"plot_similarity_matrix(obj_sims, object_names)"
]
},
{
Expand Down Expand Up @@ -1270,15 +1364,16 @@
},
"outputs": [],
"source": [
"object_names = ['red','red^','red*circle','circle','circle^']\n",
"new_object_names = ['red','red^','red*circle','circle','circle^']\n",
"new_objs = objs\n",
"\n",
"###################################################################\n",
"## Fill out the following then remove\n",
"raise NotImplementedError(\"Student exercise: complete derivation of default objects using pseudoinverse.\")\n",
"###################################################################\n",
"\n",
"objs['red^'] = objs['red*circle'] * ~objs['circle']\n",
"objs['circle^'] = objs[...] * ~objs[...]"
"new_objs['red^'] = new_objs['red*circle'] * ~new_objs['circle']\n",
"new_objs['circle^'] = new_objs[...] * ~new_objs[...]"
]
},
{
Expand All @@ -1288,7 +1383,7 @@
"execution": {}
},
"source": [
"[*Click for solution*](https://github.com/neuromatch/NeuroAI_Course/tree/main/tutorials/W2D2_NeuroSymbolicMethods/solutions/W2D2_Tutorial1_Solution_685af726.py)\n",
"[*Click for solution*](https://github.com/neuromatch/NeuroAI_Course/tree/main/tutorials/W2D2_NeuroSymbolicMethods/solutions/W2D2_Tutorial1_Solution_f77f76dd.py)\n",
"\n"
]
},
Expand All @@ -1300,13 +1395,13 @@
},
"outputs": [],
"source": [
"sims = np.zeros((len(object_names), len(object_names)))\n",
"new_obj_sims = np.zeros((len(new_object_names), len(new_object_names)))\n",
"\n",
"for name_idx, name in enumerate(object_names):\n",
" for other_idx in range(name_idx, len(object_names)):\n",
" sims[name_idx, other_idx] = sims[other_idx, name_idx] = (objs[name] | objs[object_names[other_idx]]).item()\n",
"for name_idx, name in enumerate(new_object_names):\n",
" for other_idx in range(name_idx, len(new_object_names)):\n",
" new_obj_sims[name_idx, other_idx] = new_obj_sims[other_idx, name_idx] = (new_objs[name] | new_objs[new_object_names[other_idx]]).item()\n",
"\n",
"plot_similarity_matrix(sims, object_names, values = True)"
"plot_similarity_matrix(new_obj_sims, new_object_names, values = True)"
]
},
{
Expand Down Expand Up @@ -2054,13 +2149,13 @@
"###################################################################\n",
"\n",
"set_seed(42)\n",
"encoder = sspspace.RandomSSPSpace(domain_dim=1, ssp_dim=1024)\n",
"new_encoder = sspspace.RandomSSPSpace(domain_dim=1, ssp_dim=1024)\n",
"\n",
"xs = np.linspace(-4,4,401)[:,None] #we expect the encoded values to be two-dimensional in `encoder.encode()` so we add extra dimension\n",
"phis = encoder.encode(xs)\n",
"phis = new_encoder.encode(xs)\n",
"\n",
"#`0` element is right in the middle of phis array! notice that we have 401 samples inside it\n",
"sims = phis[..., :] @ phis.T"
"real_line_sims = phis[..., :] @ phis.T"
]
},
{
Expand All @@ -2070,7 +2165,7 @@
"execution": {}
},
"source": [
"[*Click for solution*](https://github.com/neuromatch/NeuroAI_Course/tree/main/tutorials/W2D2_NeuroSymbolicMethods/solutions/W2D2_Tutorial1_Solution_06eca8eb.py)\n",
"[*Click for solution*](https://github.com/neuromatch/NeuroAI_Course/tree/main/tutorials/W2D2_NeuroSymbolicMethods/solutions/W2D2_Tutorial1_Solution_f5f2174f.py)\n",
"\n"
]
},
Expand All @@ -2082,7 +2177,7 @@
},
"outputs": [],
"source": [
"plot_real_valued_line_similarity(sims, xs, title = '$\\phi(x)\\cdot\\phi(0)$')"
"plot_real_valued_line_similarity(real_line_sims, xs, title = '$\\phi(x)\\cdot\\phi(0)$')"
]
},
{
Expand All @@ -2109,8 +2204,8 @@
"raise NotImplementedError(\"Student exercise: provide value to shift and observe the usage of the operation.\")\n",
"###################################################################\n",
"\n",
"phi_shifted = phis[200,:][None,:] * encoder.encode([[...]])\n",
"sims = phi_shifted.flatten() @ phis.T"
"phi_shifted = phis[200,:][None,:] * new_encoder.encode([[...]])\n",
"shifted_real_line_sims = phi_shifted.flatten() @ phis.T"
]
},
{
Expand All @@ -2120,7 +2215,7 @@
"execution": {}
},
"source": [
"[*Click for solution*](https://github.com/neuromatch/NeuroAI_Course/tree/main/tutorials/W2D2_NeuroSymbolicMethods/solutions/W2D2_Tutorial1_Solution_d3fe205a.py)\n",
"[*Click for solution*](https://github.com/neuromatch/NeuroAI_Course/tree/main/tutorials/W2D2_NeuroSymbolicMethods/solutions/W2D2_Tutorial1_Solution_d25db2a0.py)\n",
"\n"
]
},
Expand All @@ -2132,7 +2227,7 @@
},
"outputs": [],
"source": [
"plot_real_valued_line_similarity(sims, xs, title = '$\\phi(x)\\cdot(\\phi(0)\\circledast\\phi(\\pi/2))$')"
"plot_real_valued_line_similarity(shifted_real_line_sims, xs, title = '$\\phi(x)\\cdot(\\phi(0)\\circledast\\phi(\\pi/2))$')"
]
},
{
Expand All @@ -2152,8 +2247,8 @@
},
"outputs": [],
"source": [
"phi_shifted = phis[200,:][None,:] * encoder.encode([[-1.5*np.pi]])\n",
"sims = phi_shifted.flatten() @ phis.T"
"new_phi_shifted = phis[200,:][None,:] * new_encoder.encode([[-1.5*np.pi]])\n",
"new_shifted_real_line_sims = new_phi_shifted.flatten() @ phis.T"
]
},
{
Expand All @@ -2164,7 +2259,7 @@
},
"outputs": [],
"source": [
"plot_real_valued_line_similarity(sims, xs, title = '$\\phi(x)\\cdot(\\phi(0)\\circledast\\phi(-1.5\\pi))$')"
"plot_real_valued_line_similarity(new_shifted_real_line_sims, xs, title = '$\\phi(x)\\cdot(\\phi(0)\\circledast\\phi(-1.5\\pi))$')"
]
},
{
Expand Down
Loading

0 comments on commit 5d62e44

Please sign in to comment.