u.lekhoni.com

Some common pytorch methods

torch.empty

This function creates an un-initialized tensor. It does not contain definite known values before it is used.

# Creates a 3x3 empty tensor
x = torch.empty(3, 3)
print(x)

torch.zeros

This function creates a tensor filled with zeros.

# Creates a 3x3 tensor filled with zeros
x = torch.zeros(3, 3)
print(x)

torch.ones

This function creates a tensor filled with ones.

# Creates a 3x3 tensor filled with ones
x = torch.ones(3, 3)
print(x)

torch.randn

This function creates a tensor with elements picked randomly from a normal distribution, with a mean of 0 and a standard deviation of 1.

# Creates a 3x3 tensor with elements from a standard normal distribution
x = torch.randn(3, 3)
print(x)

torch.tensor

This function creates a tensor from data. The data can be a list or a NumPy array.

# Creates a tensor from a list
x = torch.tensor([1, 2, 3])
print(x)

# Creates a tensor from a 2D list
y = torch.tensor([[1, 2], [3, 4]])
print(y)

torch.arange

This function creates a 1D tensor of size end-start, with elements from start to end with a step step.

# Creates a tensor from 0 to 4
x = torch.arange(5)
print(x)

# Creates a tensor from 1 to 4
y = torch.arange(1, 5)
print(y)

# Creates a tensor from 1 to 8 with a step of 2
z = torch.arange(1, 9, 2)
print(z)

torch.linspace

This function creates a 1D tensor of size steps with elements from start to end spaced evenly.

# Creates a tensor with 5 steps from 0 to 1
x = torch.linspace(0, 1, 5)
print(x)

torch.eye

This function creates an identity matrix (a 2D tensor with ones on the diagonal and zeros elsewhere).

# Creates a 3x3 identity matrix
x = torch.eye(3)
print(x)