Open2
PyTorch
Attribute of Tensor
・ dtype: The data type of the tensor (e.g., torch.float32, torch.int64).
・ device: The device (CPU or GPU) on which the tensor is stored.
・ shape: The dimensions of the tensor. Equivalent to tensor.size().
・ requires_grad: Boolean flag indicating if the tensor requires gradient computation. This is useful for automatic differentiation when training neural networks.
・ grad: Holds the gradient of the tensor. This attribute is populated during the backward pass of neural network training.
・ is_leaf: Boolean indicating if the tensor is a leaf node in the computational graph (i.e., if it was created by a user and not as a result of an operation that has gradients).
example
import torch
# Create a tensor
tensor = torch.tensor([[1, 2], [3, 4]], dtype=torch.float32, requires_grad=True)
# Perform an operation
y = tensor + 5
z = y * y * 2
# Compute gradients
z.backward(torch.tensor([[1.0, 1.0], [1.0, 1.0]]))
# Access attributes and methods
print("Data type:", tensor.dtype)
print("Requires grad:", tensor.requires_grad)
print("Gradient of tensor:", tensor.grad)
print("Shape of tensor:", tensor.shape)
print("Device of tensor:", tensor.device)
Kaggle env
pip install torch==2.4.0+cu118 torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118