예제 #1
0
 unsafe public void WriteVector(NeuralComputationContext context, IntRange range, BufferedVector<float> values)
 {
     int len = values.Length;
     int begin = range.MinValue;
     var valueBuffer = GetValueBuff(context);
     fixed (float* pValues = GetValueArray(values), pValueBuffer = valueBuffer)
     {
         for (int i = 0; i < len; i++)
         {
             pValueBuffer[begin + i] = pValues[i];
         }
     }
 }
예제 #2
0
        unsafe public void ComputeError(NeuralComputationContext context, IntRange outputBuffer, BufferedVector<float> desiredOutputVector, IntRange errorBuffer, IntRange accumulationBuffer)
        {
            var valueBuffer = GetValueBuff(context);
            var mvb = desiredOutputVector.Owner as ManagedVectorBuffer<float>;
            var desiredOutputValues = mvb.GetArray(desiredOutputVector);

            fixed (float* pValueBuffer = valueBuffer, pDesiredOutputValues = desiredOutputValues)
            {
                int outputBegin = outputBuffer.MinValue;
                int errorBegin = errorBuffer.MinValue;
                int accBegin = accumulationBuffer.MinValue;
                int accCount = accumulationBuffer.MaxValue;
                for (int idx = 0; idx < desiredOutputVector.Length; idx++)
                {
                    float desiredValue = pDesiredOutputValues[idx];
                    float currentOutputValue = pValueBuffer[outputBegin + idx];
                    float error = desiredValue - currentOutputValue;
                    pValueBuffer[errorBegin + idx] = error;
                    pValueBuffer[accBegin + idx] += (float)Math.Pow(error * 0.5, 2.0);
                }
                pValueBuffer[accCount]++;
            }
        }
예제 #3
0
        internal static float[] GetValueArray(BufferedVector<float> values)
        {
            Contract.Requires(values != null);

            var mvb = values.Owner as ManagedVectorBuffer<float>;
            if (mvb == null) throw new InvalidOperationException("Vector source is unknown.");
            return mvb.GetArray(values);
        }
예제 #4
0
        protected override void ComputeError(VectorComputationContext context, BufferedVector<float> desiredOutputVector, int entryIndex, int entryCount)
        {
            var ctx = (NeuralComputationContext)context;

            base.ComputeError(context, desiredOutputVector, entryIndex, entryCount); // ErrorBuffer contains the error vector

            bool callAlgo = true;

            if ((Network.StructuralElementFlags & NNStructuralElement.BackwardImplementation) != 0) // aka BP or BPTT
            {
                callAlgo = BP_ErrorBasedComputed(ctx, entryIndex, entryCount);
            }
            else if (GCAlgo == GradientComputingAlgorithm.RTLR)
            {
                RTLR_ErrorBasedComputed(ctx);
            }

            if (callAlgo) CallErrorBasedStochasticLearningAlgorithms(ctx);
        }
예제 #5
0
 private static float[] GetValueArray(BufferedVector<float> values)
 {
     var mvb = values.Owner as ManagedVectorBuffer<float>;
     if (mvb == null) throw new InvalidOperationException("Vector source is unknown.");
     return mvb.GetArray(values);
 }
예제 #6
0
 unsafe protected override void DoWriteInput(NeuralComputationContext context, BufferedVector<float> values)
 {
     int len = values.Length;
     int begin = inputBuffer.MinValue;
     var valueBuffer = GetValueBuff(context);
     fixed (float* pValues = GetValueArray(values), pValueBuffer = valueBuffer)
     {
         for (int i = 0; i < len; i++)
         {
             pValueBuffer[begin + i] = pValues[i];
         }
     }
 }