什么是pytorch?

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])

运算

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

时间: 2024-08-30 18:34:01

什么是pytorch?的相关文章

pytorch 学习笔记之编写 C 扩展,又涨姿势了

pytorch利用CFFI 进行 C 语言扩展.包括两个基本的步骤(docs): 编写 C 代码: python 调用 C 代码,实现相应的 Function 或 Module. 在之前的文章中,我们已经了解了如何自定义 Module.至于 [py]torch 的 C 代码库的结构,我们留待之后讨论: 这里,重点关注,如何在 pytorch C 代码库高层接口的基础上,编写 C 代码,以及如何调用自己编写的 C 代码. 官方示例了如何定义一个加法运算(见 repo).这里我们定义ReLU函数(见

(转) The Incredible PyTorch

转自:https://github.com/ritchieng/the-incredible-pytorch The Incredible PyTorch What is this? This is inspired by the famous Awesome TensorFlow repository where this repository would hold tutorials, projects, libraries, videos, papers, books and anythi

解决运行pytorch程序多线程问题

当我使用pycharm运行  (https://github.com/Joyce94/cnn-text-classification-pytorch )  pytorch程序的时候,在Linux服务器上会开启多个进程,占用服务器的大量的CPU,在windows10上运行此程序的时候,本机的CPU和内存会被吃光,是因为在train.py中有大量的数据训练处理,会开启多个进程,占用大量的CPU和进程. 本机window10 linux服务器开启了多个进程 Linux服务器占用大量CPU 在pytor

PyTorch学习笔记之nn的简单实例

method 1 1 import torch 2 from torch.autograd import Variable 3 4 N, D_in, H, D_out = 64, 1000, 100, 10 5 x = Variable(torch.randn(N, D_in)) 6 y = Variable(torch.randn(N, D_out), requires_grad=False) 7 8 # define our model as a sequence of layers 9 m

pytorch中设定使用指定的GPU

转自:http://www.cnblogs.com/darkknightzh/p/6836568.html PyTorch默认使用从0开始的GPU,如果GPU0正在运行程序,需要指定其他GPU. 有如下两种方法来指定需要使用的GPU. 1. 类似tensorflow指定GPU的方式,使用CUDA_VISIBLE_DEVICES. 1.1 直接终端中设定: CUDA_VISIBLE_DEVICES=1 python main.py 1.2 python代码中设定: import os os.env

一线开发者在Reddit上讨论深度学习框架:PyTorch和TensorFlow到底哪个更好?

本文标签:   机器学习 TensorFlow Google深度学习框架 分布式机器学习 PyTorch   近日,Reddit用户 cjmcmurtrie 发了一个主题为「PyTorch vs. TensorFlow」的讨论帖,想要了解这两大流行的框架之间各自有什么优势. 原帖地址:https://redd.it/5w3q74 帖子一楼写道: 我还没有从 Torch7 迁移到 TensorFlow.我玩过 TensorFlow,但我发现 Torch7 更加直观(也许是我玩得不够?).我也尝试了

Ubutnu16.04安装pytorch

1.下载Anaconda3 首先需要去Anaconda官网下载最新版本Anaconda3(https://www.continuum.io/downloads),我下载是是带有python3.6的Anaconda3-4.4.0-Linux-x86_64.sh. 2.安装Annconda3 bash Anaconda3-4.4.0-Linux-x86_64.sh   在home/ubuntu出现anaconda3文件夹(注:ubuntu是系统用户名.下同). source ~/.bashrc 3.

PyTorch框架+Python 3面向对象编程学习笔记

一.CNN情感分类中的面向对象部分 sparse.py 1 super(Embedding, self).__init__() 表示需要父类初始化,即要运行父类的_init_(),如果没有这个,则要自定义初始化 1 self.weight = Parameter(torch.Tensor(num_embeddings, embedding_dim)) Parameter跳转 1 class Parameter(Variable): 2 """A kind of Variabl

PyTorch学习问题记录

Q1:def train() 中的model.train()的作用是什么?为什么要写? A1:class torch.nn.Module中 train(mode=True) Sets the module in training mode. This has any effect only on modules such as Dropout or BatchNorm. 参看 http://pytorch.org/docs/master/nn.html Q2:torch.gather() 和 t

基于pytorch实现word2vec

一.介绍 word2vec是Google于2013年推出的开源的获取词向量word2vec的工具包.它包括了一组用于word embedding的模型,这些模型通常都是用浅层(两层)神经网络训练词向量. Word2vec的模型以大规模语料库作为输入,然后生成一个向量空间(通常为几百维).词典中的每个词都对应了向量空间中的一个独一的向量,而且语料库中拥有共同上下文的词映射到向量空间中的距离会更近. word2vec目前普遍使用的是Google2013年发布的C语言版本,现在也有Java.C++.p