Classification Example with Keras CNN (Conv1D) model in Python

   The convolutional layer learns local patterns of given data in convolutional neural networks. It helps to extract the features of input data to provide the output. In this tutorial, you'll learn how to implement a convolutional layer to classify the Iris dataset in a simple way. We'll use the Conv1D layer of Keras API. The tutorial covers:
  1. Preparing the data
  2. Defining and fitting the model
  3. Predicting and accuracy check
  4. Source code listing
We'll start by loading the required libraries for this tutorial.

from keras.models import Sequential
from keras.layers import Dense, Conv1D, Flatten, MaxPooling1D
from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix
from sklearn.datasets import load_iris
from numpy import unique

Preparing the data

We'll use the Iris dataset as a target problem to classify in this tutorial. First, we'll load the dataset and check the x input dimensions.

iris = load_iris()
x, y = iris.data, iris.target
print(x.shape)
(150, 4) 

The next important step is to reshape the x input data. We'll create one-dimensional vectors from each row of x input data.

x = x.reshape(x.shape[0], x.shape[1], 1)
print(x.shape)
(150, 4, 1) 

We'll check the labels of y output data and find out the class numbers that will be defined in a model output layer.

print(unique(y))
[0 1 2] 
print(unique(y).sum())
3

Next, we'll split the data into the train and test parts.

xtrain, xtest, ytrain, ytest=train_test_split(x, y, test_size=0.15)


Defining and fitting the model

We'll define the Keras sequential model and add a one-dimensional convolutional layer. Input shape becomes as it is confirmed above (4,1). We'll add Dense, MaxPooling1D, and Flatten layers into the model. The output layer contains the number of output classes and 'softmax' activation.

model = Sequential()
model.add(Conv1D(64, 2, activation="relu", input_shape=(4,1)))
model.add(Dense(16, activation="relu"))
model.add(MaxPooling1D())
model.add(Flatten())
model.add(Dense(3, activation = 'softmax'))
model.compile(loss = 'sparse_categorical_crossentropy', 
     optimizer = "adam",               
              metrics = ['accuracy'])
model.summary()
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv1d_9 (Conv1D)            (None, 3, 64)             192       
_________________________________________________________________
dense_20 (Dense)             (None, 3, 16)             1040      
_________________________________________________________________
max_pooling1d_9 (MaxPooling1 (None, 1, 16)             0         
_________________________________________________________________
flatten_10 (Flatten)         (None, 16)                0         
_________________________________________________________________
dense_21 (Dense)             (None, 3)                 51        
=================================================================
Total params: 1,283
Trainable params: 1,283
Non-trainable params: 0
_________________________________________________________________ 


Predicting and accuracy check

We'll fit the model with train data then will check the training accuracy.

model.fit(xtrain, ytrain, batch_size=16,epochs=100, verbose=0)

acc = model.evaluate(xtrain, ytrain)
print("Loss:", acc[0], " Accuracy:", acc[1])
Loss: 0.14078762528933877  Accuracy: 0.9448818902331074 

Now we can predict the test data.

pred = model.predict(xtest)
pred_y = pred.argmax(axis=-1)

Finally, we'll check the prediction accuracy with the confusion matrix.

cm = confusion_matrix(ytest, pred_y)
print(cm)
[[5 0 0]
 [0 9 0]
 [0 0 9]] 
 
   In this tutorial, we've briefly learned how to fit and classify the Iris dataset with Keras Conv1D layer model in Python. The full source code is listed below.


Source code listing

from keras.models import Sequential
from keras.layers import Dense, Conv1D, Flatten, MaxPooling1D
from sklearn.model_selection import train_test_split
from sklearn.metrics import confusion_matrix
from sklearn.datasets import load_iris
from numpy import unique

iris = load_iris()
x, y = iris.data, iris.target
print(x.shape)

x = x.reshape(x.shape[0], x.shape[1], 1)
print(x.shape)

print(unique(y))
print(unique(y).sum())

xtrain, xtest, ytrain, ytest=train_test_split(x, y, test_size=0.15)

model = Sequential()
model.add(Conv1D(64, 2, activation="relu", input_shape=(4,1)))
model.add(Dense(16, activation="relu"))
model.add(MaxPooling1D())
model.add(Flatten())
model.add(Dense(3, activation = 'softmax'))
model.compile(loss = 'sparse_categorical_crossentropy', 
     optimizer = "adam",               
              metrics = ['accuracy'])
model.summary()
model.fit(xtrain, ytrain, batch_size=16,epochs=100, verbose=0)

acc = model.evaluate(xtrain, ytrain)
print("Loss:", acc[0], " Accuracy:", acc[1])

pred = model.predict(xtest)
pred_y = pred.argmax(axis=-1)

cm = confusion_matrix(ytest, pred_y)
print(cm)
 

13 comments:

  1. Thanks for sharing this simple but effective example.

    ReplyDelete
  2. Thanks from my side, too, worked well to help me understand the reshape preliminary of Conv1D layers.

    ReplyDelete
  3. I can't thank you enough for this clear and simple example that works and explains how to navigate the TensorFlow Conv1D API.

    ReplyDelete
  4. Dear
    Hope you are doing well
    My code running will but model is not learning at all
    Model acc is zero

    ReplyDelete
  5. very good .... this codes for Create the Confusion Matrix :

    ## Create the Confusion Matrix out of the Actual and Predicted Data.
    cm = confusion_matrix(ytest, pred_y)
    ## Print the Confusion Matrix.
    print(cm)


    ## Create the Confusion Matrix Display Object(cmd_obj). Note the
    ## alphabetical sorting order of the labels.
    cmd_obj = ConfusionMatrixDisplay(cm, display_labels=target_names)

    ## The plot() function has to be called for the sklearn visualization
    ## code to do its work and the Axes object to be created.
    cmd_obj.plot()

    ## Use the Axes attribute 'ax_' to get to the underlying Axes object.
    ## The Axes object controls the labels for the X and the Y axes. It
    ## also controls the title.
    cmd_obj.ax_.set(
    title='Sklearn Confusion Matrix with labels!!',
    xlabel='Predicted Fruits',
    ylabel='Actual Fruits')

    ## Finally, call the matplotlib show() function to display the visualization
    ## of the Confusion Matrix.
    plt.show()

    ReplyDelete
  6. In between Convolution and Pooling layers, what is the use of model.add(Dense(16, activation="relu"))

    ReplyDelete
  7. why not use one hot encoding for the labels? it is nominal data

    ReplyDelete
  8. excellent tutorial .....keep it up.Kindly share tutorial on 2D CNN, LSTM, GAN etc

    ReplyDelete
  9. Thank you so much sir

    ReplyDelete
  10. Many thanks, this has helped understand from simple principles- excellent tutorial

    ReplyDelete
  11. Have you ever seen the conv1d cause python to crash? Everytime I run your example code above on my system it crashes and the python kernal has to restart. I have run several other sequential models with no issues but the second I add a Conv1D layer it crashes.

    ReplyDelete
    Replies
    1. Nope, It's working well in my environment. Try to run it with new environment and check versions.

      Delete