# Convolutional Neural Network

## Preliminaries

```
import numpy as np
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten
from keras.layers.convolutional import Conv2D, MaxPooling2D
from keras.utils import np_utils
from keras import backend as K
/* Set that the color channel value will be first */
K.set_image_data_format('channels_first')
/* Set seed */
np.random.seed(0)
```

```
Using TensorFlow backend.
```

## Load MNIST Image Data

```
/* Set image information */
channels = 1
height = 28
width = 28
/* Load data and target from MNIST data */
(train_data, train_target), (test_data, test_target) = mnist.load_data()
/* Reshape training image data into features */
train_data = train_data.reshape(train_data.shape[0], channels, height, width)
/* Reshape test image data into features */
test_data = test_data.reshape(test_data.shape[0], channels, height, width)
/* Rescale pixel intensity to between 0 and 1 */
train_features = train_data / 255
test_features = test_data / 255
/* One-hot encode target */
train_target = np_utils.to_categorical(train_target)
test_target = np_utils.to_categorical(test_target)
number_of_classes = test_target.shape[1]
```

## Create Convolutional Neural Network Architecture

Convolutional neural networks (also called ConvNets) are a popular type of network that has proven very effective at computer vision (e.g. recognizing cats, dogs, planes, and even hot dogs). It is completely possible to use feedforward neural networks on images, where each pixel is a feature. However, when doing so we run into two major problems.

First, a feedforward neural networks do not take into account the spatial structure of the pixels. For example, in a 10×10 pixel image we might convert it into a vector of 100 pixel features, and in this case feedforward would consider the first feature (e.g. pixel value) to have the same relationship with the 10th feature as the 11th feature. However, in reality the 10th feature represents a pixel on the far side of the image as the first feature, while the 11th feature represents the pixel immediately below the first pixel.

Second, and relatedly, feedforward neural networks learn global relationships in the features instead of local patterns. In more practical terms, this means that feedforward neural networks are not able to detect an object regardless of where it appears in an image. For example, imagine we are training a neural network to recognize faces, these faces might appear anywhere in the image from the upper right to the middle to the lower left. The power of convolutional neural networks is their ability handle both of these issues (and others).

```
/* Start neural network */
network = Sequential()
/* Add convolutional layer with 64 filters, a 5x5 window, and ReLU activation function */
network.add(Conv2D(filters=64, kernel_size=(5, 5), input_shape=(channels, width, height), activation='relu'))
/* Add max pooling layer with a 2x2 window */
network.add(MaxPooling2D(pool_size=(2, 2)))
/* Add dropout layer */
network.add(Dropout(0.5))
/* Add layer to flatten input */
network.add(Flatten())
/* Add fully connected layer of 128 units with a ReLU activation function */
network.add(Dense(128, activation='relu'))
/* Add dropout layer */
network.add(Dropout(0.5))
/* Add fully connected layer with a softmax activation function */
network.add(Dense(number_of_classes, activation='softmax'))
```

## Compile Convolutional Neural Network

```
/* Compile neural network */
network.compile(loss='categorical_crossentropy', # Cross-entropy
optimizer='rmsprop', # Root Mean Square Propagation
metrics=['accuracy']) # Accuracy performance metric
```

## Train Convolutional Neural Network

```
/* Train neural network */
network.fit(train_features, # Features
train_target, # Target
epochs=2, # Number of epochs
verbose=0, # Don't print description after each epoch
batch_size=1000, # Number of observations per batch
validation_data=(test_features, test_target)) # Data for evaluation
```

`<keras.callbacks.History at 0x103f9b8d0>`

# Python Example for Beginners

## Two Machine Learning Fields

There are two sides to machine learning:

**Practical Machine Learning:**This is about querying databases, cleaning data, writing scripts to transform data and gluing algorithm and libraries together and writing custom code to squeeze reliable answers from data to satisfy difficult and ill defined questions. It’s the mess of reality.**Theoretical Machine Learning**: This is about math and abstraction and idealized scenarios and limits and beauty and informing what is possible. It is a whole lot neater and cleaner and removed from the mess of reality.

**Data Science Resources: Data Science Recipes and Applied Machine Learning Recipes**

**Introduction to Applied Machine Learning & Data Science for Beginners, Business Analysts, Students, Researchers and Freelancers with Python & R Codes @ Western Australian Center for Applied Machine Learning & Data Science (WACAMLDS) !!!**

Latest end-to-end Learn by Coding Recipes in Project-Based Learning:

