Skip to content

Edits to notebooks 3 and 4. #1

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
58 changes: 29 additions & 29 deletions 02.2_mnist_classifier.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
"\n",
"While Amelia's data collection process is working for most participants in her study, some do not like using the phone application to submit their survey responses. They keep sending in handwritten responses. Realizing that the data from these study participants is still vital to her research, Dr. Amelia is now looking to automate entering these responses using a program to read the numbers that make up the survey responses.\n",
"\n",
"Again, Amelia decides to start with the basics: recognizing handwritten numbers. That's where the MNIST dataset comes in. With its vast collection of handwritten digits, it's the perfect training ground for Amelia's next AI venture.\n",
"Again, Amelia decides to start with the basics: recognizing handwritten numbers. That's where the MNIST dataset comes in. With its vast collection of handwritten digits, it's the perfect training ground for Amelia's next AI adventure.\n",
"\n",
"**Note:** The cartoon of Dr Amelia's dog was generated with AI's assistance.\n",
"\n",
"Training a model on the MNIST dataset is often considered the \"Hello world!\" of AI. It is a commonly used first introduction to image recognition with deep learning.\n",
"Training a model on the MNIST dataset is often considered the \"Hello world!\" of modern computer vision. It is a commonly used first introduction to image recognition with deep learning.\n",
"\n",
"\n",
"![AI Application Development Pathway model](https://github.com/PracticumAI/deep_learning_2_draft/blob/main/M3-AppDev.00_00_22_23.Still001.png?raw=true)\n",
Expand All @@ -34,7 +34,7 @@
"\n",
"The MNIST dataset is frequently used in machine learning research and has become a standard benchmark for image classification models. Top-performing models often achieve a classification accuracy above 99%, with an error rate between 0.4% and 0.2% on the hold-out test dataset.\n",
"\n",
"In this exercise, you will implement a deep neural network (multi-layer) capable of classifying these images of handwritten digits into one of 10 classes. \n",
"In this exercise, you will implement a deep (multi-layer) neural network capable of classifying these images of handwritten digits into one of 10 classes. \n",
"\n",
"Amelia knows that to start any AI project, she'll need the right tools. She begins by importing the necessary libraries to set the stage for her digit-reading neural network.\n",
"\n",
Expand Down Expand Up @@ -69,16 +69,16 @@
"source": [
"## 2. Load the MNIST dataset\n",
"\n",
"Amelia will need to import the MNIST dataset from the [Keras module](https://keras.io/api/datasets/mnist/). The `train_features` and `test_features` variables contain the training and test images, while `train_labels` and `test_labels` contain the corresponding labels for each item in those datasets. \n",
"Amelia will need to import the MNIST dataset from the [Keras module](https://keras.io/api/datasets/mnist/). The `train_images` and `test_images` variables contain the training and test images, while `train_labels` and `test_labels` contain the corresponding labels for each item in those datasets. \n",
"\n",
"```python\n",
"# Import the MNIST dataset from TensorFlow's Keras datasets module\n",
"mnist = tf.keras.datasets.mnist\n",
"\n",
"# Load the MNIST dataset: \n",
"# - train_features and train_labels are the training images and their corresponding labels.\n",
"# - test_features and test_labels are the testing images and their corresponding labels.\n",
"(train_features,train_labels), (test_features,test_labels) = mnist.load_data()\n",
"# - train_images and train_labels are the training images and their corresponding labels.\n",
"# - test_images and test_labels are the testing images and their corresponding labels.\n",
"(train_images,train_labels), (test_images,test_labels) = mnist.load_data()\n",
"```"
]
},
Expand Down Expand Up @@ -129,14 +129,14 @@
"# Set line width for numpy array printing\n",
"np.set_printoptions(linewidth=150)\n",
"\n",
"# Select a random number from train_features\n",
"select = np.random.randint(0,len(train_features))\n",
"# Select a random image from train_images\n",
"select = np.random.randint(0,len(train_images))\n",
"\n",
"# Print the image array - longer line length above should allow it to have all 28 rows in 1 line\n",
"print(train_features[select])\n",
"print(train_images[select])\n",
"\n",
"# Display the image as an actual image\n",
"plt.imshow(train_features[select], cmap='gray')\n",
"plt.imshow(train_images[select], cmap='gray')\n",
"plt.show()\n",
"\n",
"# Print the true label for the image from train_labels\n",
Expand All @@ -149,7 +149,7 @@
"source": [
"The ouptut of the cell above should help clarify how images are encoded in our data. Each pixel has a value from 0 (black) to 255 (white). Since our images are black and white, we only have one grid of pixels. For color images, we would have three: one for each color, red, green, blue.\n",
"\n",
"Our datasets have 60,000 images in the `train_features` and 10,000 images in the `test_features`. We will use these data as we move forward."
"Our datasets have 60,000 images in `train_images` and 10,000 images in `test_images`. We will use these data as we move forward."
]
},
{
Expand All @@ -158,7 +158,7 @@
"source": [
"## 4. Normalize the data\n",
"\n",
"Before we normalize the data, look to see what the current maximum value is in `train_features`."
"Before we normalize the data, look to see what the current maximum value is in `train_images`."
]
},
{
Expand All @@ -167,23 +167,23 @@
"metadata": {},
"outputs": [],
"source": [
"# Code it! What is the max value of train_features?\n"
"# Code it! What is the max value of train_images?\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"Amelia normalizes the data to ensure her AI model can efficiently process these images. Processing your features so that they are represented by numbers between 0 and 1 is a best practice for AI model development.\n",
"Amelia normalizes the data to ensure her AI model can efficiently process these images. Processing your model inputs so that they are represented by numbers between 0 and 1 is a best practice for AI model development.\n",
"\n",
"Normalize the data by scaling the images so their values are between 0 and 1.\n",
"\n",
"```python\n",
"# Normalize the pixel values of the training and testing images to be between 0 and 1.\n",
"# This is done by dividing each pixel value by 255 (the maximum pixel value for an 8-bit image).\n",
"# Normalizing improves the training process and convergence.\n",
"train_features, test_features = train_features / 255.0, test_features / 255.0\n",
"train_images, test_images = train_images / 255.0, test_images / 255.0\n",
"```"
]
},
Expand All @@ -200,7 +200,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"Look at the maximum value of `train features` after normalization."
"Look at the maximum value of `train_images` after normalization."
]
},
{
Expand All @@ -209,7 +209,7 @@
"metadata": {},
"outputs": [],
"source": [
"# Code it! After normalization, what is the max value of train_features?\n"
"# Code it! After normalization, what is the max value of train_images?\n"
]
},
{
Expand Down Expand Up @@ -332,13 +332,13 @@
"Now, train the model on the MNIST dataset using the `fit` method. Set the training to run for 10 epochs.\n",
"\n",
"Train the model using the training data:\n",
"* `train_features`: the input images\n",
"* `train_images`: the input images\n",
"* `train_labels`: the true labels for each image\n",
"* `epochs=10`: the number of times the model will cycle through the entire dataset\n",
"\n",
"```python\n",
"\n",
"model.fit(train_features, train_labels, epochs=10)\n",
"model.fit(train_images, train_labels, epochs=10)\n",
"```"
]
},
Expand All @@ -361,13 +361,13 @@
"\n",
"\n",
"Evaluate the model's performance using the testing data:\n",
"* `test_features`: the input images from the testing set\n",
"* `test_images`: the input images from the testing set\n",
"* `test_labels`: the true labels for each image in the testing set\n",
"\n",
"The `evaluate` method returns the loss value and any additional metrics (in this case, accuracy) for the model on the testing data.\n",
"\n",
"```python\n",
"model.evaluate(test_features, test_labels)\n",
"model.evaluate(test_images, test_labels)\n",
"```"
]
},
Expand All @@ -388,7 +388,7 @@
"\n",
"Let's see how the model performs on some randomly selected images. Are its predictions correct? \n",
"\n",
"Randomly select an image from the test dataset, in this case, the 200th image.\n",
"Select an image from the test dataset, in this case, the 200th image.\n",
"\n",
"Select a specific image from the test dataset for examination or prediction.\n",
"\n",
Expand All @@ -397,8 +397,8 @@
"```python\n",
"loc = 200\n",
"\n",
"# Extract the corresponding image from the test_features array and store it in the 'test_image' variable.\n",
"test_image = test_features[loc]\n",
"# Extract the corresponding image from the test_images array and store it in the 'test_image' variable.\n",
"test_image = test_images[loc]\n",
"```"
]
},
Expand Down Expand Up @@ -541,7 +541,7 @@
" * This helps in visually examining the content of the `test_image` (which is represented as a 28x28 array of pixel values).\n",
"\n",
"```python\n",
"plt.imshow(test_features[loc])\n",
"plt.imshow(test_images[loc])\n",
"```"
]
},
Expand Down Expand Up @@ -587,9 +587,9 @@
],
"metadata": {
"kernelspec": {
"display_name": "Tensorflow-2.7.0",
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "tensorflow-2.7.0"
"name": "python3"
},
"language_info": {
"codemirror_mode": {
Expand All @@ -601,7 +601,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.12"
"version": "3.10.2"
}
},
"nbformat": 4,
Expand Down
11 changes: 5 additions & 6 deletions 03_bees_vs_wasps.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@
"\n",
"One hyperparameter to explore is the activation function, which is set when making the model. We start with a ReLU as the default, but you can try others. For simplicity, we will use the same activation function for all but the last layer of the model, but you could change them individually.\n",
"\n",
"The last layer will almost always use a Softmax, which makes all the output values between 0 and 1 and sum to 1, transforming them into probabilities of the input belonging to each possible class."
"The last layer will almost always use a Softmax, which makes all the output values between 0 and 1 and sum to 1, transforming them into \"probabilities\" of the input belonging to each possible class."
]
},
{
Expand Down Expand Up @@ -453,14 +453,13 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 1,
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"def the_whole_shebang(path, batch_size, shape, classes, activation, loss, optimizer, show_pictures=True):\n",
" \n",
" X_train, X_test = load_display_data(path, batch_size, shape, show_pictures)\n",
" model = make_model(activation=activation, shape=shape, num_classes=classes)\n",
" model, history = compile_train_model(X_train, X_test, model, loss=loss,\n",
Expand Down Expand Up @@ -517,9 +516,9 @@
],
"metadata": {
"kernelspec": {
"display_name": "Tensorflow-2.7.0",
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "tensorflow-2.7.0"
"name": "python3"
},
"language_info": {
"codemirror_mode": {
Expand All @@ -531,7 +530,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.12"
"version": "3.10.2"
}
},
"nbformat": 4,
Expand Down