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

preventing outputs #349

Merged
merged 4 commits into from
Jun 27, 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
77 changes: 39 additions & 38 deletions tutorials/W1D2_ComparingTasks/W1D2_Tutorial1.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,8 @@
"from pathlib import Path\n",
"import zipfile\n",
"import random\n",
"import contextlib\n",
"import io\n",
"\n",
"# Import third-party libraries\n",
"import numpy as np\n",
Expand Down Expand Up @@ -844,50 +846,49 @@
},
"outputs": [],
"source": [
"# Define a transformation pipeline for the MNIST dataset\n",
"mnist_transform = transforms.Compose([\n",
" transforms.Resize((32, 32)), # Resize the images to 32x32 pixels\n",
" transforms.ToTensor(), # Convert images to PyTorch tensors\n",
" transforms.Normalize(mean=(0.1307,), std=(0.3081,)) # Normalize the images with mean and standard deviation\n",
"])\n",
"with contextlib.redirect_stdout(io.StringIO()):\n",
"\n",
"# Load the MNIST training dataset with transformations applied\n",
"train_val_dataset = torchvision.datasets.MNIST(\n",
" root='./data', # Directory to store/load the data\n",
" train=True, # Specify to load the training set\n",
" transform=mnist_transform, # Apply the transformation pipeline defined earlier\n",
" download=True # Download the dataset if it's not already present\n",
")\n",
" # Define a transformation pipeline for the MNIST dataset\n",
" mnist_transform = transforms.Compose([\n",
" transforms.Resize((32, 32)), # Resize the images to 32x32 pixels\n",
" transforms.ToTensor(), # Convert images to PyTorch tensors\n",
" transforms.Normalize(mean=(0.1307,), std=(0.3081,)) # Normalize the images with mean and standard deviation\n",
" ])\n",
"\n",
"# Load the MNIST test dataset with transformations applied\n",
"test_dataset = torchvision.datasets.MNIST(\n",
" root='./data', # Directory to store/load the data\n",
" train=False, # Specify to load the test set\n",
" transform=mnist_transform, # Apply the transformation pipeline defined earlier\n",
" download=True # Download the dataset if it's not already present\n",
")\n",
" # Load the MNIST training dataset with transformations applied\n",
" train_val_dataset = torchvision.datasets.MNIST(\n",
" root='./data', # Directory to store/load the data\n",
" train=True, # Specify to load the training set\n",
" transform=mnist_transform, # Apply the transformation pipeline defined earlier\n",
" download=True # Download the dataset if it's not already present\n",
" )\n",
"\n",
"# Split the training dataset into training and validation sets\n",
"train_size = int(0.9 * len(train_val_dataset)) # Calculate the size of the training set (90% of the original)\n",
"val_size = len(train_val_dataset) - train_size # Calculate the size of the validation set (remaining 10%)\n",
"train_dataset, val_dataset = torch.utils.data.random_split(\n",
" dataset=train_val_dataset, # Original training dataset to split\n",
" lengths=[train_size, val_size] # Lengths of the resulting splits\n",
")\n",
" # Load the MNIST test dataset with transformations applied\n",
" test_dataset = torchvision.datasets.MNIST(\n",
" root='./data', # Directory to store/load the data\n",
" train=False, # Specify to load the test set\n",
" transform=mnist_transform, # Apply the transformation pipeline defined earlier\n",
" download=True # Download the dataset if it's not already present\n",
" )\n",
"\n",
"# Split the test dataset into two halves: original and transfer sets\n",
"test_size_original = int(0.5 * len(test_dataset)) # Calculate the size of the original test set (50% of the original)\n",
"test_size_transfer = len(test_dataset) - test_size_original # Calculate the size of the transfer test set (remaining 50%)\n",
"test_dataset_original, test_dataset_transfer = torch.utils.data.random_split(\n",
" dataset=test_dataset, # Original test dataset to split\n",
" lengths=[test_size_original, test_size_transfer] # Lengths of the resulting splits\n",
")\n",
" # Split the training dataset into training and validation sets\n",
" train_size = int(0.9 * len(train_val_dataset)) # Calculate the size of the training set (90% of the original)\n",
" val_size = len(train_val_dataset) - train_size # Calculate the size of the validation set (remaining 10%)\n",
" train_dataset, val_dataset = torch.utils.data.random_split(\n",
" dataset=train_val_dataset, # Original training dataset to split\n",
" lengths=[train_size, val_size] # Lengths of the resulting splits\n",
" )\n",
"\n",
"# Display the training dataset object\n",
"train_dataset\n",
" # Split the test dataset into two halves: original and transfer sets\n",
" test_size_original = int(0.5 * len(test_dataset)) # Calculate the size of the original test set (50% of the original)\n",
" test_size_transfer = len(test_dataset) - test_size_original # Calculate the size of the transfer test set (remaining 50%)\n",
" test_dataset_original, test_dataset_transfer = torch.utils.data.random_split(\n",
" dataset=test_dataset, # Original test dataset to split\n",
" lengths=[test_size_original, test_size_transfer] # Lengths of the resulting splits\n",
" )\n",
"\n",
"# Print the sizes of the training, validation, original test, and transfer test datasets\n",
"len(train_dataset), len(val_dataset), len(test_dataset_original), len(test_dataset_transfer)"
" # Display the training dataset object\n",
" train_dataset"
]
},
{
Expand Down
44 changes: 8 additions & 36 deletions tutorials/W1D2_ComparingTasks/W1D2_Tutorial2.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,7 @@
"cell_type": "markdown",
"id": "273ae64d-bc44-4e0e-b077-487da8391334",
"metadata": {
"execution": {},
"jp-MarkdownHeadingCollapsed": true
"execution": {}
},
"source": [
"---\n",
Expand Down Expand Up @@ -153,6 +152,8 @@
"\n",
"import logging\n",
"import gc\n",
"import contextlib\n",
"import io\n",
"\n",
"# PyTorch and related libraries\n",
"import torch\n",
Expand Down Expand Up @@ -181,8 +182,7 @@
"cell_type": "markdown",
"id": "910f242f",
"metadata": {
"execution": {},
"jp-MarkdownHeadingCollapsed": true
"execution": {}
},
"source": [
"## Figure settings\n"
Expand Down Expand Up @@ -211,40 +211,11 @@
"plt.style.use(\"https://raw.githubusercontent.com/NeuromatchAcademy/course-content/main/nma.mplstyle\")"
]
},
{
"cell_type": "markdown",
"id": "01471a7f",
"metadata": {
"execution": {},
"jp-MarkdownHeadingCollapsed": true
},
"source": [
"## Plotting functions\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "598cc45f-c4b7-406c-a80b-6adfae387583",
"metadata": {
"cellView": "form",
"execution": {},
"tags": [
"hide-input"
]
},
"outputs": [],
"source": [
"# @title Plotting functions\n",
"# @markdown"
]
},
{
"cell_type": "markdown",
"id": "808d728a",
"metadata": {
"execution": {},
"jp-MarkdownHeadingCollapsed": true
"execution": {}
},
"source": [
"## Helper functions"
Expand Down Expand Up @@ -526,8 +497,9 @@
" torchvision.transforms.Normalize((0.1307,), (0.3081,)) # Normalize the images with mean and standard deviation\n",
"])\n",
"\n",
"# Load the MNIST test dataset with the defined transformations\n",
"test_dset = torchvision.datasets.MNIST(\"./\", train=False, transform=mnist_transforms, download=True)\n",
"with contextlib.redirect_stdout(io.StringIO()):\n",
" # Load the MNIST test dataset with the defined transformations\n",
" test_dset = torchvision.datasets.MNIST(\"./\", train=False, transform=mnist_transforms, download=True)\n",
"\n",
"# Calculate the height and width of the MNIST images (28x28)\n",
"height = int(784**0.5)\n",
Expand Down
81 changes: 41 additions & 40 deletions tutorials/W1D2_ComparingTasks/instructor/W1D2_Tutorial1.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,8 @@
"from pathlib import Path\n",
"import zipfile\n",
"import random\n",
"import contextlib\n",
"import io\n",
"\n",
"# Import third-party libraries\n",
"import numpy as np\n",
Expand Down Expand Up @@ -844,50 +846,49 @@
},
"outputs": [],
"source": [
"# Define a transformation pipeline for the MNIST dataset\n",
"mnist_transform = transforms.Compose([\n",
" transforms.Resize((32, 32)), # Resize the images to 32x32 pixels\n",
" transforms.ToTensor(), # Convert images to PyTorch tensors\n",
" transforms.Normalize(mean=(0.1307,), std=(0.3081,)) # Normalize the images with mean and standard deviation\n",
"])\n",
"\n",
"# Load the MNIST training dataset with transformations applied\n",
"train_val_dataset = torchvision.datasets.MNIST(\n",
" root='./data', # Directory to store/load the data\n",
" train=True, # Specify to load the training set\n",
" transform=mnist_transform, # Apply the transformation pipeline defined earlier\n",
" download=True # Download the dataset if it's not already present\n",
")\n",
"\n",
"# Load the MNIST test dataset with transformations applied\n",
"test_dataset = torchvision.datasets.MNIST(\n",
" root='./data', # Directory to store/load the data\n",
" train=False, # Specify to load the test set\n",
" transform=mnist_transform, # Apply the transformation pipeline defined earlier\n",
" download=True # Download the dataset if it's not already present\n",
")\n",
"with contextlib.redirect_stdout(io.StringIO()):\n",
"\n",
" # Define a transformation pipeline for the MNIST dataset\n",
" mnist_transform = transforms.Compose([\n",
" transforms.Resize((32, 32)), # Resize the images to 32x32 pixels\n",
" transforms.ToTensor(), # Convert images to PyTorch tensors\n",
" transforms.Normalize(mean=(0.1307,), std=(0.3081,)) # Normalize the images with mean and standard deviation\n",
" ])\n",
"\n",
" # Load the MNIST training dataset with transformations applied\n",
" train_val_dataset = torchvision.datasets.MNIST(\n",
" root='./data', # Directory to store/load the data\n",
" train=True, # Specify to load the training set\n",
" transform=mnist_transform, # Apply the transformation pipeline defined earlier\n",
" download=True # Download the dataset if it's not already present\n",
" )\n",
"\n",
"# Split the training dataset into training and validation sets\n",
"train_size = int(0.9 * len(train_val_dataset)) # Calculate the size of the training set (90% of the original)\n",
"val_size = len(train_val_dataset) - train_size # Calculate the size of the validation set (remaining 10%)\n",
"train_dataset, val_dataset = torch.utils.data.random_split(\n",
" dataset=train_val_dataset, # Original training dataset to split\n",
" lengths=[train_size, val_size] # Lengths of the resulting splits\n",
")\n",
" # Load the MNIST test dataset with transformations applied\n",
" test_dataset = torchvision.datasets.MNIST(\n",
" root='./data', # Directory to store/load the data\n",
" train=False, # Specify to load the test set\n",
" transform=mnist_transform, # Apply the transformation pipeline defined earlier\n",
" download=True # Download the dataset if it's not already present\n",
" )\n",
"\n",
"# Split the test dataset into two halves: original and transfer sets\n",
"test_size_original = int(0.5 * len(test_dataset)) # Calculate the size of the original test set (50% of the original)\n",
"test_size_transfer = len(test_dataset) - test_size_original # Calculate the size of the transfer test set (remaining 50%)\n",
"test_dataset_original, test_dataset_transfer = torch.utils.data.random_split(\n",
" dataset=test_dataset, # Original test dataset to split\n",
" lengths=[test_size_original, test_size_transfer] # Lengths of the resulting splits\n",
")\n",
" # Split the training dataset into training and validation sets\n",
" train_size = int(0.9 * len(train_val_dataset)) # Calculate the size of the training set (90% of the original)\n",
" val_size = len(train_val_dataset) - train_size # Calculate the size of the validation set (remaining 10%)\n",
" train_dataset, val_dataset = torch.utils.data.random_split(\n",
" dataset=train_val_dataset, # Original training dataset to split\n",
" lengths=[train_size, val_size] # Lengths of the resulting splits\n",
" )\n",
"\n",
"# Display the training dataset object\n",
"train_dataset\n",
" # Split the test dataset into two halves: original and transfer sets\n",
" test_size_original = int(0.5 * len(test_dataset)) # Calculate the size of the original test set (50% of the original)\n",
" test_size_transfer = len(test_dataset) - test_size_original # Calculate the size of the transfer test set (remaining 50%)\n",
" test_dataset_original, test_dataset_transfer = torch.utils.data.random_split(\n",
" dataset=test_dataset, # Original test dataset to split\n",
" lengths=[test_size_original, test_size_transfer] # Lengths of the resulting splits\n",
" )\n",
"\n",
"# Print the sizes of the training, validation, original test, and transfer test datasets\n",
"len(train_dataset), len(val_dataset), len(test_dataset_original), len(test_dataset_transfer)"
" # Display the training dataset object\n",
" train_dataset"
]
},
{
Expand Down
44 changes: 8 additions & 36 deletions tutorials/W1D2_ComparingTasks/instructor/W1D2_Tutorial2.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,7 @@
"cell_type": "markdown",
"id": "273ae64d-bc44-4e0e-b077-487da8391334",
"metadata": {
"execution": {},
"jp-MarkdownHeadingCollapsed": true
"execution": {}
},
"source": [
"---\n",
Expand Down Expand Up @@ -153,6 +152,8 @@
"\n",
"import logging\n",
"import gc\n",
"import contextlib\n",
"import io\n",
"\n",
"# PyTorch and related libraries\n",
"import torch\n",
Expand Down Expand Up @@ -181,8 +182,7 @@
"cell_type": "markdown",
"id": "910f242f",
"metadata": {
"execution": {},
"jp-MarkdownHeadingCollapsed": true
"execution": {}
},
"source": [
"## Figure settings\n"
Expand Down Expand Up @@ -211,40 +211,11 @@
"plt.style.use(\"https://raw.githubusercontent.com/NeuromatchAcademy/course-content/main/nma.mplstyle\")"
]
},
{
"cell_type": "markdown",
"id": "01471a7f",
"metadata": {
"execution": {},
"jp-MarkdownHeadingCollapsed": true
},
"source": [
"## Plotting functions\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "598cc45f-c4b7-406c-a80b-6adfae387583",
"metadata": {
"cellView": "form",
"execution": {},
"tags": [
"hide-input"
]
},
"outputs": [],
"source": [
"# @title Plotting functions\n",
"# @markdown"
]
},
{
"cell_type": "markdown",
"id": "808d728a",
"metadata": {
"execution": {},
"jp-MarkdownHeadingCollapsed": true
"execution": {}
},
"source": [
"## Helper functions"
Expand Down Expand Up @@ -526,8 +497,9 @@
" torchvision.transforms.Normalize((0.1307,), (0.3081,)) # Normalize the images with mean and standard deviation\n",
"])\n",
"\n",
"# Load the MNIST test dataset with the defined transformations\n",
"test_dset = torchvision.datasets.MNIST(\"./\", train=False, transform=mnist_transforms, download=True)\n",
"with contextlib.redirect_stdout(io.StringIO()):\n",
" # Load the MNIST test dataset with the defined transformations\n",
" test_dset = torchvision.datasets.MNIST(\"./\", train=False, transform=mnist_transforms, download=True)\n",
"\n",
"# Calculate the height and width of the MNIST images (28x28)\n",
"height = int(784**0.5)\n",
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading