Exemplo n.º 1
0
        public void BackPropagation(TrainTuple train)
        {
            var nablaB = Biases.Select(it => Vector <double> .Build.Dense(it.Count, 0)).ToList();
            var nablaW = Weights.Select(it =>
                                        Matrix <double> .Build.Dense(it.RowCount, it.ColumnCount, 0)).ToList();

            var activation = Vector <double> .Build.DenseOfEnumerable(train.Input.Select(it => (double)it));

            var activations = new List <Vector <double> > {
                activation
            };
            var zs = new List <Vector <double> >();

            var weightsWithBiases = Biases.Zip(Weights, (vector, matrix) => (vector, matrix));

            foreach (var(bias, weights) in weightsWithBiases)
            {
                var z = weights.TransposeThisAndMultiply(activation) + bias;
                zs.Add(z);
                activations.Add(z.Map(Sigmoid));
            }

            var expected = Vector <double> .Build.DenseOfEnumerable(train.Output.Select(it => (double)it));

            var delta = CostDerivative(activations.Last(), expected) *
                        zs.Last().Map(SigmoidPrime);

            //nablaB[^0] = delta;
        }
Exemplo n.º 2
0
 public bool Equals(NeuralNetworkLayerModel?other)
 {
     if (other is null)
     {
         return(false);
     }
     if (ReferenceEquals(this, other))
     {
         return(true);
     }
     return
         ((Id == null && other.Id == null ||
           Id?.Equals(other.Id) == true) &&
          Index == other.Index &&
          Inputs == other.Inputs &&
          Outputs == other.Outputs &&
          (Weights == null && other.Weights == null ||
           Weights?.Length == other.Weights?.Length &&
           Weights?.Zip(other.Weights).All(t => MathF.Abs(t.First - t.Second) < 0.001f) == true) &&
          (Biases == null && other.Biases == null ||
           Biases?.Length == other.Biases?.Length &&
           Biases?.Zip(other.Biases).All(t => MathF.Abs(t.First - t.Second) < 0.001f) == true) &&
          Activation == other.Activation &&
          LastUpdateTime.Equals(other.LastUpdateTime));
 }
Exemplo n.º 3
0
        public Vector <double> Feedforward(IEnumerable <double> input)
        {
            var output = Vector <double> .Build.DenseOfEnumerable(input);

            var weightsWithBiases = Biases.Zip(Weights, (vector, matrix) => (vector, matrix));

            foreach (var(bias, weights) in weightsWithBiases)
            {
                output = (weights.TransposeThisAndMultiply(output) + bias).Map(Sigmoid);
            }

            return(output);
        }
Exemplo n.º 4
0
        public Vector <double> Feedforward(IEnumerable <byte> input)
        {
            var output = Vector <double> .Build.DenseOfEnumerable(input.Select(it => (double)it));

            var biasesWithWeights = Biases.Zip(Weights, (vector, matrix) => (vector, matrix));

            foreach (var(bias, weights) in biasesWithWeights)
            {
                output = Sigmoid(weights.Multiply(output) + bias);
            }

            return(output);
        }
Exemplo n.º 5
0
        public Vector <double> BackPropagation(TrainTuple train, double speed)
        {
            var activations = new List <Vector <double> >
            {
                Vector <double> .Build.DenseOfEnumerable(train.Input.Select(it => (double)it))
            };

            var weightedSums = new List <Vector <double> >();

            var biasesWithWeights = Biases.Zip(Weights, (vector, matrix) => (vector, matrix));

            foreach (var(biases, weights) in biasesWithWeights)
            {
                var weightedSum = weights * activations[^ 0] + biases;