Friday, December 20, 2019

How to build a simple Deep Neural Network using Keras?

It is very easy to build a simple Deep Neural Network using Keras, it requires three things to build it, they are: 

1. A Dataset for loading and scaling it.
2. Layers to build the Model.
3. Activation functions and Model class.

after building the Model, we need to compile it with an optimizer and loss function. Now the Model is ready for training with dataset. Next, we will test the model to evaluate it.

we will see in detail of all these steps.

1. Dataset - ( CIFAR-10 )

For this model we are using CIFAR-10 dataset,it is used for training the Model. Our Deep Neural Network has a input layer with hidden dense layers and output layer, with this Neural Network we can make predictions on a new dataset, this is a supervised learning method.


Loading the Dataset for scaling:

Actually images are numpy arrays, so it is required to import numpy package at the beginning. And import the keras dataset package to import CIFAR-10 dataset.

Now we will load CIFAR-10 dataset:

here, x_train and x_test are input datasets for training and testing, they are numpy arrays of shape [50000, 32, 32, 3] and [10000, 32, 32, 3] respectively. 

It’s worth noting the shape of the image data in x_train: [50000, 32, 32, 3]. The first dimension of this array references the index of the image in the dataset, the second and third relate to the size of the image, and the last is the channel (i.e., red, green, or blue, since these are RGB images). 
  
y_train and y_test are numpy arrays with shape [50000, 1] and [10000, 1] respectively, containing the integer labels in the range 0 to 9 for the class of each image.

now verify the values for : x_train


By default, image data consists of integers between 0 and 255 for each pixel channel. Similarly, x_test image data pixel values are also between 0 and 255. see below,

y_train and y_test are :


for classifying the output in 10 classes :
NUM_CLASSES is a variable for number of classes.

Neural Networks work best when each input is inside the range -1 to 1, so we need to divide by 255 to x_train and x_test pixel values.

Now check the values for x_train and x_test :

notice x_train and x_test pixel values are converted to floating point values in the range from -1 to 1, see the difference between previous values and current values.

We also need to change the integer labelling of the images to one-hot-encoded vectors of length 10. Using the following code , the new shape of y_train and y_test are therefore [50000, 10] and [10000, 10] respectively.

There are no columns or rows in this dataset; instead, this is a tensor with four dimensions. For example, if we want to know the green channel i.e, 1, and the value of the pixel in the (12, 13) position of an image index of 54, just type like this..
Like this CIFAR-10 dataset downloaded and scaled to build the model.

No comments:

Post a Comment