PyTorch scientific computing is a Python-based framework for in-depth research study.
Tensor Tensor
- Tensor type
Tensor is PyTorch data type, torch define the CPU type Tensor 7, and 8 kinds of GPU type Tensor
https://pytorch-cn.readthedocs.io/zh/latest/package_references/Tensor/
- Tensor creation
The List, Numpy converted to Tensor
#导入相应的库
import numpy as np
import torch
x = [1,2,3,4]
y = np.arange(1,4)
print(x) #[1, 2, 3, 4]
print(y) #[1 2 3]
#将List转换为tensor
z1 = torch.tensor(x)
print(z1)#tensor([1, 2, 3, 4])
#将arrage转换为tensor
z2 = torch.from_numpy(y)
print(z2)#tensor([1, 2, 3], dtype=torch.int32)
Random initialization method
# 采样自0~1均匀分布
a = torch.rand(3,3)
tensor([[0.2556, 0.6495, 0.2267],
[0.5367, 0.6287, 0.3143],
[0.0946, 0.5261, 0.0413]])
#
生成和a大小的Tebsor
b = torch.rand_like(a)
tensor([[0.8962, 0.7645, 0.8433],
[0.8876, 0.3550, 0.7918],
[0.3892, 0.0642, 0.5240]])
#生成3*3的Tensor[1,10]
c = rorch.randint(1,10,[3,3])
c
tensor([[3, 7, 9],
[5, 1, 3],
[5, 3, 1]])
#采样自N(0,1)
d = torch.randn(2,3)
tensor([[ 0.7113, 0.7134, -1.3212],
[-0.5364, -1.2826, -0.1041]])
The same number
# shape=2,3,所使用的相同的元素为7
b = torch.full([2, 3], 7)
tensor([[7., 7., 7.],
[7., 7., 7.]])
Full 1
a = torch.ones(3,3)
a
tensor([[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]])
Full 0
b = torch.zeros(3,3)
b
tensor([[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.]])
Diagonal
c = torch.eye(3,3)
c
tensor([[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.]])
Basic Operations
adding the torch
x = torch.full([5,3],8)
y = torch.full([5,3],1)
print(x,"\n",y)
print(torch.add(x,y))#或者print(x+y) or print(x.add_(y))
#输出结果:
tensor([[8., 8., 8.],
[8., 8., 8.],
[8., 8., 8.],
[8., 8., 8.],
[8., 8., 8.]])
tensor([[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]])
tensor([[9., 9., 9.],
[9., 9., 9.],
[9., 9., 9.],
[9., 9., 9.],
[9., 9., 9.]])
Any change in the tensor-place operations are fixed _. For example: x.copy_ (y), x.t_ (), will change x.
Resize: To adjust the tensor size / shape, may be used torch.view:
x = torch.full([5,4],3)
y = x.view(20)
z = x.view(2,10)
print(x,"\n",y,"\n",z)
print(x.size(),y.size(),z.size())
#output
tensor([[3., 3., 3., 3.],
[3., 3., 3., 3.],
[3., 3., 3., 3.],
[3., 3., 3., 3.],
[3., 3., 3., 3.]])
tensor([3., 3., 3., 3., 3., 3., 3., 3., 3., 3., 3., 3., 3., 3., 3., 3., 3., 3.,
3., 3.])
tensor([[3., 3., 3., 3., 3., 3., 3., 3., 3., 3.],
[3., 3., 3., 3., 3., 3., 3., 3., 3., 3.]])
torch.Size([5, 4]) torch.Size([20]) torch.Size([2, 10])
Variable (Variable)
Variable Tensor and no essential difference, however, is put into a variable calculation map, and then into Xing forward propagation, counter propagation, automatic derivation.
from torch.autograd import Variable
x = Variable(torch.rand(2,2),requires_grad=True)
x
tensor([[0.7823, 0.8837],
[0.5355, 0.4893]], requires_grad=True)
y = x+2
y.grad_fn
<AddBackward0 object at 0x0000028BBAE67FD0>
z = y*y*2
out=z.mean()
out
tensor(14.3410, grad_fn=<MeanBackward1>)
out.backward()
x.grad
tensor([[2.7823, 2.8837],
[2.5355, 2.4893]])
CUDA tensor
using this method .to tensor can move to any device.
if torch.cuda.is_available():
device = torch.device("cuda") # a CUDA device object
y = torch.ones_like(x, device=device) # directly create a tensor on GPU
x = x.to(device) # or just use strings ``.to("cuda")``
z = x + y
print(z)
print(z.to("cpu", torch.double))
#output
tensor([1.7391], device='cuda:0')
tensor([1.7391], dtype=torch.float64)