/// <summary> /// 分类问题中产生的如果是概率,需要把概率转换为编码结果 /// 后面再解码得到最终的分类 /// </summary> /// <param name="probability">概率</param> /// <param name="code">编码</param> public static void ProbabilityToCode(TensorOld probability, TensorOld code) { if (probability.Rank == 1) { probability = probability.Reshape(1, probability.ElementCount); } if (probability.Rank != 2) { throw new Exception("to do codec, Rank must be 2"); } if (probability.shape[1] == 1) { TensorOld.Apply(probability, code, a => a > 0.5 ? 1 : 0); } else { var buff = new double[probability.shape[1]]; for (int i = 0; i < probability.shape[0]; i++) { probability.GetByDim1(i, buff); ComputeCode(buff); Array.Copy(buff, 0, code.GetRawValues(), i * buff.Length, buff.Length); } } }
private void ComputeCrossEntropy(TensorOld y, TensorOld yHat) { var foreoutData = ForwardOutput.GetRawValues(); for (int i = 0; i < sampleNumber; i++) { //取出一个样本及其对应的Label y.GetByDim1(i, yBuff); yHat.GetByDim1(i, yHatBuff); //计算交叉熵 foreoutData[i] = Functions.CrossEntropy(yBuff, yHatBuff); } Array.Copy(y.values, 0, BackwardOutput.values, 0, y.ElementCount); }
public override double GetLoss(TensorOld y, TensorOld yHat) { var outData = ForwardOutput.GetRawValues(); var result = 0d; for (int i = 0; i < y.shape[0]; i++) { //取出一个样本及其对应的Label y.GetByDim1(i, yBuff); yHat.GetByDim1(i, yHatBuff); //计算交叉熵 result += Functions.CrossEntropy(yBuff, yHatBuff); } return(result / sampleNumber); }
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); } }