Pytorch框架学习(2)——张量操作与线性回归

张量操作与线性回归

1. 张量的操作:拼接、切分、索引和变换

1.1 张量的拼接与切分

  • torch.cat()

  • 功能:将张量按维度dim进行拼接(不会扩充张量的维度)

    • tensors:张量序列
    • dim:要拼接的维度
  • torch.stack()

  • 功能:在新创建的维度dim上进行拼接(创建新的维度扩充张量)

    • tensors:张量序列
    • dim:要拼接的维度
  • torch.chunk()

  • 功能:将张量按维度dim进行平均切分

  • 返回值:张量列表

  • 注意:若不能整除,最后一份张量小于其他张量

    • input:要切分的张量
    • chunks:要切分的份数
    • dim:要切分的维度
  • torch.split()

  • 功能:将张量按维度dim进行切分

  • 返回值:张量列表

    • tensor:要切分的张量
    • split_size_or_sections:为int时,表示每一份的长度;为list时,按list元素切分
    • dim:要切分的维度
t = torch.ones(2, 5)
list_of_tensors = torch.split(t, [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.]]),shape is torch.Size([2, 1])
第3个张量:tensor([[1., 1.],
[1., 1.]]),shape is torch.Size([2, 2])

1.2 张量索引

  • torch.index_select()
  • 功能:在维度dim上,按index索引数据
  • 返回值:依index索引数据拼接的张量
    • input:要索引的张量
    • dim:要索引的维度
    • index:要索引数据的序号(tensor数据结构,tensor里面的数据必须为长整型,torch.long)
t = torch.randint(0, 9, size=(3, 3))
idx = torch.tensor([0, 2], dtype=torch.long)
t_select = torch.index_select(t, dim=0, index=idx)
print('t:\n{}\nt_select\n:{}'.format(t, t_select))

结果为:

t:
tensor([[5, 8, 2],
        [1, 3, 0],
        [2, 1, 6]])
t_select
:tensor([[5, 8, 2],
        [2, 1, 6]])
  • torch.masked_select()
  • 功能:按mask中的True进行索引,通常用来筛选数据
  • 返回值:一维张量
    • input:要索引的张量
    • mask:与input同形状的布尔类型张量
t = torch.randint(0, 9, size=(3, 3))
mask = t.ge(5) # ge is mean greater than or equal.  gt is mean greater than . 还有le和lt
t_select = torch.masked_select(t, mask)
print('t:\n{}\nt_select\n:{}'.format(t, t_select))

结果为:

t:
tensor([[5, 7, 2],
        [1, 6, 6],
        [1, 1, 8]])
t_select
:tensor([5, 7, 6, 6, 8])

1.3 张量变换

  • torch.reshape()
  • 功能:变换张量形状
  • 注意:当张量在内存中是连续时,新张量与input共享数据内存
    • input:要变换的张量
    • shape:新张量的形状
        t = torch.randperm(8)
        t_reshape = torch.reshape(t, (2, 4))
        print('t:{}\nt_reshape:\n{}'.format(t, t_reshape))
        print('t.data内存地址:{}\nt_reshape.data内存地址:{}'.format(id(t.data), id(t_reshape.data)))

结果为:

t:tensor([0, 4, 1, 3, 5, 7, 6, 2])
t_reshape:
tensor([[0, 4, 1, 3],
        [5, 7, 6, 2]])
t.data内存地址:2126059754696
t_reshape.data内存地址:2126059754696
  • torch.transpose()

  • 功能:交换张量的两个维度

    • input:要交换的张量
    • dim0:要交换的维度
    • dim1:要交换的维度
  • torch.t()

  • 功能:2维张量转置,对矩阵而言,等价于torch.transpose(input, 0, 1)

  • torch.sequeeze()

  • 功能:压缩长度为1的维度(轴)

    • dim:若为None,移除所有长度为1的轴;若指定维度,当且仅当该轴长度为1时,可以被移除。
  • torch.unsequeeze()

  • 功能:依据dim扩展维度

    • dim:扩展的维度
        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])

2.张量的数学运算

张量之间可以进行加减乘除,可以进行对数、指数、幂函数运算,可以进行三角函数运算
在这里插入图片描述
这里简单介绍加法运算

  • torch.add()
  • 功能:逐元素计算***input+alpha+other***
    • input:第一个张量
    • alpha:乘项因子
    • other:第二个张量

除此之外,还有两个加法运算,一个是加法结合除法,另一个是加法结合乘法

  • torch.addcdiv() o u t i = i n p u t i + v a l u e × t e n s o r 1 i t e n s o r 2 i out_i = input_i + value \times \frac{tensor1_i}{tensor2_i} outi=inputi+value×tensor2itensor1i
  • torch.addcmul() i n t p u i + v a l u e × t e n s o r 1 i × t e n s o r 2 i intpu_i + value \times tensor1_i \times tensor2_i intpui+value×tensor1i×tensor2i

3.线性回归

线性回归是分析一个变量与另外一个(多)个变量之间关系的方法。

  • 因变量:y
  • 自变量:x
  • 关系:线性 y = w x + b y = wx + b y=wx+b
  • 分析:求解w,b

3.1 求解步骤

  1. 确定模型:
    M o d e l : y = w x + b Model : y = wx + b Model:y=wx+b
  2. 选择损失函数loss:
    一般选择均方误差MSE: 1 m ∑ i = 1 m ( y i − y i ^ ) 2 \frac{1}{m} \sum_{i=1}^{m}(y_i -\hat{y_i})^2 m1i=1m(yiyi^)2
  3. 求解梯度并更新w,b:
    w = w − L R ∗ w . g r a d w = w - LR * w.grad w=wLRw.grad(LR为步长,学习率)
    b = b − L R ∗ w . g r a d b = b - LR * w.grad b=bLRw.grad

import torch
import matplotlib.pyplot as plt


def main():

    torch.manual_seed(10)

    # 学习率
    lr = 0.05

    # 创建训练数据
    x = torch.rand(20, 1) * 10
    y = 2*x + (5 + torch.randn(20, 1)) # y值随机加入扰动

    #构建线性回归模型
    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 = (0.5 * (y - y_pred)**2).mean()

        # 反向传播
        loss.backward()

        #更新参数
        b.data.sub_(lr * b.grad)
        w.data.sub_(lr * w.grad)

        # 清零张量的梯度   20191015增加
        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('Iteratiion:{}\nw:{} b:{}'.format(iteration, w.data.numpy(), b.data.numpy()))
            plt.pause(0.5)

            if loss.data.numpy() < 1:
                break


if __name__ == '__main__':
    main()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Aidanmomo

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值