Two main features:
● N-dimensional Tensor computation (like NumPy) on GPUs
● Automatic differentiation for training deep neural networks
Tensor
a specialized data structure that are very similar to arrays and matrices
we use tensors to encode the inputs and outputs of a model, as well as the model’s parameters In PyTorch
Initializing a Tensor
Directly from data
data = [[1, 2],[3, 4]]
x_data = torch.tensor(data)
Tensor attributes describe their shape, datatype, and the device on which they are stored.
tensor = torch.rand(3,4)
print(f"Shape of tensor: {tensor.shape}")
print(f"Datatype of tensor: {tensor.dtype}")
print(f"Device tensor is stored on: {tensor.device}")
output
Shape of tensor: torch.Size([3, 4])
Datatype of tensor: torch.float32
Device tensor is stored on: cpu
Each of these operations can be run on the GPU (at typically higher speeds than on a CPU). If you’re using Colab, allocate a GPU by going to Runtime > Change runtime type > GPU.
By default, tensors are created on the CPU. We need to explicitly move tensors to the GPU using .to method (after checking for GPU availability). Keep in mind that copying large tensors across devices can be expensive in terms of time and memory!
cuda=Compute Unified Device Architecture
# We move our tensor to the GPU if available
if torch.cuda.is_available():
tensor = tensor.to("cuda")
You can use torch.cat to concatenate a sequence of tensors along a given dimension. See also torch.stack, another tensor joining op that is subtly different from torch.cat.
stack
Concatenates sequence of tensors along a new dimension.
cat
Concatenates the given sequence of seq tensors in the given dimension.
So if A and B are of shape (3, 4):
torch.cat([A, B], dim=0) will be of shape (6, 4)
torch.stack([A, B], dim=0) will be of shape (2, 3, 4)
Arithmetic operations
# This computes the matrix multiplication between two tensors. y1, y2, y3 will have the same value
y1 = tensor @ tensor.T
y2 = tensor.matmul(tensor.T)
y3 = torch.rand_like(y1)
torch.matmul(tensor, tensor.T, out=y3)
# This computes the element-wise product. z1, z2, z3 will have the same value
z1 = tensor * tensor
z2 = tensor.mul(tensor)
z3 = torch.rand_like(tensor)
torch.mul(tensor, tensor, out=z3)
Single-element tensors
If you have a one-element tensor, for example by aggregating all values of a tensor into one value, you can convert it to a Python numerical value using item():
In-place operations save some memory, but can be problematic when computing derivatives because of an immediate loss of history. Hence, their use is discouraged.
Bridge with NumPy
Tensors on the CPU and NumPy arrays can share their underlying memory locations, and changing one will change the other.
Tensor to NumPy array
t = torch.ones(5)
print(f"t: {t}")
n = t.numpy()
print(f"n: {n}")
What is PyTorch?
An machine learning framework in Python.
Two main features: ● N-dimensional Tensor computation (like NumPy) on GPUs ● Automatic differentiation for training deep neural networks
Tensor
a specialized data structure that are very similar to arrays and matrices we use tensors to encode the inputs and outputs of a model, as well as the model’s parameters In PyTorch
Initializing a Tensor
Directly from data
From a NumPy array
From another tensor
With random or constant values: shape is a tuple of tensor dimensions. In the functions below, it determines the dimensionality of the output tensor.
shape
shape is a tuple of tensor dimensions
Attributes of a Tensor
Tensor attributes describe their shape, datatype, and the device on which they are stored.
output
Operations on Tensors
有許多種操作,詳細請參考此
Each of these operations can be run on the GPU (at typically higher speeds than on a CPU). If you’re using Colab, allocate a GPU by going to Runtime > Change runtime type > GPU.
By default, tensors are created on the CPU. We need to explicitly move tensors to the GPU using .to method (after checking for GPU availability). Keep in mind that copying large tensors across devices can be expensive in terms of time and memory!
cuda=Compute Unified Device Architecture
Standard numpy-like indexing and slicing:
output
Joining tensors
You can use torch.cat to concatenate a sequence of tensors along a given dimension. See also torch.stack, another tensor joining op that is subtly different from torch.cat.
output
stack Concatenates sequence of tensors along a new dimension.
cat Concatenates the given sequence of seq tensors in the given dimension.
So if A and B are of shape (3, 4):
torch.cat([A, B], dim=0) will be of shape (6, 4) torch.stack([A, B], dim=0) will be of shape (2, 3, 4)
Arithmetic operations
Single-element tensors
If you have a one-element tensor, for example by aggregating all values of a tensor into one value, you can convert it to a Python numerical value using item():
In-place operations
Operations that store the result into the operand are called in-place. They are denoted by a suffix. For example: x.copy(y), x.t_(), will change x.
output
In-place operations save some memory, but can be problematic when computing derivatives because of an immediate loss of history. Hence, their use is discouraged.
Bridge with NumPy
Tensors on the CPU and NumPy arrays can share their underlying memory locations, and changing one will change the other.
Tensor to NumPy array
output
A change in the tensor reflects in the NumPy array.
output
NumPy array to Tensor
Changes in the NumPy array reflects in the tensor.
參考網址 https://pytorch.org/tutorials/beginner/basics/tensorqs_tutorial.html