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 21, 2024
1 parent 5d62e44 commit 47cc187
Show file tree
Hide file tree
Showing 217 changed files with 29,401 additions and 28,694 deletions.
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.
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.
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.
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.
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.
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.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
Diff not rendered.
163 changes: 85 additions & 78 deletions _sources/tutorials/W1D2_ComparingTasks/student/W1D2_Tutorial1.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -737,7 +737,25 @@
"\n",
" image_count += 1\n",
" fig.suptitle(f\"Training for {epochs} epochs with {N_train_data} points\")\n",
" plt.show()"
" plt.show()\n",
"\n",
"def cost_classification(output, target):\n",
" criterion = nn.CrossEntropyLoss()\n",
" target = target.to(torch.int64)\n",
" cost = criterion(output, target)\n",
" return cost\n",
"\n",
"def cost_regression(output, target):\n",
" criterion = nn.MSELoss()\n",
" cost = criterion(output, target)\n",
" return cost\n",
"\n",
"def cost_autoencoder(output, target):\n",
" criterion = nn.MSELoss()\n",
" output_flat = output.view(output.size(0), -1)\n",
" target_flat = target.view(target.size(0), -1)\n",
" cost = criterion(output_flat, target_flat)\n",
" return cost"
]
},
{
Expand Down Expand Up @@ -1351,14 +1369,11 @@
},
"outputs": [],
"source": [
"def cost_classification(output, target):\n",
"\n",
" ############################################################\n",
" # Hint for batch_size: Get the first dimension of the target tensor\n",
" # Hint for cost: Calculate the loss using the criterion\n",
" raise NotImplementedError(\"Student has to fill in these lines\")\n",
" ############################################################\n",
"############################################################\n",
"raise NotImplementedError(\"Student exercise: Calculate the loss using the criterion\")\n",
"############################################################\n",
"\n",
"def cost_classification(output, target):\n",
" criterion = nn.CrossEntropyLoss()\n",
" target = target.to(torch.int64)\n",
" cost = ...\n",
Expand Down Expand Up @@ -1501,6 +1516,12 @@
"triplet_flag_classification = False\n",
"epochs_max_classification = 10\n",
"\n",
"my_epoch_Classification = []\n",
"my_train_cost_Classification = [] # Add a list to store training costs\n",
"my_val_cost_Classification = [] # Add a list to store val costs\n",
"my_test_cost_Classification = [] # Add a list to store test costs\n",
"conf_matrices = [] # List to store confusion matrices\n",
"\n",
"for N_train_data in training_points:\n",
" model = ClassificationConvNet(ConvNeuralNet(), ClassificationOutputLayer()).to(device)\n",
"\n",
Expand All @@ -1522,16 +1543,10 @@
" N_train_data\n",
" )\n",
"\n",
" if N_train_data == 10:\n",
" my_epoch_Classification = [my_epoch]\n",
" my_train_cost_Classification = [my_train_cost] # Add a list to store training costs\n",
" my_val_cost_Classification = [my_val_cost] # Add a list to store val costs\n",
" my_test_cost_Classification = [my_test_cost] # Add a list to store test costs\n",
" else:\n",
" my_epoch_Classification.append(my_epoch)\n",
" my_train_cost_Classification.append(my_train_cost) # Append the training costs\n",
" my_val_cost_Classification.append(my_val_cost) # Append the training costs\n",
" my_test_cost_Classification.append(my_test_cost) # Append the training costs\n",
" my_epoch_Classification.append(my_epoch)\n",
" my_train_cost_Classification.append(my_train_cost)\n",
" my_val_cost_Classification.append(my_val_cost)\n",
" my_test_cost_Classification.append(my_test_cost)\n",
"\n",
" # Compute predictions and confusion matrix for the validation set\n",
" all_preds = []\n",
Expand All @@ -1547,10 +1562,7 @@
"\n",
" # Compute confusion matrix\n",
" conf_matrix = confusion_matrix(all_labels, all_preds)\n",
" if N_train_data == 10:\n",
" conf_matrices = [(N_train_data, conf_matrix)] # List to store confusion matrices\n",
" else:\n",
" conf_matrices.append((N_train_data, conf_matrix)) # Store the confusion matrix with the number of training points"
" conf_matrices.append((N_train_data, conf_matrix)) # Store the confusion matrix with the number of training points"
]
},
{
Expand Down Expand Up @@ -1739,14 +1751,15 @@
},
"outputs": [],
"source": [
"############################################################\n",
"# Hint for criterion: The criterion used for regression tasks is designed\n",
"# to minimize the average squared difference between predicted and actual values.\n",
"# Hint for cost: To compute the cost, apply the criterion function to\n",
"# the predicted output and the actual target values, which will return the mean squared error loss.\n",
"raise NotImplementedError(\"Student exercise\")\n",
"############################################################\n",
"\n",
"def cost_regression(output, target):\n",
" ############################################################\n",
" # Hint for criterion: The criterion used for regression tasks is designed\n",
" # to minimize the average squared difference between predicted and actual values.\n",
" # Hint for cost: To compute the cost, apply the criterion function to\n",
" # the predicted output and the actual target values, which will return the mean squared error loss.\n",
" raise NotImplementedError(\"Student exercise\")\n",
" ############################################################\n",
" criterion = ...\n",
" cost = ...\n",
" return cost"
Expand Down Expand Up @@ -1924,6 +1937,11 @@
"triplet_flag = False\n",
"epochs_max_regression = 10\n",
"\n",
"my_epoch_Regression = []\n",
"my_train_cost_Regression = []\n",
"my_val_cost_Regression = []\n",
"my_test_cost_Regression = []\n",
"\n",
"train_dataset_regression = RegressionMNIST(train_dataset)\n",
"val_dataset_regression = RegressionMNIST(val_dataset)\n",
"test_dataset_original_regression = RegressionMNIST(test_dataset_original)\n",
Expand All @@ -1938,16 +1956,10 @@
" optimizer = torch.optim.Adam(params=model.parameters(), lr=0.001)\n",
"\n",
" my_epoch, my_train_cost, my_val_cost, my_test_cost = train(model, sampled_train_loader, sampled_val_loader, test_loader_original_regression, cost_regression, optimizer, epochs_max_regression, acc_flag, triplet_flag, task_name_regression, N_train_data)\n",
" if N_train_data == 10:\n",
" my_epoch_Regression = [my_epoch]\n",
" my_train_cost_Regression= [my_train_cost]\n",
" my_val_cost_Regression= [my_val_cost]\n",
" my_test_cost_Regression= [my_test_cost]\n",
" else:\n",
" my_epoch_Regression.append(my_epoch)\n",
" my_train_cost_Regression.append(my_train_cost) # Append the training costs\n",
" my_val_cost_Regression.append(my_val_cost) # Append the val costs\n",
" my_test_cost_Regression.append(my_test_cost) # Append the test costs"
" my_epoch_Regression.append(my_epoch)\n",
" my_train_cost_Regression.append(my_train_cost) # Append the training costs\n",
" my_val_cost_Regression.append(my_val_cost) # Append the val costs\n",
" my_test_cost_Regression.append(my_test_cost) # Append the test costs"
]
},
{
Expand Down Expand Up @@ -2115,14 +2127,15 @@
},
"outputs": [],
"source": [
"############################################################\n",
"# Hint for output_flat: To flatten the output tensor for comparison, reshape it to\n",
"# have a size of (batch_size, -1) where batch_size is the number of samples.\n",
"# Hint for target_flat: Similarly, flatten the target tensor to match the shape\n",
"# of the flattened output tensor, ensuring it has a size of (batch_size, -1).\n",
"raise NotImplementedError(\"Student exercise\")\n",
"############################################################\n",
"\n",
"def cost_autoencoder(output, target):\n",
" ############################################################\n",
" # Hint for output_flat: To flatten the output tensor for comparison, reshape it to\n",
" # have a size of (batch_size, -1) where batch_size is the number of samples.\n",
" # Hint for target_flat: Similarly, flatten the target tensor to match the shape\n",
" # of the flattened output tensor, ensuring it has a size of (batch_size, -1).\n",
" raise NotImplementedError(\"Student exercise\")\n",
" ############################################################\n",
" criterion = nn.MSELoss()\n",
" output_flat = ...\n",
" target_flat = ...\n",
Expand Down Expand Up @@ -2267,6 +2280,12 @@
" shuffle=True\n",
")\n",
"\n",
"my_epoch_Autoencoder = []\n",
"my_train_cost_Autoencoder = []\n",
"my_val_cost_Autoencoder = []\n",
"my_test_cost_Autoencoder = []\n",
"reconstructions = []\n",
"\n",
"for N_train_data in training_points:\n",
" model = Autoencoder(ConvNeuralNet(), BottleneckLayer(M), ConvNeuralNetDecoder(M)).to(device)\n",
"\n",
Expand All @@ -2291,16 +2310,10 @@
" task_name_autoencoder,\n",
" N_train_data\n",
" )\n",
" if N_train_data == 10:\n",
" my_epoch_Autoencoder = [my_epoch]\n",
" my_train_cost_Autoencoder = [my_train_cost] # Add a list to store training costs\n",
" my_val_cost_Autoencoder = [my_val_cost] # Add a list to store val costs\n",
" my_test_cost_Autoencoder = [my_test_cost] # Add a list to store test costs\n",
" else:\n",
" my_epoch_Autoencoder.append(my_epoch)\n",
" my_train_cost_Autoencoder.append(my_train_cost)\n",
" my_val_cost_Autoencoder.append(my_val_cost)\n",
" my_test_cost_Autoencoder.append(my_test_cost)\n",
" my_epoch_Autoencoder.append(my_epoch)\n",
" my_train_cost_Autoencoder.append(my_train_cost)\n",
" my_val_cost_Autoencoder.append(my_val_cost)\n",
" my_test_cost_Autoencoder.append(my_test_cost)\n",
"\n",
" original_images = []\n",
" reconstructed_images = []\n",
Expand All @@ -2316,10 +2329,7 @@
" plot_reconstructions(original_images, reconstructed_images, N_train_data, epochs_max_autoencoder)\n",
" break\n",
"\n",
" if N_train_data == 10:\n",
" reconstructions = [(N_train_data, original_images, reconstructed_images)] # List to store original and reconstructed images\n",
" else:\n",
" reconstructions.append((N_train_data, original_images, reconstructed_images))"
" reconstructions.append((N_train_data, original_images, reconstructed_images))"
]
},
{
Expand Down Expand Up @@ -2605,6 +2615,12 @@
"triplet_flag_inpainting = False\n",
"epochs_max_inpainting = 10\n",
"\n",
"my_epoch_Inpainting = []\n",
"my_train_cost_Inpainting = []\n",
"my_val_cost_Inpainting = []\n",
"my_test_cost_Inpainting = []\n",
"reconstructions_inpainting = []\n",
"\n",
"# Create inpainting versions of the training, validation, and test datasets\n",
"train_dataset_inpainting = InpaintingMNIST(train_dataset)\n",
"val_dataset_inpainting = InpaintingMNIST(val_dataset)\n",
Expand Down Expand Up @@ -2642,17 +2658,11 @@
" task_name_inpainting,\n",
" N_train_data\n",
" )\n",
" # Initialize lists to store training epochs and test costs for the inpainting task\n",
" if N_train_data == 10:\n",
" my_epoch_Inpainting = [my_epoch]\n",
" my_train_cost_Inpainting = [my_train_cost] # Add a list to store training costs\n",
" my_val_cost_Inpainting = [my_val_cost] # Add a list to store val costs\n",
" my_test_cost_Inpainting = [my_test_cost] # Add a list to store test costs\n",
" else:\n",
" my_epoch_Inpainting.append(my_epoch)\n",
" my_train_cost_Inpainting.append(my_train_cost) # Append the training costs\n",
" my_val_cost_Inpainting.append(my_val_cost) # Append the training costs\n",
" my_test_cost_Inpainting.append(my_test_cost) # Append the training costs\n",
"\n",
" my_epoch_Inpainting.append(my_epoch)\n",
" my_train_cost_Inpainting.append(my_train_cost)\n",
" my_val_cost_Inpainting.append(my_val_cost)\n",
" my_test_cost_Inpainting.append(my_test_cost)\n",
" original_images = []\n",
" reconstructed_images = []\n",
" model.eval()\n",
Expand All @@ -2676,10 +2686,7 @@
" break\n",
" plt.suptitle(\"Training for 10 epochs with {} points\".format(N_train_data))\n",
"\n",
" if N_train_data == 10:\n",
" reconstructions_inpainting = [(N_train_data, original_images, reconstructed_images)]\n",
" else:\n",
" reconstructions_inpainting.append((N_train_data, original_images, reconstructed_images)) # Store the original and reconstructed images"
" reconstructions_inpainting.append((N_train_data, original_images, reconstructed_images))"
]
},
{
Expand Down Expand Up @@ -2886,7 +2893,7 @@
"execution": {}
},
"source": [
"### Transfer example 1: regression to classification\n",
"## Transfer example 1: regression to classification\n",
"\n"
]
},
Expand Down Expand Up @@ -3499,7 +3506,7 @@
"execution": {}
},
"source": [
"## Bonus discussion point 8\n",
"### Bonus discussion point 8\n",
"\n",
"\n",
"How would you find out if the representations learned by the networks are similar or different, apart from their performance on downstream tasks?"
Expand All @@ -3520,7 +3527,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"### Submit your feedback\n"
"#### Submit your feedback\n"
]
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -724,6 +724,30 @@
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e7888898-2483-4705-97dc-a0099ef7b5cd",
"metadata": {
"execution": {}
},
"outputs": [],
"source": [
"test_embeddings_untrained[0]"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5e6b3578-912c-4f96-9fd2-e6add908cb4a",
"metadata": {
"execution": {}
},
"outputs": [],
"source": [
"test_embeddings_untrained[2]"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,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
Loading

0 comments on commit 47cc187

Please sign in to comment.