/// <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); } } }
public static void SquareError(TensorOld y, TensorOld yHat, TensorOld result) { TensorOld.Minus(y, yHat, result); result.Apply(a => a * a); }
/// <summary> /// 根据gradient优化target /// </summary> /// <param name="target">优化的目标</param> /// <param name="graident">目标的梯度</param> public void Optimize(TensorOld target, TensorOld graident) { TensorOld.Apply(target, graident, target, (a, b) => a - LearningRate * b); }