Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I shape my input data for use with Conv1D in keras?

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
like image 835
user1816679 Avatar asked Jul 07 '17 19:07

user1816679


People also ask

What is the input shape for Conv1D?

For the above example, with an input shape of 8, 1(8 inputs, 1 feature), the output of Conv1D(filters = 16) will produce 16 different outcomes resulting in a shape of (8, 16).

What is the difference between Conv1D and conv2d?

conv1d is used when you slide your convolution kernels along 1 dimensions (i.e. you reuse the same weights, sliding them along 1 dimensions), whereas tf. layers. conv2d is used when you slide your convolution kernels along 2 dimensions (i.e. you reuse the same weights, sliding them along 2 dimensions).

What is stride in Conv1D?

stride controls the stride for the cross-correlation, a single number or a one-element tuple. padding controls the amount of padding applied to the input. It can be either a string {'valid', 'same'} or a tuple of ints giving the amount of implicit padding applied on both sides.

What does Conv1D layer do?

Conv1D class. 1D convolution layer (e.g. temporal convolution). This layer creates a convolution kernel that is convolved with the layer input over a single spatial (or temporal) dimension to produce a tensor of outputs. If use_bias is True, a bias vector is created and added to the outputs.


1 Answers

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])
like image 134
kvorobiev Avatar answered Sep 20 '22 15:09

kvorobiev