pytorch: A 60 Minute blitz笔记
0. What
- A replacement for NumPy to use the power of GPUs
- a deep learning research platform that provides maximum flexibility and speed
1. Basic
1.1 tensors
x = torch.empty(5, 3) # unitialized
x = torch.rand(5, 3)
x = torch.zeros(5, 3, dtype=torch.long)
x = torch.tensor([5.5, 3]) # construct from data
x = torch.randn_like(x, dtype=torch.float) # override dtype!
print(x.size())
> torch.Size([5, 3]) # support all tuple operations
Size() object support all tuple operations
1.2 Operations
- add
print(x + y) # opt1
print(torch.add(x, y)) # opt2
y.add_(x) # add x to y
Any operation thart mutates a tensor in-place is post-fixed with an
_
. For example:x.copy_(y)
,x.t_()
, will changex
- index: standard Numpy-like
print(x[:, 1])
- resizing:
torch.view
x = torch.randn(4, 4)
y = x.view(16)
z = x.view(-1, 8)
use
.item()
to get the value as a python number(for one element tensor)
1.3 Numpy Bridge
The Torch Tensor and NumPy array will share their underlying memory locations, and changing one will change the other.
- Tensor -> Array:
ts.numpy()
- Array -> Tensor:
torch.from_numpy(ar)
- CUDA tensor: using
.to(device, dtype, ...)
method