Ejemplo n.º 1
0
 /// <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);
        }
Ejemplo n.º 3
0
        public TensorOld Normalize(TensorOld input)
        {
            var result = input.GetSameShape();

            Normalize(input, result);
            return(result);
        }
Ejemplo n.º 4
0
 public TensorOld PrepareTrain(TensorOld input)
 {
     PreparePredict(input);
     BackwardOutput  = input.GetSameShape();
     FiltersGradient = Filters.GetSameShape();
     BiasGradient    = Bias.GetSameShape();
     return(ForwardOutput);
 }
Ejemplo n.º 5
0
        /// <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);
        }
Ejemplo n.º 6
0
        public override TensorOld PrepareTrain(TensorOld input)
        {
            PreparePredict(input);

            BackwardOutput = input.GetSameShape();
            maxPositions   = new TensorOld(samples, channels, outRows * outColumns, 2);

            return(ForwardOutput);
        }
Ejemplo n.º 7
0
        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);
        }
Ejemplo n.º 8
0
        /// <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);
        }
Ejemplo n.º 9
0
 public override TensorOld PreparePredict(TensorOld input)
 {
     ForwardOutput = input.GetSameShape();
     return(ForwardOutput);
 }
Ejemplo n.º 10
0
 public override TensorOld PrepareTrain(TensorOld input)
 {
     PreparePredict(input);
     BackwardOutput = input.GetSameShape();
     return ForwardOutput;
 }