void TestMmGen(DeviceType device) { { var x1 = FloatTensor.Ones(new long[] { 1, 2 }, deviceType: device); var x2 = FloatTensor.Ones(new long[] { 2, 1 }, deviceType: device); var y = x1.Mm(x2).ToDevice(DeviceType.CPU); var ydata = y.Data <float>(); Assert.Equal(2.0f, ydata[0]); } //System.Runtime.InteropServices.ExternalException : addmm for CUDA tensors only supports floating - point types.Try converting the tensors with.float() at C:\w\b\windows\pytorch\aten\src\THC / generic / THCTensorMathBlas.cu:453 if (device == DeviceType.CPU) { var x1 = LongTensor.Ones(new long[] { 1, 2 }, deviceType: device); var x2 = LongTensor.Ones(new long[] { 2, 1 }, deviceType: device); var y = x1.Mm(x2).ToDevice(DeviceType.CPU); var ydata = y.Data <long>(); Assert.Equal(2L, ydata[0]); } }
public void AvgPool2DObjectInitialized() { TorchTensor ones = FloatTensor.Ones(new long[] { 2, 2, 2 }); var obj = Functions.AvgPool2D(ones, new long[] { 2, 2 }, new long[] { 2, 2 }); Assert.Equal(typeof(TorchTensor), obj.GetType()); }
public void CopyCpuToCuda() { TorchTensor cpu = FloatTensor.Ones(new long[] { 2, 2 }); Assert.Equal("cpu", cpu.DeviceString); if (Torch.IsCudaAvailable()) { var cuda = cpu.Cuda(); Assert.Equal("cuda:0", cuda.DeviceString); // Copy back to CPU to inspect the elements var cpu2 = cuda.Cpu(); Assert.Equal("cpu", cpu2.DeviceString); var data = cpu.Data <float>(); for (int i = 0; i < 4; i++) { Assert.Equal(1, data[i]); } } else { Assert.Throws <InvalidOperationException>(() => cpu.Cuda()); } }
public void CreateFloatTensorOnes() { string path = Directory.GetCurrentDirectory(); Console.WriteLine(path); TorchTensor ones = FloatTensor.Ones(new long[] { 2, 2 }); }
public void MaxPool2DObjectInitialized() { TorchTensor ones = FloatTensor.Ones(new long[] { 2, 2, 2 }); var obj = NN.Module.MaxPool2D(ones, new long[] { 2 }, new long[] { 2 }); Assert.Equal(typeof(TorchTensor), obj.GetType()); }
public void CreateFloatTensorCheckDevice() { var ones = FloatTensor.Ones(new long[] { 2, 2 }); var device = ones.Device; Assert.AreEqual(ones.Device, "cpu"); }
private void TestTwoTensorInPlace <Tin>(Func <TorchTensor, TorchTensor, TorchTensor> tensorFunc, Func <Tin, Tin, Tin> scalarFunc) { var c1 = FloatTensor.Arange(0, 10, 1); var c2 = FloatTensor.Arange(10, 0, -1); var c3 = FloatTensor.Ones(new long[] { 10, 10 }); var x = c1 * c3; var xClone = x.Clone(); var y = c2 * c3; var z = tensorFunc(x, y); var xData = x.Data <Tin>(); var xCloneData = xClone.Data <Tin>(); var yData = y.Data <Tin>(); var zData = z.Data <Tin>(); Assert.True(xData == zData); for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { Assert.Equal(zData[i + j], scalarFunc(xCloneData[i + j], yData[i + j])); } } }
public void CreateFloatTensorCheckDevice() { var ones = FloatTensor.Ones(new long[] { 2, 2 }); var device = ones.DeviceString; Assert.Equal("cpu", ones.DeviceString); }
public void CreateFloatTensorOnes() { var shape = new long[] { 2, 2 }; TorchTensor t = FloatTensor.Ones(shape); Assert.Equal(shape, t.Shape); Assert.Equal(1.0f, t[0, 0].DataItem <float>()); Assert.Equal(1.0f, t[1, 1].DataItem <float>()); }
public void TestSetGetBiasInLinear() { var lin = NN.Module.Linear(1000, 100, true); var bias = FloatTensor.Ones(new long[] { 1000 }); lin.Bias = bias; Assert.Equal(lin.Bias?.NumberOfElements, bias.NumberOfElements); }
public void ScoreModel() { var ones = FloatTensor.Ones(new long[] { 1, 3, 224, 224 }); var module = JIT.Module.Load(@"..\..\..\Resources\model.pt"); Assert.NotNull(module); var result = module.Forward(ones); }
public void CreateFloatTensorOnesCheckData() { var ones = FloatTensor.Ones(new long[] { 2, 2 }); var data = ones.Data <float>(); for (int i = 0; i < 4; i++) { Assert.Equal(1.0, data[i]); } }
public void TestCat() { var zeros = FloatTensor.Zeros(new long[] { 1, 9 }); var ones = FloatTensor.Ones(new long[] { 1, 9 }); var centroids = new TorchTensor[] { zeros, ones }.Cat(0); var shape = centroids.Shape; Assert.Equal(new long[] { 2, 9 }, shape); }
public void TestTensorToScalarMultiplication() { using (var tensor = FloatTensor.Ones(new long[] { 2, 2 })) { var neg = tensor * (-1).ToScalar(); foreach (var val in neg.Data <float>()) { Assert.AreEqual(val, -1.0); } } }
public void GetSetItem6() { var shape = new long[] { 2, 3, 4, 5, 6, 7 }; TorchTensor t = FloatTensor.Ones(shape); Assert.Equal(shape, t.Shape); Assert.Equal(1.0f, t[0, 0, 0, 0, 0, 0].DataItem <float>()); Assert.Equal(1.0f, t[1, 2, 3, 4, 5, 6].DataItem <float>()); t[1, 2, 3, 4, 5, 6] = FloatTensor.From(2.0f); Assert.Equal(2.0f, t[1, 2, 3, 4, 5, 6].DataItem <float>()); }
public void TestCatCuda() { if (Torch.IsCudaAvailable()) { var zeros = FloatTensor.Zeros(new long[] { 1, 9 }).Cuda(); var ones = FloatTensor.Ones(new long[] { 1, 9 }).Cuda(); var centroids = new TorchTensor[] { zeros, ones }.Cat(0); var shape = centroids.Shape; Assert.Equal(new long[] { 2, 9 }, shape); Assert.Equal(DeviceType.CUDA, centroids.DeviceType); } }
public void TestSquareEuclideanDistance() { var input = new double[] { 0.1, 0.1, 0.1, 0.1, 0.2, 0.1, 0.2, 0.1, 0.1 }.ToTorchTensor(new long[] { 9 }).ToType(ScalarType.Float); var zeros = FloatTensor.Zeros(new long[] { 1, 9 }); var ones = FloatTensor.Ones(new long[] { 1, 9 }); var centroids = new TorchTensor[] { zeros, ones }.Cat(0); var distanceFromZero = input.Reshape(new long[] { -1, 1, 9 }).Sub(zeros).Pow(2.ToScalar()).Sum(new long[] { 2 }); var distanceFromOne = input.Reshape(new long[] { -1, 1, 9 }).Sub(ones).Pow(2.ToScalar()).Sum(new long[] { 2 }); var distanceFromCentroids = input.Reshape(new long[] { -1, 1, 9 }).Sub(centroids).Pow(2.ToScalar()).Sum(new long[] { 2 }); Assert.True(true); }
public void CreateFloatTensorCheckMemory() { TorchTensor?ones = null; for (int i = 0; i < 10; i++) { using (var tmp = FloatTensor.Ones(new long[] { 100, 100, 100 })) { ones = tmp; Assert.NotNull(ones); } } }
public void ExpandTest() { TorchTensor ones = FloatTensor.Ones(new long[] { 2 }); TorchTensor onesExpanded = ones.Expand(new long[] { 3, 2 }); Assert.Equal(onesExpanded.Shape, new long[] { 3, 2 }); for (int i = 0; i < 3; i++) { for (int j = 0; j < 2; j++) { Assert.Equal(1.0, onesExpanded[i, j].DataItem <float>()); } } }
public void TestMul() { var x = FloatTensor.Ones(new long[] { 100, 100 }); var y = x.Mul(0.5f.ToScalar()); var ydata = y.Data <float>(); var xdata = x.Data <float>(); for (int i = 0; i < 100; i++) { for (int j = 0; j < 100; j++) { Assert.Equal(ydata[i + j], xdata[i + j] * 0.5f); } } }
public void TestSaveLoadTensorFloat() { var file = ".saveload.float.ts"; if (File.Exists(file)) { File.Delete(file); } var tensor = FloatTensor.Ones(new long[] { 5, 6 }); tensor.Save(file); var tensorLoaded = TorchTensor.Load(file); File.Delete(file); Assert.NotNull(tensorLoaded); Assert.Equal(tensorLoaded.Type, tensor.Type); Assert.Equal(tensorLoaded, tensor); }
private void TestOneTensor <Tin, Tout>(Func <TorchTensor, TorchTensor> tensorFunc, Func <Tin, Tout> scalarFunc) { var c1 = FloatTensor.Arange(0, 10, 1); var c2 = FloatTensor.Ones(new long[] { 10, 10 }); var x = c1 * c2; var y = tensorFunc(x); var xData = x.Data <Tin>(); var yData = y.Data <Tout>(); for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { Assert.Equal(yData[i + j], scalarFunc(xData[i + j])); } } }
public void CopyCudaToCpu() { if (Torch.IsCudaAvailable()) { var cuda = FloatTensor.Ones(new long[] { 2, 2 }, DeviceType.CUDA); Assert.Equal("cuda:0", cuda.DeviceString); var cpu = cuda.Cpu(); Assert.Equal("cpu", cpu.DeviceString); var data = cpu.Data <float>(); for (int i = 0; i < 4; i++) { Assert.Equal(1, data[i]); } } else { Assert.Throws <InvalidOperationException>(() => { FloatTensor.Ones(new long[] { 2, 2 }, DeviceType.CUDA); }); } }
public void CopyCudaToCpu() { if (Torch.IsCudaAvailable()) { var cuda = FloatTensor.Ones(new long[] { 2, 2 }, "cuda"); Assert.AreEqual(cuda.Device, "cuda"); var cpu = cuda.Cpu(); Assert.AreEqual(cpu.Device, "cpu"); var data = cpu.Data <float>(); for (int i = 0; i < 4; i++) { Assert.AreEqual(data[i], 1); } } else { Assert.ThrowsException <InvalidOperationException>(() => { FloatTensor.Ones(new long[] { 2, 2 }, "cuda"); }); } }
void TestStackGen(DeviceType device) { { var t1 = FloatTensor.Zeros(new long[] { }, device); var t2 = FloatTensor.Ones(new long[] { }, device); var t3 = FloatTensor.Ones(new long[] { }, device); var res = new TorchTensor[] { t1, t2, t3 }.Stack(0); var shape = res.Shape; Assert.Equal(new long[] { 3 }, shape); Assert.Equal(device, res.DeviceType); } { var t1 = FloatTensor.Zeros(new long[] { 2, 9 }, device); var t2 = FloatTensor.Ones(new long[] { 2, 9 }, device); var res = new TorchTensor[] { t1, t2 }.Stack(0); var shape = res.Shape; Assert.Equal(new long[] { 2, 2, 9 }, shape); Assert.Equal(device, res.DeviceType); } }
public void CopyCpuToCuda() { TorchTensor cpu = FloatTensor.Ones(new long[] { 2, 2 }); Assert.AreEqual(cpu.Device, "cpu"); if (Torch.IsCudaAvailable()) { var cuda = cpu.Cuda(); Assert.AreEqual(cuda.Device, "cuda"); // Copy back to CPU to inspect the elements cpu = cuda.Cpu(); var data = cpu.Data <float>(); for (int i = 0; i < 4; i++) { Assert.AreEqual(data[i], 1); } } else { Assert.ThrowsException <InvalidOperationException>(() => cpu.Cuda()); } }
public void CreateFloatTensorOnes() { TorchTensor ones = FloatTensor.Ones(new long[] { 2, 2 }); }
public void CreateFloatTensorOnes() { var ones = FloatTensor.Ones(new long[] { 2, 2 }); Assert.IsNotNull(ones); }