3.7. Save/Load a Tensor

Cyntx provides a way to save/read Tensors to/from a file.

3.7.1. Save a Tensor

To save a Tensor to a file, simply call Tensor.Save(filepath).

  • In Python:

1A = cytnx.arange(12).reshape(3,4)
2A.Save("T1")
  • In C++:

1auto A = cytnx::arange(12).reshape(3, 4);
2A.Save("T1");

This will save Tensor A to the current directory as T1.cytn, with .cytn as file extension.

3.7.2. Load a Tensor

Now, let’s load the Tensor from the file.

  • In Python:

1A = cytnx.Tensor.Load("T1.cytn")
2print(A)
  • In C++:

1auto A = cytnx::Tensor::Load("T1.cytn");
2cout << A << endl;

Output >>

Total elem: 12
type  : Double (Float64)
cytnx device: CPU
Shape : (3,4)
[[0.00000e+00 1.00000e+00 2.00000e+00 3.00000e+00 ]
 [4.00000e+00 5.00000e+00 6.00000e+00 7.00000e+00 ]
 [8.00000e+00 9.00000e+00 1.00000e+01 1.10000e+01 ]]