3.5. To/From numpy.arrayΒΆ
Cytnx also provides conversion from and to numpy.array in the Python API.
To convert from Cytnx Tensor to numpy array, use Tensor.numpy()
In Python:
1A = cytnx.ones([3,4])
2B = A.numpy()
3print(A)
4print(type(B))
5print(B)
Output >>
Total elem: 12
type : Double (Float64)
cytnx device: CPU
Shape : (3,4)
[[1.00000e+00 1.00000e+00 1.00000e+00 1.00000e+00 ]
[1.00000e+00 1.00000e+00 1.00000e+00 1.00000e+00 ]
[1.00000e+00 1.00000e+00 1.00000e+00 1.00000e+00 ]]
<class 'numpy.ndarray'>
[[1. 1. 1. 1.]
[1. 1. 1. 1.]
[1. 1. 1. 1.]]
To convert from numpy array to Cytnx Tensor, use cytnx.from_numpy()
In Python:
1import numpy as np
2B = np.ones([3,4])
3A = cytnx.from_numpy(B)
4print(B)
5print(A)
Output >>
[[1. 1. 1. 1.]
[1. 1. 1. 1.]
[1. 1. 1. 1.]]
Total elem: 12
type : Double (Float64)
cytnx device: CPU
Shape : (3,4)
[[1.00000e+00 1.00000e+00 1.00000e+00 1.00000e+00 ]
[1.00000e+00 1.00000e+00 1.00000e+00 1.00000e+00 ]
[1.00000e+00 1.00000e+00 1.00000e+00 1.00000e+00 ]]