3
$\begingroup$

I can use a function to compute the positions of individual nodes of a neural network based on how many there are in each layer (nodesPerLayerList below). Now I want to display them all in position. The Map function below does put out the requested number of circles, but does not position them. The multiple Graphics examples in the hands-on start book type out each object explicitly.

Is there a simple way? Must I loop?

nodesPerLayerList = { 2 , 4 , 3 , 4 , 1 , 1  } ;
nodeCenterList = positionNodes[ nodesPerLayerList ] ;
nodeCenterList 

(* 
  {{{10,30},4},{{10,42},4},{{24,18},4},{{24,30},4},{{24,42},4},
  {{24,53},4},{{38,24},4},{{38,36},4},{{38,48},4},{{52,18},4},{{52,30},4},
  {{52,42},4},{{52,53},4},{{65,36},4},{{79,36},4}}
*)

And then

Map[ Graphics[ Circle[] ] , nodeCenterList ]
$\endgroup$
  • $\begingroup$ Welcome to Mathematica.SE and thank you for taking the time to format your post correctly even in your first question ever. Just in case you don't know already: When you see good questions and answers, vote them up by clicking the gray triangles because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! If you have questions about a solution, you can comment on it. $\endgroup$ – halirutan Jun 7 '18 at 19:51
  • $\begingroup$ Thank you to both halirutan and henrik for your very welcoming and helpful responses. I am off to summer school in a couple weeks and am, in the mean time, renewing my acquaintance with the top row of my keyboard, all too often with a pinkie on the shift key. $\endgroup$ – James Bailey Jun 8 '18 at 11:44
4
$\begingroup$

Map was already very close. Try Graphics[Circle @@@ nodeCenterList] (have a look at Apply to understand what is happening). Try also Map[Circle ,nodeCenterList] and observe that there will be too many braces. Apply gets rid of them by replacing them.

Note also that Circle[] stands for the unit circle centered at {0,0}.

In the end, just wrap a Graphics around all graphics primitives. You can also join Graphics with Show.

| improve this answer | |
$\endgroup$
  • $\begingroup$ The three at-signs worked a treat. i was hoping that they would do the same for the links between the circles. They did not. There are three dozen link lines of which the first few that i am getting are: $\endgroup$ – James Bailey Jun 9 '18 at 17:12
  • $\begingroup$ So, what are they? =D Line always wants to get a list of points. In this case, Map is more suitable, e.g., Line /@ {{{10, 30}, {10, 42}}, {{38, 48}, {52, 18}}} (like @@@ is the infix form of Apply, /@ is the infix form of Map. $\endgroup$ – Henrik Schumacher Jun 9 '18 at 17:15
  • $\begingroup$ hello henrik. i tried to include them but apparently timed out at five minutes. the first three i get are: {{{13.56,27.4},{20.70,20.23}},{{14.00,30.00},{20.00,30.00}},{{13.56,32.6},{20.70,39.77}}} $\endgroup$ – James Bailey Jun 9 '18 at 17:20
  • $\begingroup$ Hi James, I see what you are about to do. You can also reference the vertices by their position in the list nodeCenterList and extract the midpoints automatically like this. If you plot the vertices as white Disks on top of the edges, you don't have to compute the points where an edge meets the circles: edges = {{1, 3}, {1, 4}, {1, 5}}; edgepts = Partition[nodeCenterList[[Flatten[edges], 1]], 2]; Graphics[{ Line /@ edgepts, EdgeForm[{Black, Thick}], FaceForm[White], Disk @@@ nodeCenterList }] $\endgroup$ – Henrik Schumacher Jun 9 '18 at 17:26
  • $\begingroup$ You might also be interested in Graph (with VertexCoordinates set to ). That's the datatype specific for to networks and there are many styling options. Here an example: Graph[ Range[Length[nodeCenterList]], edges, VertexCoordinates -> nodeCenterList[[All, 1]], VertexShapeFunction -> (Disk[#1, 4] &), VertexStyle -> Directive[Lighter@Red], EdgeShapeFunction -> (Line[#1] &), EdgeStyle -> Directive[Thick, Dashed, Black], VertexLabels -> Placed["Name", Center], VertexLabelStyle -> Directive[White, Italic, 14] ] $\endgroup$ – Henrik Schumacher Jun 9 '18 at 17:32

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.