public Matrix <float>[] Visit(Matrix <float>[] incoming, BackpropParams par)
 {
     // Upsample the error by first taking the Kronecker product of a matrix of 1's the same size as the pooling region.
     // Then divide by the total size of the pooling region, thus distributing the error by mean.
     for (int j = 0; j < _unit.ChannelCount; j++)
     {
         incoming[j].KroneckerProduct(_ones, _outgoing[j]);
         _outgoing[j].Divide(_unit.PoolSize * _unit.PoolSize, _outgoing[j]);
     }
     return(_outgoing);
 }
Beispiel #2
0
 public Matrix <float>[] Visit(Matrix <float>[] incoming, BackpropParams par)
 {
     // Upsample the error by first taking the Kronecker product of a matrix of 1's the same size as the pooling region.
     // Then pointwise multiply by the pooling layer's distribution matrix.
     for (int j = 0; j < _unit.ChannelCount; j++)
     {
         incoming[j].KroneckerProduct(_ones, _outgoing[j]);
         _outgoing[j].PointwiseMultiply(_unit.Distribution[j], _outgoing[j]);
     }
     return(_outgoing);
 }
Beispiel #3
0
        public void InitializeTraining(BackpropParams par)
        {
            _params = par;
            _loss   = Vector <float> .Build.Dense(_output.Size());

            _backprop = new List <Backprop <Vector <float>, Vector <float> > >();
            _backprop.Add(new DenseLayerBackprop(_output));
            for (int i = _hidden.Length - 1; i >= 0; i--)
            {
                _backprop.Add(new DenseLayerBackprop(_hidden[i]));
            }
        }
Beispiel #4
0
 public Matrix <float>[] Visit(Vector <float> incoming, BackpropParams par)
 {
     // Unflatten error.
     for (int j = 0; j < _unit.Z; j++)
     {
         for (int m = 0; m < _unit.X; m++)
         {
             for (int n = 0; n < _unit.Y; n++)
             {
                 _outgoing[j].At(m, n, incoming.At(j * _unit.X * _unit.Y + m * _unit.Y + n));
             }
         }
     }
     return(_outgoing);
 }
Beispiel #5
0
        public Matrix <float>[] Visit(Matrix <float>[] incoming, BackpropParams par)
        {
            int stride = _unit.Stride;
            int fsize  = _unit.Weights[0][0].RowCount;
            int offset = fsize / 2;
            // Clear next layer's error and cache input from previous forward pass.
            var inp = _unit.Prev.Output();

            for (int i = 0; i < _unit.Prev.ChannelCount; i++)
            {
                _padded[i].Clear();
                _input[i].SetSubMatrix(offset, _unit.Prev.SideLength, offset, _unit.Prev.SideLength, inp[i]);
            }
            for (int j = 0; j < _unit.ChannelCount; j++)
            {
                // Multiply incoming error term with derivative of this layer's activation function.
                _unit.Activation.Derivatives(_unit.Output()[j], _ebuf);
                incoming[j].PointwiseMultiply(_ebuf, incoming[j]);
                // Propagate error to next layer. Adjust weights and biases.
                for (int i = 0; i < _unit.Prev.ChannelCount; i++)
                {
                    _deltas[i][j].Multiply(par.Momentum, _deltas[i][j]); // Apply momentum.
                    for (int m = 0; m < incoming[j].RowCount; m++)
                    {
                        for (int n = 0; n < incoming[j].ColumnCount; n++)
                        {
                            // Propagate error.
                            _unit.Weights[i][j].Multiply(incoming[j].At(m, n), _fbuf);
                            _subm.SetSubMatrix(0, m * stride, fsize, 0, n * stride, fsize, _padded[i]);
                            _subm.Add(_fbuf, _subm);
                            _padded[i].SetSubMatrix(m * stride, fsize, n * stride, fsize, _subm);
                            // Calculate deltas.
                            _subm.SetSubMatrix(0, m * stride, fsize, 0, n * stride, fsize, _input[i]);
                            _subm.Multiply(incoming[j].At(m, n) * par.LearningRate, _fbuf);
                            _deltas[i][j].Add(_fbuf, _deltas[i][j]);
                        }
                    }
                    _unit.Weights[i][j].Multiply(1f - par.Decay, _unit.Weights[i][j]);                   // Weight decay.
                    _unit.Weights[i][j].Add(_deltas[i][j], _unit.Weights[i][j]);                         // Adjust weights.
                }
                _unit.Biases.At(j, _unit.Biases.At(j) + incoming[j].RowSums().Sum() * par.LearningRate); // Adjust biases.
            }
            for (int i = 0; i < _unit.Prev.ChannelCount; i++)
            {
                _outgoing[i].SetSubMatrix(0, offset, _unit.Prev.SideLength, 0, offset, _unit.Prev.SideLength, _padded[i]);
            }
            return(_outgoing);
        }
