Tensors and Variables in PyTorch and TensorFlow
Classified in Computers
Written on in English with a size of 2.4 KB
Tensors and Variables in PyTorch and TensorFlow
Here's a brief explanation of tensors and variables in the context of deep learning frameworks like PyTorch and TensorFlow:
Tensors
- Definition: A tensor is a multi-dimensional array used to represent data (such as scalars, vectors, matrices, or higher-dimensional data).
- Common Operations: Tensors can be manipulated with mathematical operations (addition, multiplication, etc.), reshaped, sliced, etc.
In PyTorch, tensors are the core data structure:
import torch
# Create a tensor
a = torch.tensor([[1, 2], [3, 4]])
# Basic operations
b = a + 2 # Adds 2 to each element
c = a * a # Element-wise multiplication
d = a @ a # Matrix multiplication
Output:
Tensor `b`: [[3, 4], [5, 6]]
Tensor