**Applied Statistics with R for Beginners and Business Professionals**

**Data Science and Machine Learning Projects in Python: Tabular Data Analytics**

**Data Science and Machine Learning Projects in R: Tabular Data Analytics**

**Python Machine Learning & Data Science Recipes: Learn by Coding**

**R Machine Learning & Data Science Recipes: Learn by Coding**

**Comparing Different Machine Learning Algorithms in Python for Classification (FREE)**

Disclaimer: The information and code presented within this recipe/tutorial is only for educational and coaching purposes for beginners and developers. Anyone can practice and apply the recipe/tutorial presented here, but the reader is taking full responsibility for his/her actions. The author (content curator) of this recipe (code / program) has made every effort to ensure the accuracy of the information was correct at time of publication. The author (content curator) does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause.The information presented here could also be found in public knowledge domains.

**Google –> SETScholars**

A list of Python, R and SQL Codes for Applied Machine Learning and Data Science at **https://setscholars.net/**Learn by Coding Categories:

- Classification:
**https://setscholars.net/category/classification/** - Data Analytics:
**https://setscholars.net/category/data-analytics/** - Data Science:
**https://setscholars.net/category/data-science/** - Data Visualisation:
**https://setscholars.net/category/data-visualisation/** - Machine Learning Recipe:
**https://setscholars.net/category/machine-learning-recipe/** - Pandas:
**https://setscholars.net/category/pandas/** - Python:
**https://setscholars.net/category/python/** - SKLEARN:
**https://setscholars.net/category/sklearn/** - Supervised Learning:
**https://setscholars.net/category/supervised-learning/** - Tabular Data Analytics:
**https://setscholars.net/category/tabular-data-analytics/** - End-to-End Data Science Recipes:
**https://setscholars.net/category/a-star-data-science-recipe/** - Applied Statistics:
**https://setscholars.net/category/applied-statistics/** - Bagging Ensemble:
**https://setscholars.net/category/bagging-ensemble/** - Boosting Ensemble:
**https://setscholars.net/category/boosting-ensemble/** - CatBoost:
**https://setscholars.net/category/catboost/** - Clustering:
**https://setscholars.net/category/clustering/** - Data Analytics:
**https://setscholars.net/category/data-analytics/** - Data Science:
**https://setscholars.net/category/data-science/** - Data Visualisation:
**https://setscholars.net/category/data-visualisation/** - Decision Tree:
**https://setscholars.net/category/decision-tree/** - LightGBM:
**https://setscholars.net/category/lightgbm/** - Machine Learning Recipe:
**https://setscholars.net/category/machine-learning-recipe/** - Multi-Class Classification:
**https://setscholars.net/category/multi-class-classification/** - Neural Networks:
**https://setscholars.net/category/neural-networks/** - Python Machine Learning:
**https://setscholars.net/category/python-machine-learning/** - Python Machine Learning Crash Course:
**https://setscholars.net/category/python-machine-learning-crash-course/** - R Classification:
**https://setscholars.net/category/r-classification/** - R for Beginners:
**https://setscholars.net/category/r-for-beginners/** - R for Business Analytics:
**https://setscholars.net/category/r-for-business-analytics/** - R for Data Science:
**https://setscholars.net/category/r-for-data-science/** - R for Data Visualisation:
**https://setscholars.net/category/r-for-data-visualisation/** - R for Excel Users:
**https://setscholars.net/category/r-for-excel-users/** - R Machine Learning:
**https://setscholars.net/category/r-machine-learning/** - R Machine Learning Crash Course:
**https://setscholars.net/category/r-machine-learning-crash-course/** - R Regression:
**https://setscholars.net/category/r-regression/** - Regression:
**https://setscholars.net/category/regression/** - XGBOOST:
**https://setscholars.net/category/xgboost/** - Excel examples for beginners:
**https://setscholars.net/category/excel-examples-for-beginners/** - C Programming tutorials & examples:
**https://setscholars.net/category/c-programming-tutorials/** - Javascript tutorials & examples:
**https://setscholars.net/category/javascript-tutorials-and-examples/** - Python tutorials & examples:
**https://setscholars.net/category/python-tutorials/** - R tutorials & examples:
**https://setscholars.net/category/r-for-beginners/** - SQL tutorials & examples:
**https://setscholars.net/category/sql-tutorials-for-business-analyst/**

**FREE downloadable Mathematics Worksheet for Kids**)

- Year 1 Mathematics Worksheet:
**https://setscholars.net/category/year-1-mathematics-worksheet/**