/// <summary> /// 运行前的准备,用于初始化所有Tensor的结构 /// </summary> /// <param name="input">输入Tensor</param> /// <returns></returns> public override TensorOld PrepareTrain(TensorOld input) { ForwardOutput = input.GetSameShape(); BackwardOutput = input.GetSameShape(); Derivative = input.GetSameShape(); return(ForwardOutput); }
/// <summary> /// 运行前的准备,用于初始化所有Tensor的结构 /// </summary> /// <param name="input">输入Tensor</param> /// <returns></returns> public override TensorOld PrepareTrain(TensorOld input) { if (input.Rank != 2) { throw new TensorShapeException("input.Rank must be 2"); } ForwardOutput = input.GetSameShape(); BackwardOutput = input.GetSameShape(); sampleNumber = input.shape[0]; categoryNumber = input.shape[1]; sampleBuff = new double[categoryNumber]; return(ForwardOutput); }
public TensorOld Normalize(TensorOld input) { var result = input.GetSameShape(); Normalize(input, result); return(result); }
public TensorOld PrepareTrain(TensorOld input) { PreparePredict(input); BackwardOutput = input.GetSameShape(); FiltersGradient = Filters.GetSameShape(); BiasGradient = Bias.GetSameShape(); return(ForwardOutput); }
/// <summary> /// 运行前的准备,用于初始化所有Tensor的结构 /// </summary> /// <param name="input">输入Tensor</param> /// <returns></returns> public override TensorOld PrepareTrain(TensorOld input) { if (input.Rank != 2) { throw new TensorShapeException("input.Rank must be 2"); } ForwardOutput = input.GetSameShape(); BackwardOutput = input.GetSameShape(); sampleNumber = input.shape[0]; categoryNumber = input.shape[1]; sampleBuff = new double[categoryNumber]; //向量对向量求导的结果是个矩阵 //多个样本下,softmax的导数是一个三阶张量,第一维是样本数量,后面两维是jacob矩阵 Derivative = new TensorOld(sampleNumber, categoryNumber, categoryNumber); return(ForwardOutput); }
public override TensorOld PrepareTrain(TensorOld input) { PreparePredict(input); BackwardOutput = input.GetSameShape(); maxPositions = new TensorOld(samples, channels, outRows * outColumns, 2); return(ForwardOutput); }
public void Optimize(TensorOld target, TensorOld gradient) { if (!last.ContainsKey(gradient)) { last[gradient] = gradient.GetSameShape(); TensorOld.Apply(gradient, last[gradient], g => LearningRate * g); target.Minus(last[gradient]); return; } var prev = last[gradient]; TensorOld.Apply(prev, gradient, prev, (p, g) => g * LearningRate - p * Moment); target.Minus(prev); }
/// <summary> /// 训练前的准备工作,检查结构,分配内存等 /// </summary> /// <param name="input">输入,主要使用结构信息</param> /// <returns></returns> public TensorOld PrepareTrain(TensorOld input) { if (input.Rank != 2) { throw new TensorShapeException("input tensor must have Rank=2"); } ForwardOutput = new TensorOld(input.shape[0], UnitCount); BackwardOutput = input.GetSameShape(); if (Weights == null) { SetWeights(TensorOld.RandGaussian(input.shape[1], UnitCount)); WeightsGradient = Weights.GetSameShape(); } else { if (input.shape[1] != Weights.shape[0]) { throw new TensorShapeException("input and weights are not match!"); } } if (Bias == null) { SetBias(TensorOld.Zeros(1, UnitCount)); BiasGradient = Bias.GetSameShape(); } sampleStartIndex = new int[input.shape[0]]; errorStartIndex = new int[input.shape[0]]; for (int i = 0; i < sampleStartIndex.Length; i++) { sampleStartIndex[i] = i * input.shape[1]; errorStartIndex[i] = i * UnitCount; } return(ForwardOutput); }
public override TensorOld PreparePredict(TensorOld input) { ForwardOutput = input.GetSameShape(); return(ForwardOutput); }
public override TensorOld PrepareTrain(TensorOld input) { PreparePredict(input); BackwardOutput = input.GetSameShape(); return ForwardOutput; }