Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multidimensional Input to Keras

I am new to keras and I am having a problem running a model with more than one dimension. So, I have been trying a few 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 am getting an error

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

What can be the reason for this. Can anyone give an example on how to run keras model with a multi-dimensional input, i.e. how should the input be structured? Thank you.

like image 852
Anonymous Avatar asked Aug 08 '17 11:08

Anonymous


People also ask

Can keras model have multiple input and output?

Multi Input and Multi Output Models in Keras. The Keras functional API is used to define complex models in deep learning . On of its good use case is to use multiple input and output in a model. In this blog we will learn how to define a keras model which takes more than one input and output.

What is the difference between model and shape in keras?

For instance, if a, b and c are Keras tensors, it becomes possible to do: model = Model (input= [a, b], output=c) shape: A shape tuple (integers), not including the batch size. For instance, shape= (32,) indicates that the expected input will be batches of 32-dimensional vectors.

What is a keras tensor in Python?

A Keras tensor is a symbolic tensor-like object, which we augment with certain attributes that allow us to build a Keras model just by knowing the inputs and outputs of the model. For instance, if a, b and c are Keras tensors, it becomes possible to do: model = Model (input= [a, b], output=c)

Why must all variable usages happen within the Keras layer?

All variable usages must happen within Keras layers to make sure they will be tracked by the model's weights. The Keras Input can also create a placeholder from an arbitrary tf.TypeSpec , e.g:


1 Answers

I would suggest that you one-hot encoded your output classes, 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)

And then used 'categorical_crossentropy' instead of 'sparse_categorical_crossentropy':

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

Finally you should flatten your 3D-input at some point if you want 2D-output (samples x classes). Number of units in the output layer (you have only one) should match number of classes and use an appropriate activation function (e.g. 'softmax')

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

Try to have a look at the Multilayer Perceptron (MLP) for multi-class softmax classification at: https://keras.io/getting-started/sequential-model-guide/

like image 166
niklascp Avatar answered Sep 23 '22 11:09

niklascp