Klue
Klue

Reputation: 1357

Specifying Dense using keras library

I slightly misunderstand how to create a simple Sequence for my data.

The data has the following dimensions:

X_train.shape
(2369, 12)

y_train.shape
(2369,)

X_test.shape
(592, 12)

y_test.shape
(592,)

This is how I create the model:

batch_size = 128
nb_epoch = 20
in_out_neurons = X_train.shape[1] 
dimof_middle = 100

model = Sequential()
model.add(Dense(batch_size, batch_input_shape=(None, in_out_neurons)))
model.add(Activation('relu'))
model.add(Dropout(0.2))
model.add(Dense(batch_size))
model.add(Activation('relu'))
model.add(Dropout(0.2))
model.add(Dense(in_out_neurons))
model.add(Activation('linear'))

# I am solving the regression problem, not the classification one
model.compile(loss="mean_squared_error", optimizer="rmsprop") 

history = model.fit(X_train, y_train,
                    batch_size=batch_size, nb_epoch=nb_epoch,
                    verbose=1, validation_data=(X_test, y_test))

The error message:

Exception: Error when checking model input: expected dense_input_14 to have shape (None, 1) but got array with shape (2369, 12)ç

The error is:

Error when checking model target: expected activation_42 to have shape (None, 12) but got array with shape (2369, 1)

This error occurs at line:

model.add(Dense(in_out_neurons))

How to change Dense to make it work?

Another question is how to add a simple autoencoder in order to initialize weights of ANN?

Upvotes: 2

Views: 2792

Answers (1)

nemo
nemo

Reputation: 57757

One of your problems is that you seem to misunderstand what a batch is. A batch is the number of training samples computed at a time, so instead of computing one training sample from X_train at a time you use, for example, 100 at a time. The important bit here is that this has nothing to do with your model.

So when you write

model.add(Dense(batch_size, batch_input_shape=(None, in_out_neurons)))

then you create a fully connected layer with an output size of one batch. That does not make a lot of sense.

Another problem is that your model's output is 12 neurons while your Y is only one value/neuron. Your model looks like this:

  |
  v
[128]
[128]
[ 12]
  |
  v

Then what fit() does is, it inputs a matrix of shape (128, 12) ((batch size, X_train.shape[1])) into the model and attempts to compare the output of shape (128,12) from the last layer to the corresponding Y values of the batch (shape (128,1)).

Upvotes: 4

Related Questions