Пример #1
0
        public void WriteAndReadDoubleTensorViaMemoryFile()
        {
            const int size = 10;

            var file = new Torch.IO.MemoryFile("rwb");

            Assert.IsNotNull(file);
            Assert.IsTrue(file.CanWrite);

            var tensor0 = new TorchSharp.DoubleTensor(size);

            for (var i = 0; i < size; ++i)
            {
                tensor0[i] = (double)i;
            }

            file.WriteTensor(tensor0);
            Assert.AreEqual(size * sizeof(double), file.Position);
            file.Seek(0);

            var tensor1 = new TorchSharp.DoubleTensor(size);
            var rd      = file.ReadTensor(tensor1);

            Assert.AreEqual(rd, size);
            Assert.AreEqual(size * sizeof(double), file.Position);

            for (var i = 0; i < rd; ++i)
            {
                Assert.AreEqual(tensor1[i], tensor1[i]);
            }

            file.Close();
            Assert.IsFalse(file.IsOpen);
        }