Skip to content

Commit

Permalink
Merge pull request #416 from neuromatch/W2D2-TA
Browse files Browse the repository at this point in the history
W2D2 Post-Course Update (TA Feedback)
  • Loading branch information
glibesyck authored Aug 20, 2024
2 parents 463d25e + 5a0cf44 commit b96a1a5
Show file tree
Hide file tree
Showing 48 changed files with 1,413 additions and 1,004 deletions.
214 changes: 150 additions & 64 deletions tutorials/W2D2_NeuroSymbolicMethods/W2D2_Tutorial1.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,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 @@ -313,6 +313,90 @@
"set_seed(seed = 42)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"cellView": "form",
"execution": {}
},
"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 @@ -512,17 +596,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 @@ -675,21 +759,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 @@ -702,21 +786,21 @@
"source": [
"# to_remove solution\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 | triangle).item()\n",
"sim_mat[3,3] = (shape | 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 | triangle).item()\n",
"shape_sim_mat[3,3] = (shape | 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] = (triangle | shape).item()"
"shape_sim_mat[2,3] = shape_sim_mat[3,2] = (triangle | shape).item()"
]
},
{
Expand All @@ -727,7 +811,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 @@ -885,10 +969,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 @@ -973,13 +1057,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 @@ -1131,15 +1215,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 @@ -1151,10 +1236,11 @@
"outputs": [],
"source": [
"# to_remove solution\n",
"object_names = ['red','red^','red*circle','circle','circle^']\n",
"new_object_names = ['red','red^','red*circle','circle','circle^']\n",
"new_objs = objs\n",
"\n",
"objs['red^'] = objs['red*circle'] * ~objs['circle']\n",
"objs['circle^'] = objs['red*circle'] * ~objs['red']"
"new_objs['red^'] = new_objs['red*circle'] * ~new_objs['circle']\n",
"new_objs['circle^'] = new_objs['red*circle'] * ~new_objs['red']"
]
},
{
Expand All @@ -1165,13 +1251,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 @@ -1874,13 +1960,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 @@ -1894,13 +1980,13 @@
"#to_remove solution\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[200, :] @ phis.T"
"real_line_sims = phis[200, :] @ phis.T"
]
},
{
Expand All @@ -1911,7 +1997,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 @@ -1938,8 +2024,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 @@ -1952,8 +2038,8 @@
"source": [
"#to_remove solution\n",
"\n",
"phi_shifted = phis[200,:][None,:] * encoder.encode([[np.pi/2]])\n",
"sims = phi_shifted.flatten() @ phis.T"
"phi_shifted = phis[200,:][None,:] * new_encoder.encode([[np.pi/2]])\n",
"shifted_real_line_sims = phi_shifted.flatten() @ phis.T"
]
},
{
Expand All @@ -1964,7 +2050,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 @@ -1984,8 +2070,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 @@ -1996,7 +2082,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 b96a1a5

Please sign in to comment.