Ejemplo n.º 1
0
        /// <summary>
        /// 两个Tensor相乘,结果返回为新的Tensor。
        /// 目前仅支持scalar、vector、matrix的乘法
        /// </summary>
        /// <param name="a">Tensor1</param>
        /// <param name="b">Tensor2</param>
        /// <returns>包含结果的新的Tensor</returns>
        public static TensorOld Multiple(TensorOld a, TensorOld b)
        {
            if (a.ElementCount == 1)
            {
                return(Multiple(b, a.GetValue()));
            }
            if (b.ElementCount == 1)
            {
                return(Multiple(a, b.GetValue()));
            }

            if (a.Rank == 1)
            {
                a = a.Reshape(1, a.ElementCount);
            }
            if (b.Rank == 1)
            {
                b = b.Reshape(b.ElementCount, 1);
            }

            CheckMultipleShape(a, b);
            var result = new TensorOld(a.shape[0], b.shape[1]);

            Multiple(a, b, result);

            return(result);
        }
Ejemplo n.º 2
0
        /// <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);
                }
            }
        }