spce0038-machine-learning-w.../week4/exercises/Lecture15_DeepCNN_Exercises_no_solutions.ipynb
2025-02-28 11:02:07 +00:00

1 line
2.7 KiB
Plaintext

{"cells": [{"cell_type": "markdown", "id": "c1c2c049-4d1c-4a46-b035-b3f477aace8d", "metadata": {}, "source": ["# Exercises for Lecture 15 (Deep CNN architectures)"]}, {"cell_type": "code", "execution_count": null, "id": "ea8f9065-2320-422b-8a8c-a55d7fd726a3", "metadata": {}, "outputs": [], "source": ["import numpy as np\n", "import tensorflow as tf\n", "from tensorflow import keras\n", "from functools import partial\n", "\n", "# To make this notebook's output stable across runs\n", "def reset_state(seed=42):\n", " tf.keras.backend.clear_session()\n", " tf.random.set_seed(seed)\n", " np.random.seed(seed)"]}, {"cell_type": "markdown", "id": "c5dc69f6-efea-4b71-9746-25ca8d347132", "metadata": {"slideshow": {"slide_type": "subslide"}}, "source": ["## Exercise 1: Build a ResNet CNN architecture for fashion MNIST."]}, {"cell_type": "markdown", "id": "9fa998f5-7e8e-4cd6-a335-13a146c49de3", "metadata": {}, "source": ["Load and set up data."]}, {"cell_type": "code", "execution_count": null, "id": "b7fd0854-0e6f-4d4a-838b-506c4a26268c", "metadata": {}, "outputs": [], "source": ["# Load fashion MNIST data\n", "(X_train_full, y_train_full), (X_test, y_test) = keras.datasets.fashion_mnist.load_data()\n", "X_train, X_valid = X_train_full[:-30000], X_train_full[-30000:]\n", "y_train, y_valid = y_train_full[:-30000], y_train_full[-30000:]\n", "\n", "# Standardize\n", "X_mean = X_train.mean(axis=0, keepdims=True)\n", "X_std = X_train.std(axis=0, keepdims=True) + 1e-7\n", "X_train = (X_train - X_mean) / X_std\n", "X_valid = (X_valid - X_mean) / X_std\n", "X_test = (X_test - X_mean) / X_std\n", "\n", "# Add final channel axis (one channel)\n", "X_train = X_train[..., np.newaxis]\n", "X_valid = X_valid[..., np.newaxis]\n", "X_test = X_test[..., np.newaxis]"]}, {"cell_type": "markdown", "id": "4362c522-3172-4fc9-9265-761cccad73cc", "metadata": {}, "source": ["Use the subclassing API to define a `ResidualUnit` layer."]}, {"cell_type": "markdown", "id": "dd33a8d1-af2f-49cc-ad73-8b447c07955f", "metadata": {}, "source": ["Buid a ResNet model using the layer you defined above."]}, {"cell_type": "markdown", "id": "561c8d32-b8e4-426b-8092-7ec79432dde0", "metadata": {}, "source": ["Compile your model and train it."]}, {"cell_type": "markdown", "id": "1159fce2-0749-4170-ab4d-f4b8e0ed6014", "metadata": {}, "source": ["Evaluate the model performance on the test set."]}], "metadata": {"kernelspec": {"display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3"}, "language_info": {"codemirror_mode": {"name": "ipython", "version": 3}, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.12"}}, "nbformat": 4, "nbformat_minor": 5}