arrays - C# to F#: Creating and using a float[][] in F# -


i have simple method neural network in c#:

  private void initneurons()     {         //neuron initilization         list<float[]> neuronslist = new list<float[]>();          (int = 0; < _layers.length; i++) //run through layers         {             neuronslist.add(new float[_layers[i]]); //add layer neuron list         }          _neurons = neuronslist.toarray(); //convert list array     } 

how do in f#? thinking simpler. perhaps this?

namespace neuralnetwork  type neuralnetworkf(layers : int[]) =      member this.layers = layers     member this.neurons : float[][] = [| layer in layers new float[]|] 

this causes compiler error in f#. want take size of int array "_layers" , each value iterate on , add value neurons member in f#.

the way you're trying create array new float[] not valid syntax in f#. correct syntax array [| 1; 2; 3 |], or empty array [| |]. type of elements not need specified, compiler infer context. if really want explicit, can still specify type via colon, can expression: [| |] : float[].

however, useless anyway, because you're trying not create array known elements, create array of known length. that, there few functions, , 1 useful here array.create. takes 2 arguments - size , value, - , returns array of size, every element value.

type neuralnetworkf(layers : int[]) =      member this.layers = layers     member this.neurons : float[][] = [| layer in layers -> array.create layer 0.0 |] 

note in code above i've replaced do ->. second problem had. see, do performs "action", not result in item, e.g.:

let xs = [ n in 1..10               if n > 5 yield n ] 

this produce list of numbers 6 10 - not 1 10 - because produces element when n > 5. yield keyword produces element. so, if wanted produce array every layer in code, you'd have yield array:

[| layer in layers yield array.create layer 0.0 |] 

this works, there shortcut: arrow -> stands do yield, can use instead.

[| layer in layers -> array.create layer 0.0 |] 

finally, question you: need class? classes wibbly-wobbly-timey-whimey, unless have reason, recommend going record.


Comments

Popular posts from this blog

javascript - Create a stacked percentage column -

Optimising Firebase database by automatically overwriting data -

javascript - Angular UI-Grid customTemplate directive causing rows to load slowly/? -