7
$\begingroup$

I found this series of visualizations of NN layers working principle fascinating (link):

enter image description here

Can you provide some tips how to build such animation in Mathematica?

$\endgroup$
2
  • $\begingroup$ Just say 'show me the code', the likelihood of getting an answer will be the same and you will save us the time spend on thinking about efficiency. :) $\endgroup$
    – Kuba
    Commented Jun 14, 2017 at 19:13
  • $\begingroup$ @Kuba, I've noticed that 'show me the code' releases more time for thinking about closing and downvoting )) $\endgroup$
    – iot
    Commented Jun 14, 2017 at 19:36

1 Answer 1

16
$\begingroup$

Okay lets do this:

first we'll build a ground structure. Some Variables to define our Rectangle sizes. Next a variable to store our frames and a Table which constructs them. At last a ListAnimate.

topTileLength = 2;
bottomTileLength = 4;
frames = Flatten[
   Table[
    Graphics3D[{Point[{x, y, 0}]}, 
     PlotRange -> {{0, bottomTileLength}, {0, bottomTileLength}, {0, 
        2}}], (*example*)
    {x, 1, topTileLength}, {y, 1, topTileLength}]
   , 1];
ListAnimate[frames]

enter image description here

Okay so far so good. Now lets replace the example by real rectangles. We doing this by defining our own function

Rectangle3D[{xmin_,xmax_},{ymin_,ymax_},z_]:=Polygon[{{xmin,ymin,z},{xmax,ymin,z},{xmax,ymax,z},{xmin,ymax,z}}]

The list for our Graphics3D has to be flattened and adding a table to construct our Rectangles.

Graphics3D[Flatten@{
   Table[Rectangle3D[{dx - 1, dx}, {dy - 1, dy}, 1.9], {dx, 1, 
     topTileLength}, {dy, 1, topTileLength}]
   }, PlotRange -> {{0, bottomTileLength}, {0, bottomTileLength}, {0, 
    2}}]

Now lets add color for the specified condition, that we have an active field:

... Table[{If[dx==x&&dy==y,Blue,Red],Rectangle3D[...

enter image description here

We getting on our way. Now lets add the bottom layer and shift the top layer to the middle.

... midPointLength = (bottomTileLength - topTileLength)/2; ... Table[{If[dx == x && dy == y, Blue, Gray], Rectangle3D[{dx - 1 + midPointLength, dx + midPointLength}, {dy + midPointLength - 1, dy + midPointLength}, 1.9]}, {dx, 1, topTileLength}, {dy, 1, topTileLength}], Table[{If[False, Red, Gray], Rectangle3D[{dx - 1, dx}, {dy - 1, dy}, 0.1]}, {dx, 1, bottomTileLength}, {dy, 1, bottomTileLength}] ...

The condition for the bottom layer is not that hard by also introducing a radius parameter for the convolution:

... If[ x + midPointLength - radius <= dx <= x + midPointLength + radius && y + midPointLength - radius <= dy <= y + midPointLength + radius, Red, Gray] ...

enter image description here

Yeay, results.

Now finally add some Lines to the plot and afterwards make the colors a little bit more beautiful and add a little opacity as the final note:

Full Code:

Rectangle3D[{xmin_, xmax_}, {ymin_, ymax_}, z_] := 
 Polygon[{{xmin, ymin, z}, {xmax, ymin, z}, {xmax, ymax, z}, {xmin, 
    ymax, z}}]
topTileLength = 5;
bottomTileLength = 9;
radius = 2;
midPointLength = Floor[(bottomTileLength - topTileLength)/2];
frames = Flatten[
   Table[
    Graphics3D[Flatten@{Opacity[0.8],
       Table[{If[dx == x && dy == y, Darker[Cyan], Lighter[Cyan]], 
         Rectangle3D[{dx - 1 + midPointLength, 
           dx + midPointLength}, {dy + midPointLength - 1, 
           dy + midPointLength}, 1.9]}, {dx, 1, topTileLength}, {dy, 
         1, topTileLength}],
       Table[{If[
          x + midPointLength - radius <= dx <= 
            x + midPointLength + radius && 
           y + midPointLength - radius <= dy <= 
            y + midPointLength + radius, Darker[Blue], Lighter[Blue]],
          Rectangle3D[{dx - 1, dx}, {dy - 1, dy}, 0.1]}, {dx, 1, 
         bottomTileLength}, {dy, 1, bottomTileLength}],
       Black, Thick, 
       Line[{{x - 1 + midPointLength, y - 1 + midPointLength, 
          1.9}, {x - 1 + midPointLength - radius, 
          y - 1 + midPointLength - radius, 0.1}}], 
       Line[{{x + midPointLength, y - 1 + midPointLength, 
          1.9}, {x + midPointLength + radius, 
          y - 1 + midPointLength - radius, 0.1}}], 
       Line[{{x + midPointLength, y + midPointLength, 
          1.9}, {x + midPointLength + radius, 
          y + midPointLength + radius, 0.1}}], 
       Line[{{x - 1 + midPointLength, y + midPointLength, 
          1.9}, {x - 1 + midPointLength - radius, 
          y + midPointLength + radius, 0.1}}]
       }, 
     PlotRange -> {{0, bottomTileLength}, {0, bottomTileLength}, {0, 
        2}}], 
    {x, 1, topTileLength}, {y, 1, topTileLength}]
   , 1];
ListAnimate[frames]

enter image description here

Hope this helps.

$\endgroup$
2
  • $\begingroup$ yes, it helps a lot! It will take me some time to get through the code. $\endgroup$
    – iot
    Commented Jun 14, 2017 at 19:46
  • 1
    $\begingroup$ @Julien Kluge very cool. Yes, that's a good answer. $\endgroup$
    – LCarvalho
    Commented Oct 26, 2017 at 20:33

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.