Example #1
0
        public static DataSet BuildSet(IAllocator allocator, DigitImage[] images)
        {
            var inputs  = new NDArray(allocator, DType.Float32, images.Length, MnistParser.ImageSize, MnistParser.ImageSize);
            var outputs = new NDArray(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);

                Variable.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 = NDArray.FromArray(allocator, images.Select(x => (float)x.label).ToArray());

            return(new DataSet()
            {
                inputs = inputs, targets = outputs, targetValues = targetValues
            });
        }
Example #2
0
        private static (ImageFrame, ImageFrame) BuildSet(DigitImage[] images, bool flatten = false)
        {
            var cpuAllocator = new TensorSharp.Cpu.CpuAllocator();

            var inputs  = new Tensor(Global.Device, DType.Float32, images.Length, 1, ImageSize, ImageSize);
            var outputs = new Tensor(Global.Device, DType.Float32, images.Length, 10);

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

                Variable.FromArray(images[i].pixels, cpuAllocator)
                .AsType(DType.Float32)
                .ToDevice(Global.Device)
                .Evaluate(target);

                target = target / 255;
            }

            Ops.FillOneHot(outputs, LabelCount, images.Select(x => (int)x.label).ToArray());
            if (flatten)
            {
                inputs = inputs.View(images.Length, 784);
            }
            return(new ImageFrame(inputs), new ImageFrame(outputs));
        }