private void ComputeCrossEntropy(TensorOld y, TensorOld yHat) { var forwardoutData = ForwardOutput.GetRawValues(); var backwardoutData = BackwardOutput.GetRawValues(); for (int i = 0; i < sampleNumber; i++) { //取出一个样本及其对应的Label y.GetByDim1(i, yBuff); yHat.GetByDim1(i, yHatBuff); //计算交叉熵 forwardoutData[i] = Functions.CrossEntropy(yBuff, yHatBuff); //计算损失函数关于输入的导数 Derivatives.CrossEntropy(yBuff, yHatBuff, derBuff); Array.Copy(derBuff, 0, backwardoutData, i * derBuff.Length, derBuff.Length); } }
/// <summary> /// 计算Loss和Loss对yHat的导数(梯度) /// </summary> /// <param name="y"></param> /// <param name="yHat"></param> public override void Compute(TensorOld y, TensorOld yHat) { //只记录了平方误差,使用GetLoss可获取均方误差 Functions.SquareError(y, yHat, ForwardOutput); Derivatives.MeanSquareError(y, yHat, BackwardOutput); }