1
$\begingroup$

I am doing a toy example with mushroom dataset to learn class embedding with keras:

I am trying to embed a single feature:

inputs = Input(shape=[1])
emb = Embedding(input_dim=cap_shape_dummy.shape[1], output_dim=3)(inputs)
output = Dense(units=1,activation='sigmoid')(emb)
model = Model(inputs=inputs,outputs=output)

However I always receive the same error when I try to fit the model:

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

Am I doing anything wrong? Did I miss anything with category embedding?
Thanks.

$\endgroup$
2
  • $\begingroup$ I think your problem can be solved by adding 'model.add(Flatten())' before the dense layer, or see the answer of this question: stackoverflow.com/questions/48674881/… $\endgroup$
    – Hunar
    Apr 11, 2019 at 9:01
  • $\begingroup$ Why would I need to Flatten? the output fronm my embedding is not multi dim? Would you please explain me what each param mean? because maybe is not clear $\endgroup$
    – 3nomis
    Apr 11, 2019 at 12:15

1 Answer 1

-1
$\begingroup$
inputs = Input(shape=[1])
emb = Embedding(input_dim=cap_shape_dummy.shape[1], output_dim=3)(inputs)
flat = Flatten()(emb)
output = Dense(units=1,activation='sigmoid')(flat)
model = Model(inputs=inputs,outputs=output)
$\endgroup$
1
  • 3
    $\begingroup$ Please format your code as code, and add some explanation behind it. $\endgroup$ Jul 16, 2020 at 11:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.