PyTorch 入门笔记总结

PyTorch 入门笔记总结

1、Tensor张量

创建张量

Torch构造张量有如下几种形式:

import torch
#直接构造类似于numpy中ndarray的随机张量
x = troch.rand(5,3)
print(x)
#利用已有数据构造张量
x = torch.tensor([[[5,3,1],[6,4,2]])
print(x)
#根据已经存在的tensor创建
x = x.new_ones(5, 3, dtype=torch.double)     
print(x) 
x = torch.randn_like(x, dtype=torch.float32)    
print(x)                                      

#输出
tensor([[0.4041, 0.4289, 0.2413],
        [0.8269, 0.2149, 0.6988],
        [0.3254, 0.6165, 0.4514],
        [0.9963, 0.1263, 0.7608],
        [0.6543, 0.1745, 0.6938]])

tensor([[5, 3, 1],
        [6, 4, 2]])

tensor([[1., 1., 1.],
        [1., 1., 1.],
        [1., 1., 1.],
        [1., 1., 1.],
        [1., 1., 1.]], dtype=torch.float64)
        
tensor([[-0.4634,  0.2839, -0.3666],
        [-0.5293,  0.2354,  0.6911],
        [-1.6419,  0.5335,  0.7720],
        [ 0.5266, -0.9511,  0.0539],
        [-2.4723,  0.4116, -0.3734]]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值