【pyTorch基础】初窥张量

import numpy as np
import torch
# 等差数列(开始,结束,个数)
torch.linspace(0.1,1,5)
tensor([0.1000, 0.3250, 0.5500, 0.7750, 1.0000])
# 空矩阵
print(torch.empty([3,4]))
# 找了一块不用的内存,不在乎里面的数据,可能是之前遗留下来的(返回未初始化的数据)
print(torch.empty(3,5))
tensor([[0., 0., 0., 0.],
        [0., 0., 0., 0.],
        [0., 0., 0., 0.]])
tensor([[1.0286e-38, 1.0194e-38, 9.6429e-39, 9.2755e-39, 9.1837e-39],
        [9.3674e-39, 1.0745e-38, 1.0653e-38, 9.5510e-39, 1.0561e-38],
        [1.0194e-38, 1.1112e-38, 1.0561e-38, 9.9184e-39, 1.0653e-38]])
# 以base为底,(2,6)五等份,从2的2次方到2的6次方
torch.logspace(2,6,5,base = 2)
tensor([ 4.,  8., 16., 32., 64.])
# (m,n)全a的矩阵
torch.full((3,4),2)
tensor([[2, 2, 2, 2],
        [2, 2, 2, 2],
        [2, 2, 2, 2]])
# 二维单位矩阵(E)
torch.eye(5)
tensor([[1., 0., 0., 0., 0.],
        [0., 1., 0., 0., 0.],
        [0., 0., 1., 0., 0.],
        [0., 0., 0., 1., 0.],
        [0., 0., 0., 0., 1.]])

依概率分布创建张量

# 高斯分布:torch.normal(mean, std, out=None)
print(torch.normal(2, 3, size=(1,5)))
# 一一对应生成一个
print(torch.normal(mean = torch.arange(1.,11.), std = torch.arange(1,0,-0.1)))
tensor([[6.0627, 3.5489, 3.7053, 0.9814, 3.8663]])
tensor([-0.7955,  1.0690,  3.7225,  3.0768,  4.5076,  5.9667,  6.9369,  8.6122,
         8.8240,  9.8426])
# 标准正态分布
torch.randn((3,4))
tensor([[-0.1535, -1.9643, -0.8889, -0.4511],
        [ 0.4905,  0.1066,  0.5044,  1.4474],
        [ 1.2457, -0.8081,  2.6163, -1.1373]])
# 整数均匀分布(均匀分布 => 范围内的数被取到的概率一样 => low,high范围内的整数随机数) [low,high)
torch.randint(1, 10, (2,3))
tensor([[3, 7, 8],
        [9, 7, 8]])
# 生成从0到n-1的随机排列(随机调整数据序列)
torch.randperm(5)
tensor([2, 0, 1, 4, 3])
# 伯努利分布(0-1分布)
s = torch.empty(3, 3).uniform_(0,1) # 生成(0,1)之间的随机数
print(s)
print(torch.bernoulli(s))
tensor([[0.8420, 0.6974, 0.6081],
        [0.2279, 0.3899, 0.1904],
        [0.9900, 0.6994, 0.6235]])
tensor([[1., 1., 1.],
        [1., 1., 0.],
        [1., 1., 0.]])

张量拼接与切分

# torch.cat:按dim指定维度拼接,0为行,1为列
x = torch.randint(1, 10, (2, 3))
torch.cat((x, x, x),dim = 0)
tensor([[7, 3, 7],
        [7, 4, 7],
        [7, 3, 7],
        [7, 4, 7],
        [7, 3, 7],
        [7, 4, 7]])
# torch.stack:新创建一个维度,拼接(在高一维拼接)
y = torch.randint(1, 10, (2,3))
torch.stack((x,y),1)
tensor([[[7, 3, 7],
         [9, 1, 8]],
        [[7, 4, 7],
         [9, 5, 9]]])
