Table of Contents
Creation and basic operations of tensor
Initializing a Tensor–Creation method
Attributes of a Tensor–Attributes of a tensor
Operations on Tensors — Operations on Tensors Official Documentation
Creation and basic operations of tensor
Initializing a Tensor–creation method
Directly from data — Create directly with data
Create a tensor directly from data, and the data type is automatically inferred. Parameters can be lists or tuples, but not sets.
data = [[1, 2],[3, 4]] x_data = torch.tensor(data)
From a NumPy array — created through numpy array
Tensors can be created from NumPy arrays (and vice versa – see Bridge with NumPy).
np_array = np.array(data1) x_np = torch.from_numpy(np_array)
#Run results: tensor([1., 2., 3., 9.], dtype=torch.float64)
From another tensor — created through other tensor
The new tensor retains the properties (shape, data type) of the parameter tensor unless explicitly overridden.
x_ones = torch.ones_like(x_data) # retains the properties of x_data print(f"Ones Tensor: \ {x_ones} \ ") ? x_rand = torch.rand_like(x_data, dtype=torch.float) # overrides the datatype of x_data print(f"Random Tensor: \ {x_rand} \ ")
Ones Tensor: tensor([[1, 1], [1, 1]]) ? Random Tensor: tensor([[0.8823, 0.9150], [0.3829, 0.9593]])
With random or constant values — Create with random or constant values
‘shape’ is a tuple of tensor dimensions. In the function below, it determines the dimensionality of the output tensor. This means using a tuple to determine the dimension of the tensor. In fact, a list can also be used.
shape = (2,3,) #You can also use shape = [2,3] rand_tensor = torch.rand(shape) ones_tensor = torch.ones(shape) zeros_tensor = torch.zeros(shape) ? print(f"Random Tensor: \ {rand_tensor} \ ") print(f"Ones Tensor: \ {ones_tensor} \ ") print(f"Zeros Tensor: \ {zeros_tensor}")
Random Tensor: tensor([[0.3904, 0.6009, 0.2566], [0.7936, 0.9408, 0.1332]]) ? OnesTensor: tensor([[1., 1., 1.], [1., 1., 1.]]) ? Zeros Tensor: tensor([[0., 0., 0.], [0., 0., 0.]])
Attributes of a Tensor–Attributes of a tensor
Tensor properties describe their shape, data type, and the device on which they are stored. That is, the three attributes of shape, dtype, and device.
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}")
Shape of tensor: torch.Size([3, 4]) Datatype of tensor: torch.float32 Device tensor is stored on: cpu
Operations on Tensors — Official documentation for operations on tensors
More than 100 tensor operations are comprehensively described here, including arithmetic, linear algebra, matrix operations (transpose, index, slice), sampling, and more.
Each of these operations can be run on a GPU (generally faster than on a CPU).
By default, tensors are created on the CPU. We need to explicitly move the tensor to the GPU using the ‘.to’ method (after checking GPU availability). Copying large tensors across devices can be expensive in terms of time and memory!
# We move our tensor to the GPU if available if torch.cuda.is_available(): tensor = tensor.to("cuda")
Try some of the things on the list. Almost the same as numpy.
Standard numpy-like indexing and slicing — Standard numpy indexing and slicing
tensor = torch.ones(4, 4) print(f"First row: {tensor[0]}") print(f"First column: {tensor[:, 0]}") print(f"Last column: {tensor[..., -1]}") tensor[:,1] = 0 print(tensor)
First row: tensor([1., 1., 1., 1.]) First column: tensor([1., 1., 1., 1.]) Last column: tensor([1., 1., 1., 1.]) tensor([[1., 0., 1., 1.], [1., 0., 1., 1.], [1., 0., 1., 1.], [1., 0., 1., 1.]])
Concatenate Tensors You can use torch.cat
to concatenate a sequence of tensors along a given dimension. See also torch.stack, another slightly different tensor concatenation operator than torch.cat.
torch.cat(tensors, dim=0, ***, out=None)
Concatenates the given sequence of tensors in the given dimension. All tensors must have the same shape (except dim) or be empty.
torch.cat() can be seen as the inverse operation of torch.split() and torch.chunk().
-
parameter
tensors (sequence of Tensors) – any sequence of python tensors of the same type. The provided non-empty tensor Must have the same shape, except dim. dim (int, optional) – the dimension of the concatenated tensor
-
keyword arguments
out (Tensor, optional) – Output tensor.
Example:
>>> x = torch.randn(2, 3) >>> x tensor([[ 0.6580, -1.0969, -0.4614], [-0.1034, -0.5790, 0.1497]]) >>> torch.cat((x, x, x), 0) #The dimension is 0, the number of columns remains unchanged, and the number of rows increases tensor([[ 0.6580, -1.0969, -0.4614], [-0.1034, -0.5790, 0.1497], [0.6580, -1.0969, -0.4614], [-0.1034, -0.5790, 0.1497], [0.6580, -1.0969, -0.4614], [-0.1034, -0.5790, 0.1497]]) >>> torch.cat((x, x, x), 1) #The dimension is 1, the number of rows remains unchanged, and the number of columns increases tensor([[ 0.6580, -1.0969, -0.4614, 0.6580, -1.0969, -0.4614, 0.6580, -1.0969, -0.4614], [-0.1034, -0.5790, 0.1497, -0.1034, -0.5790, 0.1497, -0.1034, -0.5790, 0.1497]])
t1 = torch.cat([tensor, tensor, tensor], dim=1) print(t1)
tensor([[1., 0., 1., 1., 1., 0., 1., 1., 1., 0., 1., 1.], [1., 0., 1., 1., 1., 0., 1., 1., 1., 0., 1., 1.], [1., 0., 1., 1., 1., 0., 1., 1., 1., 0., 1., 1.], [1., 0., 1., 1., 1., 0., 1., 1., 1., 0., 1., 1.]])
Arithmetic operations — arithmetic operations
# This will compute a matrix multiplication between two tensors, the number of columns of the first matrix must equal the number of rows of the second matrix. y1, y2, y3 will have the same value # ``tensor.T`` returns the transpose of the tensor y1 = [email protected] ? y2 = tensor.matmul(tensor.T) ? y3 = torch.rand_like(y1) torch.matmul(tensor, tensor.T, out=y3)
? ?
# This will calculate the element product, that is, multiply the elements at the corresponding positions. The number of rows and columns of the two matrices must be the same. 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)
tensor([[1., 0., 1., 1.], [1., 0., 1., 1.], [1., 0., 1., 1.], [1., 0., 1., 1.]])
Single-element tensor If you have a single-element tensor, for example by aggregating all the values of the tensor into a single value, you can use item() to convert it to a Python number:
agg = tensor.sum() agg_item = agg.item() print(agg_item, type(agg_item))
12.0 <class 'float'>
In-place operations Operations that store results in operands are called in-place operations. They are represented by the “” suffix. For example: x.copy(y), x.t_(), will change x.
print(f"{tensor} \ ") tensor.add_(5) print(tensor)
tensor([[1., 0., 1., 1.], [1., 0., 1., 1.], [1., 0., 1., 1.], [1., 0., 1., 1.]])
tensor([[6., 5., 6., 6.], [6., 5., 6., 6.], [6., 5., 6., 6.], [6., 5., 6., 6.]])
Notice
Operating in place saves some memory, but can cause problems when calculating derivatives because the history is lost immediately. Therefore, their use is discouraged.
Tensors
-
torch.is_tensor(obj)[SOURCE]
Returns True if obj is a PyTorch tensor.
Note that this function is simply doing
isinstance(obj, Tensor)
. Using thatisinstance
check is better for typechecking with mypy, and more explicit – so it’s recommended to use that instead ofis_tensor
.-
Parameters
obj (Object) – Object to test
Example:
>>> x = torch.tensor([1, 2, 3]) >>>torch.is_tensor(x) True
-
torch.is_nonzero(input)
Returns True if the input
is a single element tensor which is not equal to zero after type conversions. i.e. not equal to torch.tensor([0.])
or torch.tensor([0])
or torch.tensor([False])
. Throws a RuntimeError
if torch.numel() ! = 1
(even in case of sparse tensors).
-
Parameters
input (Tensor) – the input tensor.
Examples:
>>> torch.is_nonzero(torch.tensor([0.])) False >>> torch.is_nonzero(torch.tensor([1.5])) True >>> torch.is_nonzero(torch.tensor([False])) False >>> torch.is_nonzero(torch.tensor([3])) True >>> torch.is_nonzero(torch.tensor([1, 3, 5])) Traceback (most recent call last): ... RuntimeError: bool value of Tensor with more than one value is ambiguous >>> torch.is_nonzero(torch.tensor([])) Traceback (most recent call last): ... RuntimeError: bool value of Tensor with no values is ambiguous
torch.numel(input) → int
Returns the total number of elements in the input
tensor.
-
Parameters
input (Tensor) – the input tensor.
Example:
>>> a = torch.randn(1, 2, 3, 4, 5) >>> torch.numel(a) 120 >>> a = torch.zeros(4,4) >>> torch.numel(a) 16
The knowledge points of the article match the official knowledge files, and you can further learn relevant knowledge. Python entry skill treeArtificial intelligenceDeep learning 388527 people are learning the system