Exemple #1
0
 /// <summary>Sets the input array and creates and sets the output array.</summary>
 /// <param name="inputArray">The input array to be set.</param>
 /// <param name="inputSkip">The index of the first entry of the input array to be used.</param>
 /// <returns>The created output array.</returns>
 public float[] SetInputGetOutput(float[] inputArray, int inputSkip)
 {
     this.input      = inputArray;
     this.inputSkip  = inputSkip;
     this.outputSkip = 0;
     return(this.output = Backbone.CreateArray <float>(this.Length));
 }
Exemple #2
0
 /// <summary>Creates an instance of the <code>Autoencoder</code> class.</summary>
 /// <param name="encoder">The first part of the autoencoder.</param>
 /// <param name="decoder">The second part of the autoencoder.</param>
 /// <param name="codeSize">The lenght of the output of the first part of the autoencoder and the input of the second.</param>
 public Autoencoder(ILayer <TData, float[]> encoder, ILayer <float[], TData> decoder, int codeSize)
 {
     this.encoder   = encoder;
     this.decoder   = decoder;
     this.codeSize  = codeSize;
     this.error     = Backbone.CreateArray <float>(codeSize);
     this.siameseID = new object();
 }
Exemple #3
0
 /// <summary>Backpropagates the given error trough the network.</summary>
 /// <param name="outputErrorArray">The output error to be backpropagated.</param>
 /// <param name="outputErrorSkip">The index of the first entry of the output error array to be used.</param>
 /// <param name="learning">Whether the network is being used in a training session.</param>
 public void BackPropagate(float[] outputErrorArray, int outputErrorSkip, bool learning = true)
 {
     Backbone.CopyArray(outputErrorArray, outputErrorSkip, this.error2, 0, this.OutputSize);
     for (int i = this.Layers.Count - 1; i >= 0; i -= 1)
     {
         this.Layers.ElementAt(i).BackPropagate(this.error2, this.error1, learning);
         float[] aux = this.error1;
         this.error1 = this.error2;
         this.error2 = aux;
     }
 }
Exemple #4
0
        /// <summary>Either creates a siamese of the given <code>FeedForwardNN</code> instance or clones it.</summary>
        /// <param name="original">The original instance to be created a siamese of or cloned.</param>
        /// <param name="siamese"><code>true</code> if a siamese is to be created, <code>false</code> if a clone is.</param>
        protected FeedForwardNN(FeedForwardNN original, bool siamese) : base(original, siamese)
        {
            int maxLength = Math.Max(original.Layers.Max(delegate(IArraysLayer layer)
            {
                return(layer.InputSize);
            }), (original.LastLayer).OutputSize);

            this.error1          = Backbone.CreateArray <float>(maxLength);
            this.error2          = Backbone.CreateArray <float>(maxLength);
            this.layersConnected = false;
        }
Exemple #5
0
        /// <summary>Creates an instance of the <code>FeedForwardNN</code> class.</summary>
        /// <param name="layers">The layers to be included in the network.</param>
        public FeedForwardNN(ICollection <IArraysLayer> layers) : base(layers.ToArray())
        {
            int maxLength = Math.Max(layers.Max(delegate(IArraysLayer layer)
            {
                return(layer.InputSize);
            }), layers.Last().OutputSize);

            this.error1          = Backbone.CreateArray <float>(maxLength);
            this.error2          = Backbone.CreateArray <float>(maxLength);
            this.layersConnected = false;
        }
Exemple #6
0
 /// <summary>Creates an instance of the <code>NeuronsString</code> class.</summary>
 /// <param name="length">The lenght of the layer.</param>
 /// <param name="createIO">Whether the input array and the output array of the layer are to be created.</param>
 public NeuronsString(int length, bool createIO = false)
 {
     if (createIO)
     {
         this.input      = Backbone.CreateArray <float>(length);
         this.output     = Backbone.CreateArray <float>(length);
         this.inputSkip  = 0;
         this.outputSkip = 0;
     }
     this.length    = length;
     this.siameseID = new object();
 }
