3
$\begingroup$

I have been experimenting with Mathematica Tensor Flow (NetChain and NetTrain) and I would love to know if a simple step exists to present an input with a mixture of numerical and categorical variables. For instance, among my predictors (inputs) I have marital status, which is a categorical variable, do I need to create dummy variables before presenting it to the NN is there a short-cut that I'm not aware of? I'm aware of NetEncoder, and NetDecoder.

Any insight is well appreciated.

$\endgroup$
3
$\begingroup$

You need to correctly use CatenateLayer, here is an example:

NetGraph[{CatenateLayer[]}, {{NetPort["CategoricalInput"], NetPort["Input"]} -> 1},
 "Input" -> {10}, 
 "CategoricalInput" -> NetEncoder[{"Class", {True, False}, "UnitVector"}]
 ]
$\endgroup$
  • $\begingroup$ Thank You! Now, how do I create a graph network from this: $\endgroup$ – user34018 Jan 22 '17 at 19:17
  • $\begingroup$ This is what I tried, but failed NetGraph[<|"l1" -> CatenateLayer[], "l2" -> 20, "l3" -> Ramp, "l4" -> 2, "l5" -> SoftmaxLayer[]|>, , { {"l2" -> "l3" -> "l4" -> "l5"}}, {{NetPort["CategoricalInput"], NetPort["Input"]} -> "l1"}, "Input" -> 10, "CategoricalInput" -> NetEncoder[{"Class", {True, False}, "UnitVector"}],] $\endgroup$ – user34018 Jan 22 '17 at 19:18
  • $\begingroup$ @user34018 Clean that up, why there two , and nested lists {{? NetGraph[<|"l1" -> CatenateLayer[], "l2" -> 20, "l3" -> Ramp, "l4" -> 2, "l5" -> SoftmaxLayer[]|>, {"l2" -> "l3" -> "l4" -> "l5", {NetPort["CategoricalInput"], NetPort["Input"]} -> "l1"}, "Input" -> 10, "CategoricalInput" -> NetEncoder[{"Class", {True, False}, "UnitVector"}]] $\endgroup$ – swish Jan 22 '17 at 19:22
  • $\begingroup$ Here is a sample of my data {{0, 0, 1, "MS_S2", "F"} -> "no", {0.5, 0, 1, "MS_S2", "M"} -> "no", {0.5, 0.5, 1, "MS_S2", "M"} -> "no"} but I get "Input spec of the form input -> output can only be used on a network with a single input and a single output. $\endgroup$ – user34018 Jan 23 '17 at 2:01
  • $\begingroup$ Here is the transformation to my dataset: tData = Table[<|"Input" -> trainingData[[i, {7, 8, 9}]], "MaritalStatus" -> trainingData[[i, 10]], "Gender" -> trainingData[[i, 11]], "Output" -> Last[trainingData[[i]]]|>, {i, Length[trainingData]}] $\endgroup$ – user34018 Jan 23 '17 at 2:27
0
$\begingroup$

To work one has to set the dataset as follows:

tData = Table[<|"Input" -> trainingData[[i, {7, 8, 9}]], 
   "MaritalStatus" -> trainingData[[i, 10]], 
   "Gender" -> trainingData[[i, 11]], 
   "Output" -> Last[trainingData[[i]]]|>, {i, Length[trainingData]}]

Meaning each variable has to match the name in the NetPort

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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