卷积核的grad为none_pytorch入门与实践(1):张量的基本操作和自动微分requires_grad=True...

d34699c0f31ce38803db82c178391f89.png

PyTorch是一个基于Python的库,提供了一个具有灵活易用的深度学习框架,是近年来最受欢迎的深度学习框架之一。

如果你是新新新手,可以先学习以下教程: 深度学习之PyTorch实战-基础学习及搭建环境 PyTorch中文文档

改编自:

(1) DEEP LEARNING WITH PYTORCH: A 60 MINUTE BLITZ

(2) https://www.kesci.com/home/project/5e0038642823a10036ae9ebf/code

1 初识PyTorch

1.1 张量

1.导入pytorch包

import torch

2.创建一个未初始化的5x3张量

x = torch.empty(5, 3)
print(x)
tensor([[2.7517e+12, 7.5338e+28, 3.0313e+32],
        [6.3828e+28, 1.4603e-19, 1.0899e+27],
        [6.8943e+34, 1.1835e+22, 7.0976e+22],
        [1.8515e+28, 4.1988e+07, 3.0357e+32],
        [2.7224e+20, 7.7782e+31, 4.7429e+30]])

3.创建一个随机初始化的5x3张量

x = torch.rand(5, 3)
print(x)
tensor([[0.2218, 0.1979, 0.9491],
        [0.2082, 0.6139, 0.1096],
        [0.1491, 0.5660, 0.7435],
        [0.8009, 0.0788, 0.0223],
        [0.4252, 0.2422, 0.8202]])

4.创建一个5x3的0张量,类型为long

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

5.直接从数组创建张量

x = torch.tensor([5.5, 3])
print(x)
tensor([5.5000, 3.0000])

6.创建一个5x3的单位张量,类型为double

x = torch.ones(5, 3, dtype=torch.double)
print(x)
tensor([[1., 1., 1.],
        [1., 1., 1.],
        [1., 1., 1.],
        [1., 1., 1.],
        [1., 1., 1.]], dtype=torch.float64)

7.从已有的张量创建相同维度的新张量,并且重新定义类型为float

x = torch.randn_like(x, dtype=torch.float)
print(x)
tensor([[ 0.0385,  0.2725,  0.7144],
        [ 0.1436,  0.0642,  0.4125],
        [ 0.2571,  2.0826, -1.0106],
        [-0.4239,  0.6424,  1.3656],
        [-0.4444,  0.5675,  1.3937]])

8.打印一个张量的维度

print(x.size())
torch.Size([5, 3])

9.将两个张量相加

y = torch.rand(5, 3)
print(x + y)
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值