Exemple #7
0
 /// <summary>Either creates a siamese of the given <code>BiasedConnectionMatrix</code> instance or clones it.</summary>
 /// <param name="original">The original instance to be created a siamese of or cloned.</param>
 /// <param name="siamese"><code>true</code> if a siamese is to be created, <code>false</code> if a clone is.</param>
 protected BiasedConnectionMatrix(BiasedConnectionMatrix original, bool siamese) : base(original, siamese)
 {
     if (siamese)
     {
         this.biases        = original.biases;
         this.biasGradients = original.biasGradients;
         this.biasMomentum  = original.biasMomentum;
     }
     else
     {
         this.biases        = Backbone.CreateArray <float>(original.OutputSize);
         this.biasGradients = Backbone.CreateArray <float>(original.OutputSize);
         this.biasMomentum  = Backbone.CreateArray <float>(original.OutputSize);
     }
 }
 /// <summary>Creates an instance of the <code>ConnectionMatrix</code> class.</summary>
 /// <param name="inputSize">The size of the input of the layer.</param>
 /// <param name="outputSize">The size of the output of the layer.</param>
 /// <param name="createIO">Whether the input array and the output array of the layer are to be created.</param>
 public ConnectionMatrix(int inputSize, int outputSize, bool createIO = false)
 {
     if (createIO)
     {
         this.input      = Backbone.CreateArray <float>(inputSize);
         this.output     = Backbone.CreateArray <float>(outputSize);
         this.inputSkip  = 0;
         this.outputSkip = 0;
     }
     this.inputSize  = inputSize;
     this.outputSize = outputSize;
     this.weights    = Backbone.CreateArray <float>(inputSize, outputSize);
     Backbone.RandomizeMatrix(this.weights, inputSize, outputSize, 2.0F / (inputSize + outputSize));
     this.gradients = Backbone.CreateArray <float>(inputSize, outputSize);
     this.momentum  = Backbone.CreateArray <float>(inputSize, outputSize);
     this.siameseID = new object();
 }
Exemple #9
0
 /// <summary>Either creates a siamese of the given <code>Autoencoder</code> instance or clones it.</summary>
 /// <param name="original">The original instance to be created a siamese of or cloned.</param>
 /// <param name="siamese"><code>true</code> if a siamese is to be created, <code>false</code> if a clone is.</param>
 protected Autoencoder(Autoencoder <TData, TErrFunc> original, bool siamese)
 {
     if (siamese)
     {
         this.encoder   = original.encoder.CreateSiamese();
         this.decoder   = original.decoder.CreateSiamese();
         this.codeSize  = original.CodeSize;
         this.error     = Backbone.CreateArray <float>(original.CodeSize);
         this.siameseID = original.SiameseID;
     }
     else
     {
         this.encoder   = original.encoder.Clone();
         this.decoder   = original.decoder.Clone();
         this.codeSize  = original.CodeSize;
         this.error     = Backbone.CreateArray <float>(original.CodeSize);
         this.siameseID = new object();
     }
 }
 /// <summary>Either creates a siamese of the given <code>ConnectionMatrix</code> instance or clones it.</summary>
 /// <param name="original">The original instance to be created a siamese of or cloned.</param>
 /// <param name="siamese"><code>true</code> if a siamese is to be created, <code>false</code> if a clone is.</param>
 protected ConnectionMatrix(ConnectionMatrix original, bool siamese)
 {
     this.inputSize  = original.InputSize;
     this.outputSize = original.OutputSize;
     if (siamese)
     {
         this.weights   = original.Weights;
         this.gradients = original.Gradients;
         this.momentum  = original.Momentum;
         this.siameseID = original.SiameseID;
     }
     else
     {
         this.weights = Backbone.CreateArray <float>(original.InputSize, original.OutputSize);
         Backbone.RandomizeMatrix(this.weights, original.InputSize, original.OutputSize, 2.0F / (original.InputSize + original.OutputSize));
         this.gradients = Backbone.CreateArray <float>(original.InputSize, original.OutputSize);
         this.momentum  = Backbone.CreateArray <float>(original.InputSize, original.OutputSize);
         this.siameseID = new object();
     }
 }
 /// <summary>Updates the weights of the layer.</summary>
 /// <param name="rate">The learning rate to be used.</param>
 /// <param name="momentum">The momentum to be used.</param>
 public virtual void UpdateWeights(float rate, float momentum = 0.0F)
 {
     Backbone.UpdateConnectionMatrix(this.Weights, this.Gradients, this.Momentum, this.InputSize, this.OutputSize, rate, momentum);
 }
