WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Keras · Intermediate · question 30 of 100

Can you explain the concept of a 1D, 2D, and 3D convolution in Keras, and provide an example of when each might be used?

📕 Buy this interview preparation book: 100 Keras questions & answers — PDF + EPUB for $5

Convolutional Neural Networks (CNNs) are widely used for image and signal processing tasks, where they are able to learn and extract features. Convolutional layers in Keras can perform 1D, 2D, or 3D convolutions, depending on the type of input data.

1D convolution: This is used for processing sequential data. The input data is typically a time series, audio signal, or one-dimensional signal data. The kernel slides along the input data in one dimension, performing the dot product between the kernel and input at each step. A common application of 1D convolution is in natural language processing, where a sequence of words in a sentence can be convolved to extract important features.

Example:

from keras.layers import Conv1D

model = Sequential()
model.add(Conv1D(filters=64, kernel_size=3, activation='relu', input_shape=(100,1)))

In the above example, we define a 1D convolution layer with 64 filters and kernel size of 3. The input shape is (100,1), indicating a sequence of 100 time steps and 1 input channel.

2D convolution: This is used for processing image data. The input data is typically a 2D matrix (e.g. an image) with one or more channels. The kernel slides in two dimensions over the image, performing the dot product between the kernel and input at each step. 2D convolution is commonly used in tasks such as object detection or image classification.

Example:

from keras.layers import Conv2D

model = Sequential()
model.add(Conv2D(filters=32, kernel_size=(3, 3), activation='relu', input_shape=(28, 28, 1)))

In the above example, we define a 2D convolution layer with 32 filters and kernel size of (3, 3). The input shape is (28, 28, 1), indicating an image with dimensions of 28x28 and 1 input channel.

3D convolution: This is used for processing volumetric data. The input data is typically a 3D matrix (e.g. medical imaging data such as CT scans), with one or more channels. The kernel slides in three dimensions over the volume, performing the dot product between the kernel and input at each step. 3D convolution is commonly used in tasks such as object detection or segmentation in medical imaging.

Example:

from keras.layers import Conv3D

model = Sequential()
model.add(Conv3D(filters=32, kernel_size=(3, 3, 3), activation='relu', input_shape=(32, 32, 32, 1)))

In the above example, we define a 3D convolution layer with 32 filters and kernel size of (3, 3, 3). The input shape is (32, 32, 32, 1), indicating a volumetric data with dimensions of 32x32x32 and 1 input channel.

In summary, the choice between 1D, 2D, or 3D convolution depends on the input data type. 1D convolution for sequential data, 2D convolution for image data, and 3D convolution for volumetric data.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Keras interview — then scores it.
📞 Practice Keras — free 15 min
📕 Buy this interview preparation book: 100 Keras questions & answers — PDF + EPUB for $5

All 100 Keras questions · All topics