Learn practical skills, build real-world projects, and advance your career
import torch
import numpy as np

x = np.array([[1, 2], [3, 4.]])
x
array([[1., 2.],
       [3., 4.]])
# Convert the numpy array to a torch tensor.
y = torch.from_numpy(x)
y
tensor([[1., 2.],
        [3., 4.]], dtype=torch.float64)
x.dtype, y.dtype
(dtype('float64'), torch.float64)
# Convert a torch tensor to a numpy array
z = y.numpy()
z
array([[1., 2.],
       [3., 4.]])