本学习笔记主要摘自“深度之眼”,做一个总结,方便查阅。
使用Pytorch版本为1.2。
1.张量的拼接
1.1 torch.cat()
功能:将张量按维度dim进行拼接
# ======================================= example 1 =======================================
# torch.cat
flag = True
#flag = False
if flag:
t = torch.ones((2, 3))
t_0 = torch.cat([t, t], dim=0)
t_1 = torch.cat([t, t, t], dim=1)
print("t_0:{} shape:{}\nt_1:{} shape:{}".format(t_0, t_0.shape, t_1, t_1.shape))
输出:
t_0:tensor([[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]]) shape:torch.Size([4, 3])
t_1:tensor([[1., 1., 1., 1., 1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1., 1., 1., 1., 1.]]) shape:torch.Size([2, 9])
1.2 torch.stack()
功能:在新创建的维度dim进行拼接
测试代码:
# ======================================= example 2 =======================================
# torch.stack
flag = True
#flag = False
if flag:
t = torch.ones((2, 3))
t_stack = torch.stack([t, t, t], dim=0)
print("\nt_stack:{} shape:{}".format(t_stack, t_stack.shape))
输出:
t_stack:tensor([[[1., 1., 1.],
[1., 1., 1.]],
[[1., 1., 1.],
[1., 1., 1.]],
[[1., 1., 1.],
[1., 1., 1.]]]) shape:torch.Size([3, 2, 3])
1.3 torch.chunk()
功能:将张量按维度dim进行平均切分
返回值:张量列表
注意:若不能整除,最后一份张量小于其他张量
测试代码:
# ======================================= example 3 =======================================
# torch.chunk
flag = True
#flag = False
if flag:
a = torch.ones((2, 7)) # 7
list_of_tensors = torch.chunk(a, dim=1, chunks=3) # 3
for idx, t in enumerate(list_of_tensors):
print("第{}个张量:{}, shape is {}".format(idx+1, t, t.shape))
输出:
第1个张量:tensor([[1., 1., 1.],
[1., 1., 1.]]), shape is torch.Size([2, 3])
第2个张量:tensor([[1., 1., 1.],
[1., 1., 1.]]), shape is torch.Size([2, 3])
第3个张量:tensor([[1.],
[1.]]), shape is torch.Size([2, 1])
1.4 torch.split()
功能:将张量按维度dim进行切分
返回值:张量列表
# ======================================= example 4 =======================================
# torch.split
# flag = True
flag = True
if flag:
t = torch.ones((2, 5))
t1 = torch.ones((2, 5))
list_of_tensors = torch.split(t, 2, dim=1) # [2 , 1, 2]
for idx, t in enumerate(list_of_tensors):
print("第{}个张量:{}, shape is {}".format(idx+1, t, t.shape))
list_of_tensors = torch.split(t1, [2, 1, 2], dim=1)
for idx, t in enumerate(list_of_tensors):
print("第{}个张量:{}, shape is {}".format(idx+1, t, t.shape))
输出:
第1个张量:tensor([[1., 1.],
[1., 1.]]), shape is torch.Size([2, 2])
第2个张量:tensor([[1., 1.],
[1., 1.]]), shape is torch.Size([2, 2])
第3个张量:tensor([[1.],
[1.]]), shape is torch.Size([2, 1])
第1个张量:tensor([[1., 1.],
[1., 1.]]), shape is torch.Size([2, 2])
第2个张量:tensor([[1.],
[1.]]), shape is torch.Size([2, 1])
第3个张量:tensor([[1., 1.],
[1., 1.]]), shape is torch.Size([2, 2])
2.张量索引
2.1 torch.index_select()
功能:在维度dim上,按index索引数据
返回值:依index索引数据拼接的张量
# ======================================= example 5 =======================================
# torch.index_select
flag = True
#flag = False
if flag:
t = torch.randint(0, 9, size=(3, 3))
idx = torch.tensor([0, 2], dtype=torch.long) # float
t_select = torch.index_select(t, dim=0, index=idx)
print("t:\n{}\nt_select:\n{}".format(t, t_select))
输出:
t:
tensor([[4, 5, 0],
[5, 7, 1],
[2, 5, 8]])
t_select:
tensor([[4, 5, 0],
[2, 5, 8]])
2.2 torch.make_select()
功能:按mask中的True进行索引
返回值:一维张量
测试代码:
# ======================================= example 6 =======================================
# torch.masked_select
flag = True
#flag = False
if flag:
t = torch.randint(0, 9, size=(3, 3))
mask = t.le(5) # ge is mean greater than or equal/ gt: greater than le lt
t_select = torch.masked_select(t, mask)
print("t:\n{}\nmask:\n{}\nt_select:\n{} ".format(t, mask, t_select))
输出:
t:
tensor([[4, 5, 0],
[5, 7, 1],
[2, 5, 8]])
mask:
tensor([[ True, True, True],
[ True, False, True],
[ True, True, False]])
t_select:
tensor([4, 5, 0, 5, 1, 2, 5])
3.张量的变换
3.1 torch.reshape()
功能:变换张量形状
注意事项:当张量在内存中是连续的,新张量与input共享数据内存。
# ======================================= example 7 =======================================
# torch.reshape
flag = True
#flag = False
if flag:
t = torch.randperm(8)
t_reshape = torch.reshape(t, (-1, 2, 2)) # -1
print("t:{}\nt_reshape:\n{}".format(t, t_reshape))
t[0] = 1024
print("t:{}\nt_reshape:\n{}".format(t, t_reshape))
print("t.data 内存地址:{}".format(id(t.data)))
print("t_reshape.data 内存地址:{}".format(id(t_reshape.data)))
输出:
t:tensor([5, 4, 2, 6, 7, 3, 1, 0])
t_reshape:
tensor([[[5, 4],
[2, 6]],
[[7, 3],
[1, 0]]])
t:tensor([1024, 4, 2, 6, 7, 3, 1, 0])
t_reshape:
tensor([[[1024, 4],
[ 2, 6]],
[[ 7, 3],
[ 1, 0]]])
t.data 内存地址:1169043563128
t_reshape.data 内存地址:1169043563128
3.2 torch.transpose()
功能:交换张量的两个维度
测试代码:
# ======================================= example 8 =======================================
# torch.transpose
flag = True
#flag = False
if flag:
# torch.transpose
t = torch.rand((2, 3, 4))
t_transpose = torch.transpose(t, dim0=1, dim1=2) # c*h*w h*w*c
print("t shape:{}\nt_transpose shape: {}".format(t.shape, t_transpose.shape))
输出:
t shape:torch.Size([2, 3, 4])
t_transpose shape: torch.Size([2, 4, 3])
3.3 torch.t()
功能:2维张量装置,对矩阵而言,等价于torch.transpose(input, 0, 1)
3.4 torch.squeeze()
功能:压缩长度为1的维度(轴)
测试代码:
# ======================================= example 9 =======================================
# torch.squeeze
flag = True
#flag = False
if flag:
t = torch.rand((1, 2, 3, 1))
t_sq = torch.squeeze(t)
t_0 = torch.squeeze(t, dim=0)
t_1 = torch.squeeze(t, dim=1)
print(t.shape)
print(t_sq.shape)
print(t_0.shape)
print(t_1.shape)
输出:
torch.Size([1, 2, 3, 1])
torch.Size([2, 3])
torch.Size([2, 3, 1])
torch.Size([1, 2, 3, 1])
3.5 torch.unsqueeze()
功能:依据dim扩展维度
torch.add()
功能:逐元素计算input + alpha x other
测试代码:
# ======================================= example 8 =======================================
# torch.add
flag = True
#flag = False
if flag:
t_0 = torch.randn((3, 3))
t_1 = torch.ones_like(t_0)
t_add = torch.add(t_0, 10, t_1)
print("t_0:\n{}\nt_1:\n{}\nt_add_10:\n{}".format(t_0, t_1, t_add))
输出:
t_0:
tensor([[ 0.6614, 0.2669, 0.0617],
[ 0.6213, -0.4519, -0.1661],
[-1.5228, 0.3817, -1.0276]])
t_1:
tensor([[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]])
t_add_10:
tensor([[10.6614, 10.2669, 10.0617],
[10.6213, 9.5481, 9.8339],
[ 8.4772, 10.3817, 8.9724]])
torch.addcmul()
功能:
out i = _{i}= i= input i + _{i}+ i+ value × \times × tensor 1 i × 1_{i} \times 1i× tensor 2 i 2_{i} 2i
torch.addcdiv()
功能:
out i = _{i}= i= input i + _{i}+ i+ value × tensor 1 i tensor 2 i \times \frac{\text { tensor } 1_{i}}{\text { tensor } 2_{i}} × tensor 2i tensor 1i
求解步骤
1.确定模型
Model: y = w x + b y=w x+b y=wx+b
2.选择损失函数
MSE: 1 m ∑ i = 1 m ( y i − y ^ i ) 2 \frac{1}{m} \sum_{i=1}^{m}\left(y_{i}-\hat{y}_{i}\right)^{2} m1∑i=1m(yi−y^i)2
3.求解梯度并更新w,b
w = w − L R ∗ w ⋅ g r a d w=w-L R^{*} w \cdot g r a d w=w−LR∗w⋅grad
b = b − L R ∗ w ⋅ g r a d b=b-L R^{*} w \cdot g r a d b=b−LR∗w⋅grad
测试代码:
import torch
import matplotlib.pyplot as plt
torch.manual_seed(10)
lr = 0.05 # 学习率 20191015修改
# 创建训练数据
x = torch.rand(20, 1) * 10 # x data (tensor), shape=(20, 1)
y = 2*x + (5 + torch.randn(20, 1)) # y data (tensor), shape=(20, 1)
# 构建线性回归参数
w = torch.randn((1), requires_grad=True)
b = torch.zeros((1), requires_grad=True)
for iteration in range(1000):
# 前向传播
wx = torch.mul(w, x)
y_pred = torch.add(wx, b)
# 计算 MSE loss
loss = (0.5 * (y - y_pred) ** 2).mean()
# 反向传播
loss.backward()
# 更新参数
b.data.sub_(lr * b.grad)
w.data.sub_(lr * w.grad)
# 清零张量的梯度
w.grad.zero_()
b.grad.zero_()
# 绘图
if iteration % 20 == 0:
plt.scatter(x.data.numpy(), y.data.numpy())
plt.plot(x.data.numpy(), y_pred.data.numpy(), 'r-', lw=5)
plt.text(2, 20, 'Loss=%.4f' % loss.data.numpy(), fontdict={'size': 20, 'color': 'red'})
plt.xlim(1.5, 10)
plt.ylim(8, 28)
plt.title("Iteration: {}\nw: {} b: {}".format(iteration, w.data.numpy(), b.data.numpy()))
plt.pause(0.5)
if loss.data.numpy() < 1:
break
1 线性回归模型
调整线性回归模型停止条件以及y = 2*x + (5 + torch.randn(20, 1))中的斜率,训练一个线性回归模型:
测试代码:
"""
@file name : lesson-03-Linear-Regression.py
@author : tingsongyu
@date : 2018-08-26
@brief : 一元线性回归模型
"""
import torch
import matplotlib.pyplot as plt
torch.manual_seed(10)
lr = 0.01 # 学习率
best_loss = float("inf")
# 创建训练数据
x = torch.rand(200, 1) * 10 # x data (tensor), shape=(20, 1)
y = 2*x + (5 + torch.randn(200, 1)) # y data (tensor), shape=(20, 1)
# 构建线性回归参数
w = torch.randn((1), requires_grad=True)
b = torch.zeros((1), requires_grad=True)
for iteration in range(10000):
# 前向传播
wx = torch.mul(w, x)
y_pred = torch.add(wx, b)
# 计算 MSE loss
loss = (0.5 * (y - y_pred) ** 2).mean()
# 反向传播
loss.backward()
current_loss = loss.item()
if current_loss < best_loss:
best_loss = current_loss
best_w = w
best_b = b
# 绘图
if iteration%100 == 0:
if loss.data.numpy() < 3:
plt.scatter(x.data.numpy(), y.data.numpy())
plt.plot(x.data.numpy(), y_pred.data.numpy(), 'r-', lw=5)
plt.text(2, 20, 'Loss=%.4f' % loss.data.numpy(), fontdict={'size': 20, 'color': 'red'})
plt.xlim(1.5, 10)
plt.ylim(8, 40)
plt.title("Iteration: {}\nw: {} b: {}".format(iteration, w.data.numpy(), b.data.numpy()))
plt.pause(0.5)
if loss.data.numpy() < 0.55:
break
# 更新参数
w.data.sub_(lr * w.grad)
b.data.sub_(lr * b.grad)
# 梯度清零
w.grad.zero_()
b.grad.zero_()
计算图是用来描述运算的有向无环图。
计算图有两个主要元素:结点(Node)和边(Edge)。
结点表示数据,如向量,矩阵,张量 边表示运算,如加减乘除卷积等。
3 动态图与静态图的区别是什么?
Pytorch 是动态图,每一次训练,都会销毁图并重新创建,这样做花销很大,但是更加灵活。
而 Tensorflow 是静态图,一旦定义训练时就不能修改。