pytorch使用
基本介绍
Tensors and relation to numpy
import torch
import numpy as np
# we create tensors in a similar way to numpy nd arrays
x_numpy = np.array([0.1, 0.2, 0.3])
x_torch = torch.tensor([0.1, 0.2, 0.3])
print('x_numpy,x_torch')
print(x_numpy, x_torch)
print()
#to and from numpy, pytorch
print('to and from numpy and pytorch')
print(torch.from_numpy(x_numpy), x_torch.numpy())
print()
#wen can do basic operations like +-*/
y_numpy = np.array([3, 4, 5.])
y_torch = torch.tensor([3, 4, 5.])
print('x+y=')
print(x_numpy+y_numpy, x_torch+y_torch)
print()
#many functions that are in numpy are also in pytorch
print('norm')#norm-- 范数
print(np.linalg.norm(x_numpy), torch.norm(x_torch))
print()
#to apply an operation along a dimension
#we use the dim keword argument instead of axis
print('mean along the 0th dimension')
x_numpy = np.array([[1, 2], [3, 4]])
y_torch = torch.tensor([[1, 2], [3, 4]])
print(np.mean(x_numpy, axis=0), torch.mean(x_torch, dim=0))
输出
// 输出
x_numpy,x_torch
[0.1 0.2 0.3] tensor([0.1000, 0.2000, 0.3000])
to and from numpy and pytorch
tensor([0.1000, 0.2000, 0.3000], dtype=torch.float64) [0.1 0.2 0.3]
x+y=
[3.1 4.2 5.3] tensor([3.1000, 4.2000, 5.3000])
norm
0.37416573867739417 tensor(0.3742)
mean along the 0th dimension
[2. 3.] tensor(0.2000)
Tensor.view
# 和numpy的reshape一样
N, C, W, H = 10000, 3, 28, 28
X = torch.randn((N, C, W, H))
print(X.shape)
print(X.view(N, C, 784).shape)
print(X.view(-1, C, 784).shape) # automatically choose the 0th
输出
#output
torch.Size([10000, 3, 28, 28])
torch.Size([10000, 3, 784])
torch.Size([10000, 3, 784])
broadcast :
x = torch.empty(5, 1, 4, 1)
y = torch.empty(3, 1, 1)
print((x+y).size())
输出
#output
torch.Size([5, 3, 4, 1])
computation Graph
torch 在计算的时候,会自动建一个图,把计算过程简称一张图
# computation Graph
输出
#output
CUDA语法 使用gpu计算数据
# code
cpu = torch.device("cpu")
gpu = torch.device("cuda")
x = torch.rand(10)
print(x)
x = x.to(gpu) # to gpu
print(x)
x = x.to(cpu) # to cpu
print(x)
输出
#output
tensor([0.1262, 0.0894, 0.9269, 0.5523, 0.7255, 0.0495, 0.4450, 0.9474, 0.5318,
0.0641])
tensor([0.1262, 0.0894, 0.9269, 0.5523, 0.7255, 0.0495, 0.4450, 0.9474, 0.5318,
0.0641], device='cuda:0')
tensor([0.1262, 0.0894, 0.9269, 0.5523, 0.7255, 0.0495, 0.4450, 0.9474, 0.5318,
0.0641])
Pytorch 梯度的计算
using gradient 简单计算梯度
视频 21分钟
Linear Regression 做线性回归
。。。。未完待续
常用模块
nn.Linear
参考资料
全连接层的简单实现(只有一层)
# Creat Fully Connected Network
class NN(nn.Module):
def __init__(self, input_size, num_classes):
super(NN, self).__init__()
self.fc1 = nn.Linear(input_size, num_classes,bias=False)
def forward(self, x):
x = self.fc1(x)
return x
net = NN(input_size=208*28, num_classes=10)