public void TestExplicitGenerators() { // This tests that the default generator can be disposed, but will keep on going. lock (_lock) { long a, b, c; using (var gen = Torch.ManualSeed(4711)) { a = gen.InitialSeed; } using (TorchGenerator gen = TorchGenerator.Default, genA = new TorchGenerator(4355)) { b = gen.InitialSeed; c = genA.InitialSeed; } Assert.Equal(a, b); Assert.NotEqual(a, c); Assert.Equal(4355, c); } }
public void TestDefaultGenerators() { // This tests that the default generator can be disposed, but will keep on going, long a, b, c; lock (_lock) { using (var gen = Torch.ManualSeed(4711)) { a = gen.InitialSeed; } using (var gen = TorchGenerator.Default) { b = gen.InitialSeed; } Assert.Equal(a, b); using (var gen = Torch.ManualSeed(17)) { c = gen.InitialSeed; } Assert.NotEqual(a, c); var x = Float32Tensor.rand(new long[] { 10, 10, 10 }); Assert.Equal(new long[] { 10, 10, 10 }, x.shape); } }
public void TestGeneratorState() { // This test fails intermittently with CUDA. Just skip it. if (Torch.IsCudaAvailable()) { return; } // After restoring a saved RNG state, the next number should be the // same as right after the snapshot. lock (_lock) { using (var gen = Torch.ManualSeed(4711)) { // Take a snapshot var state = gen.State; Assert.NotNull(state); // Generate a number var val1 = Float32Tensor.randn(new long[] { 1 }); var value1 = val1[0].ToSingle(); // Genereate a different number var val2 = Float32Tensor.randn(new long[] { 1 }); var value2 = val2[0].ToSingle(); Assert.NotEqual(value1, value2); // Restore the state gen.State = state; // Generate the first number again. var val3 = Float32Tensor.randn(new long[] { 1 }); var value3 = val3[0].ToSingle(); Assert.Equal(value1, value3); } } }