Skip to content Skip to sidebar Skip to footer

How Does It Works The Input_shape Variable In Conv1d In Keras?

Ciao, I'm working with CNN 1d on Keras but I have tons of troubles with the input shape variable. I have a time series of 100 timesteps and 5 features with boolean labels. I want t

Solution 1:

This should work:

from keras.models import Sequential
from keras.layers import Dense, Conv1D
import numpy as np

N_FEATURES=5
N_TIMESTEPS=10
X = np.random.rand(100, N_FEATURES)
Y = np.random.randint(0,2, size=100)

# Create a Sequential model
model = Sequential()
# Change the input shape to input_shape=(N_TIMESTEPS, N_FEATURES)
model.add(Conv1D(filters=32, kernel_size=N_TIMESTEPS, activation='relu', input_shape=(N_TIMESTEPS, N_FEATURES)))
# If it is a binary classification then you want 1 neuron - Dense(1, activation='sigmoid')
model.add(Dense(1, activation='sigmoid'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])

Please see the comments before each line of code. Moreover, the input shape that Conv1D expects is (time_steps, feature_size_per_time_step). The translation of that for your code is (N_TIMESTEPS, N_FEATURES).

Post a Comment for "How Does It Works The Input_shape Variable In Conv1d In Keras?"