11

I have in my dummy dataset 12 vectors of length 200, each vector representing one sample. Let's say x_train is an array with shape (12, 200).

When I do:

model = Sequential()
model.add(Conv1D(2, 4, input_shape=(1, 200)))

I get the error:

ValueError: Error when checking model input: expected conv1d_1_input to have 3 dimensions, but got array with shape (12, 200)

How do I shape my input array correctly?

Here is my updated script:

data = np.loadtxt('temp/data.csv', delimiter=' ')
trainData = []
testData = []
trainlabels = []
testlabels = []

with open('temp/trainlabels', 'r') as f:
    trainLabelFile = list(csv.reader(f))

with open('temp/testlabels', 'r') as f:
    testLabelFile = list(csv.reader(f))

for i in range(2):
    for idx in trainLabelFile[i]:
        trainData.append(data[int(idx)])
        # append 0 to labels for neg, 1 for pos
        trainlabels.append(i)

for i in range(2):
    for idx in testLabelFile[i]:
        testData.append(data[int(idx)])
        # append 0 to labels for neg, 1 for pos
        testlabels.append(i)

# print(trainData.shape)
X = np.array(trainData)
Y = np.array(trainlabels)
X2 = np.array(testData)
Y2 = np.array(testlabels)

model = Sequential()
model.add(Conv1D(1, 1, input_shape=(12, 1, 200)))

opt = 'adam'
model.compile(loss='mean_squared_error', optimizer=opt, metrics=['accuracy'])

model.fit(X, Y, epochs=epochs)

I am now getting a new error:

ValueError: Input 0 is incompatible with layer conv1d_1: expected ndim=3, found ndim=4
user1816679
  • 845
  • 3
  • 11
  • 19

3 Answers3

3

You needs to reshape your input data according to Conv1D layer input format - (batch_size, steps, input_dim). Try

x_train = x_train.reshape(x_train.shape[0], 1, x_train.shape[1])
kvorobiev
  • 5,012
  • 4
  • 29
  • 35
1

A bit late but just to answer the question, the input shape is (number of different models-batch, number of data per model, the dimensions of the data). In your case( 12,200,1)

TheGame
  • 53
  • 2
  • 9
0

In Keras documentation, it is written that input_shape is a 3D tensor with shape (batch_size, steps, input_dim). The meaning is as follows:

  1. batch_size is the number of samples. It is 12 for you.
  2. steps is the time dimension of the data. You can set it to 1 as you have only one channel in the data.
  3. input_dim is the dimension of one sample. It is 200 for you.

Answer to your question is to reshape your data to (12,1,200).

devil in the detail
  • 2,905
  • 17
  • 15
  • I tried that but I just got the error again: ValueError: Error when checking model target: expected conv1d_1 to have 3 dimensions, but got array with shape (12, 1). Do I have to change my input_shape argument too? – user1816679 Jul 07 '17 at 20:42
  • Yes. That is the only way to tell keras that the input is of shape `(12,1,200)`. – devil in the detail Jul 07 '17 at 20:47
  • @user1816679 If you don't want to specify number of samples for training then write `input_shape=(None, 1, 200))`. – devil in the detail Jul 07 '17 at 20:54
  • OK I did that, but now I'm getting a new error: ValueError: Input 0 is incompatible with layer conv1d_1: expected ndim=3, found ndim=4 – user1816679 Jul 07 '17 at 20:59
  • Okay, my mistake. `input_shape=(None, 1, 200))` will be wrong, `input_shape=(None, 200))` should work fine. I can suggest edits if you can provide your script. Otherwise [this](https://stackoverflow.com/questions/43235531/convolutional-neural-network-conv1d-input-shape) has a better explanation for the `input_shape`. – devil in the detail Jul 07 '17 at 21:16
  • (None, 200) is what I was using before and gave me an error. (12, 1, 200) and (None, 1, 200) both give the new error – user1816679 Jul 07 '17 at 21:27