Exemple #12
0
 /// <summary>Backpropagates the given error trough the layer.</summary>
 /// <param name="outputErrorArray">The output error to be backpropagated.</param>
 /// <param name="outputErrorSkip">The index of the first entry of the output error array to be used.</param>
 /// <param name="inputErrorArray">The array to be written the input error into.</param>
 /// <param name="inputErrorSkip">The index of the first entry of the input error array to be used.</param>
 /// <param name="learning">Whether the layer is being used in a training session.</param>
 public override void BackPropagate(float[] outputErrorArray, int outputErrorSkip, float[] inputErrorArray, int inputErrorSkip, bool learning)
 {
     Backbone.BackpropagateDropout(this.Input, this.InputSkip, this.Output, this.OutputSkip, this.Length, this.dropped, this.DropChance, learning, outputErrorArray, outputErrorSkip, inputErrorArray, inputErrorSkip);
 }
Exemple #13
0
 /// <summary>Feeds this layer forward.</summary>
 /// <param name="learning">Whether the layer is being used in a training session.</param>
 public override void Feed(bool learning)
 {
     Backbone.ApplyDropout(this.Input, this.InputSkip, this.Output, this.OutputSkip, this.Length, this.dropped, this.DropChance, learning);
 }
Exemple #14
0
 /// <summary>Creates an instance of the <code>DropoutLayer</code> class.</summary>
 /// <param name="length">The lenght of the layer.</param>
 /// <param name="dropChance">The dropout chance of the layer.</param>
 /// <param name="createIO">Whether the input array and the output array of the layer are to be created.</param>
 public DropoutLayer(int length, float dropChance, bool createIO = false) : base(length, createIO)
 {
     this.dropped    = Backbone.CreateArray <bool>(this.Length);
     this.dropChance = dropChance;
 }
Exemple #15
0
 /// <summary>Either creates a siamese of the given <code>DropoutLayer</code> instance or clones it.</summary>
 /// <param name="original">The original instance to be created a siamese of or cloned.</param>
 /// <param name="siamese"><code>true</code> if a siamese is to be created, <code>false</code> if a clone is.</param>
 protected DropoutLayer(DropoutLayer original, bool siamese) : base(original, siamese)
 {
     this.dropped    = Backbone.CreateArray <bool>(original.Length);
     this.dropChance = original.DropChance;
 }
Exemple #16
0
 /// <summary>Creates an array which can be used as output error.</summary>
 /// <returns>The created array.</returns>
 protected override float[] NewError()
 {
     return(Backbone.CreateArray <float>(this.OutputSize));
 }
Exemple #17
0
 /// <summary>Feeds the layer forward.</summary>
 /// <param name="learning">Whether the layer is being used in a training session.</param>
 public override void Feed(bool learning = false)
 {
     Backbone.ApplySoftmax(this.Input, this.InputSkip, this.Output, this.OutputSkip, this.Length);
 }
Exemple #18
0
 /// <summary>Feeds the layer forward.</summary>
 /// <param name="learning">Whether the layer is being used in a training session.</param>
 public virtual void Feed(bool learning = false)
 {
     Backbone.ApplyNeuronsString(this.input, this.inputSkip, this.output, this.outputSkip, this.length, this.Activation);
 }
Exemple #19
0
 /// <summary>Sets the input array of the network and creates the output array.</summary>
 /// <param name="inputArray">The input array to be set.</param>
 /// <param name="inputSkip">The index of the first entry of the output array to be used.</param>
 /// <returns>The created output array.</returns>
 public float[] SetInputGetOutput(float[] inputArray, int inputSkip)
 {
     float[] retVal = Backbone.CreateArray <float>(this.OutputSize);
     this.SetInputAndOutput(inputArray, InputSkip, retVal, 0);
     return(retVal);
 }
Exemple #20
0
 /// <summary>Backpropagates the given error trough the network.</summary>
 /// <param name="outputErrorArray">The error to be backpropagated.</param>
 /// <param name="outputErrorSkip">The index of the first entry of the output error to be used.</param>
 /// <param name="inputErrorArray">The array to be written the input error into.</param>
 /// <param name="inputErrorSkip">The index of the first entry of the input error to be used.</param>
 /// <param name="learning">Whether the layer is being used in a learning session.</param>
 public override void BackPropagate(float[] outputErrorArray, int outputErrorSkip, float[] inputErrorArray, int inputErrorSkip, bool learning)
 {
     Backbone.BackpropagateSoftmax(this.Input, this.InputSkip, this.Output, this.OutputSkip, this.Length, outputErrorArray, outputErrorSkip, inputErrorArray, inputErrorSkip, learning);
 }
