How it works...

The keras library in R provides various datasets, using which we can develop deep learning models. In step 1, we imported the Fashion-MNIST dataset, using the dataset_fashion_mnist() function and checked the dimensions of its training and testing partitions. We also looked at the data and label of a sample image. Next, we defined the label names for the data and visualized one sample image for each label. 

Base R graphics provide functions to plot interesting plots. The par() function is used to set various graphical parameters, and the image() function creates a grid of colored or grayscale rectangles with colors corresponding to the values in the image matrix.

In step 2, we reshaped our data and normalized it within the range of 0 to 1. We also one-hot encoded the target label matrix using the to_categorical() function. After we completed the data preparation, in step 3, we configured our CNN model and looked at its summary. In the model configuration, we added two convolutional layers with 8 and 16 filters of sizes 4 × 4 and 3 × 3 respectively, and each layer used the ReLU activation function.

Next, we used layer_flatten() to convert the output matrix of the convolutional layer into a linear array, which gets fed as an input into the nodes of our dense neural network. Our dense network contained a hidden layer with 16 units and an output layer with 10 units because we had 10 target labels. Next, we looked at the summary of the model; it gave us the information about output shape and number of parameters in each layer. The following are the formulas to calculate these for the convolutional layer:

  • The output shape of each layer: If the input to our convolutional layer is  and we apply  filters of , then the output shape is given by the following formula:
  • The number of parameters in each layer can be calculated by the following formula: 

After the model configuration, we compiled the model using a stochastic gradient descent optimizer and a categorical cross-entropy loss function. Next, we trained the model. Lastly, in step 4, we evaluated the performance of the model on the testing dataset and printed the evaluation metrics and generated predictions for the testing dataset.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset