3
$\begingroup$

Why doesn't this work:

NetGraph[{PartLayer[{1}], PartLayer[{2}],
EmbeddingLayer[50, "Input" -> NetEncoder[{"Class", {"male", "female"}}]],
EmbeddingLayer[50, "Input" -> NetEncoder[{"Class", {"child", "adult", "senior"}}]],
CatenateLayer[]}, {1 -> 3 -> 5, 2 -> 4 -> 5}]

NetGraph::tyfail2: Inferred inconsistent dimensions for array "Weights" of third layer (a 2*50 matrix of real numbers versus a 3*50 matrix of real numbers).

But this does:

NetGraph[{PartLayer[{1}], PartLayer[{2}],
EmbeddingLayer[50, "Input" -> NetEncoder[{"Class", {"male", "female", "placeholder"}}]],
EmbeddingLayer[50, "Input" -> NetEncoder[{"Class", {"child", "adult", "senior"}}]],
CatenateLayer[]}, {1 -> 3 -> 5, 2 -> 4 -> 5}]

Obviously, I'm misunderstanding something very basic, but I can't figure out what it is.

$\endgroup$
4
  • $\begingroup$ Welcome to Mathematica StackExchange! What is the example of an input that you with to feed into this NetGraph? $\endgroup$
    – Domen
    Commented Aug 1, 2023 at 11:52
  • $\begingroup$ My actual task is much more complicated but the issue boils down to this. I am trying to train a neural net with multiple one-hot encoded classifications of different numbers of classes. In my actual task, there are three: one with 42,735 possible classes, one with 3320, and one with 62. That seems simple enough, yet I have not found anything on it online. $\endgroup$
    – user233932
    Commented Aug 1, 2023 at 15:16
  • $\begingroup$ Right now, I'm just trying to get the NetGraph to work before moving on to NetTrain. $\endgroup$
    – user233932
    Commented Aug 1, 2023 at 15:30
  • 1
    $\begingroup$ I don't know what is wrong, but perhaps a simple solution is to separate both inputs (ie. provide two input ports instead of using PartLayer): NetGraph[{EmbeddingLayer[50, "Input" -> NetEncoder[{"Class", {"male", "female"}}]], EmbeddingLayer[50, "Input" -> NetEncoder[{"Class", {"child", "adult", "senior"}}]], CatenateLayer[]}, {NetPort["Input1"] -> 1 -> 3, NetPort["Input2"] -> 2 -> 3}] $\endgroup$
    – Domen
    Commented Aug 1, 2023 at 16:38

1 Answer 1

0
$\begingroup$

Each element in a vector used in a neural network must be of the same type.

In the NetGraph of your first example, your first EmbeddingLayer takes in input of type Restricted["Integer",2] (an integer from 1 to 2, since there are 2 classes), whereas the second one takes an input of type Restricted["Integer",3].

Because of this, the input of your NetGraph is required to be a vector whose elements are simultaneously Restricted["Integer",2] and Restricted["Integer",3], and that's not allowed.

A workaround for this, already mentioned in the comments by Domen, is to have 2 separate inputs for your NetGraph, each of a different type.

NetGraph[{EmbeddingLayer[50, "Input" -> NetEncoder[{"Class", {"male", "female"}}]], EmbeddingLayer[50, "Input" -> NetEncoder[{"Class", {"child", "adult", "senior"}}]], CatenateLayer[]}, {NetPort["Input1"] -> 1 -> 3, NetPort["Input2"] -> 2 -> 3}]
$\endgroup$

Your Answer

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

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