# torch.split:为int时,表示每一份的长度;为list时,按list元素切分
a = torch.arange(10).reshape(5,2)
print('a:',a)
print('为int时:',torch.split(a, 1)) # 返回tensor列表
print('为list时:',torch.split(a,[2,3]))
a: tensor([[0, 1],
        [2, 3],
        [4, 5],
        [6, 7],
        [8, 9]])
为int时: (tensor([[0, 1]]), tensor([[2, 3]]), tensor([[4, 5]]), tensor([[6, 7]]), tensor([[8, 9]]))
为list时: (tensor([[0, 1],
        [2, 3]]), tensor([[4, 5],
        [6, 7],
        [8, 9]]))

张量索引

# torch.index_select:按dim指定,截取;indices参数必须是tensor数组的格式
x = torch.randn(3, 4)
print(x)
indices = torch.tensor([0, 2])
torch.index_select(x, 1, indices)
tensor([[-0.3880,  0.3159, -1.5236, -1.0030],
        [ 1.2080,  0.5500,  0.0146, -1.0543],
        [-1.7705,  0.6398,  2.2544, -2.0102]])

tensor([[-0.3880, -1.5236],
        [ 1.2080,  0.0146],
        [-1.7705,  2.2544]])
# torch.masked_select
x = torch.randn(3,4)
print(x)
mask = x.ge(0.5)# 返回True or False 以input为界
print(mask)
torch.masked_select(x, mask) # 按True进行索引,返回一维数组
tensor([[ 0.0288, -0.0047,  1.6408,  0.2148],
        [-0.7997, -0.3897,  1.1881, -0.5227],
        [-0.5981,  1.8946,  0.1165,  0.7575]])
        
tensor([[False, False,  True, False],
        [False, False,  True, False],
        [False,  True, False,  True]])
        
tensor([1.6408, 1.1881, 1.8946, 0.7575])

张量变换

# reshape 10 = 2 * 5
torch.arange(10).reshape(2,5)
tensor([[0, 1, 2, 3, 4],
        [5, 6, 7, 8, 9]])
# torch.transpose:按维度转置
x = torch.randint(1,10,(3,4))
print(x)
y = torch.transpose(x, 0, 1)# dim=1和dim=0交换
print(y)
# print(x.T) 二维的矩阵转置可以直接x.T得到
tensor([[2, 1, 9, 7],
        [9, 4, 5, 8],
        [6, 9, 9, 8]])
tensor([[2, 9, 6],
        [1, 4, 9],
        [9, 5, 9],
        [7, 8, 8]])
# torch.squeeze:压缩长度为1的维度
x = torch.zeros(2, 1, 2, 1, 2)
print(x.shape)
y = torch.squeeze(x)
print(y,'\n',y.shape)
torch.Size([2, 1, 2, 1, 2])
tensor([[[0., 0.],
         [0., 0.]],

        [[0., 0.],
         [0., 0.]]]) 
 torch.Size([2, 2, 2])
# torch.unsqueeze:按照dim扩展维度
x = torch.tensor([1, 2, 3, 4])
torch.unsqueeze(x, dim = 1)
tensor([[1],
        [2],
        [3],
        [4]])

线性回归Demo

# 首先随机生成训练样本x,y
x = torch.rand(20,1) * 10 
y = 2 * x + (torch.rand(20,1) + 6) # y = ax + b
# 初始化线性回归参数w,b
w = torch.randn((1), requires_grad=True)
b = torch.randn((1), requires_grad=True)
# 设置学习率lr
lr = 0.05
for iterator in range(100):
    wx = torch.mul(w, x)# 向量相乘
    y_pre = torch.add(wx,b)# 向量相加
    # 均方误差损失loss=
    loss = (0.5 * (y - y_pre) ** 2).mean()
    # 损失函数求导,梯度下降法最小化loss
    loss.backward()
    # 更新参数
    w.data.sub_(lr * w.grad) # '-' 表示 -=
    b.data.sub_(lr * b.grad)
    # 梯度清零(可以放到求导之前)
    w.grad.data.zero_()
    b.grad.data.zero_()
print(w.data, b.data)
tensor([2.1755]) tensor([5.2428])
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值