张量的操作:拼接、切分、索引和变换
1张量的拼接与切分
1.1 torch.cat(tensors,dim=0,out=None)
1.2 torch.stack(tensors,dim=0,out=None)
区别:cat不会扩展张量的维度,而stack会扩展张量的维度
# 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))
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))
1.3 torch.chunk(input,chunks,dim=0)
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))
chunks=2时(整除后向上取整,最后一个剩余多少则为多少)
1.4 torch.split(tensor,split_size_or_sections,dim=0)(可以指定切分长度)
flag = True
#flag = False
if flag:
t = 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的时候一定要让切分的求和要等于指定维度的长度
flag = True
#flag = False
if flag:
t = torch.ones((2, 5))
list_of_tensors = torch.split(t, [2 , 1, 2], dim=1) # [2 , 1, 2]
for idx, t in enumerate(list_of_tensors):
print("第{}个张量:{}, shape is {}".format(idx+1, t, t.shape))
2、张量索引
2.1 torch.index_select(input,dim,index,out=None)
flag = True
#flag = False
if flag:
t = torch.randint(0, 9, size=(3, 3)) #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))
2.2 torch.masked_select(input,mask,out=None)(筛选数据)
flag = True
#flag = False
if flag:
t = torch.randint(0, 9, size=(3, 3))
mask = t.ge(5) #(大于等于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))
3、张量变换
3.1 torch.reshape(input,shape)
flag = True
#flag = False
if flag:
t = torch.randperm(8)
t_reshape = torch.reshape(t, (2,4)) # -1(根据另一维度计算而得)
print("t:{}\nt_reshape:\n{}".format(t, t_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)))
3.2 torch.transpose(input,dim0,dim1)
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))
将numpy用reshape改变形状时,不会改变连续型,但用transpose时会变成非连续的,但是不论是否变成连续性,地址都改变了
3.3 torch.t(input)
3.4 torch.squeeze(input,dim=None,out=None)
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)
3.5 torch.unsqueeze(input,dim,out=None)
张量数据运算
torch.add(input,alpha=1,other,out=None)
torch.addcmul(input,value=1,tensor1,tensor2,out=None)
pythonic:
torch.addcmul()
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))
一元线性模型
线性回归是分析一个变量于另外一(多)个变量之间关系的方法
因变量:y 自变量:x 关系:线性
y = wx +b 分析:求解w、b
求解步骤:
1. 确定模型 model:y = wx +b
3.求解梯度并更新w、b w = w - lr*w.grad b = b - lr*w.grad
import torch
import matplotlib.pyplot as plt
torch.manual_seed(10)
#%%
lr = 0.1 # 学习率
# 创建训练数据
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、b)
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)
# 绘图
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、计算图
计算图是用来描述运算的有向无环图
计算图有两个主要元素:结点(Node)和边(Edge),结点表示数据,如向量、矩阵、张量,边表示运算,如加减乘除卷积等
用计算图表示: y=(x+w)*(w+1) a = x+w b = w+1 y = a*b
叶子结点:用户创建的结点称为叶子结点,如x和w(设置叶子结点,主要是为了节省内存)
is_leaf:指示张量是否为也子结点
import torch
w = torch.tensor([1.], requires_grad=True)
x = torch.tensor([2.], requires_grad=True)
a = torch.add(w, x) # retain_grad()
b = torch.add(w, 1)
y = torch.mul(a, b)
y.backward()
print(w.grad)
# 查看叶子结点
print("is_leaf:\n", w.is_leaf, x.is_leaf, a.is_leaf, b.is_leaf, y.is_leaf)
# 查看梯度
print("gradient:\n", w.grad, x.grad, a.grad, b.grad, y.grad)
# 查看 grad_fn
print("grad_fn:\n", w.grad_fn, x.grad_fn, a.grad_fn, b.grad_fn, y.grad_fn)
grad_fn:记录创建该张量时所用的方法(函数)
y.grad_fn =
a.grad_fn =
b.grad_fn =
动态图
根据计算图搭建方式,可将计算图分为动态图和静态图
动态图:运算与搭建同时进行(灵活、易调节)
静态图:先搭建图,后运算(高效、不灵活)