Reputation: 43
I'm a newbie in machine learning and Keras. Actually I have worked with scikit-learn but Keras seemed a little bit more complicated. My problem is that I have some data in 3D and want to fit that into a Dense layer(I have also tried with Conv2D, and Conv1D layers). What I did is as follows:
arr1 = np.random.random((30,2))
arr2 = np.random.random((30,2))
arr3 = np.random.random((30,2))
arr4 = np.random.random((30,2))
arr5 = np.random.random((30,2))
arr6 = np.random.random((30,2))
x_matrix = np.dstack(
(arr1
,arr2
,arr3
,arr4
,arr5
,arr6)
).swapaxes(1,2)
print(x_matrix.shape)
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(x_matrix, y_matrix, test_size=0.33, random_state=42)
from keras.models import Sequential
model = Sequential()
from keras.layers import Dense, Conv2D, Conv1D, Flatten
model = Sequential()
model.add(Dense(6, activation='sigmoid', input_shape=(6,2)))
model.compile(loss='categorical_crossentropy',
optimizer='sgd',
metrics=['accuracy'])
model.fit(np.array(X_train), np.array(y_train), epochs=20, batch_size=1)#
score = model.evaluate(X_test, y_test)
print(score)
And I'm getting the error at fit step. The error is as follows:
ValueError: Error when checking target: expected dense_1 to have 3 dimensions, but got array with shape (20, 2)
And for Conv1D layer I tried this:
model.add(Conv1D(6, (2), activation='sigmoid', input_shape=(6 ,2)))
And came up with the eror:
ValueError: Error when checking target: expected conv1d_1 to have 3 dimensions, but got array with shape (20, 2)
Conv2D seemed more complicated I probably would not need this as my input layer but with the below call I still had the same error.
model.add(Conv2D(6, (2,2), activation='sigmoid', input_shape=(20,6 ,2)))
ValueError: Error when checking input: expected conv2d_1_input to have 4 dimensions, but got array with shape (20, 6, 2)
What I'm asking is: how can I fit fit such a data into a neural network with Keras?
Upvotes: 4
Views: 8575
Reputation: 86600
First, you must understand what your data is and what you want to do with it.
Then you decide how to shape the data and which layers to use.
There are some important conventions, though:
(30,6,2)
, you decided that you have 30 samples, and each sample has shape (6,2)
-- This is why it's important to know your data and what you want to do. target
in the message: (20,2)
<- this is the shape of Y.(30,6,units)
(samples, length, input_channels)
, output shape is (samples, modified_length, filters)
. (samples, width, heigth, input_channels)
, and will output (samples, modified_width, modified_height, filters)
Flatten
, a Reshape
, a GlobalMaxPooling1D
or a GlobalAveragePooling1D
layer. Hint: use model.summary()
to see the output shapes of every layer and also the final output shape.
Hint2: first define clearly your data and your goals, then the shapes of X and Y, then the shapes of the model.
Upvotes: 4