C # in F #: creating and using float [] [] in F #

I have this simple method for a neural network in C #:

  private void InitNeurons()
    {
        //Neuron Initilization
        List<float[]> neuronsList = new List<float[]>();

        for (int i = 0; i < _layers.Length; i++) //run through all layers
        {
            neuronsList.Add(new float[_layers[i]]); //add layer to neuron list
        }

        _neurons = neuronsList.ToArray(); //convert list to array
    }

      

How do I do this in F #? I thought it might be easier. Perhaps something like this?

namespace NeuralNetwork

type NeuralNetworkF(layers : int[]) = 
    member this.Layers = layers
    member this.Neurons : float[][] = [| for layer in layers do new float[]|]

      

This results in a compiler error in F #. I want to take the size of the "_layers" array and for each value iterate over and add that value to the neuron member in F #.

+3


source to share


2 answers


The way you are trying to create an array new float[]

is not syntax in F #. The correct syntax for an array is - [| 1; 2; 3 |]

, or for an empty array, it should be easy [| |]

. The type of the elements does not need to be specified; the compiler will infer it from the context. But if you really want to be clear, you can still specify the type of a colon, as you can with any expression [| |] : float[]

.

However, this is all useless to you, because what you are trying to do is not create an array with known elements, but create an array of known length. There are several functions for this, and the one that will be useful to you is Array.create

. It takes two arguments, size and value, and returns an array of that size, where each element is that value.

type NeuralNetworkF(layers : int[]) = 
    member this.Layers = layers
    member this.Neurons : float[][] = [| for layer in layers -> Array.create layer 0.0 |]

      

Note that in the above code, I also replaced do

with ->

. This is the second problem you had. You see, do

performs an "action" that does not necessarily result in an element, for example:

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

      



This will render the number 6

in 10

- not 1

in 10

- because it only creates the element n > 5

. The keyword yield

is what creates the item. So, if you want to create an array for each layer of your code, you need yield

this array:

[| for layer in layers do yield Array.create layer 0.0 |]

      

It works, but there is a shortcut: the arrow ->

stands for do yield

, so you can use that instead.

[| for layer in layers -> Array.create layer 0.0 |]

      

Finally , a question for you: do you really need a class? The wibbly-wobbly-timey-whimey classes, so unless you have a compelling reason, I recommend going with the entry.

+4


source


There is a good explanation there, @FyodorSoikin, but the following is probably even nicer:



let newNeuralNetwork (layers : _ []) =
    fun i -> Array.zeroCreate<float> layers.[i]
    |> Array.init layers.Length

      

+4


source







All Articles