简体   繁体   中英

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 to train a CNN 1d that works with a sliding window of length 10. This is a very simple code I wrote:

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)


# CNN
model.Sequential()
model.add(Conv1D(filter=32, kernel_size=N_TIMESTEPS, activation='relu', input_shape=N_FEATURES
model.add(Dense(1, activation='sigmoid'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])

My problem here is that I get the following error:

  File "<ipython-input-2-43966a5809bd>", line 2, in <module>
    model.add(Conv1D(filter=32, kernel_size=10, activation='relu', input_shape=N_FEATURES))
TypeError: __init__() takes at least 3 arguments (3 given)

I've also tried by passing to the input_shape the following values:

input_shape=(None, N_FEATURES)
input_shape=(1, N_FEATURES)
input_shape=(N_FEATURES, None)
input_shape=(N_FEATURES, 1)
input_shape=(N_FEATURES, )

Do you know what's wrong with the code or in general can you explain the logic behind in input_shape variable in Keras CNN?

The crazy thing is that my problem is the same of the following:

Keras CNN Error: expected Sequence to have 3 dimensions, but got array with shape (500, 400)

But I cannot solve it with the solution given in the post.

The Keras version is 2.0.6-tf

Thanks

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) .

Your code is missing parentheses, and has spelling mistakes in arguments. Input shape needs to be an iterable, so try (N_FEATURES,) instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM