Пример #1
0
        public static DataSet BuildSet(IAllocator allocator, DigitImage[] images)
        {
            var inputs  = new Tensor(allocator, DType.Float32, images.Length, MnistParser.ImageSize, MnistParser.ImageSize);
            var outputs = new Tensor(allocator, DType.Float32, images.Length, MnistParser.LabelCount);

            var cpuAllocator = new TensorSharp.Cpu.CpuAllocator();

            for (int i = 0; i < images.Length; ++i)
            {
                var target = inputs.TVar().Select(0, i);

                TVar.FromArray(images[i].pixels, cpuAllocator)
                .AsType(DType.Float32)
                .ToDevice(allocator)
                .Evaluate(target);

                target.Div(255)
                .Evaluate(target);
            }


            Ops.FillOneHot(outputs, MnistParser.LabelCount, images.Select(x => (int)x.label).ToArray());
            var targetValues = Tensor.FromArray(allocator, images.Select(x => (float)x.label).ToArray());

            return(new DataSet()
            {
                inputs = inputs, targets = outputs, targetValues = targetValues
            });
        }
Пример #2
0
        public static TVar Softmax(this TVar x)
        {
            long[]       shape = x.Evaluate().Shape;
            List <float> data  = new List <float>();

            for (long i = 0; i < shape[0]; i++)
            {
                var s_x = x.Select(0, i);
                var exp = s_x.Exp();
                var sum = exp.SumAll();
                var s_t = (exp / sum.ToScalar()).View(1, shape[1]).Evaluate();
                data.AddRange(s_t.ToArray().Cast <float>());
            }

            return(TVar.FromArray(data.ToArray(), Global.Device).View(shape));
        }