Keras: Problem with conv1D dimension

Created on 13 Apr 2017  路  5Comments  路  Source: keras-team/keras

Hi, I have tried to build a CNN with one layer, but I have some problem with it.
Indeed, the compilator says me that

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

This is the code

import numpy
from keras.models import Sequential
from keras.layers.convolutional import Conv1D
numpy.random.seed(7)
datasetTraining = numpy.loadtxt("CancerAdapter.csv",delimiter=",")
X = datasetTraining[:,1:31]
Y = datasetTraining[:,0]
datasetTesting = numpy.loadtxt("CancereEvaluation.csv",delimiter=",")
X_test = datasetTraining[:,1:31]
Y_test = datasetTraining[:,0]
model = Sequential()
model.add(Conv1D(2,2,activation='relu',input_shape=X.shape))
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
model.fit(X, Y, epochs=150, batch_size=5)
scores = model.evaluate(X_test, Y_test)
print("\n%s: %.2f%%" % (model.metrics_names[1], scores[1]*100))
stale

Most helpful comment

see my answer on stackoverflow

All 5 comments

see my answer on stackoverflow

This issue has been automatically marked as stale because it has not had recent activity. It will be closed after 30 days if no further activity occurs, but feel free to re-open a closed issue if needed.

How would you extend this reshaping (expand_dim) approach to a case where we have a CNN-LSTM? I have one with a TimeDistributed() layer in the LSTM model, which gets the CNN model as an argument.

`import numpy as np
from numpy import mean
from numpy import std
from numpy import dstack
from pandas import read_csv
from matplotlib import pyplot
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import Flatten
from keras.layers import Dropout
from keras.layers.convolutional import Conv1D
from keras.layers.convolutional import MaxPooling1D
from keras.utils import to_categorical
from sklearn.model_selection import train_test_split
from numpy import zeros, newaxis
seed = 737454
np.random.seed(seed)

X = veriyeni1.values[:]
Y = veriyeni2.values[:]   
#X = np.expand_dims(X, axis=2)
#Y = np.expand_dims(Y, axis=2)

(X_train, X_test, Y_train, Y_test) = train_test_split(X, Y, test_size=0.33, random_state=seed)
print(X_train.shape)`

(33500, 2)

n_timesteps, n_features, n_outputs = X_train.shape[0], X_train.shape[1], Y_train.shape[0]
verbose, epochs, batch_size = 0, 10, 32
model = Sequential()
model.add(Conv1D(filters=64, kernel_size=3, activation='relu', input_shape=X_train.shape))
model.add(Conv1D(filters=64, kernel_size=3, activation='relu'))
model.add(Dropout(0.5))
model.add(MaxPooling1D(pool_size=2))
model.add(Flatten())
model.add(Dense(100, activation='relu'))
model.add(Dense(n_outputs, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
model.fit(X, Y, epochs=epochs, batch_size=batch_size, verbose=verbose)

ValueError: Error when checking input: expected conv1d_37_input to have 3 dimensions, but got array with shape (50000, 2)

X = veriyeni1.values[:]
Y = veriyeni2.values[:]   
X = np.expand_dims(X, axis=2)
Y = np.expand_dims(Y, axis=2)

(X_train, X_test, Y_train, Y_test) = train_test_split(X, Y, test_size=0.33, random_state=seed)
print(X_train.shape)

(33500, 2, 1)

......

Input 0 is incompatible with layer conv1d_39: expected ndim=3, found ndim=4

see my answer on stackoverflow

I try this answer but Its not working

Was this page helpful?
0 / 5 - 0 ratings

Related issues

fredtcaroli picture fredtcaroli  路  3Comments

anjishnu picture anjishnu  路  3Comments

yil8 picture yil8  路  3Comments

kylemcdonald picture kylemcdonald  路  3Comments

amityaffliction picture amityaffliction  路  3Comments