Pytorch基础入门练习
import 导入
import torch # 基本的torch函数
import torch.autograd as autograd # 自动求导
import torch.nn as nn # 神经网络类都在这个里面
import torch.nn.functional as F # 几乎所有的激励函数
import torch.optim as optim # 优化
创建Tensors
# create 1D vector
v_data = [1., 2., 3.]
v = torch.tensor(v_data)
print(v)
# create 2D vector
M_data = [[1., 2., 3.],[4., 5., 6.]]
M = torch.tensor(M_data)
print(M)
# create 3D vector
T_data = [[[1., 2.],[3., 4.]],[[5., 6.],[7., 8.]]]
T = torch.tensor(T_data)
print(T)
var foo = 'bar';
下面展示一些 运行结果
。
tensor([1., 2., 3.])
tensor([[1., 2., 3.],
[4., 5., 6.]])
tensor([[[1., 2.],
[3., 4.]],
[[5., 6.],
[7., 8.]]])
获取Tensor的部分值
# 获取tensor的部分值
# 这里要比tensorflow 好用多了QAQ
print(v[0])
print(M[0])
print(T[0])
下面展示 运行结果
。
tensor(1.)
tensor([1., 2., 3.])
tensor([[1., 2.],
[3., 4.]])
产生随机数
# 随机数
x = torch.randn((3, 4, 5))
print(x)
运行结果如下
:
tensor([[[-0.7559, -0.1650, -0.2283, 0.7088, -0.1870],
[ 0.0914, 0.4764, 1.2566, -0.6386, -0.0722],
[-0.8834, -0.9981, -1.5278, -0.5788, 0.9297],
[-0.0450, -1.7502, 0.9460, -1.3609, 1.3793]],
[[ 1.1496, 1.4190, -0.3966, 0.8439, -0.2627],
[ 0.5515, 0.2070, -0.8803, -1.5191, 0.7596],
[-1.0275, -1.1738, 2.0765, 2.5308, -1.0748],
[-1.6574, -1.1648, -1.9078, -0.5660, 0.5558]],
[[-1.8764, -0.8884, 1.2439, -2.0752, 0.2381],
[ 1.2270, 2.7735, 0.0249, -0.4611, -0.9217],
[ 2.3551, -0.4531, 0.2677, 0.4329, 1.7990],
[ 2.3443, -1.5808, -1.5181, -0.2277, -0.1178]]])
Tensor运算
a = torch.tensor([1, 2, 3])
b = torch.tensor([4, 5, 6])
c = a+b
print(c)
运行结果如下:
tensor([5, 7, 9])
[res] torch.cat( [res,] x_1, x_2, [dimension] ):
x_1 = torch.randn(2, 5)
y_1 = torch.randn(3, 5)
z_1 =torch.cat([x_1, y_1])#没有最后一个参数,默认是0,则最终维度的第0维度为x_1与y_1第0维度的和,最终维度的其他维度不变.以下同理
print(z_1)
x_2 = torch.randn(2, 3)
y_2 = torch.randn(2, 5)
z_2 = torch.cat([x_2, y_2], 1)
print(z_2)
运行结果如下:
tensor([[ 0.9761, 0.3068, 2.0089, 1.5800, -1.5596],
[-0.5638, -1.6565, 0.3520, -1.8214, 1.3104],
[ 2.3029, 2.6305, 1.2756, 0.3129, -1.1587],
[ 0.2029, 1.6141, 0.2416, 1.7751, -0.0401],
[ 0.6935, -0.8595, -0.0923, 0.6760, 1.7727]])
tensor([[-0.7705, -1.4831, 0.7549, -0.3064, -1.2749, -1.1668, -0.2255, -0.8785],
[-0.1929, 0.6399, -0.6258, 0.4568, -0.6885, -0.2360, -0.1745, 0.7706]])
Tensor维度变形reshaping
`print(x.view(2,12)) # 将234 -> 2*12
# Tensor(reshaping)
x = torch.randn(2, 3, 4)
print(x)
print(x.view(2, -1))#-1的话,意味着最后的相乘为维数,这里为2*之后的成绩
下面展示 运行结果
。
tensor([[[-0.1259, -0.2706, -0.3104, 1.3265],
[-0.5548, 1.4568, 0.6568, -1.3463],
[ 0.1177, -0.5917, -1.0518, -1.5515]],
[[-1.3372, -0.9601, 0.5557, -0.7694],
[-0.3657, 1.0275, -1.2712, -2.0646],
[-0.7945, -0.9716, 0.0876, -2.8072]]])
tensor([[-0.1259, -0.2706, -0.3104, 1.3265, -0.5548, 1.4568, 0.6568, -1.3463,
0.1177, -0.5917, -1.0518, -1.5515],
[-1.3372, -0.9601, 0.5557, -0.7694, -0.3657, 1.0275, -1.2712, -2.0646,
-0.7945, -0.9716, 0.0876, -2.8072]])
计算图和自动微分
x = autograd.Variable(torch.Tensor([1., 2., 3]), requires_grad=True)
print(x)
print(x.data)#.data显示具体数据
y = autograd.Variable( torch.Tensor([4., 5., 6]), requires_grad=True )
z = x + y
print(z.data)
下面展示 运行结果
。
tensor([1., 2., 3.], requires_grad=True)
tensor([1., 2., 3.])
tensor([5., 7., 9.])
# 显示z中所有元素之和
s = z.sum()
print(s)
s.backward() # 反向传播
print(x.grad) # 对x求导
下面展示 运行结果
。
tensor(21., grad_fn=<SumBackward0>)
tensor([1., 1., 1.])
#答案解释
#x = [1,2,3]
#y = [4,5,6]
#z = x + y = [x0+y0, x1+y1, x2+y2]
#s = z.sum() = x0+y0+x1+y1+x2+y2
#x.grad 在s运算中对x求导 也就是当中的x0,x1,x2求导 为1,1,1
Deep Learning Building Blocks: Affine maps, non-linearities and objectives(线性映射与非线性映射)
Affine maps
也可以说是线性映射,即为f(x) = Ax + b
nn.Linear(inputSize,outputSize,bias=True)
输入(N, inputSize)
输出(N, outputSize)
lin = nn.Linear(5,3)
data = autograd.Variable(torch.randn(2, 5))
print(lin(data))
下面展示 运行结果
。
tensor([[-0.6649, 1.1707, -0.5394],
[-0.9146, 1.1783, 0.4715]], grad_fn=<AddmmBackward>)
Non-Linearities
非线性,常用的函数有 tanh(x),σ(x),ReLU(x) 这些都是激励函数
在pytorch中大部分激励函数在torch.functional中
data = autograd.Variable( torch.randn(2, 2) )
print(data)
print (F.relu(data))#relu函数是小于零是0,大于零就是它本身
下面展示 运行结果
。
tensor([[ 0.1168, 1.3061],
[ 0.9262, -1.6161]])
tensor([[0.1168, 1.3061],
[0.9262, 0.0000]])
Softmax and Probabilities
softmax是x_i/sum(x)
data = autograd.Variable( torch.randn(5) )
print(data)
print(F.softmax(data))
print(F.softmax(data).sum())
print(F.log_softmax(data))
下面展示 运行结果
。
tensor([ 0.2949, -0.7197, -1.4672, -0.5649, 0.9722])
tensor([0.2547, 0.0923, 0.0437, 0.1078, 0.5014])
tensor(1.0000)
tensor([-1.3676, -2.3822, -3.1297, -2.2274, -0.6903])