Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

W2D1 Post-Course Update (Superset) #409

Merged
merged 5 commits into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions tutorials/W2D1_Macrocircuits/W2D1_Tutorial1.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -580,7 +580,7 @@
"\n",
" return nn.Sequential(*layers)\n",
"\n",
"net = make_MLP(10, 3, 2)"
"net = make_MLP(n_in = 10, W = 3, D = 2)"
]
},
{
Expand Down Expand Up @@ -710,7 +710,7 @@
" nn.init.normal_(param, std = ...)\n",
"\n",
"initialize_layers(net, 1)\n",
"np.testing.assert_allclose(next(net.parameters())[0][0].item(), 0.6093441247940063, err_msg = \"Expected value of parameter is different!\")"
"np.testing.assert_allclose(next(net.parameters())[0][0].item(), 0.609, err_msg = \"Expected value of parameter is different!\", atol = 1e-3)"
]
},
{
Expand All @@ -737,7 +737,7 @@
" nn.init.normal_(param, std = sigma/np.sqrt(n_in))\n",
"\n",
"initialize_layers(net, 1)\n",
"np.testing.assert_allclose(next(net.parameters())[0][0].item(), 0.6093441247940063, err_msg = \"Expected value of parameter is different!\")"
"np.testing.assert_allclose(next(net.parameters())[0][0].item(), 0.609, err_msg = \"Expected value of parameter is different!\", atol = 1e-3)"
]
},
{
Expand Down Expand Up @@ -803,7 +803,7 @@
" return X, ...\n",
"\n",
"X, y = make_data(net, 10, 10000000)\n",
"np.testing.assert_allclose(X[0][0].item(), 1.9269152879714966, err_msg = \"Expected value of data is different!\")"
"np.testing.assert_allclose(X[0][0].item(), 1.927, err_msg = \"Expected value of data is different!\", atol = 1e-3)"
]
},
{
Expand Down Expand Up @@ -835,7 +835,7 @@
" return X, y\n",
"\n",
"X, y = make_data(net, 10, 10000000)\n",
"np.testing.assert_allclose(X[0][0].item(), 1.9269152879714966, err_msg = \"Expected value of data is different!\")"
"np.testing.assert_allclose(X[0][0].item(), 1.927, err_msg = \"Expected value of data is different!\", atol = 1e-3)"
]
},
{
Expand Down Expand Up @@ -916,7 +916,7 @@
" return Es\n",
"\n",
"Es = train_model(net, X, y, 10, 1e-3)\n",
"np.testing.assert_allclose(Es[0], 0.0, err_msg = \"Expected value of loss is different!\")"
"np.testing.assert_allclose(Es[0], 0.0, err_msg = \"Expected value of loss is different!\", atol = 1e-3)"
]
},
{
Expand Down Expand Up @@ -964,7 +964,7 @@
" return Es\n",
"\n",
"Es = train_model(net, X, y, 10, 1e-3)\n",
"np.testing.assert_allclose(Es[0], 0.0, err_msg = \"Expected value of loss is different!\")"
"np.testing.assert_allclose(Es[0], 0.0, err_msg = \"Expected value of loss is different!\", atol = 1e-3)"
]
},
{
Expand Down Expand Up @@ -1037,7 +1037,7 @@
" return loss\n",
"\n",
"loss = compute_loss(net, X, y)\n",
"np.testing.assert_allclose(loss, 0.0, err_msg = \"Expected value of loss is different!\")"
"np.testing.assert_allclose(loss, 0.0, err_msg = \"Expected value of loss is different!\", atol = 1e-3)"
]
},
{
Expand Down Expand Up @@ -1070,7 +1070,7 @@
" return loss\n",
"\n",
"loss = compute_loss(net, X, y)\n",
"np.testing.assert_allclose(loss, 0.0, err_msg = \"Expected value of loss is different!\")"
"np.testing.assert_allclose(loss, 0.0, err_msg = \"Expected value of loss is different!\", atol = 1e-3)"
]
},
{
Expand Down
4 changes: 2 additions & 2 deletions tutorials/W2D1_Macrocircuits/W2D1_Tutorial2.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -1197,7 +1197,7 @@
" Outputs:\n",
" - (float): mean error for train data.\n",
" \"\"\"\n",
" return np.mean(...)\n",
" return np.mean(np.array([fit_relu(..., ..., ..., ..., n_hidden=n_hidden, reg = reg)[1] for _ in range(n_reps)]))\n",
"\n",
"test_errs = [sweep_test(x_train, y_train, x_test, y_test, n_hidden=..., n_reps=100, reg = 0.0) for n_hid in ...]\n",
"\n",
Expand Down Expand Up @@ -1238,7 +1238,7 @@
" Outputs:\n",
" - (float): mean error for train data.\n",
" \"\"\"\n",
" return np.mean(np.array([fit_relu(x_train, y_train, x_test, y_test, n_hidden=n_hidden, reg = reg)[1] for i in range(n_reps)]))\n",
" return np.mean(np.array([fit_relu(x_train, y_train, x_test, y_test, n_hidden=n_hidden, reg = reg)[1] for _ in range(n_reps)]))\n",
"\n",
"test_errs = [sweep_test(x_train, y_train, x_test, y_test, n_hidden=n_hid, n_reps=100, reg = 0.0) for n_hid in n_hids]\n",
"\n",
Expand Down
17 changes: 17 additions & 0 deletions tutorials/W2D1_Macrocircuits/W2D1_Tutorial3.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2312,6 +2312,23 @@
"1. Is there any difference between the trajectories for the modular and holistic agents? If so, what does it imply?"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"execution": {}
},
"outputs": [],
"source": [
"#to_remove explanation\n",
"\n",
"\"\"\"\n",
"Discussion: Is there any difference between the trajectories for the modular and holistic agents? If so, what does it imply?\n",
"\n",
"The holistic agent's trajectory has a higher curvature and length than that of the modular agent, suggesting that the modular agent's trajectory is more optimal. This is because, based on the RL objective with a discount factor smaller than 1, the trajectory should be as efficient (involving fewer steps) as possible.\n",
"\"\"\""
]
},
{
"cell_type": "code",
"execution_count": null,
Expand Down
18 changes: 9 additions & 9 deletions tutorials/W2D1_Macrocircuits/instructor/W2D1_Tutorial1.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -580,7 +580,7 @@
"\n",
" return nn.Sequential(*layers)\n",
"\n",
"net = make_MLP(10, 3, 2)"
"net = make_MLP(n_in = 10, W = 3, D = 2)"
]
},
{
Expand Down Expand Up @@ -712,7 +712,7 @@
" nn.init.normal_(param, std = ...)\n",
"\n",
"initialize_layers(net, 1)\n",
"np.testing.assert_allclose(next(net.parameters())[0][0].item(), 0.6093441247940063, err_msg = \"Expected value of parameter is different!\")\n",
"np.testing.assert_allclose(next(net.parameters())[0][0].item(), 0.609, err_msg = \"Expected value of parameter is different!\", atol = 1e-3)\n",
"\n",
"```"
]
Expand Down Expand Up @@ -741,7 +741,7 @@
" nn.init.normal_(param, std = sigma/np.sqrt(n_in))\n",
"\n",
"initialize_layers(net, 1)\n",
"np.testing.assert_allclose(next(net.parameters())[0][0].item(), 0.6093441247940063, err_msg = \"Expected value of parameter is different!\")"
"np.testing.assert_allclose(next(net.parameters())[0][0].item(), 0.609, err_msg = \"Expected value of parameter is different!\", atol = 1e-3)"
]
},
{
Expand Down Expand Up @@ -807,7 +807,7 @@
" return X, ...\n",
"\n",
"X, y = make_data(net, 10, 10000000)\n",
"np.testing.assert_allclose(X[0][0].item(), 1.9269152879714966, err_msg = \"Expected value of data is different!\")\n",
"np.testing.assert_allclose(X[0][0].item(), 1.927, err_msg = \"Expected value of data is different!\", atol = 1e-3)\n",
"\n",
"```"
]
Expand Down Expand Up @@ -841,7 +841,7 @@
" return X, y\n",
"\n",
"X, y = make_data(net, 10, 10000000)\n",
"np.testing.assert_allclose(X[0][0].item(), 1.9269152879714966, err_msg = \"Expected value of data is different!\")"
"np.testing.assert_allclose(X[0][0].item(), 1.927, err_msg = \"Expected value of data is different!\", atol = 1e-3)"
]
},
{
Expand Down Expand Up @@ -922,7 +922,7 @@
" return Es\n",
"\n",
"Es = train_model(net, X, y, 10, 1e-3)\n",
"np.testing.assert_allclose(Es[0], 0.0, err_msg = \"Expected value of loss is different!\")\n",
"np.testing.assert_allclose(Es[0], 0.0, err_msg = \"Expected value of loss is different!\", atol = 1e-3)\n",
"\n",
"```"
]
Expand Down Expand Up @@ -972,7 +972,7 @@
" return Es\n",
"\n",
"Es = train_model(net, X, y, 10, 1e-3)\n",
"np.testing.assert_allclose(Es[0], 0.0, err_msg = \"Expected value of loss is different!\")"
"np.testing.assert_allclose(Es[0], 0.0, err_msg = \"Expected value of loss is different!\", atol = 1e-3)"
]
},
{
Expand Down Expand Up @@ -1045,7 +1045,7 @@
" return loss\n",
"\n",
"loss = compute_loss(net, X, y)\n",
"np.testing.assert_allclose(loss, 0.0, err_msg = \"Expected value of loss is different!\")\n",
"np.testing.assert_allclose(loss, 0.0, err_msg = \"Expected value of loss is different!\", atol = 1e-3)\n",
"\n",
"```"
]
Expand Down Expand Up @@ -1080,7 +1080,7 @@
" return loss\n",
"\n",
"loss = compute_loss(net, X, y)\n",
"np.testing.assert_allclose(loss, 0.0, err_msg = \"Expected value of loss is different!\")"
"np.testing.assert_allclose(loss, 0.0, err_msg = \"Expected value of loss is different!\", atol = 1e-3)"
]
},
{
Expand Down
4 changes: 2 additions & 2 deletions tutorials/W2D1_Macrocircuits/instructor/W2D1_Tutorial2.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -1201,7 +1201,7 @@
" Outputs:\n",
" - (float): mean error for train data.\n",
" \"\"\"\n",
" return np.mean(...)\n",
" return np.mean(np.array([fit_relu(..., ..., ..., ..., n_hidden=n_hidden, reg = reg)[1] for _ in range(n_reps)]))\n",
"\n",
"test_errs = [sweep_test(x_train, y_train, x_test, y_test, n_hidden=..., n_reps=100, reg = 0.0) for n_hid in ...]\n",
"\n",
Expand Down Expand Up @@ -1244,7 +1244,7 @@
" Outputs:\n",
" - (float): mean error for train data.\n",
" \"\"\"\n",
" return np.mean(np.array([fit_relu(x_train, y_train, x_test, y_test, n_hidden=n_hidden, reg = reg)[1] for i in range(n_reps)]))\n",
" return np.mean(np.array([fit_relu(x_train, y_train, x_test, y_test, n_hidden=n_hidden, reg = reg)[1] for _ in range(n_reps)]))\n",
"\n",
"test_errs = [sweep_test(x_train, y_train, x_test, y_test, n_hidden=n_hid, n_reps=100, reg = 0.0) for n_hid in n_hids]\n",
"\n",
Expand Down
17 changes: 17 additions & 0 deletions tutorials/W2D1_Macrocircuits/instructor/W2D1_Tutorial3.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2322,6 +2322,23 @@
"1. Is there any difference between the trajectories for the modular and holistic agents? If so, what does it imply?"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"execution": {}
},
"outputs": [],
"source": [
"#to_remove explanation\n",
"\n",
"\"\"\"\n",
"Discussion: Is there any difference between the trajectories for the modular and holistic agents? If so, what does it imply?\n",
"\n",
"The holistic agent's trajectory has a higher curvature and length than that of the modular agent, suggesting that the modular agent's trajectory is more optimal. This is because, based on the RL objective with a discount factor smaller than 1, the trajectory should be as efficient (involving fewer steps) as possible.\n",
"\"\"\""
]
},
{
"cell_type": "code",
"execution_count": null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ def make_data(net, n_in, n_examples):
return X, y

X, y = make_data(net, 10, 10000000)
np.testing.assert_allclose(X[0][0].item(), 1.9269152879714966, err_msg = "Expected value of data is different!")
np.testing.assert_allclose(X[0][0].item(), 1.927, err_msg = "Expected value of data is different!", atol = 1e-3)
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ def compute_loss(net, X, y):
return loss

loss = compute_loss(net, X, y)
np.testing.assert_allclose(loss, 0.0, err_msg = "Expected value of loss is different!")
np.testing.assert_allclose(loss, 0.0, err_msg = "Expected value of loss is different!", atol = 1e-3)
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ def initialize_layers(net,sigma):
nn.init.normal_(param, std = sigma/np.sqrt(n_in))

initialize_layers(net, 1)
np.testing.assert_allclose(next(net.parameters())[0][0].item(), 0.6093441247940063, err_msg = "Expected value of parameter is different!")
np.testing.assert_allclose(next(net.parameters())[0][0].item(), 0.609, err_msg = "Expected value of parameter is different!", atol = 1e-3)
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,4 @@ def train_model(net, X, y, n_epochs, lr, progressbar=True):
return Es

Es = train_model(net, X, y, 10, 1e-3)
np.testing.assert_allclose(Es[0], 0.0, err_msg = "Expected value of loss is different!")
np.testing.assert_allclose(Es[0], 0.0, err_msg = "Expected value of loss is different!", atol = 1e-3)
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def sweep_test(x_train, y_train, x_test, y_test, n_hidden = 10, n_reps = 100, re
Outputs:
- (float): mean error for train data.
"""
return np.mean(np.array([fit_relu(x_train, y_train, x_test, y_test, n_hidden=n_hidden, reg = reg)[1] for i in range(n_reps)]))
return np.mean(np.array([fit_relu(x_train, y_train, x_test, y_test, n_hidden=n_hidden, reg = reg)[1] for _ in range(n_reps)]))

test_errs = [sweep_test(x_train, y_train, x_test, y_test, n_hidden=n_hid, n_reps=100, reg = 0.0) for n_hid in n_hids]

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

"""
Discussion: Is there any difference between the trajectories for the modular and holistic agents? If so, what does it imply?

The holistic agent's trajectory has a higher curvature and length than that of the modular agent, suggesting that the modular agent's trajectory is more optimal. This is because, based on the RL objective with a discount factor smaller than 1, the trajectory should be as efficient (involving fewer steps) as possible.
"""
18 changes: 9 additions & 9 deletions tutorials/W2D1_Macrocircuits/student/W2D1_Tutorial1.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -580,7 +580,7 @@
"\n",
" return nn.Sequential(*layers)\n",
"\n",
"net = make_MLP(10, 3, 2)"
"net = make_MLP(n_in = 10, W = 3, D = 2)"
]
},
{
Expand Down Expand Up @@ -691,7 +691,7 @@
" nn.init.normal_(param, std = ...)\n",
"\n",
"initialize_layers(net, 1)\n",
"np.testing.assert_allclose(next(net.parameters())[0][0].item(), 0.6093441247940063, err_msg = \"Expected value of parameter is different!\")"
"np.testing.assert_allclose(next(net.parameters())[0][0].item(), 0.609, err_msg = \"Expected value of parameter is different!\", atol = 1e-3)"
]
},
{
Expand All @@ -701,7 +701,7 @@
"execution": {}
},
"source": [
"[*Click for solution*](https://github.com/neuromatch/NeuroAI_Course/tree/main/tutorials/W2D1_Macrocircuits/solutions/W2D1_Tutorial1_Solution_a2d6d2f1.py)\n",
"[*Click for solution*](https://github.com/neuromatch/NeuroAI_Course/tree/main/tutorials/W2D1_Macrocircuits/solutions/W2D1_Tutorial1_Solution_8c945e68.py)\n",
"\n"
]
},
Expand Down Expand Up @@ -768,7 +768,7 @@
" return X, ...\n",
"\n",
"X, y = make_data(net, 10, 10000000)\n",
"np.testing.assert_allclose(X[0][0].item(), 1.9269152879714966, err_msg = \"Expected value of data is different!\")"
"np.testing.assert_allclose(X[0][0].item(), 1.927, err_msg = \"Expected value of data is different!\", atol = 1e-3)"
]
},
{
Expand All @@ -778,7 +778,7 @@
"execution": {}
},
"source": [
"[*Click for solution*](https://github.com/neuromatch/NeuroAI_Course/tree/main/tutorials/W2D1_Macrocircuits/solutions/W2D1_Tutorial1_Solution_d81fba1b.py)\n",
"[*Click for solution*](https://github.com/neuromatch/NeuroAI_Course/tree/main/tutorials/W2D1_Macrocircuits/solutions/W2D1_Tutorial1_Solution_6b3d3e34.py)\n",
"\n"
]
},
Expand Down Expand Up @@ -860,7 +860,7 @@
" return Es\n",
"\n",
"Es = train_model(net, X, y, 10, 1e-3)\n",
"np.testing.assert_allclose(Es[0], 0.0, err_msg = \"Expected value of loss is different!\")"
"np.testing.assert_allclose(Es[0], 0.0, err_msg = \"Expected value of loss is different!\", atol = 1e-3)"
]
},
{
Expand All @@ -870,7 +870,7 @@
"execution": {}
},
"source": [
"[*Click for solution*](https://github.com/neuromatch/NeuroAI_Course/tree/main/tutorials/W2D1_Macrocircuits/solutions/W2D1_Tutorial1_Solution_29135326.py)\n",
"[*Click for solution*](https://github.com/neuromatch/NeuroAI_Course/tree/main/tutorials/W2D1_Macrocircuits/solutions/W2D1_Tutorial1_Solution_c3274cd4.py)\n",
"\n"
]
},
Expand Down Expand Up @@ -937,7 +937,7 @@
" return loss\n",
"\n",
"loss = compute_loss(net, X, y)\n",
"np.testing.assert_allclose(loss, 0.0, err_msg = \"Expected value of loss is different!\")"
"np.testing.assert_allclose(loss, 0.0, err_msg = \"Expected value of loss is different!\", atol = 1e-3)"
]
},
{
Expand All @@ -947,7 +947,7 @@
"execution": {}
},
"source": [
"[*Click for solution*](https://github.com/neuromatch/NeuroAI_Course/tree/main/tutorials/W2D1_Macrocircuits/solutions/W2D1_Tutorial1_Solution_4929cd6d.py)\n",
"[*Click for solution*](https://github.com/neuromatch/NeuroAI_Course/tree/main/tutorials/W2D1_Macrocircuits/solutions/W2D1_Tutorial1_Solution_6fa930a8.py)\n",
"\n"
]
},
Expand Down
6 changes: 3 additions & 3 deletions tutorials/W2D1_Macrocircuits/student/W2D1_Tutorial2.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -1112,7 +1112,7 @@
" Outputs:\n",
" - (float): mean error for train data.\n",
" \"\"\"\n",
" return np.mean(...)\n",
" return np.mean(np.array([fit_relu(..., ..., ..., ..., n_hidden=n_hidden, reg = reg)[1] for _ in range(n_reps)]))\n",
"\n",
"test_errs = [sweep_test(x_train, y_train, x_test, y_test, n_hidden=..., n_reps=100, reg = 0.0) for n_hid in ...]\n",
"\n",
Expand All @@ -1131,11 +1131,11 @@
"execution": {}
},
"source": [
"[*Click for solution*](https://github.com/neuromatch/NeuroAI_Course/tree/main/tutorials/W2D1_Macrocircuits/solutions/W2D1_Tutorial2_Solution_e6df4a06.py)\n",
"[*Click for solution*](https://github.com/neuromatch/NeuroAI_Course/tree/main/tutorials/W2D1_Macrocircuits/solutions/W2D1_Tutorial2_Solution_7717ab4f.py)\n",
"\n",
"*Example output:*\n",
"\n",
"<img alt='Solution hint' align='left' width=778.0 height=577.0 src=https://raw.githubusercontent.com/neuromatch/NeuroAI_Course/main/tutorials/W2D1_Macrocircuits/static/W2D1_Tutorial2_Solution_e6df4a06_0.png>\n",
"<img alt='Solution hint' align='left' width=778.0 height=577.0 src=https://raw.githubusercontent.com/neuromatch/NeuroAI_Course/main/tutorials/W2D1_Macrocircuits/static/W2D1_Tutorial2_Solution_7717ab4f_0.png>\n",
"\n"
]
},
Expand Down
Loading