Exemple #21
0
 /// <summary>Backpropagates the given error trough the layer.</summary>
 /// <param name="outputErrorArray">The output error to be backpropagated.</param>
 /// <param name="outputErrorSkip">The index of the first position of the output error array to be used.</param>
 /// <param name="inputErrorArray">The array to be written the input error into.</param>
 /// <param name="inputErrorSkip">The index of the first position of the input error array to be used.</param>
 /// <param name="learning"></param>
 public override void BackPropagate(float[] outputErrorArray, int outputErrorSkip, float[] inputErrorArray, int inputErrorSkip, bool learning)
 {
     Backbone.BackpropagateBiasedConnectionMatrix(this.Input, this.InputSkip, this.InputSize, this.Output, this.OutputSkip, this.OutputSize, this.Weights, this.biases, outputErrorArray, outputErrorSkip, inputErrorArray, inputErrorSkip, this.Gradients, this.biasGradients, learning);
 }
Exemple #22
0
 /// <summary>Creates an instance of the <code>BiasedConnectionMatrix</code> class.</summary>
 /// <param name="inputSize">The length of the input of the layer.</param>
 /// <param name="outputSize">The lenght of the output of the layer.</param>
 /// <param name="createIO">Whether the input array and the output array are to be created.</param>
 public BiasedConnectionMatrix(int inputSize, int outputSize, bool createIO = false) : base(inputSize, outputSize, createIO)
 {
     this.biases        = Backbone.CreateArray <float>(this.OutputSize);
     this.biasGradients = Backbone.CreateArray <float>(this.OutputSize);
     this.biasMomentum  = Backbone.CreateArray <float>(this.OutputSize);
 }
Exemple #23
0
 /// <summary>Backpropagates the layer.</summary>
 /// <param name="outputErrorArray">The output error of the layer.</param>
 /// <param name="outputErrorSkip">The index of the first entry of the output error to be used.</param>
 /// <param name="inputErrorArray">The array to be written the input error into.</param>
 /// <param name="inputErrorSkip">The index of the first entry of the input layer to be used.</param>
 /// <param name="learning">Whether the layer is being used in a learning session.</param>
 public virtual void BackPropagate(float[] outputErrorArray, int outputErrorSkip, float[] inputErrorArray, int inputErrorSkip, bool learning)
 {
     Backbone.BackpropagateNeuronsString(this.input, this.inputSkip, this.output, this.outputSkip, this.length, outputErrorArray, outputErrorSkip, inputErrorArray, inputErrorSkip, this.ActivationDerivative, learning);
 }
Exemple #24
0
 /// <summary>Feeds the layer forward.</summary>
 /// <param name="learning">Whether the layer is being used in a training session. Unused.</param>
 public override void Feed(bool learning = false)
 {
     Backbone.ApplyBiasedConnectionMatrix(this.Input, this.InputSkip, this.InputSize, this.Output, this.OutputSkip, this.OutputSize, this.Weights, this.biases);
 }
Exemple #25
0
 /// <summary>Backpropagates the given error trough the network.</summary>
 /// <param name="outputErrorArray">The output error to be backpropagated.</param>
 /// <param name="outputErrorSkip">The index of the first entry of the output error to be used.</param>
 /// <param name="inputErrorArray">The array to be written the input error into.</param>
 /// <param name="inputErrorSkip">The index of the first entry of the output error array to be used.</param>
 /// <param name="learning">Whether the network is being used in a training session.</param>
 public void BackPropagate(float[] outputErrorArray, int outputErrorSkip, float[] inputErrorArray, int inputErrorSkip, bool learning)
 {
     this.BackPropagate(outputErrorArray, outputErrorSkip, learning);
     Backbone.CopyArray(this.error2, 0, inputErrorArray, inputErrorSkip, this.InputSize);
 }
Exemple #26
0
 /// <summary>Updates the weights of the layer.</summary>
 /// <param name="rate">The learning rate to be used.</param>
 /// <param name="momentum">The momentum to be used.</param>
 public override void UpdateWeights(float rate, float momentum = 0)
 {
     Backbone.UpdateBiasedConnectionMatrix(Weights, Gradients, Momentum, biases, biasGradients, this.biasMomentum, InputSize, OutputSize, rate, momentum);
 }
 /// <summary>Feeds the layer forward.</summary>
 /// <param name="learning">Whether the layer is being used in a training session. Unused.</param>
 public virtual void Feed(bool learning = false)
 {
     Backbone.ApplyConnectionMatrix(this.Input, this.InputSkip, this.InputSize, this.Output, this.OutputSkip, this.OutputSize, this.Weights);
 }