pytorch学习1

【1】基础


导入torch

import torch

创建一个shape=[5,3]的tensor

# 构造一个空的5*3的矩阵
x=torch.empty(5,3)
print(x)
output:
tensor([[8.9082e-39, 5.9694e-39, 8.9082e-39],
        [1.0194e-38, 9.1837e-39, 4.6837e-39],
        [9.9184e-39, 9.0000e-39, 1.0561e-38],
        [1.0653e-38, 4.1327e-39, 8.9082e-39],
        [9.8265e-39, 9.4592e-39, 1.0561e-38]])

用tensor.rand()创建一个张量,包含了从区间[0,1)的均匀分布中抽取的一组随机数,形状由可变参数sizes 定义

x=torch.rand(5,3)
print(x)
output:
tensor([[0.8593, 0.3479, 0.3056],
        [0.1706, 0.6891, 0.6022],
        [0.3662, 0.4153, 0.1220],
        [0.2897, 0.0779, 0.7554],
        [0.0485, 0.0746, 0.2097]])

用tensor.randn()创建一个张量,包含了从标准正态分布(均值为0,方差为 1,即高斯白噪声)中抽取一组随机数。

x=torch.randn(5,3)
print(x)
output:
tensor([[-0.6925,  0.9722, -1.4809],
        [ 1.7686,  0.1854, -1.0683],
        [ 1.3334,  0.1189, -0.3679],
        [ 0.1987,  0.1779, -0.2427],
        [-1.9862, -1.0243, -0.1536]])

用构造一个矩阵元素全为 0,而且数据类型是 long.

x = torch.zeros(5, 3, dtype=torch.long)
print(x)
output:
tensor([[0, 0, 0],
        [0, 0, 0],
        [0, 0, 0],
        [0, 0, 0],
        [0, 0, 0]])

用tensor.tensor()直接构造一个初始化了的张量

x=torch.tensor([5,3,1])
print(x)
output:
tensor([5, 3, 1])

创建一个 tensor 基于已经存在的 tensor的shape

# 创建一个 tensor 基于已经存在的 tensor。
x = x.new_ones(5, 3, dtype=torch.double)      
# new_* methods take in sizes
print(x)

# tensor的shape和以存在的x是一样的
x = torch.randn_like(x, dtype=torch.float)    
print(x)                                      
# result has the same size

获取tensor的维度信息

x=torch.rand(5,3)
# 返回tensor的整个维度信息
print(x)
# 返回x第二个轴的维度的size
print(x.size(1))
output:
tensor([[0.9289, 0.3810, 0.7514],
        [0.9904, 0.1104, 0.1477],
        [0.6452, 0.5387, 0.3023],
        [0.8693, 0.4785, 0.0501],
        [0.7556, 0.6608, 0.5158]])
3

常见的加法操作

# 加法操作1
y=torch.rand(5,3)
print(y)
print(x+y)

# 加法操作2: 提供一个输出 tensor 作为参数
# result保存操作的结果
result = torch.empty(5, 3)
torch.add(x, y, out=result)
print(result)

# 加法操作3: adds x to y
# 任何使张量会发生变化的操作都有一个前缀 ‘_’。如x.copy_(y),y.add_(x)
y.add_(x)
print(y)

改变tensor的大小和形状,tensor.view(),tensor.reshape(),

# 改变大小:如果你想改变一个 tensor 的大小或者形状,你可以使用torch.view()
x=torch.rand(5,3)
# y.shape=[3,5]
y=x.view(size=(3,5))
# z.shape=[1,15]
z=x.reshape(15)
# m.shape=[3,2],且m中的元素是x中的前3*2个元素
m=x.resize_(3,2)

向量的拼接torch.cat((tensor1,tensor2),dim=0,1)

# 在给定维度上对输入的张量序列seq 进行连接操作。
# torch.cat张量的拼接操作
x=torch.randn(3,2)
print(x)
y=torch.randn(3,2)
print(y)
# 0在行上拼接,1在列上拼接
print(torch.cat((x,y),0))
print(torch.cat((x,y),1))

索引torch.index_select(input, dim, index, out=None)

# torch.index_select(input, dim, index, out=None) → Tensor
#input (Tensor) – 输入张量
#dim (int) – 索引的轴,0表示按行,1表示按列
#index (LongTensor) – 包含索引下标的一维张量
#out (Tensor, optional) – 目标张量
x = torch.randn(3, 4)
print(x)
indices = torch.LongTensor([0, 2])
# 按列索引第0,2列位置的数
print(torch.index_select(x,1,indices))
output:
tensor([[ 0.5914, -0.5157,  1.4603,  2.9129],
        [-1.4002, -0.0375,  0.1472, -0.3042],
        [-1.3447, -0.6746,  1.1775, -0.3939]])
tensor([[ 0.5914,  1.4603],
        [-1.4002,  0.1472],
        [-1.3447,  1.1775]])

tensor的转置torch.transpose(input, dim0, dim1, out=None) → Tensor

# tensor.t()张量的转置
x=torch.randn(3,2)
print(x)
print(torch.t(x))
#torch.transpose(input, dim0, dim1, out=None) → Tensor
#返回输入矩阵input的转置。交换维度dim0和dim1。 输出张量与输入张量共享内存,所以改变其中一个会导致另外一个也被修改。

改变tensor形状的tensor.repeat()操作,它可以复制原有的tensor上元素到新tensor上的指定轴(维度上),在写代码时你会发现这个操作非常好用。

import torch
x = torch.tensor([1, 2, 3])
# 第一个轴上复制4次
print(x.repeat(4))
print("###################################")
# 第一个轴上复制4次,第二个轴上复制1一次
print(x.repeat(4, 1))
print("###################################")
# 第一个轴上复制1次,第二个轴上复制4一次
print(x.repeat(1, 4))
print("###################################")
# 第一个轴上复制4次,第二个轴上复制2次
print(x.repeat(4, 2))
print("###################################")
# 第一个轴上复制4次,第二个轴上复制3次
print(x.repeat(4, 3))
print("###################################")
# 第一个轴上复制4次,第二个轴上复制3次,第三个轴上复制2次
print(x.repeat(4,3,2))
output
tensor([1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3])
###################################
tensor([[1, 2, 3],
        [1, 2, 3],
        [1, 2, 3],
        [1, 2, 3]])
###################################
tensor([[1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3]])
###################################
tensor([[1, 2, 3, 1, 2, 3],
        [1, 2, 3, 1, 2, 3],
        [1, 2, 3, 1, 2, 3],
        [1, 2, 3, 1, 2, 3]])
###################################
tensor([[1, 2, 3, 1, 2, 3, 1, 2, 3],
        [1, 2, 3, 1, 2, 3, 1, 2, 3],
        [1, 2, 3, 1, 2, 3, 1, 2, 3],
        [1, 2, 3, 1, 2, 3, 1, 2, 3]])
###################################
tensor([[[1, 2, 3, 1, 2, 3],
         [1, 2, 3, 1, 2, 3],
         [1, 2, 3, 1, 2, 3]],

        [[1, 2, 3, 1, 2, 3],
         [1, 2, 3, 1, 2, 3],
         [1, 2, 3, 1, 2, 3]],

        [[1, 2, 3, 1, 2, 3],
         [1, 2, 3, 1, 2, 3],
         [1, 2, 3, 1, 2, 3]],

        [[1, 2, 3, 1, 2, 3],
         [1, 2, 3, 1, 2, 3],
         [1, 2, 3, 1, 2, 3]]])
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值