码迷,mamicode.com
首页 > 其他好文 > 详细

什么是pytorch?

时间:2018-09-10 21:19:38      阅读:306      评论:0      收藏:0      [点我收藏+]

标签:one   color   bsp   gpu   类型   oat   tensor   运算   计算   

Pytorch是基于python的科学计算包,为两类受众提供服务

  • 作为Numpy的替换,让你可以使用GPU的算力
  • 作为一个深度学习计算平台提供最大的计算灵活性与速度

开始体验pytorch的基础功能

Tensor:

tensor与Numpy的高维数据概念类似,可以在GPU上进行计算

import torch

建立一个5*3的未初始化的tensor

x=torch.empty(5,3)
print(x)

out:
tensor(1.00000e-07 *
       [[-0.0000,  0.0000,  0.0000],
        [ 0.0000,  9.4713,  0.0000],
        [ 9.4201,  0.0000,  0.0000],
        [ 0.0000, -0.0000,  0.0000],
        [-0.0000,  0.0000, -0.0000]])

 建立一个随机初始化的tensor

x=torch.rand(5,3)
print(x)

out:

tensor([[ 0.7816, 0.8146, 0.9424],

[ 0.0888, 0.5530, 0.9181],
[ 0.8362, 0.1937, 0.0084],
[ 0.2004, 0.2818, 0.8674],
[ 0.6464, 0.4978, 0.8994]])

 建立一个tensor用0填充并使用long类型

x=torch.zeros(5,3,dtype=torch.long)
print(x)

out:

tensor([[ 0, 0, 0],
[ 0, 0, 0],
[ 0, 0, 0],
[ 0, 0, 0],
[ 0, 0, 0]])

 直接从数据创建tensor

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

out:

tensor([ 5.5000, 3.0000])

我们也可以基于现有的tensor建立新的tensor,这样新的tensor会复用之前的属性,比如类型等

x=torch.new_ones(5,3,dtype=torch.double)
print(x)

x=torch.randn_like(x,dtype=torch.float)
print(x)

out:

tensor([[ 1., 1., 1.],
[ 1., 1., 1.],
[ 1., 1., 1.],
[ 1., 1., 1.],
[ 1., 1., 1.]], dtype=torch.float64)

tensor([[ 0.3648, 0.5067, 1.1720],
[-1.3361, 0.9999, 0.4133],
[-0.2699, 0.7601, -1.1138],
[-1.8955, -0.4079, 1.0827],
[-0.0156, 0.3810, 1.2646]])

获得tensor尺寸

print(x.size())

out:

torch.Size([5, 3])

 

运算

 

什么是pytorch?

标签:one   color   bsp   gpu   类型   oat   tensor   运算   计算   

原文地址:https://www.cnblogs.com/Thinker-pcw/p/9622767.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!