Joe Rakhimov
Joe Rakhimov

Reputation: 5083

How to add convolutional layer to Keras model?

There is data that contains A, B, C. C value should be predicted from A and B values.

enter image description here

The data was split and scaled.

enter image description here

Model without convolutional layer.

from keras.models import Sequential
from keras.layers import Dense, Conv1D

features_count = len(X_train.columns)

model = Sequential([
    Dense(64, input_shape=(features_count,)),
    Activation('relu'),
    Dense(32),
    Activation('softmax'),
    Dense(1),
])

model.compile(optimizer="adam", loss='mse')
model.fit(X_train.values, y_train.values, epochs=10, batch_size=1, verbose=1)
model.evaluate(X_test, y_test, batch_size=1)

Result: 1.0033315420150757

Added Conv1D:

model = Sequential([
    Conv1D(filters=256, kernel_size=5, padding='same', activation='relu', input_shape=(features_count,)),
    Dense(64),
    Activation('relu'),
    Dense(32),
    Activation('softmax'),
    Dense(1),
])

Result: Input 0 is incompatible with layer conv1d_3: expected ndim=3, found ndim=2

input_shape=(features_count,)

was replaced by

input_shape=(features_count,1)

Result: Error when checking input: expected conv1d_4_input to have 3 dimensions, but got array with shape (3, 2)

What is wrong with adding convolutional layer in this way? How to add a convolutional layer to Keras model?

Upvotes: 2

Views: 3081

Answers (1)

Augusto Maillo
Augusto Maillo

Reputation: 180

Conv1D expects a 2D data, for it can apply convolution for first dimension along the second dimension. So, your input data must have shape (Batch size, 1, 2). But, Dense layer need 1D, so between Conv1D and Dense you need to add a Flatten layer to transform your data in 1D.

from keras.models import Sequential
from keras.layers import Dense, Conv1D, Activation, Flatten
import numpy as np
X_train = np.array([[-1.0,-1.0], [1.0, -1.0], [1.0,1.0]])
y_train = np.array([[1.0], [-1.0], [0.0]])

X_test = np.array([[1.0,-1.0], [1.0, -1.0]])
y_test = np.array([[1.0], [-1.0]])

features_count = 2

model = Sequential([
    Conv1D(filters=256, kernel_size=5, padding='same', activation='relu', input_shape=(1,features_count)),
    Flatten(),
    Dense(64),
    Activation('relu'),
    Dense(32),
    Activation('softmax'),
    Dense(1),
])

X_train = X_train.reshape(X_train.shape[0], 1, X_train.shape[1])
X_test = X_test.reshape(X_test.shape[0], 1, X_test.shape[1])

model.compile(optimizer="adam", loss='mse')
model.fit(X_train, y_train, epochs=10, batch_size=1, verbose=1)
model.evaluate(X_test, y_test, batch_size=1)

Result: 1.00140380859375

Hope this help you.

Upvotes: 2

Related Questions