Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
    
Would MapThread be more appropriate? You would get a list of associations. I can't tell if that's what you're after. – Michael E2 Mar 12 at 19:55
    
@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? – raxacoricofallapatorius Mar 12 at 19:58
up vote 3 down vote accepted

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}|>}
*)
share|improve this answer
    
does it thread through association or throug {Most@n, Rest@n} part? :) – garej Mar 12 at 21:55
    
@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. – Michael E2 Mar 12 at 21:57

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}|>} *)
share|improve this answer

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}|>}

share|improve this answer
    
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. – Xavier Mar 12 at 20:40
    
@Xavier, I was trying to be closer to OP and then decided to make an imprivement with Partition :)) – garej Mar 12 at 21:06
    
I see :) The @@@ and Partition is nice. +1 – Xavier Mar 12 at 21:09
    
@Xavier, Thank you, +1 for your version as well, I did not know about Activate construct. – garej Mar 12 at 21:11

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.