简体   繁体   中英

Fitting 3D data as Input into Keras Sequential Model Layer

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?

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:

  • The first dimension in your data is the number of "samples/examples". Since you created a shape (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.
  • X and Y must have the same number of samples. So, if you have 30 samples in X, then you should definitely have 30 samples in Y as well, but it seems that your data considers it as having 20 samples. See shape of target in the message: (20,2) <- this is the shape of Y.
  • The other dimensions are free, but:
    • Dense layers will work only on the last dimension, leaving the others untouched: output shape is (30,6,units)
    • Conv1D layers interpret 3D inputs as: (samples, length, input_channels) , output shape is (samples, modified_length, filters) .
    • Conv2D layers need 4D inputs: (samples, width, heigth, input_channels) , and will output (samples, modified_width, modified_height, filters)
  • The output shape of your model must match the shape of Y. And here, once again, you must understand what Y is, and make sure that you prepare your model to match it.
  • If at some point in the model you need to make your 3D data become 2D, you will need to use either a 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM