Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Data Processing As you could see in the above plot, the images are grayscale images have pixel values that range from 0 to 2 5
Data Processing
As you could see in the above plot, the images are grayscale images have pixel values that range from to Also, these images have a dimension of x As a result, you'll need to preprocess the data before you feed it into the model.
As a first step, convert each x image of the train and test set into a matrix of size x x which is fed into the network.
#Reshape data
trainX trainXreshape
testX testXreshape
trainXshape, testXshape
Print the shape of the data to determine its size and print the output here. mark
The data right now is in an int format, so before you feed it into the network you need to convert its type to float and you also have to rescale the pixel values in range inclusive. So let's do that!
#Normalize data between and
trainX trainXastypefloat
testX testXastypefloat
trainX trainX
testX testX
In onehot encoding, you convert the categorical data into a vector of numbers. The reason why you convert the categorical data in one hot encoding is that machine learning algorithms cannot work with categorical data directly. You generate one boolean column for each category or class. Only one of these columns could take on the value for each sample. Hence, the term onehot encoding.
For your problem statement, the one hot encoding will be a row vector, and for each image, it will have a dimension of x The important thing to note here is that the vector consists of all zeros except for the class that it represents, and for that, it is For example, the ankle boot image that you plotted above has a label of so for all the ankle boot images, the one hot encoding vector would be
So let's convert the training and testing labels into onehot encoding vectors:
# Change the labels from categorical to onehot encoding
trainYonehot tocategoricaltrainY
testYonehot tocategoricaltestY
Please print the output of above given commands Mark
Splitting the data into training and validation sets
This next step is a crucial one. In machine learning or any data specific task, you should partition the data correctly. For the model to generalize well, you split the training data into two parts, one designed for training and another one for validation. In this case, you will train the model on of the training data and validate it on of the remaining training data. This will also help to reduce overfitting since you will be validating the model on the data it would not have seen in training phase, which will help in boosting the test performance.
#Split the training and testing data into and configuration
trainXvalidXtrainlabel,validlabel traintestsplittrainX trainYonehot, testsize randomstate
Building the Deep Neural Network
The images are of size x You convert the image matrix to an array, rescale it between and reshape it so that it's of size x x and feed this as an input to the network.
You'll use three convolutional layers:
The first layer will have x filters,
The second layer will have x filters and
The third layer will have x filters.
In addition, there are three maxpooling layers each of size x You will use a batch size of using a higher batch size of or is also preferable it all depends on the memory. It contributes massively to determining the learning parameters and affects the prediction accuracy. You will train the network for epochs.
cnn tfkeras.models.Sequential
batchsize
epochs
numclasses
In Keras, you can just stack up layers by adding the desired layer one by one. That's exactly what you'll do here: you'll first add a first convolutional layer with ConvD Note that you use this function because you're working with images! Next, you add the Leaky ReLU activation function which helps the network learn nonlinear decision boundaries. Since you have ten different classes, you'll need a nonlinear decision boundary that could separate these ten classes which are not linearly separable.
More specifically, you add Leaky ReLUs because they attempt to fix the problem of dying Rectified Linear Units ReLUs The ReLU activation function is used a lot in neural network architectures and more specifically in convolutional networks, where it has proven to be more effective than the widely used logistic sigmoid function. As of this activation function is the most popular one for deep neural networks. The ReLU function allows the activation to be thresholded at zero. However, during the training, ReLU units can "die". This can happen when a large gradient flows through a ReLU neuron: it can cause the weights to update in such a way that the neuron will never activate on any data point again. If this happens, then the gradient flowing th
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started