import torch
import numpy as np
# From Python lists
= torch.tensor([[1, 2], [3, 4]])
tensor_from_list print(f"From list: {tensor_from_list}")
# From NumPy arrays
= np.array([[1, 2], [3, 4]])
numpy_array = torch.from_numpy(numpy_array)
tensor_from_numpy print(f"From numpy: {tensor_from_numpy}")
# Common initialization patterns
= torch.zeros(3, 3)
zeros_tensor = torch.ones(2, 4)
ones_tensor = torch.randn(2, 3) # Normal distribution
random_tensor
print(f"Zeros: {zeros_tensor}")
print(f"Random: {random_tensor}")
From list: tensor([[1, 2],
[3, 4]])
From numpy: tensor([[1, 2],
[3, 4]])
Zeros: tensor([[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.]])
Random: tensor([[ 1.8291, 1.4825, -0.9290],
[ 1.0408, 0.5881, -0.0163]])