3
$\begingroup$

The possible values to specify the Padding option of a PaddingLayer include a fixed value, "Fixed" for a repetition of the last value, and "Reflected" for a reflection of the input... but I see no way to achieve a periodic padding, which would be equally reasonable (and is possible e.g. in ArrayPad). Maybe this is hidden, or can be achieved in a Neural Network in some other way?

For concretenes, I'm looking for something like

PeriodicPaddingLayer[{{1,2}}]@Range@10
(*{10,1,2,3,4,5,6,7,8,9,10,1,2}*)
$\endgroup$

1 Answer 1

0
$\begingroup$

This is not a very elegant approach but it does the job in your example and might get you started.

I am using

$Version

"12.0.0 for Linux x86 (64-bit) (April 7, 2019)"

Let's begin by getting the list you described using the ArrayPad

Let's create your list

list = Range@10;

and a pad

pad = {{1, 2}};

and the we do

exp1 = ArrayPad[list, pad, "Periodic"]

which yields

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

In order to achieve the same thing using PaddingLayer the only way I could come up with is the following:

For a new pad given by

pad = {{1, 1}};

we run

exp2 = Fold[SubsetMap[Reverse, #1, #2] &, 
   PaddingLayer[pad, Padding -> "Fixed"]@list, {{1, 12}}] // 
  Append[#, 2] &

which gives

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

Final comment: It is interesting to compare the time needed in each case.

exp1 = ArrayPad[list, pad, "Periodic"] // RepeatedTiming

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

while on the other hand

exp2 = Fold[SubsetMap[Reverse, #1, #2] &, 
    PaddingLayer[pad, Padding -> "Fixed"]@list, {{1, 12}}] // 
   Append[#, 2] & // RepeatedTiming

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

$\endgroup$
2
  • 3
    $\begingroup$ Thanks, but I'm looking for a layer in the sense that I need an object that can be used within a larger neural network (created, with NetGraph, NetChain, etc). My current best is: ` PeriodicPaddingLayer[n_Integer, m_Integer] := NetGraph[ { "left" -> PartLayer[;; m], "right" -> PartLayer[-n ;;], "catenate" -> CatenateLayer[] }, { NetPort@"Input" -> {"left", "right"}, {"right", NetPort@"Input", "left"} -> "catenate" } ] ` which on my system works slightly faster than your version (though GPU compatibility is the real plus here). $\endgroup$ Commented May 31, 2021 at 14:37
  • $\begingroup$ @FidelI.Schaposnik thanks for that. Your seems to be a more programming approach in spirit $\endgroup$
    – user49048
    Commented Jun 1, 2021 at 9:16

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.