How does it works the input_shape variable in Conv1d in Keras?

…衆ロ難τιáo~ 提交于 2019-12-11 19:13:02

问题


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


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




回答2:


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



来源:https://stackoverflow.com/questions/57554413/how-does-it-works-the-input-shape-variable-in-conv1d-in-keras

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!