K. Paris
K. Paris

Reputation: 61

How to define shapes of inputs with keras

I'm new here! I'm actually a trainee in a really nice enterprise. They want me to learn deep-learning and tensorflow, which I think is really nice. The prob is that, I found it really complicated but I'm trying my best.

So, for the beginning, I'm trying to make a little classifier who can class if a number is even or odd. I tried it with keras, but it seems that I got some little problems to define the shapes.

Here's my code:

import keras
import numpy as np

x_train = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15])
y_train = np.array([1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0])
x_test = np.array([884,65,9,995,5,32,7,684,5])
y_test = np.array([1,0,0,0,0,1,0,1,0])

print(x_train.shape)
print(y_train.shape)
print(x_test.shape)
print(y_test.shape)

(16,)
(16,)
(9,)
(9,)

from keras.layers import Dense, Activation
from keras.models import Sequential
model = Sequential()

model.add(Dense(1, input_shape=(16,)))
model.add(Activation('relu'))
model.add(Dense(2))
model.add(Activation('softmax'))

model.compile(loss='categorical_crossentropy', optimizer='sgd',metrics=
['accuracy'])
model.fit(x_train, y_train, epochs=10,)
loss_and_metrics = model.predict(x_test, y_test, batch_size=128)

ValueError: Error when checking input: expected dense_54_input to have shape (None, 16) but got array with shape (16, 1)

I got this at the end, I searched some hours but I didn't find or understand how to solve this problem... Thanks for your help!

Upvotes: 1

Views: 3332

Answers (1)

Fábio Perez
Fábio Perez

Reputation: 26048

You have 16 one-dimensional (one integer) samples, so the input_shape of your network must be 1, not 16.

Also, you need to convert your output values (y vectors) to a binary class matrix. Use keras.utils.to_categorical for this.

Also, I think you won't get good results with this network architecture (number of neurons, input representation and activation functions). Check this question for reference.

Upvotes: 2

Related Questions