1
$\begingroup$

If I define

network[n_List]:=Thread[
    (<|"w"->Table[RandomVariate[NormalDistribution[]],#2, #1],
       "b"-> Table[RandomVariate[NormalDistribution[]],#2]|> &)[Most@n,Rest@n]];

and attempt, for example

network[{1,2,3}]

I get

Table::itraw: Raw object 2 cannot be used as an iterator. >>

What's going on here? How can I thread the association over my arguments?

$\endgroup$
  • $\begingroup$ Would MapThread be more appropriate? You would get a list of associations. I can't tell if that's what you're after. $\endgroup$ – Michael E2 Mar 12 '16 at 19:55
  • $\begingroup$ @MichaelE2: The desired result would be what I'd get from Thread[l[Most@n,Rest@n]]/.l->(<|"w"-> Table[RandomVariate[NormalDistribution[]],#2,#1], "b"-> Table[RandomVariate[NormalDistribution[]],#2]|> &). How would I do that with MapThread? $\endgroup$ – orome Mar 12 '16 at 19:58
3
$\begingroup$

Something like this?

network[n_List] := 
  MapThread[
   (<|"w" -> Table[RandomVariate[NormalDistribution[]], #2, #1], 
      "b" -> Table[RandomVariate[NormalDistribution[]], #2]|> &),
   {Most@n, Rest@n}];

SeedRandom[0];
network[{1, 2, 3}]
(*
  {<|"w" -> {{-0.619487}, {-0.798718}},
     "b" -> {0.36195, -1.09919}|>,
   <|"w" -> {{-1.30657, 0.74753}, {-0.283601, 0.0598676}, {0.509336, -0.663203}}, 
     "b" -> {0.452192, -0.27623, -0.457804}|>}
*)
$\endgroup$
  • $\begingroup$ does it thread through association or throug {Most@n, Rest@n} part? :) $\endgroup$ – garej Mar 12 '16 at 21:55
  • $\begingroup$ @garej It threads through {Most@n, Rest@n}, which is what happens in the OP's code in the response to my query in the comments. $\endgroup$ – Michael E2 Mar 12 '16 at 21:57
3
$\begingroup$

With Thread:

network[n_List] := Activate@Thread[Inactivate[
           <|"w" -> Table[RandomVariate[NormalDistribution[]], #2, #1], 
             "b" -> Table[RandomVariate[NormalDistribution[]], #2]|> &][Most@n, Rest@n]];

SeedRandom[0];
network[{1, 2, 3}]

(* {<|"w" -> {{-0.619487}, {-0.798718}}, 
      "b" -> {0.36195, -1.09919}|>, 
    <|"w" -> {{-1.30657, 0.74753}, {-0.283601, 0.0598676}, {0.509336, -0.663203}}, 
      "b" -> {0.452192, -0.27623, -0.457804}|>} *)
$\endgroup$
1
$\begingroup$

To make it more readable v, c stand for RandomVariate[NormalDistribution[]]

network[n_List] := 
  (<|"w" -> Table[v, #2, #1], "b" -> Table[c, #2]|> &) @@@ Partition[n, 2, 1];

{<|"w" -> {{v}, {v}}, "b" -> {c, c}|>,

<|"w" -> {{v, v}, {v, v}, {v, v}}, "b" -> {c, c, c}|>}

$\endgroup$
  • $\begingroup$ The Thread is not necessary in this situation, as the evaluation within it is performed before it fires. This definition of network will return the same result if Thread is removed. $\endgroup$ – user31159 Mar 12 '16 at 20:40
  • $\begingroup$ @Xavier, I was trying to be closer to OP and then decided to make an imprivement with Partition :)) $\endgroup$ – garej Mar 12 '16 at 21:06
  • $\begingroup$ I see :) The @@@ and Partition is nice. +1 $\endgroup$ – user31159 Mar 12 '16 at 21:09
  • $\begingroup$ @Xavier, Thank you, +1 for your version as well, I did not know about Activate construct. $\endgroup$ – garej Mar 12 '16 at 21:11

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.