Multidimensional Entry in Keras

I am new to keras and I have a problem with a model with more than one dimension. So, I tried some samples. This is one of them.

from keras.models import Sequential
from keras.layers import Dense
import numpy as np

X_train = np.array([[[1, 2], [3, 4]], [[1, 2], [3, 4]]])
model = Sequential([
    Dense(32, input_shape=X_train.shape[1:]),
])
model.compile(loss='sparse_categorical_crossentropy', optimizer='sgd')
model.fit(X_train, [1, 2])


I expect the above sample to run, but I get an error

Error when checking target: expected dense_1 to have 3 dimensions, but got array with shape (2, 1)

What is the reason for this. Can someone give an example on how to run the keras model using multidimensional input, i.e. How to structure the input? Thank.

+4
source share
1 answer

I would suggest that you warm up your output classes encoded, i.e. using:

# Convert labels to categorical one-hot encoding
labels = np.array([1, 2]) # 0 - num_classes - 1
y_train = keras.utils.to_categorical(labels, num_classes=3)

'sparse_categorical_crossentropy' '_':

model.compile(loss='categorical_crossentropy',
              optimizer=sgd,
              metrics=['accuracy'])

, 3D- - , 2D- ( x ). ( ) (, "softmax" )

model.add(Flatten(input_shape=X_train.shape[1:]))
model.add(Dense(3, activation='softmax'))

(MLP) softmax : https://keras.io/getting-started/sequential-model-guide/

+4

Source: https://habr.com/ru/post/1683240/


All Articles