Esempio n. 1
0
        /// <summary>
        /// 训练前的准备工作,检查并确定所需Tensor的结构并分配好内存
        /// </summary>
        /// <param name="y">样本标签</param>
        /// <param name="yHat">输出标签</param>
        public override void PrepareTrain(TensorOld y, TensorOld yHat)
        {
            TensorOld.CheckShape(y, yHat);

            ForwardOutput  = y.GetSameShape();
            BackwardOutput = y.GetSameShape();
        }
Esempio n. 2
0
        /// <summary>
        /// 给t的每个元素加上d,结果返回为新的Tensor
        /// </summary>
        /// <param name="t">Tensor</param>
        /// <param name="d">要加上的值</param>
        /// <returns></returns>
        public static TensorOld Add(TensorOld t, double d)
        {
            var result = t.GetSameShape();

            Add(t, d, result);
            return(result);
        }
Esempio n. 3
0
        /// <summary>
        /// Tensor每个元素乘上数字d,结果返回为新的Tensor
        /// </summary>
        /// <param name="t">Tensor</param>
        /// <param name="d">数字d</param>
        /// <returns>包含结果的新的Tensor</returns>
        public static TensorOld Multiple(TensorOld t, double d)
        {
            var result = t.GetSameShape();

            Multiple(t, d, result);
            return(result);
        }
Esempio n. 4
0
        /// <summary>
        /// 分类问题中产生的如果是概率,需要把概率转换为编码结果
        /// 后面再解码得到最终的分类
        /// </summary>
        /// <param name="probability">概率</param>
        /// <returns>编码</returns>
        public static TensorOld ProbabilityToCode(TensorOld probability)
        {
            var code = probability.GetSameShape();

            ProbabilityToCode(probability, code);
            return(code);
        }
Esempio n. 5
0
        /// <summary>
        /// 两个Tensor对应元素相处,结果返回为新的Tensor,要求两个Tensor结构一致
        /// </summary>
        /// <param name="a">被除数</param>
        /// <param name="b">除数</param>
        /// <returns>包含结果的新的Tensor</returns>
        public static TensorOld DivideElementWise(TensorOld a, TensorOld b)
        {
            var result = a.GetSameShape();

            DivideElementWise(a, b, result);
            return(result);
        }
Esempio n. 6
0
        /// <summary>
        /// 用d去除以Tensor中的每个元素,结果返回为新的Tensor
        /// </summary>
        /// <param name="d">被除数</param>
        /// <param name="t">除数</param>
        /// <returns>包含结果的新的Tensor</returns>
        public static TensorOld Divide(double d, TensorOld t)
        {
            var result = t.GetSameShape();

            Apply(t, result, a => d / a);
            return(result);
        }
Esempio n. 7
0
        /// <summary>
        /// Tensor的每个元素除以d,结果返回为新的Tensor
        /// </summary>
        /// <param name="t">被除数</param>
        /// <param name="d">除数</param>
        /// <returns>包含结果的新的Tensor</returns>
        public static TensorOld Divide(TensorOld t, double d)
        {
            var result = t.GetSameShape();

            Apply(t, result, a => a / d);
            return(result);
        }
Esempio n. 8
0
        public static TensorOld Apply(TensorOld a, TensorOld b, Func <double, double, double> function)
        {
            var result = a.GetSameShape();

            Apply(a, b, result, function);
            return(result);
        }
Esempio n. 9
0
        /// <summary>
        /// 指定Tensor的每个元素应用function,结果返回为新的Tensor
        /// </summary>
        /// <param name="tensor">应用function的Tensor</param>
        /// <param name="function">引用的function</param>
        /// <returns>结果返回为新的Tensor</returns>
        public static TensorOld Apply(TensorOld tensor, Func <double, double> function)
        {
            var result = tensor.GetSameShape();

            Apply(tensor, result, function);
            return(result);
        }
Esempio n. 10
0
        public static TensorOld MeanSquareError(TensorOld y, TensorOld yHat)
        {
            TensorOld.CheckShape(y, yHat);

            var result = y.GetSameShape();

            MeanSquareError(y, yHat, result);
            return(result);
        }
Esempio n. 11
0
        /// <summary>
        /// 训练前的准备工作,检查并确定所需Tensor的结构并分配好内存
        /// </summary>
        /// <param name="y">样本标签</param>
        /// <param name="yHat">输出标签</param>
        public override void PrepareTrain(TensorOld y, TensorOld yHat)
        {
            TensorOld.CheckShape(y, yHat);
            if (y.Rank != 2)
            {
                throw new TensorShapeException("y and yHat must Rank=2");
            }

            ForwardOutput  = new TensorOld(y.shape[0]);
            BackwardOutput = yHat.GetSameShape();
            yBuff          = new double[y.shape[1]];
            yHatBuff       = new double[y.shape[1]];
            derBuff        = new double[y.shape[1]];
            sampleNumber   = y.Shape[0];
        }
Esempio n. 12
0
        /// <summary>
        /// 两个Tensor的点积,结果返回为新的Tensor,要求两个Tensor结构一致
        /// </summary>
        /// <param name="a">Tensor1</param>
        /// <param name="b">Tensor2</param>
        /// <returns>包含结果的新的Tensor</returns>
        public static TensorOld MultipleElementWise(TensorOld a, TensorOld b)
        {
            if (a.ElementCount == 1)
            {
                return(Multiple(b, a.GetValue()));
            }
            if (b.ElementCount == 1)
            {
                return(Multiple(a, b.GetValue()));
            }

            var result = a.GetSameShape();

            MultipleElementWise(a, b, result);
            return(result);
        }
Esempio n. 13
0
        /// <summary>
        /// Tensor和Tensor对应元素相加,结果返回为新的Tensor,要求两个Tensor结构相同
        /// </summary>
        /// <param name="a">Tensor</param>
        /// <param name="b">Tensor</param>
        /// <returns>相加后的结果</returns>
        public static TensorOld Add(TensorOld a, TensorOld b)
        {
            if (a.ElementCount == 1)
            {
                return(Add(b, a.GetValue()));
            }
            if (b.ElementCount == 1)
            {
                return(Add(a, b.GetValue()));
            }

            CheckShape(a, b);
            var result = a.GetSameShape();

            Add(a, b, result);
            return(result);
        }