Skip to content

Commit

Permalink
Merge pull request #417 from neuromatch/W1D3-superset
Browse files Browse the repository at this point in the history
W1D3 Post-Course Update (Superset)
  • Loading branch information
glibesyck authored Aug 21, 2024
2 parents a7fea07 + f5b9eda commit 5ad4631
Show file tree
Hide file tree
Showing 17 changed files with 75 additions and 206 deletions.
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ tensorflow==2.8
ffmpeg
imageio-ffmpeg
torchlens
rsatoolbox
rsatoolbox==0.1.5
vibecheck
seaborn
plotly
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,9 @@
"source": [
"# @title Install dependencies\n",
"\n",
"!pip install -q ipympl ipywidgets mpl_interactions[\"jupyter\"] rsatoolbox torchlens\n",
"!pip install -q ipympl ipywidgets mpl_interactions[\"jupyter\"] torchlens\n",
"!pip install -q graphviz\n",
"!pip install rsatoolbox==0.1.5\n",
"\n",
"# To install jupyter-matplotlib (ipympl) via pip\n",
"!pip install -q torchlens\n",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@
"source": [
"# @title Install and import feedback gadget\n",
"\n",
"!pip install torch torchvision matplotlib numpy scikit-learn rsatoolbox scipy vibecheck --quiet\n",
"!pip install torch torchvision matplotlib numpy scikit-learn scipy vibecheck --quiet\n",
"!pip install rsatoolbox==0.1.5 --quiet\n",
"\n",
"from vibecheck import DatatopsContentReviewContainer\n",
"def content_review(notebook_section: str):\n",
Expand Down Expand Up @@ -1269,7 +1270,7 @@
"source": [
"# @title Representational Path\n",
"\n",
"imgs, labels = next(iter(test_loader)) #grab 500 samples from the test set\n",
"imgs, labels = next(iter(test_loader))\n",
"\n",
"model_features = extract_features(model, imgs.to(device), return_layers='all')\n",
"\n",
Expand Down Expand Up @@ -1330,7 +1331,6 @@
"source": [
"# @title Test your idea by executing this cell!\n",
"\n",
"#imgs, labels = next(iter(test_loader)) #grab 500 samples from the test set\n",
"imgs, labels = sample_images(test_loader, n=5)\n",
"model_features = extract_features(model, imgs.to(device), return_layers='all')\n",
"\n",
Expand Down Expand Up @@ -1476,43 +1476,24 @@
"execution": {}
},
"source": [
"## Coding Exercise 1: Dimensionality reduction visualization\n",
"## Dimensionality reduction visualization\n",
"\n",
"Use MDS to visualize the changes in the representational geometry across the layers of this network in response to original MNIST test images."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"execution": {}
},
"outputs": [],
"source": [
"#################################################\n",
"# Fill out and remove\n",
"raise NotImplementedError(\"Student exercise: fill in the missing variables\")\n",
"#################################################\n",
"\n",
"return_layers = ['input', 'conv1', 'conv2', 'fc1', 'fc2']\n",
"imgs, labels = sample_images(test_loader, n=50) # grab 500 samples from the test set\n",
"model_features = ...\n",
"\n",
"plot_dim_reduction(model_features, labels, transformer_funcs =['MDS'])"
"Now, we are going to use MDS to visualize the changes in the representational geometry across the layers of this network in response to the original MNIST test images."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"cellView": "form",
"execution": {}
},
"outputs": [],
"source": [
"# to_remove solution\n",
"# @title Plot the representational geometry changes\n",
"\n",
"return_layers = ['input', 'conv1', 'conv2', 'fc1', 'fc2']\n",
"imgs, labels = sample_images(test_loader, n=50) # grab 500 samples from the test set\n",
"imgs, labels = sample_images(test_loader, n=50) # grab 50 samples from the test set\n",
"model_features = extract_features(model_robust, imgs.to(device), return_layers)\n",
"\n",
"plot_dim_reduction(model_features, labels, transformer_funcs =['MDS'])"
Expand Down Expand Up @@ -1556,7 +1537,7 @@
},
"outputs": [],
"source": [
"imgs, labels = sample_images(test_loader, n=50) #grab 500 samples from the test set\n",
"imgs, labels = sample_images(test_loader, n=50) #grab 50 samples from the test set\n",
"\n",
"model_features = extract_features(model, imgs.to(device), return_layers='all')\n",
"model_features_robust = extract_features(model_robust, imgs.to(device), return_layers='all')\n",
Expand All @@ -1583,7 +1564,7 @@
},
"outputs": [],
"source": [
"imgs, labels = sample_images(test_loader, n=50) #grab 500 samples from the test set\n",
"imgs, labels = sample_images(test_loader, n=50) #grab 50 samples from the test set\n",
"\n",
"adv_imgs = generate_adversarial(model, imgs.to(device), labels.to(device), eps)\n",
"\n",
Expand Down Expand Up @@ -1623,33 +1604,9 @@
"execution": {}
},
"source": [
"## Coding Exercise 2: Compare representational paths\n",
"\n",
"Train another instance of the model on the original MNIST data for only one epoch with a different seed. Then compare the representational paths of the two instances that are both trained on the original MNIST with one another. Display the paths in blue and cyan colors."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"execution": {}
},
"outputs": [],
"source": [
"#################################################\n",
"## TODO for students: fill in the missing variables ##\n",
"# Fill out function and remove\n",
"raise NotImplementedError(\"Student exercise: fill in the missing variables\")\n",
"#################################################\n",
"## Compare representational paths\n",
"\n",
"args = build_args()\n",
"torch.manual_seed(args.seed+1)\n",
"args.epochs = 1\n",
"#build_model\n",
"model_new_seed = Net().to(device)\n",
"\n",
"optimizer = optim.Adadelta(...)\n",
"train_model(args, model_new_seed, optimizer)"
"Train another instance of the model on the original MNIST data for only one epoch with a different seed (the basic model is trained for one epoch too). Then compare the representational paths of the two instances that are both trained on the original MNIST with one another. Display the paths in blue and cyan colors."
]
},
{
Expand All @@ -1660,8 +1617,6 @@
},
"outputs": [],
"source": [
"# to_remove solution\n",
"\n",
"args = build_args()\n",
"torch.manual_seed(args.seed+1)\n",
"args.epochs = 1\n",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,9 @@
"source": [
"# @title Install and import feedback gadget\n",
"\n",
"!pip install numpy pandas torch torchvision matplotlib ipython Pillow rsatoolbox plotly networkx requests vibecheck --quiet\n",
"!pip install numpy pandas torch torchvision matplotlib ipython Pillow plotly networkx requests vibecheck --quiet\n",
"!pip install rsatoolbox==0.1.5 --quiet\n",
"\n",
"\n",
"from vibecheck import DatatopsContentReviewContainer\n",
"def content_review(notebook_section: str):\n",
Expand Down Expand Up @@ -1676,7 +1678,7 @@
"source": [
"## Coding Exercise 1: RDMs of AlexNet\n",
"\n",
"Use the RSA toolbox to compute the RDMs for the layers of AlexNet."
"Use the RSA toolbox to compute the RDMs for the layers of AlexNet. It should be done in the very same way as RDMs for fMRI patterns above."
]
},
{
Expand Down Expand Up @@ -1820,7 +1822,7 @@
"# Fill out function and remove\n",
"raise NotImplementedError(\"Student exercise: fill in the missing variables\")\n",
"#################################################\n",
"v1_rdms = ...\n",
"v1_rdms = fmri_rdms.subset('roi', ...)\n",
"show_rdm_plotly(v1_rdms, rdm_descriptor='subject')"
]
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@
"source": [
"# @title Install dependencies and import feedback gadget\n",
"\n",
"!pip install numpy xarray scipy scikit-learn matplotlib seaborn tqdm rsatoolbox vibecheck datatops --quiet\n",
"!pip install numpy xarray scipy scikit-learn matplotlib seaborn tqdm vibecheck datatops --quiet\n",
"!pip install rsatoolbox==0.1.5 --quiet\n",
"\n",
"from vibecheck import DatatopsContentReviewContainer\n",
"def content_review(notebook_section: str):\n",
Expand Down Expand Up @@ -1003,7 +1004,7 @@
"n_neurons = 2\n",
"stimulus_idx = 0,1 # choose two stimuli\n",
"#################################################\n",
"raise NotImplementedError(\"Student exercise: fill in the missing variables\")\n",
"raise NotImplementedError(\"Student exercise: complete Euclidean distance calculation by the formula provided above\")\n",
"#################################################\n",
"b_j = clean_dataset[n_neurons].loc[stimulus_idx[0]].values # select the stimulus response\n",
"b_k = clean_dataset[n_neurons].loc[stimulus_idx[1]].values\n",
Expand Down Expand Up @@ -1368,9 +1369,9 @@
" projected_dist[n_neurons]=[]\n",
" for m_dims in m_dims_list:\n",
" #################################################\n",
" raise NotImplementedError(\"Student exercise: fill in the missing variables\")\n",
" raise NotImplementedError(\"Student exercise: generate matrix A which projects from dimensionality neurons's amount to d-dimensional space\")\n",
" #################################################\n",
" A = ...\n",
" A = np.random.normal(loc=..., scale=..., size=(..., ...))\n",
" A *= np.sqrt(1/m_dims)\n",
" transformed_data = (data.values @ A)\n",
" transformed_data = np2xr(transformed_data, coords={'stim': data.stim.values, 'neuron': np.arange(m_dims)})\n",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,9 @@
"source": [
"# @title Install dependencies\n",
"\n",
"!pip install -q ipympl ipywidgets mpl_interactions[\"jupyter\"] rsatoolbox torchlens\n",
"!pip install -q ipympl ipywidgets mpl_interactions[\"jupyter\"] torchlens\n",
"!pip install -q graphviz\n",
"!pip install rsatoolbox==0.1.5\n",
"\n",
"# To install jupyter-matplotlib (ipympl) via pip\n",
"!pip install -q torchlens\n",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@
"source": [
"# @title Install and import feedback gadget\n",
"\n",
"!pip install torch torchvision matplotlib numpy scikit-learn rsatoolbox scipy vibecheck --quiet\n",
"!pip install torch torchvision matplotlib numpy scikit-learn scipy vibecheck --quiet\n",
"!pip install rsatoolbox==0.1.5 --quiet\n",
"\n",
"from vibecheck import DatatopsContentReviewContainer\n",
"def content_review(notebook_section: str):\n",
Expand Down Expand Up @@ -1269,7 +1270,7 @@
"source": [
"# @title Representational Path\n",
"\n",
"imgs, labels = next(iter(test_loader)) #grab 500 samples from the test set\n",
"imgs, labels = next(iter(test_loader))\n",
"\n",
"model_features = extract_features(model, imgs.to(device), return_layers='all')\n",
"\n",
Expand Down Expand Up @@ -1330,7 +1331,6 @@
"source": [
"# @title Test your idea by executing this cell!\n",
"\n",
"#imgs, labels = next(iter(test_loader)) #grab 500 samples from the test set\n",
"imgs, labels = sample_images(test_loader, n=5)\n",
"model_features = extract_features(model, imgs.to(device), return_layers='all')\n",
"\n",
Expand Down Expand Up @@ -1476,45 +1476,24 @@
"execution": {}
},
"source": [
"## Coding Exercise 1: Dimensionality reduction visualization\n",
"## Dimensionality reduction visualization\n",
"\n",
"Use MDS to visualize the changes in the representational geometry across the layers of this network in response to original MNIST test images."
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"execution": {}
},
"source": [
"```python\n",
"#################################################\n",
"# Fill out and remove\n",
"raise NotImplementedError(\"Student exercise: fill in the missing variables\")\n",
"#################################################\n",
"\n",
"return_layers = ['input', 'conv1', 'conv2', 'fc1', 'fc2']\n",
"imgs, labels = sample_images(test_loader, n=50) # grab 500 samples from the test set\n",
"model_features = ...\n",
"\n",
"plot_dim_reduction(model_features, labels, transformer_funcs =['MDS'])\n",
"\n",
"```"
"Now, we are going to use MDS to visualize the changes in the representational geometry across the layers of this network in response to the original MNIST test images."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"cellView": "form",
"execution": {}
},
"outputs": [],
"source": [
"# to_remove solution\n",
"# @title Plot the representational geometry changes\n",
"\n",
"return_layers = ['input', 'conv1', 'conv2', 'fc1', 'fc2']\n",
"imgs, labels = sample_images(test_loader, n=50) # grab 500 samples from the test set\n",
"imgs, labels = sample_images(test_loader, n=50) # grab 50 samples from the test set\n",
"model_features = extract_features(model_robust, imgs.to(device), return_layers)\n",
"\n",
"plot_dim_reduction(model_features, labels, transformer_funcs =['MDS'])"
Expand Down Expand Up @@ -1558,7 +1537,7 @@
},
"outputs": [],
"source": [
"imgs, labels = sample_images(test_loader, n=50) #grab 500 samples from the test set\n",
"imgs, labels = sample_images(test_loader, n=50) #grab 50 samples from the test set\n",
"\n",
"model_features = extract_features(model, imgs.to(device), return_layers='all')\n",
"model_features_robust = extract_features(model_robust, imgs.to(device), return_layers='all')\n",
Expand All @@ -1585,7 +1564,7 @@
},
"outputs": [],
"source": [
"imgs, labels = sample_images(test_loader, n=50) #grab 500 samples from the test set\n",
"imgs, labels = sample_images(test_loader, n=50) #grab 50 samples from the test set\n",
"\n",
"adv_imgs = generate_adversarial(model, imgs.to(device), labels.to(device), eps)\n",
"\n",
Expand Down Expand Up @@ -1625,35 +1604,9 @@
"execution": {}
},
"source": [
"## Coding Exercise 2: Compare representational paths\n",
"\n",
"Train another instance of the model on the original MNIST data for only one epoch with a different seed. Then compare the representational paths of the two instances that are both trained on the original MNIST with one another. Display the paths in blue and cyan colors."
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"execution": {}
},
"source": [
"```python\n",
"#################################################\n",
"## TODO for students: fill in the missing variables ##\n",
"# Fill out function and remove\n",
"raise NotImplementedError(\"Student exercise: fill in the missing variables\")\n",
"#################################################\n",
"## Compare representational paths\n",
"\n",
"args = build_args()\n",
"torch.manual_seed(args.seed+1)\n",
"args.epochs = 1\n",
"#build_model\n",
"model_new_seed = Net().to(device)\n",
"\n",
"optimizer = optim.Adadelta(...)\n",
"train_model(args, model_new_seed, optimizer)\n",
"\n",
"```"
"Train another instance of the model on the original MNIST data for only one epoch with a different seed (the basic model is trained for one epoch too). Then compare the representational paths of the two instances that are both trained on the original MNIST with one another. Display the paths in blue and cyan colors."
]
},
{
Expand All @@ -1664,8 +1617,6 @@
},
"outputs": [],
"source": [
"# to_remove solution\n",
"\n",
"args = build_args()\n",
"torch.manual_seed(args.seed+1)\n",
"args.epochs = 1\n",
Expand Down
Loading

0 comments on commit 5ad4631

Please sign in to comment.