pytorch入门_安装与基本数据定义

#pytorch 是什么
pytorch是一个动态的框架,是一个基于 Python 的科学计算包,NumPy 的替代品,可以利用 GPU 的性能进行计算。针对深度学习研究平台拥有足够的灵活性和速度。
#与tensorflow区别
在PyTorch中,图结构是动态的,这意味着图在运行时构建。而在TensorFlow中,图结构是静态的,这意味着图先被“编译”然后再运行。
#安装
1.先安装annaconda
https://www.jianshu.com/p/d69619c02534
2.去官网获取适配自己电脑环境的包
https://pytorch.org/get-started/locally/
image.png
3.在annaconda 控制命令行执行命令
image.png

image.png

这样就安装好了

#基本数据定义

#导入 pytorch
from future import print_function
import torch
#构建一个5行3矩阵,不初始化
x = torch.empty(5, 3)
###输出###
tensor(1.00000e-04 *
       [[-0.0000,  0.0000,  1.5135],
        [ 0.0000,  0.0000,  0.0000],
        [ 0.0000,  0.0000,  0.0000],
        [ 0.0000,  0.0000,  0.0000],
        [ 0.0000,  0.0000,  0.0000]])
#构建一个5行3矩阵,随机初始化
x = torch.rand(5, 3)
###输出###
tensor([[ 0.6291,  0.2581,  0.6414],
        [ 0.9739,  0.8243,  0.2276],
        [ 0.4184,  0.1815,  0.5131],
        [ 0.5533,  0.5440,  0.0718],
        [ 0.2908,  0.1850,  0.5297]])
构建全为0的矩阵,并且指定数据类型为long
x = torch.zeros(5, 3, dtype=torch.long)
###输出###
tensor([[ 0,  0,  0],
        [ 0,  0,  0],
        [ 0,  0,  0],
        [ 0,  0,  0],
        [ 0,  0,  0]])
构建张量
x = torch.tensor([5.5, 3])
###输出###
tensor([ 5.5000,  3.0000])
#创建一个和另外一个tensor一样维度的数据
x = x.new_ones(5, 3, dtype=torch.double)    
x = torch.randn_like(x, dtype=torch.float)  
###输出###
tensor([[ 1.,  1.,  1.],
        [ 1.,  1.,  1.],
        [ 1.,  1.,  1.],
        [ 1.,  1.,  1.],
        [ 1.,  1.,  1.]], dtype=torch.float64)
tensor([[-0.2183,  0.4477, -0.4053],
        [ 1.7353, -0.0048,  1.2177],
        [-1.1111,  1.0878,  0.9722],
        [-0.7771, -0.2174,  0.0412],
        [-2.1750,  1.3609, -0.3322]])
#获取tensor的维度
x.size()
###输出###
torch.Size([5, 3])

#加法操作

#将x和y的对应元素相加
y = torch.rand(5, 3)
x+y
###输出###
tensor([[-0.1859,  1.3970,  0.5236],
        [ 2.3854,  0.0707,  2.1970],
        [-0.3587,  1.2359,  1.8951],
        [-0.1189, -0.1376,  0.4647],
        [-1.8968,  2.0164,  0.1092]])

#####
#调用torch的加法方法
torch.add(x, y)
###输出###
tensor([[-0.1859,  1.3970,  0.5236],
        [ 2.3854,  0.0707,  2.1970],
        [-0.3587,  1.2359,  1.8951],
        [-0.1189, -0.1376,  0.4647],
        [-1.8968,  2.0164,  0.1092]])
#将tensor相加的结果保存到指定的tensor
result = torch.empty(5, 3)
torch.add(x, y, out=result)
result
###输出###
tensor([[-0.1859,  1.3970,  0.5236],
        [ 2.3854,  0.0707,  2.1970],
        [-0.3587,  1.2359,  1.8951],
        [-0.1189, -0.1376,  0.4647],
        [-1.8968,  2.0164,  0.1092]])
#调用tensor本身的加法方法
y.add_(x)
###输出###
tensor([[-0.1859,  1.3970,  0.5236],
        [ 2.3854,  0.0707,  2.1970],
        [-0.3587,  1.2359,  1.8951],
        [-0.1189, -0.1376,  0.4647],
        [-1.8968,  2.0164,  0.1092]])
#改变tensor的维度
x = torch.randn(4, 4)
y = x.view(16)
z = x.view(-1, 8)# -1表示让机器让机器自己算应该是多少
###输出###
torch.Size([4, 4]) torch.Size([16]) torch.Size([2, 8])
#只有一个元素的tensor,可以用.item()来获取这个value
x = torch.randn(1)
x.item()
###输出###
tensor([ 0.9422])
0.9422121644020081

image.png
关注我们,我们会每天坚持推送学到的新知识给你
微信公众号

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值