Things on this page are fragmentary and immature notes/thoughts of the author. Please read with your own judgement!
import torch
import numpy as npx = torch.tensor(
[
[1.0, 2, 3, 4, 5],
[6.0, 7, 8, 9, 10],
]
)
xtensor([[ 1., 2., 3., 4., 5.],
[ 6., 7., 8., 9., 10.]])torch.Tensor vs torch.tensor vs torch.as_tensor¶
torch.Tensoralways returnstorch.FloatTensor.torch.tensorinfers the data type and allows users to specify the data type. It is suggested that you usetorch.tensorinstead oftorch.Tensor.torch.tensoralways copies data whiletorch.as_tensoravoids copying data if possible. One such an example is when you convert a numpy array to a Tensor. However, notice that bothtorch.tensorandtorch.as_tensorcopies data if a list is feeded to them.In most situations, you should use
torch.tensor. Never usetorch.Tensor. Be cautious if you usetorch.as_tensor.
a1 = np.array([1, 2, 3])
t1 = torch.Tensor(arr)
t1tensor([1., 2., 3.])a1[0] = 1000
t1tensor([1., 2., 3.])a2 = np.array([1, 2, 3])
t2 = torch.Tensor(arr)
t2tensor([1., 2., 3.])a2[0] = 1000
t2tensor([1., 2., 3.])a3 = np.array([1, 2, 3])
t3 = torch.as_tensor(a3)
t3tensor([1, 2, 3])a3[0] = 1000
t3tensor([1000, 2, 3])torch.rand(10)tensor([0.7872, 0.1416, 0.2451, 0.7463, 0.3549, 0.6301, 0.3934, 0.4746, 0.0036,
0.0931])torch.stack¶
Concatenates sequence of tensors along a new dimension. All tensors need to be of the same size.
data = [torch.tensor([1, 2, 3]), torch.tensor([4, 5, 6])]torch.stack(data)tensor([[1, 2, 3],
[4, 5, 6]])torch.stack takes a list/tuple of tensors.
It does NOT work on a generator/iterator of tensors.
torch.stack(t for t in data)---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-6-4a0a55595f8d> in <module>
----> 1 torch.stack(t for t in data)
TypeError: stack(): argument 'tensors' (position 1) must be tuple of Tensors, not generatorTensor.detach¶
Returns a new Tensor, detached from the current graph.
The result will never require gradient.
Note: Returned Tensor shares the same storage with the original one. In-place modifications on either of them will be seen, and may trigger errors in correctness checks. IMPORTANT NOTE: Previously, in-place size / stride / storage changes (such as resize_ / resize_as_ / set_ / transpose_) to the returned tensor also update the original tensor. Now, these in-place changes will not update the original tensor anymore, and will instead trigger an error. For sparse tensors: In-place indices / values changes (such as zero_ / copy_ / add_) to the returned tensor will not update the original tensor anymore, and will instead trigger an error.
?x.detachDocstring:
Returns a new Tensor, detached from the current graph.
The result will never require gradient.
.. note::
Returned Tensor shares the same storage with the original one.
In-place modifications on either of them will be seen, and may trigger
errors in correctness checks.
IMPORTANT NOTE: Previously, in-place size / stride / storage changes
(such as `resize_` / `resize_as_` / `set_` / `transpose_`) to the returned tensor
also update the original tensor. Now, these in-place changes will not update the
original tensor anymore, and will instead trigger an error.
For sparse tensors:
In-place indices / values changes (such as `zero_` / `copy_` / `add_`) to the
returned tensor will not update the original tensor anymore, and will instead
trigger an error.
Type: builtin_function_or_method
Tensor.mean¶
x.mean()tensor(5.5000)Tensor.item¶
x.item()---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-7-3396a1b2b617> in <module>
----> 1 x.item()
ValueError: only one element tensors can be converted to Python scalarsy = torch.tensor([2])
ytensor([2])y.item()2y = torch.tensor(2)
ytensor(2)y.item()2Tensor.backward¶
Generally speaking, you only need to call this method on the loss tensor.
Move a Tensor to a Specific Device in PyTorch¶
Please refer to Move a Tensor to a Specific Device in PyTorch for more details.
Convert a Tensor to a Numpy Array in PyTorch¶
Please refer to Convert a Tensor to a Numpy Array in PyTorch for more details.
Tensor Transformations in TorchVision¶
Please refer to Tensor Transformations in TorchVision for more details.
Resize a Tensor in PyTorch¶
Please refer to Resize a Tensor in PyTorch for more details.