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 }); }
/// <summary> /// Evaluates the specified write target. /// </summary> /// <param name="writeTarget">The write target.</param> /// <returns>Tensor.</returns> public override NDArray Evaluate(NDArray writeTarget) { if (writeTarget != null) { writeTarget.CopyFrom(array); return(writeTarget); } else { return(NDArray.FromArray(allocator, array)); } }
private void RunCopy(Array srcData, DType destType) { var allocator = new CpuAllocator(); var a = NDArray.FromArray(allocator, srcData); var b = new NDArray(allocator, destType, a.Shape); Ops.Copy(b, a); for (int i = 0; i < srcData.Length; ++i) { Assert.AreEqual(Convert.ToSingle(srcData.GetValue(i)), b.GetElementAsFloat(i)); } }