Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keras 2D Dense Layer for Output

I am playing with a model which should take a 8x8 chess board as input, encoded as a 224x224 grey image, and then output a 64x13 one-hot-encoded logistic regression = probabilities of pieces on the squares.

Now, after the Convolutional layers I don't quite know, how to proceed to get a 2D-Dense layer as a result/target.

I tried adding a Dense(64,13) as a layer to my Sequential model, but I get the error "Dense` can accept only 1 positional arguments ('units',)"

Is it even possible to train for 2D-targets?

EDIT1: Here is the relevant part of my code, simplified:

# X.shape = (10000, 224, 224, 1)
# Y.shape = (10000, 64, 13)

model = Sequential([
    Conv2D(8, (3,3), activation='relu', input_shape=(224, 224, 1)),
    Conv2D(8, (3,3), activation='relu'),

    # some more repetitive Conv + Pooling Layers here

    Flatten(),

    Dense(64,13)
])

TypeError: Dense can accept only 1 positional arguments ('units',), but you passed the following positional arguments: [64, 13]

EDIT2: As Anand V. Singh suggested, I changed Dense(64, 13) to Dense(832), which works fine. Loss = mse.

Wouldn't it be better to use "sparse_categorical_crossentropy" as loss and 64x1 encoding (instead of 64x13) ?

like image 517
Theo H. Avatar asked Sep 13 '25 05:09

Theo H.


2 Answers

In Dense you only pass the number of layers you expect as output, if you want (64x13) as output, put the layer dimension as Dense(832) (64x13 = 832) and then reshape later. You will also need to reshape Y so as to accurately calculate loss, which will be used for back propagation.

# X.shape = (10000, 224, 224, 1)
# Y.shape = (10000, 64, 13)
Y = Y.reshape(10000, 64*13)
model = Sequential([
    Conv2D(8, (3,3), activation='relu', input_shape=(224, 224, 1)),
    Conv2D(8, (3,3), activation='relu'),
    # some more repetitive Conv + Pooling Layers here
    Flatten(),
    Dense(64*13)
])

That should get the job done, if it doesn't post where it fails and we can proceed further.

like image 177
anand_v.singh Avatar answered Sep 21 '25 03:09

anand_v.singh


A Reshape layer allows you to control the output shape.

Flatten(),
Dense(64*13),
Reshape((64,13))#2D
like image 44
Kalanos Avatar answered Sep 21 '25 04:09

Kalanos