pytorch之深度学习

pytorch使用

一、pytorch使用教程

pytorch是一个基于Python的科学数据包,是numpy的替代品。

Tensors(张量)的使用

tensors类似与numpy中的ndarrays,同时tensors可以进行GPU加速:

  1. tensors的简单使用
import torch
(1)构建一个矩阵,不初始化
x=torch.empty(5,3)
print(x)
显示结果
tensor([[1.0561e-38, 1.0653e-38, 4.1327e-39],
        [8.9082e-39, 9.8265e-39, 9.4592e-39],
        [1.0561e-38, 1.0653e-38, 1.0469e-38],
        [9.5510e-39, 8.7245e-39, 9.0918e-39],
        [1.0102e-38, 9.6429e-39, 8.7245e-39]])
(2)构造一个5x3随机初始化矩阵
x=torch.rand(5,3)
print(x)
输出:
tensor([[0.4719, 0.3377, 0.4123],
        [0.8551, 0.5074, 0.5010],
        [0.9962, 0.7364, 0.4845],
        [0.5003, 0.7620, 0.9850],
        [0.0910, 0.4591, 0.5037]])
(3)构造一个全为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]])

(4)构造一个张量,直接使用数据

x=torch.tensor([10,2])
print(x)
输出:
tensor([10,  2])

(5)创建一个tensor基于已经存在的tensor

x=x.new_ones(5,3,dtype=torch.double)
print(x)
x=torch.randn_like(x,dtype=torch.float)
print(x)
输入:
tensor([[1., 1., 1.],
        [1., 1., 1.],
        [1., 1., 1.],
        [1., 1., 1.],
        [1., 1., 1.]], dtype=torch.float64)
tensor([<
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值