Beispiel #1
0
        private void UpdateMiniBatch(TemporaryNetwork tempNet, List <DigitImageForStuding> batch, Parametrs param)
        {
            var nablaB = CreateEmptyContenerForBiases(tempNet);

            var nablaW = CreateEmptyContenerForWeights(tempNet);

            foreach (var singleBatch in batch)
            {
                var delta = Backprop(tempNet, singleBatch);
                for (var i = 0; i < nablaB.Count; i++)
                {
                    nablaB[i] = nablaB[i].Add(delta.DeltaBiases[i]);
                }

                for (var i = 0; i < nablaW.Count; i++)
                {
                    nablaW[i] = nablaW[i].Add(delta.DeltaWeights[i]);
                }
            }

            for (var i = 0; i < _weights.Count; i++)
            {
                var layerNumber = i + 2;
                //var first = _weights[i].Subtract((eta/batch.Count)*nablaW[i]); dodano L2
                var first = tempNet.GetWeightsForNLayer(layerNumber).Multiply(1 - param.Eta * (param.Lambda / param.NumberOfNeuronsInHiddenLayer)).Subtract((param.Eta / batch.Count) * nablaW[i]);


                //var momentum = param.Beta*(_weights[i].Subtract(_previousWeights[i])); Momentum na razie idzie do kosza

                tempNet.SetWeightsForNLayer(layerNumber, first);
            }

            for (var i = 0; i < _biases.Count; i++)
            {
                var layerNumber = i + 2;

                var newBias = tempNet.GetBiasOnNLayer(layerNumber).Subtract((param.Eta / batch.Count) * nablaB[i]);
                tempNet.SetBiasOnNLayer(layerNumber, newBias);
            }
        }
Beispiel #2
0
        private Delta Backprop(TemporaryNetwork tempNet, DigitImageForStuding singleBatch)
        {
            var nablaB = CreateEmptyContenerForBiases(tempNet);
            var nablaW = CreateEmptyContenerForWeights(tempNet);

            var activision  = singleBatch.Pixels;
            var activisions = new List <Matrix <double> > {
                activision
            };

            //list to store all z vectors, layer by layer
            var zs = new List <Matrix <double> >();

            for (var i = 0; i < 2; i++)
            {
                var layerNumber = i + 2;
                var z           = tempNet.GetWeightsForNLayer(layerNumber).Multiply(activision) + tempNet.GetBiasOnNLayer(layerNumber);
                zs.Add(z);
                activision = z.Map(_activationFunction.Function);
                activisions.Add(activision);
            }

            var part1 = CostDerivate(activisions[activisions.Count - 1], singleBatch.Label);
            var part2 = zs[zs.Count - 1].Map(_activationFunction.Calcucus);

            var delta = part1.PointwiseMultiply(part2);

            nablaB[nablaB.Count - 1] = delta;
            nablaW[nablaW.Count - 1] = delta.Multiply(activisions[activisions.Count - 2].Transpose());

            for (var i = 2; i < _numLayers; i++)
            {
                var z          = zs[zs.Count - i];
                var sp         = z.Map(_activationFunction.Calcucus);
                var layerLabel = _weights.Count - i + 1 + 2;
                delta = tempNet.GetWeightsForNLayer(layerLabel).Transpose().Multiply(delta).PointwiseMultiply(sp);
                nablaB[nablaB.Count - i] = delta;

                nablaW[nablaW.Count - i] = delta.Multiply(activisions[activisions.Count - i - 1].Transpose());
            }

            return(new Delta(nablaW, nablaB));
        }