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]; } } }
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]++; } }
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); }
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); }
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); }
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]; } } }