Beispiel #6
0
        public void InitializeTraining(BackpropParams par)
        {
            _params = par;
            _loss   = Vector <float> .Build.Dense(OutputLayer.Size());

            _backprop = new List <Backprop <Matrix <float>[], Matrix <float>[]> >();
            for (int i = ConvolutionalLayers.Length - 1; i >= 0; i--)
            {
                _backprop.Add(new MeanPoolLayerBackprop(SubSampleLayers[i]));
                _backprop.Add(new ConvolutionalLayerBackprop(ConvolutionalLayers[i]));
            }
            _unflatten      = new FlattenLayerBackprop(FlattenLayer);
            _hiddenBackprop = new DenseLayerBackprop(LinearHiddenLayer);
            _split          = new TreeLayerBackprop(CombinationLayer);
            _outback        = new DenseLayerBackprop(OutputLayer);
        }
Beispiel #7
0
 public QLearningCNN(bool prioritizedSweeping, QOption option)
 {
     PrioritySweeping = prioritizedSweeping;
     Discretize       = option.Discretize;
     TrainingInterval = option.TrainingInterval;
     TrainingCycles   = option.TrainingCycle;
     EpsilonStart     = option.EpsilonStart;
     EpsilonEnd       = option.EpsilonEnd;
     Discount         = option.Discount;
     BatchSize        = PrioritySweeping ? 5 : option.BatchSize;
     MaxStoreSize     = PrioritySweeping ? 30 : option.MaxPoolSize;
     LearningParams   = new BackpropParams {
         LearningRate = option.LearningRate, Momentum = 0.9f, Decay = 0.0f
     };
     _networkArgs = option.NetworkArgs;
 }
Beispiel #8
0
 public Vector <float> Visit(Vector <float> incoming, BackpropParams par)
 {
     // Multiply incoming error term with derivative of this layer's activation function.
     _unit.Activation.Derivatives(_unit.Output(), _vbuf);
     incoming.PointwiseMultiply(_vbuf, incoming);
     // Calculate outgoing error term (first factor of next layer's error) based on weights and errors in this layer.
     _unit.Weights.TransposeThisAndMultiply(incoming, _outgoing);
     // Calculate delta weights (applying momentum).
     incoming.OuterProduct(_unit.Prev.Output(), _mbuf);
     _mbuf.Multiply(par.LearningRate, _mbuf);
     _deltas.Multiply(par.Momentum, _deltas);
     _deltas.Add(_mbuf, _deltas);
     // Adjust weights and biases.
     _unit.Weights.Multiply(1f - par.Decay, _unit.Weights);
     _unit.Weights.Add(_deltas, _unit.Weights);
     incoming.Multiply(par.LearningRate, _vbuf);
     _unit.Biases.Add(_vbuf, _unit.Biases);
     return(_outgoing);
 }
Beispiel #9
0
 public VectorPair Visit(Vector <float> incoming, BackpropParams par)
 {
     incoming.CopySubVectorTo(_outgoing.left, 0, 0, _unit.LeftSize);
     incoming.CopySubVectorTo(_outgoing.right, _unit.LeftSize, 0, _unit.RightSize);
     return(_outgoing);
 }