Files
richard-churchman cc0a2bee21 Extensive modifications to the serialisation of Neural Network models to support Newtonsoft Json.net rather than Binary Serializer.
Included open-source Accord.net library code natively to the solution given the deprecated state of that project.  Accord.net will be supported inside Jube for own needs (such as upgrading to .Net 7+).

Accord.net libraries modified to compile to .Net 6.

Modified Accord.net libraries to better support Newtonsoft Json.net serialisation.

Included annotations in the Accord.net libraries to house important dependency statistics. Serialised json can be used independently of Jube to recall a Neural Network.

Removal of Binary Serializer totally as a precursor to the projects support of .Net 7+.

Extensive bug fixing in crucial functions of Neural Network training and recall.  While this part of the system remains in Alpha, it is substantially more fit for purpose.
2023-12-24 10:22:22 +01:00

158 lines
5.0 KiB
C#

// Accord Neural Net Library
// The Accord.NET Framework
// http://accord-framework.net
//
// Copyright © César Souza, 2009-2017
// cesarsouza at gmail.com
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
//
namespace Accord.Neuro.Layers
{
using Accord.Neuro.ActivationFunctions;
using Accord.Neuro.Neurons;
using System;
using Accord.Compat;
/// <summary>
/// Stochastic Activation Layer.
/// </summary>
///
/// <remarks>
/// This class represents a layer of <see cref="StochasticNeuron">stochastic neurons</see>.
/// </remarks>
///
[Serializable]
public class StochasticLayer : ActivationLayer
{
private new StochasticNeuron[] neurons;
private double[] sample;
/// <summary>
/// Gets the layer's neurons.
/// </summary>
///
public new StochasticNeuron[] Neurons
{
get { return neurons; }
}
/// <summary>
/// Gets the layer's sample values generated in the last
/// call of any of the <see cref="Generate"/> methods.
/// </summary>
///
public double[] Sample
{
get { return sample; }
}
/// <summary>
/// Initializes a new instance of the <see cref="StochasticLayer"/> class.
/// </summary>
///
/// <param name="neuronsCount">Layer's neurons count.</param>
/// <param name="inputsCount">Layer's inputs count.</param>
///
public StochasticLayer(int neuronsCount, int inputsCount)
: this(new BernoulliFunction(alpha: 1), neuronsCount, inputsCount) { }
/// <summary>
/// Initializes a new instance of the <see cref="StochasticLayer"/> class.
/// </summary>
///
/// <param name="function">The activation function for the neurons in the layer.</param>
/// <param name="neuronsCount">The neurons count.</param>
/// <param name="inputsCount">The inputs count.</param>
///
public StochasticLayer(IStochasticFunction function, int neuronsCount, int inputsCount)
: base(neuronsCount, inputsCount, function)
{
neurons = new StochasticNeuron[neuronsCount];
for (int i = 0; i < neurons.Length; i++)
base.neurons[i] = this.neurons[i] =
new StochasticNeuron(inputsCount, function);
}
/// <summary>
/// Compute output vector of the layer.
/// </summary>
///
/// <param name="input">Input vector.</param>
///
/// <returns>
/// Returns layer's output vector.
/// </returns>
///
public override double[] Compute(double[] input)
{
double[] output = new double[neuronsCount];
for (int i = 0; i < neurons.Length; i++)
output[i] = neurons[i].Compute(input);
this.output = output;
return output;
}
/// <summary>
/// Compute probability vector of the layer.
/// </summary>
///
/// <param name="input">Input vector.</param>
///
/// <returns>
/// Returns layer's probability vector.
/// </returns>
///
public double[] Generate(double[] input)
{
double[] sample = new double[neuronsCount];
double[] output = new double[neuronsCount];
for (int i = 0; i < neurons.Length; i++)
{
output[i] = neurons[i].Compute(input);
sample[i] = neurons[i].Generate(output[i]);
}
this.sample = sample;
this.output = output;
return sample;
}
/// <summary>
/// Copy the weights of another layer in reversed order. This
/// can be used to update visible layers from hidden layers and
/// vice-versa.
/// </summary>
///
/// <param name="layer">The layer to copy the weights from.</param>
///
public void CopyReversedWeightsFrom(StochasticLayer layer)
{
for (int i = 0; i < Neurons.Length; i++)
for (int j = 0; j < inputsCount; j++)
this.Neurons[i].Weights[j] = layer.Neurons[j].Weights[i];
}
}
}