Question: correct formats for Mathematica's NetTrain function
Explanation of the problem
Background
So Mathematica 11 was released earlier this month, and while there are many improvements and they seem quite proud of its neural network functions, they are still experimental. Thus the purpose of this question is to hopefully augment Mathematica's documentation with a list of all acceptable input formats for common data sets people may have.
Following Taliesin Beynon's Answer
So, if I understood correctly (both you and the documentation NetTrain_Doc Then you are saying something like this?
newInput = {<|"Input" -> {1, 2, 3}, "Output" -> "one"|>,
<|"Input" -> {4, 5, 6}, "Output" -> "two"|>,
<|"Input" -> {1, 2, 3}, "Output" -> "three"|>,
<|"Input" -> {4, 5, 6}, "Output" -> "one"|>,
<|"Input" -> {1, 2, 3}, "Output" -> "one"|>,
<|"Input" -> {1, 2, 3}, "Output" -> "three"|>,
<|"Input" -> {4, 5, 6}, "Output" -> "two"|>,
<|"Input" -> {1, 2, 3}, "Output" -> "one"|>};
newInput[[;; , 2]] = NetEncoder[{"Class", {"one", "two", "three"}, "UnitVector"}][newInput[[;; , 2]]]
net = NetInitialize[NetGraph[{Tanh, DotPlusLayer[3]}, {1 -> 2}, "Input" -> 3]]
NetTrain[net, newInput];
Alternatively, one can just do Dataset[newInput]
and it works as well...
()
This does work, so thank you. Follow up questions:
- Can I somehow get the NetEncoder to work within the NetChain?
- Can you provide an equivalent demonstration for the tensor of rank 3 question below?
Prior to Taliesin Beynon's Answer
Example
Here I am putting the code for a simple "dataset," that someone might have in many of the possible forms that are supposedly accepted by NetTrain. It is a general "spreadsheet" where the first n-1 columns are different variables and the nth column is either the class or value the person wants to predict.
list = {{"feature1", "feature2", "feature3", "class"},{1, 2, 3, "one"},{4, 5, 6, "two"},{1, 2, 3, "three"},{4, 5, 6, "one"},{1, 2, 3, "one"},{1, 2, 3, "three"},{4, 5, 6, "two"},{1, 2, 3, "one"}};
rowNames = {"record1", "record2", "record3", "record4", "record5", "record6", "record7", "record8"};
associations = Table[list[[i, 1 ;; 3]] -> list[[i, 4]], {i, 2, Length[list]}];
dataset = Dataset[Association[
Table[rowNames[[i]] ->
Table[Association[
Table[<|list[[1, i]] -> list[[j, i]]|>, {i, 1,
Dimensions[list][[2]]}]], {j, 2,
Dimensions[list][[1]]}][[i]], {i, 1, Length[rowNames]}]]];
list // MatrixForm
associations // MatrixForm
dataset
The output for this (for those who do not wish to copy-past into a .nb) is shown as an image below. list is a tensor of rank 2, associations a tensor of rank 1 and dataset is not a tensor.
An arbitray neural net
inputDimension = 3;
net = NetInitialize[NetGraph[{BatchNormalizationLayer[], Tanh, LogisticSigmoid, Tanh, TotalLayer[], TotalLayer[], TotalLayer[], CatenateLayer[], DotPlusLayer[50], DotPlusLayer[1], Tanh}, {1 -> 2, 1 -> 3, 1 -> 4, 2 -> 5, 3 -> 5, 3 -> 6, 4 -> 6, 2 -> 7, 4 -> 7, 5 -> 8, 6 -> 8, 7 -> 8, 8 -> 9, 9 -> 10, 10 -> 11}, "Input" -> inputDimension]]
So how does one format the above data representations for net train?
e.g. (obviously ignoring cross validation and other options right now)
NetTrain[net,list[[2 ;;]]]; (*My typo as I wrote this quickly and was pointed out by Wjx, we do not nedd the first row*)
NetTrain[net,associations];
NetTrain[net,dataset];
These generate the following errors
- Training data should be an association of lists, or a rule from input to examples.
- Data provided to port output should be a list of numeric arrays of dimensions {1}
- Datasets provided to NetTrain must consist of a list of associations with fixed keys.
If you think this might be because of the net structure, these errors are constant for any net structure, e.g.
net2 = NetInitialize[NetGraph[{Tanh, DotPlusLayer[1]}, {1 -> 2}, "Input" -> inputDimension]]
Bonus: Tensors of Rank 3
I, personally, do not quite understand the structure of tensors of rank 3. To my knowledge, a tensor of rank 3, has the following dimensions {a, b, c}, where:
- a is the number of elements if we unwrap the outermost layer
- b is the number of lists if we unwrap one of the layers we got from a
- c is the number of elements in one of the innermost layers
For completeness please using the same "data" (or as close to as possible) provided above to explain tensors of rank three and their correct input format for NetTrain.
What to answer, please
How do we take the above "data" and format it so that it will be acceptable for NetTrain
Association
is a unique format, not simply rule arrays, but in my opinion, the second one shall work after proper tuning, or simply change one two three to 1 2 3. The third way with dataset failed simply because you created the dataset in a noncompatable form. I suggest you at least go through the NetTrain documentation in detail and run a few simple examples before you ask questions. $\endgroup$