Skip to content

Commit

Permalink
feedback integration
Browse files Browse the repository at this point in the history
  • Loading branch information
glibesyck committed Aug 21, 2024
1 parent b96a1a5 commit 32ce996
Show file tree
Hide file tree
Showing 2 changed files with 317 additions and 101 deletions.
165 changes: 86 additions & 79 deletions tutorials/W1D2_ComparingTasks/W1D2_Tutorial1.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -684,7 +684,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 @@ -1258,14 +1276,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 @@ -1404,6 +1419,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 @@ -1424,17 +1445,11 @@
" task_name_classification,\n",
" 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",
" \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 @@ -1450,10 +1465,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 @@ -1640,14 +1652,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 @@ -1820,6 +1833,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 @@ -1834,16 +1852,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 @@ -2006,14 +2018,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 @@ -2154,6 +2167,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 @@ -2178,16 +2197,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 @@ -2203,10 +2216,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 @@ -2489,6 +2499,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 @@ -2526,17 +2542,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 @@ -2560,10 +2570,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 @@ -2765,7 +2772,7 @@
"execution": {}
},
"source": [
"### Transfer example 1: regression to classification\n",
"## Transfer example 1: regression to classification\n",
"\n"
]
},
Expand Down Expand Up @@ -3365,7 +3372,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 Down Expand Up @@ -3450,7 +3457,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.19"
"version": "3.11.5"
}
},
"nbformat": 4,
Expand Down
Loading

0 comments on commit 32ce996

Please sign in to comment.