# How to partition variable length vectors via mathematica's neural network?

I need to partition variable length vectors in neural network recently. For instance, the network input is {1,2,3,4,...,2n}, I want to partition two elements as a group, so the result seems like {{1,2},{3,4},...,{2n-1,2n}}, and notice that the input is variable length vectors.

I have tried to use ReshapeLayer[] to do this, the code like ReshapeLayer[{Automatic,2},"Input"->{"Varying"}], and received the message, ReshapeLayer::valfail: Validation failed for ReshapeLayer: with variable-length inputs, Automatic can only be used if the product of known input dimensions is a multiple of the product of known output dimensions.

So how can I do this via mathematica neural network? Is there anyway?

## Update

Thank you bro @Alexey Golyshev, but I still meet a problem, it seems that this method doesn't work inside a network.

What I want to do is, extract atom feature vectors according to my indexes, and partition two of them as a group.

Network inputs, Atom: Dimensions@Atom={n1,8}. NearIndex: Dimensions@NearIndex={n2,1}.

Full code:

enc=NetChain[{Ramp},"Input"->NetEncoder[{"Function", Partition[#,2] &, {"Varying",2,8}}]]
net=NetGraph[
{"Near"->ExtractLayer[],
"NearAtom"->enc},

{NetPort["Atom"]->NetPort["Near","Input"],
NetPort["NearIndex"]->NetPort["Near","Position"],
"Near"->"NearAtom"}
]

atom1=Table[i,{i,12},{j,8}];
atom2=Table[i,{i,24},{j,8}];
index1=Partition[Range[4],1];
index2=Partition[Range[6],1];

output=net[<|"Atom"->{atom1,atom2},"NearIndex"->{index1,index2}|>]


Then return the message NetGraph::tyfail2: Inferred inconsistent ranks for output of layer "Near" (a rank-3 array versus a matrix).

Network output dimensions should be:

Dimensions/@output


{{2,2,8},{3,2,8}}

• Please show us the complete code of what you've tried. Apr 26, 2021 at 12:50

enc = NetEncoder[{"Function", Partition[#, 2] &, {"Varying", 2}}]


net = NetChain[
{
Ramp
},
"Input" -> enc
]


net@Range[10]


{{1., 2.}, {3., 4.}, {5., 6.}, {7., 8.}, {9., 10.}}

UPDATE

net = NetGraph[
<|
1 -> ExtractLayer["Input" -> {"Varying", 2, 8}, "Output" -> {"Varying", 2, 8}]
|>,
{
NetPort["Input"] -> NetPort[1, "Input"],
NetPort["Position"] -> NetPort[1, "Position"]
},
"Input" -> NetEncoder[{"Function", Partition[#, 2] &, {"Varying", 2, 8}}]
]


atom1 = Table[i, {i, 12}, {j, 8}];
atom2 = Table[i, {i, 24}, {j, 8}];
index1 = Partition[Range[4], 1];
index2 = Partition[Range[6], 1];

net[<|"Input" -> {atom1, atom2}, "Position" -> {index1, index2}|>]


Dimensions /@ %


{{4, 2, 8}, {6, 2, 8}}

• Thanks bro, and I want to do extract first, then partition, so the output dimensions should be {{2,2,8},{3,2,8}}, is there any way? very thanks! Apr 27, 2021 at 8:22
• Hello! I don't think that's possible. Apr 27, 2021 at 8:58