{"id":3482,"date":"2024-08-20T17:12:30","date_gmt":"2024-08-20T17:12:30","guid":{"rendered":"https:\/\/towardsdatascience.com\/implementing-convolutional-neural-networks-in-tensorflow-bc1c4f00bd34\/"},"modified":"2025-01-08T15:32:23","modified_gmt":"2025-01-08T15:32:23","slug":"implementing-convolutional-neural-networks-in-tensorflow-bc1c4f00bd34","status":"publish","type":"post","link":"https:\/\/towardsdatascience.com\/implementing-convolutional-neural-networks-in-tensorflow-bc1c4f00bd34\/","title":{"rendered":"Implementing Convolutional Neural Networks in TensorFlow"},"content":{"rendered":"<p class=\"wp-block-paragraph\">Welcome to the practical implementation guide of our <strong><a href=\"https:\/\/medium.com\/@shreya.rao\/list\/deep-learning-illustrated-ae6c27de1640\">Deep Learning Illustrated<\/a><\/strong> series. In this series, we bridge the gap between theory and application, bringing to life the neural network concepts explored in previous articles.<\/p>\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p><a href=\"https:\/\/medium.com\/@shreya.rao\/list\/ae6c27de1640\"><strong>Deep Learning, Illustrated<\/strong><\/a><\/p><\/blockquote>\n<p class=\"wp-block-paragraph\">In today&#8217;s article, we&#8217;ll build a Convolutional Neural Network (CNN) using TensorFlow. Be sure to read the previous <a href=\"https:\/\/medium.com\/r?url=https%3A%2F%2Ftowardsdatascience.com%2Fdeep-learning-illustrated-part-3-convolutional-neural-networks-96b900b0b9e0\">CNN article<\/a>, as this one assumes you&#8217;re already familiar with the inner workings and mathematical foundations of a CNN. We&#8217;ll be focusing only on implementation here, so prior knowledge will help you follow along more easily.<\/p>\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p><a href=\"https:\/\/towardsdatascience.com\/deep-learning-illustrated-part-3-convolutional-neural-networks-96b900b0b9e0\"><strong>Deep Learning Illustrated, Part 3: Convolutional Neural Networks<\/strong><\/a><\/p><\/blockquote>\n<p class=\"wp-block-paragraph\">We&#8217;ll create the same simple image classifier that predicts whether a given image is an &#8216;X&#8217; or not.<\/p>\n<figure class=\"wp-block-image size-large\"><img data-dominant-color=\"f3f4ef\" data-has-transparency=\"false\" style=\"--dominant-color: #f3f4ef;\" loading=\"lazy\" decoding=\"async\" width=\"1400\" height=\"661\" class=\"wp-image-310239 not-transparent\" src=\"https:\/\/towardsdatascience.com\/wp-content\/uploads\/2024\/08\/0bGkqEU9pbwPfs09D-1.jpeg\" alt=\"\" srcset=\"https:\/\/towardsdatascience.com\/wp-content\/uploads\/2024\/08\/0bGkqEU9pbwPfs09D-1.jpeg 1400w, https:\/\/towardsdatascience.com\/wp-content\/uploads\/2024\/08\/0bGkqEU9pbwPfs09D-1-300x142.jpeg 300w, https:\/\/towardsdatascience.com\/wp-content\/uploads\/2024\/08\/0bGkqEU9pbwPfs09D-1-1024x483.jpeg 1024w, https:\/\/towardsdatascience.com\/wp-content\/uploads\/2024\/08\/0bGkqEU9pbwPfs09D-1-768x363.jpeg 768w\" sizes=\"auto, (max-width: 1400px) 100vw, 1400px\" \/><\/figure>\n<p class=\"wp-block-paragraph\">And we&#8217;ll break down each step in detail along the way to ensure you understand both the how and the why!<\/p>\n<h3 class=\"wp-block-heading\">Step 1: Importing the necessary libraries<\/h3>\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-python\">import tensorflow as tf\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense\nfrom tensorflow.keras.optimizers import Adam\nimport numpy as np\nimport matplotlib.pyplot as plt<\/code><\/pre>\n<p class=\"wp-block-paragraph\">TensorFlow and Keras (which is a high-level API within TensorFlow) will handle the creation and training of our CNN, while NumPy and Matplotlib will help us with data manipulation and visualization.<\/p>\n<p class=\"wp-block-paragraph\"><em>NOTE: To ensure that our results are consistent each time we run the code, we&#8217;ll set a random seed:<\/em><\/p>\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-python\"># Setting seed for reproducibility\nnp.random.seed(42)\ntf.random.set_seed(42)<\/code><\/pre>\n<p class=\"wp-block-paragraph\"><em>Setting a seed ensures consistent results by making sure the random processes in the code run the same way each time. Think of it like shuffling a deck of cards in exactly the same order every time we play.<\/em><\/p>\n<h3 class=\"wp-block-heading\">Step 2: Understanding and Generating the Data<\/h3>\n<p class=\"wp-block-paragraph\">Let&#8217;s first generate the images that our model will learn to classify. Previously we saw that an &#8216;X&#8217; can be represented by a 5&#215;5 pixel image like so:<\/p>\n<figure class=\"wp-block-image size-large\"><img data-dominant-color=\"c7c7c7\" data-has-transparency=\"false\" style=\"--dominant-color: #c7c7c7;\" loading=\"lazy\" decoding=\"async\" width=\"1400\" height=\"689\" class=\"wp-image-310241 not-transparent\" src=\"https:\/\/towardsdatascience.com\/wp-content\/uploads\/2024\/08\/0IuEn3SA-yVjm1W_9-1.png\" alt=\"\" srcset=\"https:\/\/towardsdatascience.com\/wp-content\/uploads\/2024\/08\/0IuEn3SA-yVjm1W_9-1.png 1400w, https:\/\/towardsdatascience.com\/wp-content\/uploads\/2024\/08\/0IuEn3SA-yVjm1W_9-1-300x148.png 300w, https:\/\/towardsdatascience.com\/wp-content\/uploads\/2024\/08\/0IuEn3SA-yVjm1W_9-1-1024x504.png 1024w, https:\/\/towardsdatascience.com\/wp-content\/uploads\/2024\/08\/0IuEn3SA-yVjm1W_9-1-768x378.png 768w\" sizes=\"auto, (max-width: 1400px) 100vw, 1400px\" \/><\/figure>\n<p class=\"wp-block-paragraph\">Let&#8217;s translate this to code:<\/p>\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-python\"># &#039;X&#039; pattern\ndef generate_x_image():\n    return np.array([\n        [1, 0, 0, 0, 1],\n        [0, 1, 0, 1, 0],\n        [0, 0, 1, 0, 0],\n        [0, 1, 0, 1, 0],\n        [1, 0, 0, 0, 1]\n    ])<\/code><\/pre>\n<p class=\"wp-block-paragraph\">This function generates a simple 5&#215;5 image of an &#8216;X&#8217;. Next, we&#8217;ll create a function that generates random 5&#215;5 images that do not resemble an &#8216;X&#8217;:<\/p>\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-python\">def generate_not_x_image():\n    # Ensuring not to generate an &#039;X&#039; pattern\n    while True:\n        img = np.random.randint(2, size=(5, 5))\n        if not np.array_equal(img, generate_x_image()):\n            return img<\/code><\/pre>\n<h3 class=\"wp-block-heading\">Step 3: Building the Dataset<\/h3>\n<p class=\"wp-block-paragraph\">With our functions ready, we can now create a dataset of 1,000 images. We&#8217;ll label them accordingly, with 1 for images of an &#8216;X&#8217; and 0 for those that are not:<\/p>\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-python\"># Create a dataset\nnum_samples = 1000\nimages = []\nlabels = []\n\nfor _ in range(num_samples):\n    if np.random.rand() &amp;gt; 0.5:\n        images.append(generate_x_image())\n        labels.append(1)\n    else:\n        images.append(generate_not_x_image())\n        labels.append(0)\n\nimages = np.array(images).reshape(-1, 5, 5, 1)\nlabels = np.array(labels)<\/code><\/pre>\n<p class=\"wp-block-paragraph\">This code generates 1,000 images, half of which contain an &#8216;X&#8217; and the other half don&#8217;t. We then reshape the images to ensure they have the correct dimensions for our CNN.<\/p>\n<p class=\"wp-block-paragraph\">To train our model effectively, we&#8217;ll split this dataset into training and testing sets:<\/p>\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-python\"># Split the data into training and testing sets\nfrom sklearn.model_selection import train_test_split\nx_train, x_test, y_train, y_test = train_test_split(images, labels, test_size=0.2, random_state=42)<\/code><\/pre>\n<p class=\"wp-block-paragraph\">This split reserves 80% of the data for training the model and 20% for testing it. The test set helps us evaluate how well the model performs on new, unseen data.<\/p>\n<p class=\"wp-block-paragraph\">Before we dive into model building, let&#8217;s take a look at some of the images in our dataset to understand what we&#8217;re working with.<\/p>\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-python\"># Function to display images\ndef display_sample_data(images, labels, num_samples=5):\n    plt.figure(figsize=(10, 2))\n    for i in range(num_samples):\n        ax = plt.subplot(1, num_samples, i + 1)\n        plt.imshow(images[i].reshape(5, 5), cmap=&#039;gray_r&#039;)\n        plt.title(f&#039;Label: {labels[i]}&#039;)\n        plt.axis(&#039;off&#039;)\n    plt.show()<\/code><\/pre>\n<p class=\"wp-block-paragraph\">This function displays images from our training set, helping us confirm that the data is correctly labeled and formatted.<\/p>\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-python\"># Display first 5 samples of our training data\ndisplay_sample_data(x_train, y_train)<\/code><\/pre>\n<figure class=\"wp-block-image size-large\"><img data-dominant-color=\"a7a7a7\" data-has-transparency=\"false\" style=\"--dominant-color: #a7a7a7;\" loading=\"lazy\" decoding=\"async\" width=\"1578\" height=\"338\" class=\"wp-image-310243 not-transparent\" src=\"https:\/\/towardsdatascience.com\/wp-content\/uploads\/2024\/08\/13268gjkAwIHKjtYFwalETw-1.png\" alt=\"\" srcset=\"https:\/\/towardsdatascience.com\/wp-content\/uploads\/2024\/08\/13268gjkAwIHKjtYFwalETw-1.png 1578w, https:\/\/towardsdatascience.com\/wp-content\/uploads\/2024\/08\/13268gjkAwIHKjtYFwalETw-1-300x64.png 300w, https:\/\/towardsdatascience.com\/wp-content\/uploads\/2024\/08\/13268gjkAwIHKjtYFwalETw-1-1024x219.png 1024w, https:\/\/towardsdatascience.com\/wp-content\/uploads\/2024\/08\/13268gjkAwIHKjtYFwalETw-1-768x165.png 768w, https:\/\/towardsdatascience.com\/wp-content\/uploads\/2024\/08\/13268gjkAwIHKjtYFwalETw-1-1536x329.png 1536w\" sizes=\"auto, (max-width: 1578px) 100vw, 1578px\" \/><\/figure>\n<h3 class=\"wp-block-heading\">Step 4: Building the CNN Model<\/h3>\n<p class=\"wp-block-paragraph\">Now that our data is ready, let&#8217;s build the CNN! Here&#8217;s the architecture we used previously:<\/p>\n<p class=\"wp-block-paragraph\"><strong><a href=\"https:\/\/towardsdatascience.com\/deep-learning-illustrated-part-3-convolutional-neural-networks-96b900b0b9e0#15ee\">1 &#8211; Convolutional Layer<\/a><\/strong>: Applies four 3&#215;3 filters to an input image to detect features and creates four feature maps<\/p>\n<figure class=\"wp-block-image size-large\"><img data-dominant-color=\"dad9d7\" data-has-transparency=\"true\" style=\"--dominant-color: #dad9d7;\" loading=\"lazy\" decoding=\"async\" width=\"1400\" height=\"635\" class=\"wp-image-310245 has-transparency\" src=\"https:\/\/towardsdatascience.com\/wp-content\/uploads\/2024\/08\/0INn2CSzIhbRAU2ky-1.png\" alt=\"\" srcset=\"https:\/\/towardsdatascience.com\/wp-content\/uploads\/2024\/08\/0INn2CSzIhbRAU2ky-1.png 1400w, https:\/\/towardsdatascience.com\/wp-content\/uploads\/2024\/08\/0INn2CSzIhbRAU2ky-1-300x136.png 300w, https:\/\/towardsdatascience.com\/wp-content\/uploads\/2024\/08\/0INn2CSzIhbRAU2ky-1-1024x464.png 1024w, https:\/\/towardsdatascience.com\/wp-content\/uploads\/2024\/08\/0INn2CSzIhbRAU2ky-1-768x348.png 768w\" sizes=\"auto, (max-width: 1400px) 100vw, 1400px\" \/><\/figure>\n<p class=\"wp-block-paragraph\"><strong><a href=\"https:\/\/medium.com\/towards-data-science\/deep-learning-illustrated-part-3-convolutional-neural-networks-96b900b0b9e0#9b9e\">2 &#8211; Max-Pooling Layer<\/a><\/strong>: Reduces the dimensions of the feature maps, making the model more efficient<\/p>\n\n<p class=\"wp-block-paragraph\"><strong><a href=\"https:\/\/medium.com\/towards-data-science\/deep-learning-illustrated-part-3-convolutional-neural-networks-96b900b0b9e0#8c24\">3 &#8211; Flatten Layer<\/a><\/strong>: Converts the 2D data into a 1D array, preparing it for the neural network<\/p>\n<figure class=\"wp-block-image size-large\"><img data-dominant-color=\"f2ebdc\" data-has-transparency=\"true\" style=\"--dominant-color: #f2ebdc;\" loading=\"lazy\" decoding=\"async\" width=\"1000\" height=\"1067\" class=\"wp-image-310247 has-transparency\" src=\"https:\/\/towardsdatascience.com\/wp-content\/uploads\/2024\/08\/02BhDyrCfWDzSHCfX-1.png\" alt=\"\" srcset=\"https:\/\/towardsdatascience.com\/wp-content\/uploads\/2024\/08\/02BhDyrCfWDzSHCfX-1.png 1000w, https:\/\/towardsdatascience.com\/wp-content\/uploads\/2024\/08\/02BhDyrCfWDzSHCfX-1-281x300.png 281w, https:\/\/towardsdatascience.com\/wp-content\/uploads\/2024\/08\/02BhDyrCfWDzSHCfX-1-960x1024.png 960w, https:\/\/towardsdatascience.com\/wp-content\/uploads\/2024\/08\/02BhDyrCfWDzSHCfX-1-768x819.png 768w\" sizes=\"auto, (max-width: 1000px) 100vw, 1000px\" \/><\/figure>\n<p class=\"wp-block-paragraph\"><strong><a href=\"https:\/\/medium.com\/towards-data-science\/deep-learning-illustrated-part-3-convolutional-neural-networks-96b900b0b9e0#52f5\">4 &#8211; Hidden Layer<\/a><\/strong>: A fully connected hidden layer with three neurons all with ReLU activation functions<\/p>\n<p class=\"wp-block-paragraph\"><strong><a href=\"https:\/\/medium.com\/towards-data-science\/deep-learning-illustrated-part-3-convolutional-neural-networks-96b900b0b9e0#52f5\">5 &#8211; Output Layer<\/a><\/strong>: A single neuron with a sigmoid activation function<\/p>\n<figure class=\"wp-block-image size-large\"><img data-dominant-color=\"edeae7\" data-has-transparency=\"true\" style=\"--dominant-color: #edeae7;\" loading=\"lazy\" decoding=\"async\" width=\"1000\" height=\"1047\" class=\"wp-image-310250 has-transparency\" src=\"https:\/\/towardsdatascience.com\/wp-content\/uploads\/2024\/08\/0rqPdzicqP9XaDGvB-1.png\" alt=\"\" srcset=\"https:\/\/towardsdatascience.com\/wp-content\/uploads\/2024\/08\/0rqPdzicqP9XaDGvB-1.png 1000w, https:\/\/towardsdatascience.com\/wp-content\/uploads\/2024\/08\/0rqPdzicqP9XaDGvB-1-287x300.png 287w, https:\/\/towardsdatascience.com\/wp-content\/uploads\/2024\/08\/0rqPdzicqP9XaDGvB-1-978x1024.png 978w, https:\/\/towardsdatascience.com\/wp-content\/uploads\/2024\/08\/0rqPdzicqP9XaDGvB-1-768x804.png 768w\" sizes=\"auto, (max-width: 1000px) 100vw, 1000px\" \/><\/figure>\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-python\">model = Sequential([\n    # 1 - Convolutional Layer\n    Conv2D(4, (3, 3), activation=&#039;relu&#039;, input_shape=(5, 5, 1)),\n    # 2 - Max-Pooling Layer\n    MaxPooling2D(pool_size=(2, 2)),\n    # 3 - Flatten Layer\n    Flatten(),\n    # 4 - Hidden Layer\n    Dense(3, activation=&#039;relu&#039;),\n    # 5 - Output Layer\n    Dense(1, activation=&#039;sigmoid&#039;)\n])<\/code><\/pre>\n<h3 class=\"wp-block-heading\">Step 5: Compiling the Model<\/h3>\n<p class=\"wp-block-paragraph\">Compiling the model is crucial as it defines how the model will learn. Here&#8217;s what each part of the compile function does:<\/p>\n<ol class=\"wp-block-list\">\n<li><strong><a href=\"https:\/\/www.tensorflow.org\/api_docs\/python\/tf\/keras\/optimizers\">Optimizer (Adam)<\/a><\/strong>: The optimizer adjusts the model&#8217;s weights to minimize the loss function. We use <strong>Adam<\/strong> here, but <a href=\"https:\/\/medium.com\/r?url=https%3A%2F%2Fwww.tensorflow.org%2Fapi_docs%2Fpython%2Ftf%2Fkeras%2Foptimizers\">this is a list<\/a> of other optimizers that can be used.<\/li>\n<li><strong><a href=\"https:\/\/www.tensorflow.org\/api_docs\/python\/tf\/keras\/losses\">Loss Function (Binary Crossentropy)<\/a><\/strong>: Measures how far off the model&#8217;s predictions are from the actual results. Since we&#8217;re dealing with binary classification (X or not-X), <strong><a href=\"https:\/\/arize.com\/blog-course\/binary-cross-entropy-log-loss\/\">binary cross entropy<\/a><\/strong> is appropriate. The loss value is what the model tries to minimize during training. Lower loss values indicate better performance (i.e., the model&#8217;s predictions are closer to the actual values). The loss directly influences how the model is trained and the optimizer uses it to update the model&#8217;s weights in the direction that minimizes the loss.<\/li>\n<li><strong><a href=\"https:\/\/www.tensorflow.org\/api_docs\/python\/tf\/keras\/metrics\">Metrics (Accuracy)<\/a><\/strong>: Metrics are used to evaluate the model&#8217;s performance. We use <strong>accuracy<\/strong> to track the proportion of correct predictions out of all predictions made. <em>Metrics are additional measures that evaluate the performance of the model but are not used by the optimizer to adjust the model during training<\/em>. They provide a way to assess how well the model is performing.<\/li>\n<\/ol>\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p><strong>Note<\/strong>: While accuracy is a common metric, it&#8217;s not always the most reliable, especially in certain scenarios like imbalanced datasets. However, for simplicity, we&#8217;ll use it here. If you&#8217;re interested in exploring other evaluation metrics that might provide a more nuanced view of model performance, this <a href=\"https:\/\/medium.com\/towards-data-science\/machine-learning-illustrated-classification-evaluation-metrics-dfc33b373c43\">article<\/a> covers some alternatives.<\/p><\/blockquote>\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-python\">model.compile(optimizer=Adam(), loss=&#039;binary_crossentropy&#039;, metrics=[&#039;accuracy&#039;])<\/code><\/pre>\n<h3 class=\"wp-block-heading\">Step 6: Training the Model<\/h3>\n<p class=\"wp-block-paragraph\">Training the model involves feeding it the training data multiple times (epochs) so it can learn to make better predictions. An epoch is one complete pass through the entire training dataset. We&#8217;ll train our model for 10 epochs:<\/p>\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-python\">history = model.fit(x_train, y_train, epochs=10, validation_data=(x_test, y_test))<\/code><\/pre>\n<figure class=\"wp-block-image size-large\"><img data-dominant-color=\"434544\" data-has-transparency=\"false\" style=\"--dominant-color: #434544;\" loading=\"lazy\" decoding=\"async\" width=\"1972\" height=\"724\" class=\"wp-image-310255 not-transparent\" src=\"https:\/\/towardsdatascience.com\/wp-content\/uploads\/2024\/08\/1CQrGjkUB5lglE2s8buarWg-1.png\" alt=\"\" srcset=\"https:\/\/towardsdatascience.com\/wp-content\/uploads\/2024\/08\/1CQrGjkUB5lglE2s8buarWg-1.png 1972w, https:\/\/towardsdatascience.com\/wp-content\/uploads\/2024\/08\/1CQrGjkUB5lglE2s8buarWg-1-300x110.png 300w, https:\/\/towardsdatascience.com\/wp-content\/uploads\/2024\/08\/1CQrGjkUB5lglE2s8buarWg-1-1024x376.png 1024w, https:\/\/towardsdatascience.com\/wp-content\/uploads\/2024\/08\/1CQrGjkUB5lglE2s8buarWg-1-768x282.png 768w, https:\/\/towardsdatascience.com\/wp-content\/uploads\/2024\/08\/1CQrGjkUB5lglE2s8buarWg-1-1536x564.png 1536w\" sizes=\"auto, (max-width: 1972px) 100vw, 1972px\" \/><\/figure>\n<h3 class=\"wp-block-heading\">Step 7: Evaluating the Model<\/h3>\n<p class=\"wp-block-paragraph\">After training, we evaluate the model&#8217;s performance on the test data:<\/p>\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-python\">loss, accuracy = model.evaluate(x_test, y_test)\nprint(f&#039;Test Accuracy: {accuracy * 100:.2f}%&#039;)<\/code><\/pre>\n<figure class=\"wp-block-image size-large\"><img data-dominant-color=\"434444\" data-has-transparency=\"false\" style=\"--dominant-color: #434444;\" loading=\"lazy\" decoding=\"async\" width=\"1206\" height=\"92\" class=\"wp-image-310261 not-transparent\" src=\"https:\/\/towardsdatascience.com\/wp-content\/uploads\/2024\/08\/1lIBR0b11G67hosoZ6C7taQ-1.png\" alt=\"\" srcset=\"https:\/\/towardsdatascience.com\/wp-content\/uploads\/2024\/08\/1lIBR0b11G67hosoZ6C7taQ-1.png 1206w, https:\/\/towardsdatascience.com\/wp-content\/uploads\/2024\/08\/1lIBR0b11G67hosoZ6C7taQ-1-300x23.png 300w, https:\/\/towardsdatascience.com\/wp-content\/uploads\/2024\/08\/1lIBR0b11G67hosoZ6C7taQ-1-1024x78.png 1024w, https:\/\/towardsdatascience.com\/wp-content\/uploads\/2024\/08\/1lIBR0b11G67hosoZ6C7taQ-1-768x59.png 768w\" sizes=\"auto, (max-width: 1206px) 100vw, 1206px\" \/><\/figure>\n<p class=\"wp-block-paragraph\">The accuracy metric tells us that 94.8% of the images in the test data were correctly classified.<\/p>\n<h3 class=\"wp-block-heading\">Step 8: Visualizing the Training Process<\/h3>\n<p class=\"wp-block-paragraph\">Finally, let&#8217;s visualize how the model&#8217;s accuracy changed over the epochs. This helps us understand how well the model learned during training:<\/p>\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-python\">plt.plot(history.history[&#039;accuracy&#039;], label=&#039;accuracy&#039;)\nplt.plot(history.history[&#039;val_accuracy&#039;], label=&#039;val_accuracy&#039;)\nplt.xlabel(&#039;Epoch&#039;)\nplt.ylabel(&#039;Accuracy&#039;)\nplt.legend()\nplt.show()<\/code><\/pre>\n<figure class=\"wp-block-image size-large\"><img data-dominant-color=\"faf9f9\" data-has-transparency=\"true\" style=\"--dominant-color: #faf9f9;\" loading=\"lazy\" decoding=\"async\" width=\"1124\" height=\"846\" class=\"wp-image-310264 has-transparency\" src=\"https:\/\/towardsdatascience.com\/wp-content\/uploads\/2024\/08\/1nie-KC-btBeqnv2WbyfU7w-1.png\" alt=\"\" srcset=\"https:\/\/towardsdatascience.com\/wp-content\/uploads\/2024\/08\/1nie-KC-btBeqnv2WbyfU7w-1.png 1124w, https:\/\/towardsdatascience.com\/wp-content\/uploads\/2024\/08\/1nie-KC-btBeqnv2WbyfU7w-1-300x226.png 300w, https:\/\/towardsdatascience.com\/wp-content\/uploads\/2024\/08\/1nie-KC-btBeqnv2WbyfU7w-1-1024x771.png 1024w, https:\/\/towardsdatascience.com\/wp-content\/uploads\/2024\/08\/1nie-KC-btBeqnv2WbyfU7w-1-768x578.png 768w\" sizes=\"auto, (max-width: 1124px) 100vw, 1124px\" \/><\/figure>\n<p class=\"wp-block-paragraph\">And that&#8217;s it. We&#8217;ve built a simple convolutional neural network to predict if an image is an &#8216;X&#8217; or not using TensorFlow in less than 5 minutes!<\/p>\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/medium.com\/@shreya.rao\/list\/66587055cb38\"><strong>Deep Learning in Practice<\/strong><\/a><\/p>\n<p class=\"wp-block-paragraph\">As always, feel free to connect with me on <a href=\"https:\/\/www.linkedin.com\/in\/shreyarao24\/\">LinkedIn<\/a> for any comments\/questions!<\/p>\n<p class=\"wp-block-paragraph\">Note: Unless specified, all images are by the author.<\/p>\n<\/blockquote>","protected":false},"excerpt":{"rendered":"<p>Step-by-step code guide to building a Convolutional Neural Network<\/p>\n","protected":false},"author":18,"featured_media":3483,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"is_member_only":true,"sub_heading":"Step-by-step code guide to building a Convolutional Neural Network","footnotes":""},"categories":[17,44,20,22],"tags":[447,448,445,446,449],"sponsor":[],"coauthors":[31488],"class_list":["post-3482","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-artificial-intelligence","category-data-science","category-deep-learning","category-machine-learning","tag-artificial-intelligence","tag-data-science","tag-deep-learning","tag-machine-learning","tag-neural-networks"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Implementing Convolutional Neural Networks in TensorFlow | Towards Data Science<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/towardsdatascience.com\/implementing-convolutional-neural-networks-in-tensorflow-bc1c4f00bd34\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Implementing Convolutional Neural Networks in TensorFlow | Towards Data Science\" \/>\n<meta property=\"og:description\" content=\"Step-by-step code guide to building a Convolutional Neural Network\" \/>\n<meta property=\"og:url\" content=\"https:\/\/towardsdatascience.com\/implementing-convolutional-neural-networks-in-tensorflow-bc1c4f00bd34\/\" \/>\n<meta property=\"og:site_name\" content=\"Towards Data Science\" \/>\n<meta property=\"article:published_time\" content=\"2024-08-20T17:12:30+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-01-08T15:32:23+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/towardsdatascience.com\/wp-content\/uploads\/2024\/08\/0c09RmbCCpfjAbSMq.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1400\" \/>\n\t<meta property=\"og:image:height\" content=\"644\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Shreya Rao\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@TDataScience\" \/>\n<meta name=\"twitter:site\" content=\"@TDataScience\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Shreya Rao\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"6 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/towardsdatascience.com\/implementing-convolutional-neural-networks-in-tensorflow-bc1c4f00bd34\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/towardsdatascience.com\/implementing-convolutional-neural-networks-in-tensorflow-bc1c4f00bd34\/\"},\"author\":{\"name\":\"TDS Editors\",\"@id\":\"https:\/\/towardsdatascience.com\/#\/schema\/person\/f9925d336b6fe962b03ad8281d90b8ee\"},\"headline\":\"Implementing Convolutional Neural Networks in TensorFlow\",\"datePublished\":\"2024-08-20T17:12:30+00:00\",\"dateModified\":\"2025-01-08T15:32:23+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/towardsdatascience.com\/implementing-convolutional-neural-networks-in-tensorflow-bc1c4f00bd34\/\"},\"wordCount\":985,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/towardsdatascience.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/towardsdatascience.com\/implementing-convolutional-neural-networks-in-tensorflow-bc1c4f00bd34\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/towardsdatascience.com\/wp-content\/uploads\/2024\/08\/0c09RmbCCpfjAbSMq.png\",\"keywords\":[\"Artificial Intelligence\",\"Data Science\",\"Deep Learning\",\"Machine Learning\",\"Neural Networks\"],\"articleSection\":[\"Artificial Intelligence\",\"Data Science\",\"Deep Learning\",\"Machine Learning\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/towardsdatascience.com\/implementing-convolutional-neural-networks-in-tensorflow-bc1c4f00bd34\/\",\"url\":\"https:\/\/towardsdatascience.com\/implementing-convolutional-neural-networks-in-tensorflow-bc1c4f00bd34\/\",\"name\":\"Implementing Convolutional Neural Networks in TensorFlow | Towards Data Science\",\"isPartOf\":{\"@id\":\"https:\/\/towardsdatascience.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/towardsdatascience.com\/implementing-convolutional-neural-networks-in-tensorflow-bc1c4f00bd34\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/towardsdatascience.com\/implementing-convolutional-neural-networks-in-tensorflow-bc1c4f00bd34\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/towardsdatascience.com\/wp-content\/uploads\/2024\/08\/0c09RmbCCpfjAbSMq.png\",\"datePublished\":\"2024-08-20T17:12:30+00:00\",\"dateModified\":\"2025-01-08T15:32:23+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/towardsdatascience.com\/implementing-convolutional-neural-networks-in-tensorflow-bc1c4f00bd34\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/towardsdatascience.com\/implementing-convolutional-neural-networks-in-tensorflow-bc1c4f00bd34\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/towardsdatascience.com\/implementing-convolutional-neural-networks-in-tensorflow-bc1c4f00bd34\/#primaryimage\",\"url\":\"https:\/\/towardsdatascience.com\/wp-content\/uploads\/2024\/08\/0c09RmbCCpfjAbSMq.png\",\"contentUrl\":\"https:\/\/towardsdatascience.com\/wp-content\/uploads\/2024\/08\/0c09RmbCCpfjAbSMq.png\",\"width\":1400,\"height\":644},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/towardsdatascience.com\/implementing-convolutional-neural-networks-in-tensorflow-bc1c4f00bd34\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/towardsdatascience.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Implementing Convolutional Neural Networks in TensorFlow\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/towardsdatascience.com\/#website\",\"url\":\"https:\/\/towardsdatascience.com\/\",\"name\":\"Towards Data Science\",\"description\":\"Publish AI, ML &amp; data-science insights to a global community of data professionals.\",\"publisher\":{\"@id\":\"https:\/\/towardsdatascience.com\/#organization\"},\"alternateName\":\"TDS\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/towardsdatascience.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/towardsdatascience.com\/#organization\",\"name\":\"Towards Data Science\",\"alternateName\":\"TDS\",\"url\":\"https:\/\/towardsdatascience.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/towardsdatascience.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/towardsdatascience.com\/wp-content\/uploads\/2025\/02\/tds-logo.jpg\",\"contentUrl\":\"https:\/\/towardsdatascience.com\/wp-content\/uploads\/2025\/02\/tds-logo.jpg\",\"width\":696,\"height\":696,\"caption\":\"Towards Data Science\"},\"image\":{\"@id\":\"https:\/\/towardsdatascience.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/x.com\/TDataScience\",\"https:\/\/www.youtube.com\/c\/TowardsDataScience\",\"https:\/\/www.linkedin.com\/company\/towards-data-science\/\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/towardsdatascience.com\/#\/schema\/person\/f9925d336b6fe962b03ad8281d90b8ee\",\"name\":\"TDS Editors\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/towardsdatascience.com\/#\/schema\/person\/image\/23494c9101089ad44ae88ce9d2f56aac\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/?s=96&d=mm&r=g\",\"caption\":\"TDS Editors\"},\"description\":\"Building a vibrant data science and machine learning community. Share your insights and projects with our global audience: bit.ly\/write-for-tds\",\"url\":\"https:\/\/towardsdatascience.com\/author\/towardsdatascience\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Implementing Convolutional Neural Networks in TensorFlow | Towards Data Science","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/towardsdatascience.com\/implementing-convolutional-neural-networks-in-tensorflow-bc1c4f00bd34\/","og_locale":"en_US","og_type":"article","og_title":"Implementing Convolutional Neural Networks in TensorFlow | Towards Data Science","og_description":"Step-by-step code guide to building a Convolutional Neural Network","og_url":"https:\/\/towardsdatascience.com\/implementing-convolutional-neural-networks-in-tensorflow-bc1c4f00bd34\/","og_site_name":"Towards Data Science","article_published_time":"2024-08-20T17:12:30+00:00","article_modified_time":"2025-01-08T15:32:23+00:00","og_image":[{"width":1400,"height":644,"url":"https:\/\/towardsdatascience.com\/wp-content\/uploads\/2024\/08\/0c09RmbCCpfjAbSMq.png","type":"image\/png"}],"author":"Shreya Rao","twitter_card":"summary_large_image","twitter_creator":"@TDataScience","twitter_site":"@TDataScience","twitter_misc":{"Written by":"Shreya Rao","Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/towardsdatascience.com\/implementing-convolutional-neural-networks-in-tensorflow-bc1c4f00bd34\/#article","isPartOf":{"@id":"https:\/\/towardsdatascience.com\/implementing-convolutional-neural-networks-in-tensorflow-bc1c4f00bd34\/"},"author":{"name":"TDS Editors","@id":"https:\/\/towardsdatascience.com\/#\/schema\/person\/f9925d336b6fe962b03ad8281d90b8ee"},"headline":"Implementing Convolutional Neural Networks in TensorFlow","datePublished":"2024-08-20T17:12:30+00:00","dateModified":"2025-01-08T15:32:23+00:00","mainEntityOfPage":{"@id":"https:\/\/towardsdatascience.com\/implementing-convolutional-neural-networks-in-tensorflow-bc1c4f00bd34\/"},"wordCount":985,"commentCount":0,"publisher":{"@id":"https:\/\/towardsdatascience.com\/#organization"},"image":{"@id":"https:\/\/towardsdatascience.com\/implementing-convolutional-neural-networks-in-tensorflow-bc1c4f00bd34\/#primaryimage"},"thumbnailUrl":"https:\/\/towardsdatascience.com\/wp-content\/uploads\/2024\/08\/0c09RmbCCpfjAbSMq.png","keywords":["Artificial Intelligence","Data Science","Deep Learning","Machine Learning","Neural Networks"],"articleSection":["Artificial Intelligence","Data Science","Deep Learning","Machine Learning"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/towardsdatascience.com\/implementing-convolutional-neural-networks-in-tensorflow-bc1c4f00bd34\/","url":"https:\/\/towardsdatascience.com\/implementing-convolutional-neural-networks-in-tensorflow-bc1c4f00bd34\/","name":"Implementing Convolutional Neural Networks in TensorFlow | Towards Data Science","isPartOf":{"@id":"https:\/\/towardsdatascience.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/towardsdatascience.com\/implementing-convolutional-neural-networks-in-tensorflow-bc1c4f00bd34\/#primaryimage"},"image":{"@id":"https:\/\/towardsdatascience.com\/implementing-convolutional-neural-networks-in-tensorflow-bc1c4f00bd34\/#primaryimage"},"thumbnailUrl":"https:\/\/towardsdatascience.com\/wp-content\/uploads\/2024\/08\/0c09RmbCCpfjAbSMq.png","datePublished":"2024-08-20T17:12:30+00:00","dateModified":"2025-01-08T15:32:23+00:00","breadcrumb":{"@id":"https:\/\/towardsdatascience.com\/implementing-convolutional-neural-networks-in-tensorflow-bc1c4f00bd34\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/towardsdatascience.com\/implementing-convolutional-neural-networks-in-tensorflow-bc1c4f00bd34\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/towardsdatascience.com\/implementing-convolutional-neural-networks-in-tensorflow-bc1c4f00bd34\/#primaryimage","url":"https:\/\/towardsdatascience.com\/wp-content\/uploads\/2024\/08\/0c09RmbCCpfjAbSMq.png","contentUrl":"https:\/\/towardsdatascience.com\/wp-content\/uploads\/2024\/08\/0c09RmbCCpfjAbSMq.png","width":1400,"height":644},{"@type":"BreadcrumbList","@id":"https:\/\/towardsdatascience.com\/implementing-convolutional-neural-networks-in-tensorflow-bc1c4f00bd34\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/towardsdatascience.com\/"},{"@type":"ListItem","position":2,"name":"Implementing Convolutional Neural Networks in TensorFlow"}]},{"@type":"WebSite","@id":"https:\/\/towardsdatascience.com\/#website","url":"https:\/\/towardsdatascience.com\/","name":"Towards Data Science","description":"Publish AI, ML &amp; data-science insights to a global community of data professionals.","publisher":{"@id":"https:\/\/towardsdatascience.com\/#organization"},"alternateName":"TDS","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/towardsdatascience.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/towardsdatascience.com\/#organization","name":"Towards Data Science","alternateName":"TDS","url":"https:\/\/towardsdatascience.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/towardsdatascience.com\/#\/schema\/logo\/image\/","url":"https:\/\/towardsdatascience.com\/wp-content\/uploads\/2025\/02\/tds-logo.jpg","contentUrl":"https:\/\/towardsdatascience.com\/wp-content\/uploads\/2025\/02\/tds-logo.jpg","width":696,"height":696,"caption":"Towards Data Science"},"image":{"@id":"https:\/\/towardsdatascience.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/x.com\/TDataScience","https:\/\/www.youtube.com\/c\/TowardsDataScience","https:\/\/www.linkedin.com\/company\/towards-data-science\/"]},{"@type":"Person","@id":"https:\/\/towardsdatascience.com\/#\/schema\/person\/f9925d336b6fe962b03ad8281d90b8ee","name":"TDS Editors","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/towardsdatascience.com\/#\/schema\/person\/image\/23494c9101089ad44ae88ce9d2f56aac","url":"https:\/\/secure.gravatar.com\/avatar\/?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/?s=96&d=mm&r=g","caption":"TDS Editors"},"description":"Building a vibrant data science and machine learning community. Share your insights and projects with our global audience: bit.ly\/write-for-tds","url":"https:\/\/towardsdatascience.com\/author\/towardsdatascience\/"}]}},"distributor_meta":false,"distributor_terms":false,"distributor_media":false,"distributor_original_site_name":"Towards Data Science","distributor_original_site_url":"https:\/\/towardsdatascience.com","push-errors":false,"_links":{"self":[{"href":"https:\/\/towardsdatascience.com\/wp-json\/wp\/v2\/posts\/3482","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/towardsdatascience.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/towardsdatascience.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/towardsdatascience.com\/wp-json\/wp\/v2\/users\/18"}],"replies":[{"embeddable":true,"href":"https:\/\/towardsdatascience.com\/wp-json\/wp\/v2\/comments?post=3482"}],"version-history":[{"count":0,"href":"https:\/\/towardsdatascience.com\/wp-json\/wp\/v2\/posts\/3482\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/towardsdatascience.com\/wp-json\/wp\/v2\/media\/3483"}],"wp:attachment":[{"href":"https:\/\/towardsdatascience.com\/wp-json\/wp\/v2\/media?parent=3482"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/towardsdatascience.com\/wp-json\/wp\/v2\/categories?post=3482"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/towardsdatascience.com\/wp-json\/wp\/v2\/tags?post=3482"},{"taxonomy":"sponsor","embeddable":true,"href":"https:\/\/towardsdatascience.com\/wp-json\/wp\/v2\/sponsor?post=3482"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/towardsdatascience.com\/wp-json\/wp\/v2\/coauthors?post=3482"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}