Week3 tutorials update#1031
Open
cindyhfls wants to merge 26 commits into
Open
Conversation
…iscussion only Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
W3D4 T1: trimmed from 7 exercises to 5 (GridWorld, MDP, Q steps-to-go, Value Iteration, Policy Iteration); added scaffolding markdown before each exercise; Q-learning and ε-greedy moved to W3D5 T1 W3D5: new T1 created (Q-learning + ε-greedy, moved from W3D4); former T1 (DL Discussion 3) renamed to T2 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
W3D4 T1: trimmed from 7 exercises to 5 (GridWorld, MDP, Q steps-to-go, Value Iteration, Policy Iteration); added scaffolding markdown before each exercise; Q-learning and ε-greedy moved to W3D5 T1 W3D5: - New T1: Q-learning + ε-greedy (moved from W3D4), feedback_prefix W3D5_T1 - T2: DL Discussion 3 (renamed from old T1); title corrected to Tutorial 2 - T3 (Bonus): Reinforcement Learning For Games pulled from upstream W3D5_Tutorial1; title changed to Bonus Tutorial, day line and feedback_prefix (W3D5_T3_Bonus) updated - Folder renamed W3D5_DeepLearningDiscussion3 → W3D5_AdvancedReinforcementLearningAndDeepLearningDiscussion3 - materials.yml: name updated, tutorials count 1 → 3, slides added for T1 (ztgws), T2 (2f7ay), T3 Bonus (3zn9w) Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
W3D2: update Tutorial1 content and README to match DeepLearningDiscussion2 naming W3D3: apply coding style and dependency inlining to Tutorial1 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…ean up, fill most of the code and kept code completion to minimum
c552757 to
dd75164
Compare
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
iamzoltan
reviewed
Jun 9, 2026
| }, | ||
| "source": [ | ||
| "# Tutorial 1: Deep Learning Case Study 2: Architectures and Multimodal DL \n", | ||
| "# Tutorial 1: Deep Learning Thinking 2: Architectures and Multimodal DL thinking\n", |
iamzoltan
reviewed
Jun 9, 2026
| "source": [ | ||
| "---\n", | ||
| "# Section 1: Intro Deep Learning Case Study 2\n", | ||
| "# Section 1: Intro Deep Learning Thinking 2\n", |
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
iamzoltan
reviewed
Jun 12, 2026
iamzoltan
left a comment
Contributor
There was a problem hiding this comment.
This ones is also almost ready. some minor comments
| "cellView": "form" | ||
| }, | ||
| "outputs": [], | ||
| "source": "# @title Module: Load utilities\n\nimport os\n\nimport numpy as np\nimport torch\nimport torch.nn as nn\nimport torchvision\n\n\n\ndef load_encoder(save_direc, model_type=\"simclr\", dataset_type=\"full\", \n neg_pairs=\"all\", verbose=True):\n \"\"\"\n load_encoder(save_direc)\n\n Loads encoder (VAE or SimCLR) with pre-trained parameters.\n\n Required args:\n - save_direc (str): directory where pre-trained encoder parameters are \n saved\n\n Optional args:\n - model_type (str): type of pre-trained encoder model (default: \"simclr\")\n - dataset_type (str): type of dataset used in the pre-training \n (default: \"full\") \n - neg_pairs (str or num): Number of negeative pairs used in loss \n calculation, if loading a SimCLR model. (default: \"all\")\n - verbose (bool): If True, details of the encoder being loaded are \n printed. (default: True)\n\n Returns:\n - encoder (models.EncoderCore): encoder loaded with pre-trained parameters\n \"\"\"\n\n\n if dataset_type == \"full\":\n dataset_type_str = \"\"\n elif dataset_type in [\"biased\", \"bias_ctrl\"]:\n dataset_type_str = f\"_{dataset_type}\"\n else:\n raise ValueError(\"dataset_type can only be 'full', 'biased' or \"\n f\"'bias_ctrl', but found '{dataset_type}'.\")\n\n vae = False\n simclr_transforms_str, simclr_transforms_str_pr = \"\", \"\"\n neg_str, neg_str_pr = \"\", \"\"\n \n seed = 2021\n if model_type == \"vae\":\n batch_size = 500\n vae = True\n model_name = \"VAE\"\n if dataset_type == \"full\":\n num_epochs = 300\n elif dataset_type in [\"biased\", \"bias_ctrl\"]:\n num_epochs = 450\n \n elif model_type == \"simclr\":\n batch_size = 1000\n model_name = \"SimCLR\"\n simclr_transforms_str_pr = (\"\\nwith the following random affine \"\n \"transforms:\\n\\tdegrees=90\\n\\ttranslation=(0.2, 0.2)\"\n \"\\n\\tscale=(0.8, 1.2)\")\n simclr_transforms_str = \"_deg90_trans0-2_scale0-8to1-2\"\n if dataset_type == \"full\":\n num_epochs = 60\n if neg_pairs != \"all\":\n neg_pairs = int(neg_pairs)\n if neg_pairs != 2:\n raise ValueError(\"If not 'all'', neg_pairs must be set \"\n \"to 2, as that is the only value that was used in \"\n \"the saved models.\")\n neg_str_pr = (f\"\\nwith {neg_pairs} negative pairs per image \"\n \"used in the contrastive loss, and\")\n neg_str = f\"_{neg_pairs}neg\"\n elif dataset_type in [\"biased\", \"bias_ctrl\"]:\n if neg_pairs != \"all\":\n raise ValueError(\n \"No saved model for SimCLR with few negative pairs using \"\n \"the biased or bias_ctrl datasets.\"\n )\n num_epochs = 150\n \n elif model_type == \"supervised\":\n model_name = model_type\n batch_size = 1000\n num_epochs = 10\n\n elif model_type == \"random\":\n model_name = model_type\n batch_size = 0\n num_epochs = 0\n\n else:\n raise ValueError(\"Recorded model types only include 'supervised', \"\n f\"'random', 'vae', 'simclr', but not '{model_type}'.\")\n \n encoder_path = (\n f\"{model_type}_encoder{dataset_type_str}{neg_str}_{num_epochs}ep_\"\n f\"bs{batch_size}{simclr_transforms_str}_seed{seed}.pth\"\n )\n full_path = os.path.join(save_direc, \"checkpoints\", encoder_path)\n \n if verbose:\n model_details = (f\" => trained for {num_epochs} epochs \"\n f\"(batch_size of {batch_size}) on the {dataset_type} dSprites \"\n f\"subset dataset{neg_str_pr}{simclr_transforms_str_pr}.\")\n print(f\"Loading {model_name} encoder from '{full_path}'.\\n\"\n f\"{model_details}\")\n\n encoder = models.EncoderCore(vae=vae)\n encoder.load_state_dict(torch.load(full_path))\n\n return encoder\n\n\ndef load_vae_decoder(save_direc, verbose=True):\n \"\"\"\n load_vae_decoder(save_direc)\n\n Loads VAE decoder with pre-trained parameters.\n\n Required args:\n - save_direc (str): directory where pre-trained decoder parameters are \n saved\n\n Optional args:\n - verbose (bool): If True, details of the decoder being loaded are \n printed. (default: True)\n\n Returns:\n - encoder (models.VAE_decoder): decoder loaded with pre-trained parameters\n \"\"\"\n \n batch_size = 500\n seed = 2021\n model_name = \"VAE\"\n num_epochs = 300\n decoder_path =f\"vae_decoder_{num_epochs}ep_bs{batch_size}_seed{seed}.pth\"\n\n full_path = os.path.join(save_direc, \"checkpoints\", decoder_path)\n \n if verbose:\n model_details = (f\" => trained for {num_epochs} epochs \"\n f\"(batch_size of {batch_size}) on the full dSprites subset \"\n \"dataset.\")\n print(f\"Loading {model_name} decoder from '{full_path}'.\\n\"\n f\"{model_details}\")\n\n decoder = models.VAE_decoder()\n decoder.load_state_dict(torch.load(full_path))\n\n return decoder\n\n\nclass ResNet18_with_encoder(torchvision.models.resnet.ResNet):\n \"\"\"\n ResNet18_with_encoder()\n\n torchvision ResNet18 with explicitly defined encoder attribute, and \n get_features() method.\n\n Optional args:\n - pretrained (bool): If True, the model is pretrained. (default: True)\n \"\"\"\n \n def __init__(self, pretrained=True):\n\n self._untrained = not(pretrained)\n\n weights = torchvision.models.ResNet18_Weights.DEFAULT if pretrained else None\n resnet18 = torchvision.models.resnet18(\n weights=weights, progress=False\n )\n self.__dict__.update(resnet18.__dict__)\n \n self.pretrained = pretrained\n self.input_dim = (3, 224, 224)\n\n self._define_encoder()\n self.feat_size = self._get_feat_extr_output_size(self.input_dim)\n\n @property\n def untrained(self):\n return self._untrained\n\n @property\n def vae(self):\n return False\n\n def _define_encoder(self):\n # first 8\n self.encoder = nn.Sequential(\n self.conv1,\n self.bn1,\n self.relu,\n self.maxpool,\n self.layer1,\n self.layer2,\n self.layer3,\n self.layer4,\n )\n\n def _get_feat_extr_output_size(self, input_dim):\n dummy_tensor = torch.ones(1, *input_dim)\n reset_training = self.training\n self.eval()\n with torch.no_grad(): \n output_dim = self.encoder(dummy_tensor).shape\n if reset_training:\n self.train()\n return np.prod(output_dim)\n\n def get_features(self, X):\n with torch.no_grad():\n feats = self.encoder(X)\n return feats\n\n def forward(self, *args, **kwargs):\n self._untrained = False\n super().forward(*args, **kwargs)\n\n\nclass VGG16_with_encoder(torchvision.models.vgg.VGG):\n \"\"\"\n VGG16_with_encoder()\n\n torchvision VGG16 with explicitly defined encoder attribute, and \n get_features() method.\n\n Optional args:\n - pretrained (bool): If True, the model is pretrained. (default: True)\n \"\"\"\n\n def __init__(self, pretrained=True):\n\n self._untrained = not(pretrained)\n\n weights = torchvision.models.VGG16_Weights.DEFAULT if pretrained else None\n vgg16 = torchvision.models.vgg16(\n weights=weights, progress=False\n )\n self.__dict__.update(vgg16.__dict__)\n \n self.pretrained = pretrained\n\n self._define_encoder()\n self.input_dim = (3, 64, 64)\n self.feat_size = self._get_feat_extr_output_size(self.input_dim)\n\n @property\n def untrained(self):\n return self._untrained\n\n @property\n def vae(self):\n return False\n\n def _define_encoder(self):\n self.encoder = self.features # alias\n\n def _get_feat_extr_output_size(self, input_dim):\n dummy_tensor = torch.ones(1, *input_dim)\n reset_training = self.training\n self.eval()\n with torch.no_grad(): \n output_dim = self.encoder(dummy_tensor).shape\n if reset_training:\n self.train()\n return np.prod(output_dim)\n\n def get_features(self, X):\n with torch.no_grad():\n feats = self.encoder(X)\n return feats\n\n def forward(self, *args, **kwargs):\n self._untrained = False\n super().forward(*args, **kwargs)\n\n\nclass SimCLR_spijk_with_encoder(nn.Module):\n \"\"\"\n SimCLR_spijk_with_encoder()\n\n SimCLR implementation from https://github.com/Spijkervet/SimCLR, with \n explicitly defined get_features() method.\n\n Optional args:\n - pretrained (bool): If True, the model is pretrained. (default: True)\n \"\"\"\n\n def __init__(self, pretrained=True):\n \n self.projection_dim = 64\n self._untrained = not(pretrained)\n\n import simclr\n\n encoder = simclr.modules.get_resnet(\"resnet18\", pretrained=pretrained)\n simclr_model = simclr.SimCLR(\n encoder, self.projection_dim, encoder.fc.in_features\n )\n self.__dict__.update(simclr_model.__dict__)\n\n self.pretrained = pretrained\n\n if self.pretrained:\n src = (\"https://github.com/Spijkervet/SimCLR/releases/download/\"\n \"1.1/checkpoint_100.tar\")\n\n state_dict = torch.hub.load_state_dict_from_url(\n src, progress=False, map_location=\"cpu\"\n )\n\n self.load_state_dict(state_dict)\n\n self.input_dim = (3, 224, 224)\n self.feat_size = self._get_feat_extr_output_size(self.input_dim)\n\n\n @property\n def untrained(self):\n return self._untrained\n\n @property\n def vae(self):\n return False\n\n def _get_feat_extr_output_size(self, input_dim):\n dummy_tensor = torch.ones(1, *input_dim)\n reset_training = self.training\n self.eval()\n with torch.no_grad(): \n output_dim = self.encoder(dummy_tensor).shape\n if reset_training:\n self.train()\n return np.prod(output_dim)\n\n def get_features(self, X):\n with torch.no_grad():\n feats = self.encoder(X)\n return feats\n\n def forward(self, *args, **kwargs):\n self._untrained = False\n super().forward(*args, **kwargs)\n" |
Contributor
There was a problem hiding this comment.
Can we separate this into a list of strings?
Comment on lines
-801
to
-806
| " #################################################\n", | ||
| " # Set the proper actions\n", | ||
| " raise NotImplementedError(\"Implement `plan` function!\")\n", | ||
| " #################################################\n", | ||
| " while goal_queue:\n", | ||
| " goal = goal_queue.pop(0) # pop from front of list\n", |
Contributor
There was a problem hiding this comment.
was removing this student question intentional?
Comment on lines
+4820
to
+4822
| "\n", | ||
| "The following code:\n", | ||
| "* Loads the parameters of a full Variational AutoEncoder (VAE) network (encoder and decoder) pre-trained on the generative task of reconstructing the input images, under the Kullback–Leibler Divergence (KLD) minimization constraint over the latent space that characterizes VAEs, using `load.load_encoder()` and `load.load_decoder()`." | ||
| "* Loads the parameters of a full Variational AutoEncoder (VAE) network (encoder and decoder) pre-trained on the generative task of reconstructing the input images, under the Kullback\u2013Leibler Divergence (KLD) minimization constraint over the latent space that characterizes VAEs, using `load.load_encoder()` and `load.load_decoder()`." |
Contributor
There was a problem hiding this comment.
can we convert these back to their chars?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.