Skip to content

Commit

Permalink
Merge pull request #401 from neuromatch/W2D3-discord
Browse files Browse the repository at this point in the history
W2D3 Post-Course Update
  • Loading branch information
glibesyck authored Aug 12, 2024
2 parents 96c15ab + 39a7007 commit f37a2eb
Show file tree
Hide file tree
Showing 3 changed files with 228 additions and 69 deletions.
99 changes: 76 additions & 23 deletions tutorials/W2D3_Microlearning/W2D3_Tutorial1.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@
"source": [
"# @title Install and import feedback gadget\n",
"\n",
"!pip install vibecheck datatops matplotlib numpy torch torchvision tqdm --quiet\n",
"!pip install vibecheck datatops matplotlib numpy torch pandas torchvision tqdm --quiet\n",
"\n",
"from vibecheck import DatatopsContentReviewContainer\n",
"def content_review(notebook_section: str):\n",
Expand Down Expand Up @@ -513,6 +513,60 @@
"\n",
"import contextlib\n",
"import io\n",
"import pandas as pd\n",
"import requests\n",
"import os\n",
"import hashlib\n",
"\n",
"def download_file(fname, url, expected_md5):\n",
" \"\"\"\n",
" Downloads a file from the given URL and saves it locally.\n",
" \"\"\"\n",
" if not os.path.isfile(fname):\n",
" try:\n",
" r = requests.get(url)\n",
" except requests.ConnectionError:\n",
" print(\"!!! Failed to download data !!!\")\n",
" return\n",
" if r.status_code != requests.codes.ok:\n",
" print(\"!!! Failed to download data !!!\")\n",
" return\n",
" if hashlib.md5(r.content).hexdigest() != expected_md5:\n",
" print(\"!!! Data download appears corrupted !!!\")\n",
" return\n",
" with open(fname, \"wb\") as fid:\n",
" fid.write(r.content)\n",
"\n",
"data_files = [\n",
" {\n",
" \"fname\": \"accuracy.csv\",\n",
" \"url\": \"https://osf.io/aqhd3/download\",\n",
" \"expected_md5\": \"bfcad2350de4c4a6eeeb1f3342371390\"\n",
" },\n",
" {\n",
" \"fname\": \"cosine_similarity.csv\",\n",
" \"url\": \"https://osf.io/w4pv7/download\",\n",
" \"expected_md5\": \"97ac863216a44909bb930715855a1d9e\"\n",
" },\n",
" {\n",
" \"fname\": \"losses.csv\",\n",
" \"url\": \"https://osf.io/drfg6/download\",\n",
" \"expected_md5\": \"e6a50509c676b6a934d653afae3a60c6\"\n",
" },\n",
" {\n",
" \"fname\": \"snr.csv\",\n",
" \"url\": \"https://osf.io/z5mjy/download\",\n",
" \"expected_md5\": \"13b4f0e43cc8dce12a4d191ae2d31c0e\"\n",
" }\n",
"]\n",
"\n",
"for data_file in data_files:\n",
" download_file(data_file[\"fname\"], data_file[\"url\"], data_file[\"expected_md5\"])\n",
"\n",
"accuracy_data = pd.read_csv(\"accuracy.csv\")\n",
"cosine_similarity_data = pd.read_csv(\"cosine_similarity.csv\")\n",
"losses_data = pd.read_csv(\"losses.csv\")\n",
"snr_data = pd.read_csv(\"snr.csv\")\n",
"\n",
"with contextlib.redirect_stdout(io.StringIO()):\n",
" # Load the MNIST dataset, 50K training images, 10K validation, 10K testing\n",
Expand Down Expand Up @@ -551,8 +605,7 @@
{
"cell_type": "markdown",
"metadata": {
"execution": {},
"jp-MarkdownHeadingCollapsed": true
"execution": {}
},
"source": [
"---\n",
Expand Down Expand Up @@ -861,7 +914,7 @@
"\n",
"# plot performance over time\n",
"with plt.xkcd():\n",
" plt.plot(losses_perturb, label=\"Weight Perturbation\", color='b')\n",
" plt.plot(losses_data['weight_perturbation'], label=\"Weight Perturbation\", color='b') #pre-saved history of loss\n",
" plt.xlabel(\"Updates\")\n",
" plt.ylabel(\"MSE\")\n",
" plt.legend()\n",
Expand Down Expand Up @@ -993,8 +1046,8 @@
"\n",
"# plot performance over time\n",
"with plt.xkcd():\n",
" plt.plot(losses_node_perturb, label=\"Node Perturbation\", color='c')\n",
" plt.plot(losses_perturb, label=\"Weight Perturbation\", color='b')\n",
" plt.plot(losses_data['node_perturbation'], label=\"Node Perturbation\", color='c') #pre-saved history of loss\n",
" plt.plot(losses_data['weight_perturbation'], label=\"Weight Perturbation\", color='b') #pre-saved history of loss\n",
" plt.xlabel(\"Updates\")\n",
" plt.ylabel(\"MSE\")\n",
" plt.legend()\n",
Expand Down Expand Up @@ -1251,9 +1304,9 @@
"\n",
"# plot performance over time\n",
"with plt.xkcd():\n",
" plt.plot(losses_node_perturb, label=\"Node Perturbation\", color='c')\n",
" plt.plot(losses_perturb, label=\"Weight Perturbation\", color='b')\n",
" plt.plot(losses_backprop, label=\"Backprop\", color='r')\n",
" plt.plot(losses_data['node_perturbation'], label=\"Node Perturbation\", color='c') #pre-saved history of loss\n",
" plt.plot(losses_data['weight_perturbation'], label=\"Weight Perturbation\", color='b') #pre-saved history of loss\n",
" plt.plot(losses_data['backpropagation'], label=\"Backprop\", color='r') #pre-saved history of loss\n",
" plt.xlabel(\"Updates\")\n",
" plt.ylabel(\"MSE\")\n",
" plt.legend()\n",
Expand All @@ -1264,7 +1317,7 @@
"with plt.xkcd():\n",
" plt.figure()\n",
" x = [0, 1, 2]\n",
" snr_vals = [snr_perturb, snr_node_perturb, snr_backprop]\n",
" snr_vals = [snr_data['weight_perturbation'][0], snr_data['node_perturbation'][0], snr_data['backpropagation'][0]] #pre-saved snrs\n",
" colors = ['b', 'c', 'r']\n",
" labels = ['Weight Perturbation', 'Node Perturbation', 'Backprop']\n",
" plt.bar(x, snr_vals, color=colors, tick_label=labels)\n",
Expand Down Expand Up @@ -1612,8 +1665,8 @@
"\n",
"# plot performance over time\n",
"with plt.xkcd():\n",
" plt.plot(losses_feedback, label=\"Feedback Alignment\", color='g')\n",
" plt.plot(losses_backprop, label=\"Backprop\", color='r')\n",
" plt.plot(losses_data['feedback_alignment'], label=\"Feedback Alignment\", color='g') #pre-saved history of loss\n",
" plt.plot(losses_data['backpropagation'], label=\"Backprop\", color='r') #pre-saved history of loss\n",
" plt.xlabel(\"Updates\")\n",
" plt.ylabel(\"MSE\")\n",
" plt.legend()\n",
Expand Down Expand Up @@ -1897,9 +1950,9 @@
"\n",
"# plot performance over time\n",
"with plt.xkcd():\n",
" plt.plot(losses_feedback, label=\"Feedback Alignment\", color='g')\n",
" plt.plot(losses_backprop, label=\"Backprop\", color='r')\n",
" plt.plot(losses_kolepoll, label=\"Kolen-Pollack\", color='k')\n",
" plt.plot(losses_data['feedback_alignment'], label=\"Feedback Alignment\", color='g') #pre-saved history of loss\n",
" plt.plot(losses_data['backpropagation'], label=\"Backprop\", color='r') #pre-saved history of loss\n",
" plt.plot(losses_data['kolen_pollack'], label=\"Kolen-Pollack\", color='k') #pre-saved history of loss\n",
" plt.xlabel(\"Updates\")\n",
" plt.ylabel(\"MSE\")\n",
" plt.legend()\n",
Expand Down Expand Up @@ -1962,9 +2015,9 @@
"source": [
"# @title Plot the gradient similarity to backprop over training with shaded error regions\n",
"with plt.xkcd():\n",
" plt.plot(cosine_sim_backprop, label=\"Backprop\", color='r')\n",
" plt.plot(cosine_sim_feedback, label=\"Feedback Alignment\", color='g')\n",
" plt.plot(cosine_sim_kolepoll, label=\"Kolen-Pollack\", color='k')\n",
" plt.plot(cosine_similarity_data[\"backpropagation\"], label=\"Backprop\", color='r') #pre-saved cosine similarities\n",
" plt.plot(cosine_similarity_data[\"feedback_alignment\"], label=\"Feedback Alignment\", color='g')\n",
" plt.plot(cosine_similarity_data[\"kolen_pollack\"], label=\"Kolen-Pollack\", color='k')\n",
" plt.xlabel(\"Epochs\")\n",
" plt.ylabel(\"Cosine Sim\")\n",
" plt.legend()\n",
Expand Down Expand Up @@ -1992,11 +2045,11 @@
"source": [
"# @title Classification accuracy comparison\n",
"with plt.xkcd():\n",
" plt.plot(accuracy_perturb)\n",
" plt.plot(accuracy_node_perturb)\n",
" plt.plot(accuracy_feedback)\n",
" plt.plot(accuracy_kolepoll)\n",
" plt.plot(accuracy_backprop)\n",
" plt.plot(accuracy_data['weight_perturbation']) #pre-saved accuracies\n",
" plt.plot(accuracy_data['node_perturbation'])\n",
" plt.plot(accuracy_data['feedback_alignment'])\n",
" plt.plot(accuracy_data['kolen_pollack'])\n",
" plt.plot(accuracy_data['backpropagation'])\n",
" plt.legend(['Weight perturbation', 'Node perturbation', 'Feedback alignment', 'Kolen-Pollack', 'Backprop'])\n",
" plt.xlabel('Epochs')\n",
" plt.ylabel('Accuracy (%)')\n",
Expand Down
99 changes: 76 additions & 23 deletions tutorials/W2D3_Microlearning/instructor/W2D3_Tutorial1.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@
"source": [
"# @title Install and import feedback gadget\n",
"\n",
"!pip install vibecheck datatops matplotlib numpy torch torchvision tqdm --quiet\n",
"!pip install vibecheck datatops matplotlib numpy torch pandas torchvision tqdm --quiet\n",
"\n",
"from vibecheck import DatatopsContentReviewContainer\n",
"def content_review(notebook_section: str):\n",
Expand Down Expand Up @@ -513,6 +513,60 @@
"\n",
"import contextlib\n",
"import io\n",
"import pandas as pd\n",
"import requests\n",
"import os\n",
"import hashlib\n",
"\n",
"def download_file(fname, url, expected_md5):\n",
" \"\"\"\n",
" Downloads a file from the given URL and saves it locally.\n",
" \"\"\"\n",
" if not os.path.isfile(fname):\n",
" try:\n",
" r = requests.get(url)\n",
" except requests.ConnectionError:\n",
" print(\"!!! Failed to download data !!!\")\n",
" return\n",
" if r.status_code != requests.codes.ok:\n",
" print(\"!!! Failed to download data !!!\")\n",
" return\n",
" if hashlib.md5(r.content).hexdigest() != expected_md5:\n",
" print(\"!!! Data download appears corrupted !!!\")\n",
" return\n",
" with open(fname, \"wb\") as fid:\n",
" fid.write(r.content)\n",
"\n",
"data_files = [\n",
" {\n",
" \"fname\": \"accuracy.csv\",\n",
" \"url\": \"https://osf.io/aqhd3/download\",\n",
" \"expected_md5\": \"bfcad2350de4c4a6eeeb1f3342371390\"\n",
" },\n",
" {\n",
" \"fname\": \"cosine_similarity.csv\",\n",
" \"url\": \"https://osf.io/w4pv7/download\",\n",
" \"expected_md5\": \"97ac863216a44909bb930715855a1d9e\"\n",
" },\n",
" {\n",
" \"fname\": \"losses.csv\",\n",
" \"url\": \"https://osf.io/drfg6/download\",\n",
" \"expected_md5\": \"e6a50509c676b6a934d653afae3a60c6\"\n",
" },\n",
" {\n",
" \"fname\": \"snr.csv\",\n",
" \"url\": \"https://osf.io/z5mjy/download\",\n",
" \"expected_md5\": \"13b4f0e43cc8dce12a4d191ae2d31c0e\"\n",
" }\n",
"]\n",
"\n",
"for data_file in data_files:\n",
" download_file(data_file[\"fname\"], data_file[\"url\"], data_file[\"expected_md5\"])\n",
"\n",
"accuracy_data = pd.read_csv(\"accuracy.csv\")\n",
"cosine_similarity_data = pd.read_csv(\"cosine_similarity.csv\")\n",
"losses_data = pd.read_csv(\"losses.csv\")\n",
"snr_data = pd.read_csv(\"snr.csv\")\n",
"\n",
"with contextlib.redirect_stdout(io.StringIO()):\n",
" # Load the MNIST dataset, 50K training images, 10K validation, 10K testing\n",
Expand Down Expand Up @@ -551,8 +605,7 @@
{
"cell_type": "markdown",
"metadata": {
"execution": {},
"jp-MarkdownHeadingCollapsed": true
"execution": {}
},
"source": [
"---\n",
Expand Down Expand Up @@ -863,7 +916,7 @@
"\n",
"# plot performance over time\n",
"with plt.xkcd():\n",
" plt.plot(losses_perturb, label=\"Weight Perturbation\", color='b')\n",
" plt.plot(losses_data['weight_perturbation'], label=\"Weight Perturbation\", color='b') #pre-saved history of loss\n",
" plt.xlabel(\"Updates\")\n",
" plt.ylabel(\"MSE\")\n",
" plt.legend()\n",
Expand Down Expand Up @@ -995,8 +1048,8 @@
"\n",
"# plot performance over time\n",
"with plt.xkcd():\n",
" plt.plot(losses_node_perturb, label=\"Node Perturbation\", color='c')\n",
" plt.plot(losses_perturb, label=\"Weight Perturbation\", color='b')\n",
" plt.plot(losses_data['node_perturbation'], label=\"Node Perturbation\", color='c') #pre-saved history of loss\n",
" plt.plot(losses_data['weight_perturbation'], label=\"Weight Perturbation\", color='b') #pre-saved history of loss\n",
" plt.xlabel(\"Updates\")\n",
" plt.ylabel(\"MSE\")\n",
" plt.legend()\n",
Expand Down Expand Up @@ -1253,9 +1306,9 @@
"\n",
"# plot performance over time\n",
"with plt.xkcd():\n",
" plt.plot(losses_node_perturb, label=\"Node Perturbation\", color='c')\n",
" plt.plot(losses_perturb, label=\"Weight Perturbation\", color='b')\n",
" plt.plot(losses_backprop, label=\"Backprop\", color='r')\n",
" plt.plot(losses_data['node_perturbation'], label=\"Node Perturbation\", color='c') #pre-saved history of loss\n",
" plt.plot(losses_data['weight_perturbation'], label=\"Weight Perturbation\", color='b') #pre-saved history of loss\n",
" plt.plot(losses_data['backpropagation'], label=\"Backprop\", color='r') #pre-saved history of loss\n",
" plt.xlabel(\"Updates\")\n",
" plt.ylabel(\"MSE\")\n",
" plt.legend()\n",
Expand All @@ -1266,7 +1319,7 @@
"with plt.xkcd():\n",
" plt.figure()\n",
" x = [0, 1, 2]\n",
" snr_vals = [snr_perturb, snr_node_perturb, snr_backprop]\n",
" snr_vals = [snr_data['weight_perturbation'][0], snr_data['node_perturbation'][0], snr_data['backpropagation'][0]] #pre-saved snrs\n",
" colors = ['b', 'c', 'r']\n",
" labels = ['Weight Perturbation', 'Node Perturbation', 'Backprop']\n",
" plt.bar(x, snr_vals, color=colors, tick_label=labels)\n",
Expand Down Expand Up @@ -1616,8 +1669,8 @@
"\n",
"# plot performance over time\n",
"with plt.xkcd():\n",
" plt.plot(losses_feedback, label=\"Feedback Alignment\", color='g')\n",
" plt.plot(losses_backprop, label=\"Backprop\", color='r')\n",
" plt.plot(losses_data['feedback_alignment'], label=\"Feedback Alignment\", color='g') #pre-saved history of loss\n",
" plt.plot(losses_data['backpropagation'], label=\"Backprop\", color='r') #pre-saved history of loss\n",
" plt.xlabel(\"Updates\")\n",
" plt.ylabel(\"MSE\")\n",
" plt.legend()\n",
Expand Down Expand Up @@ -1903,9 +1956,9 @@
"\n",
"# plot performance over time\n",
"with plt.xkcd():\n",
" plt.plot(losses_feedback, label=\"Feedback Alignment\", color='g')\n",
" plt.plot(losses_backprop, label=\"Backprop\", color='r')\n",
" plt.plot(losses_kolepoll, label=\"Kolen-Pollack\", color='k')\n",
" plt.plot(losses_data['feedback_alignment'], label=\"Feedback Alignment\", color='g') #pre-saved history of loss\n",
" plt.plot(losses_data['backpropagation'], label=\"Backprop\", color='r') #pre-saved history of loss\n",
" plt.plot(losses_data['kolen_pollack'], label=\"Kolen-Pollack\", color='k') #pre-saved history of loss\n",
" plt.xlabel(\"Updates\")\n",
" plt.ylabel(\"MSE\")\n",
" plt.legend()\n",
Expand Down Expand Up @@ -1968,9 +2021,9 @@
"source": [
"# @title Plot the gradient similarity to backprop over training with shaded error regions\n",
"with plt.xkcd():\n",
" plt.plot(cosine_sim_backprop, label=\"Backprop\", color='r')\n",
" plt.plot(cosine_sim_feedback, label=\"Feedback Alignment\", color='g')\n",
" plt.plot(cosine_sim_kolepoll, label=\"Kolen-Pollack\", color='k')\n",
" plt.plot(cosine_similarity_data[\"backpropagation\"], label=\"Backprop\", color='r') #pre-saved cosine similarities\n",
" plt.plot(cosine_similarity_data[\"feedback_alignment\"], label=\"Feedback Alignment\", color='g')\n",
" plt.plot(cosine_similarity_data[\"kolen_pollack\"], label=\"Kolen-Pollack\", color='k')\n",
" plt.xlabel(\"Epochs\")\n",
" plt.ylabel(\"Cosine Sim\")\n",
" plt.legend()\n",
Expand Down Expand Up @@ -1998,11 +2051,11 @@
"source": [
"# @title Classification accuracy comparison\n",
"with plt.xkcd():\n",
" plt.plot(accuracy_perturb)\n",
" plt.plot(accuracy_node_perturb)\n",
" plt.plot(accuracy_feedback)\n",
" plt.plot(accuracy_kolepoll)\n",
" plt.plot(accuracy_backprop)\n",
" plt.plot(accuracy_data['weight_perturbation']) #pre-saved accuracies\n",
" plt.plot(accuracy_data['node_perturbation'])\n",
" plt.plot(accuracy_data['feedback_alignment'])\n",
" plt.plot(accuracy_data['kolen_pollack'])\n",
" plt.plot(accuracy_data['backpropagation'])\n",
" plt.legend(['Weight perturbation', 'Node perturbation', 'Feedback alignment', 'Kolen-Pollack', 'Backprop'])\n",
" plt.xlabel('Epochs')\n",
" plt.ylabel('Accuracy (%)')\n",
Expand Down
Loading

0 comments on commit f37a2eb

Please sign in to comment.