- Preparing the data
- Defining and fitting the model
- Predicting and accuracy check
- 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
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)
Thanks for sharing this simple but effective example.
ReplyDeleteThanks from my side, too, worked well to help me understand the reshape preliminary of Conv1D layers.
ReplyDeleteI can't thank you enough for this clear and simple example that works and explains how to navigate the TensorFlow Conv1D API.
ReplyDeleteDear
ReplyDeleteHope you are doing well
My code running will but model is not learning at all
Model acc is zero
very good .... this codes for Create the Confusion Matrix :
ReplyDelete## 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()
Thanks.
ReplyDeleteIn between Convolution and Pooling layers, what is the use of model.add(Dense(16, activation="relu"))
ReplyDeletewhy not use one hot encoding for the labels? it is nominal data
ReplyDeleteexcellent tutorial .....keep it up.Kindly share tutorial on 2D CNN, LSTM, GAN etc
ReplyDeleteThank you so much sir
ReplyDeleteMany thanks, this has helped understand from simple principles- excellent tutorial
ReplyDeleteHave 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.
ReplyDeleteNope, It's working well in my environment. Try to run it with new environment and check versions.
Delete