Pytorch 1.0.0 学习笔记:

Pytorch 的学习可以参考:Welcome to PyTorch Tutorials

Pytorch 是什么?

快速上手 Pytorch!

Tensors(张量)

from __future__ import print_function
import torch

创建一个没有初始化的 \(5\times 3\) 矩阵:

x = torch.empty(5, 3)
print(x)
tensor([[0.0000e+00, 0.0000e+00, 0.0000e+00],
[0.0000e+00, 0.0000e+00, 0.0000e+00],
[0.0000e+00, 0.0000e+00, 0.0000e+00],
[0.0000e+00, 1.9730e-42, 0.0000e+00],
[0.0000e+00, 7.3909e+22, 0.0000e+00]])

创建一个已经初始化的 \(5\times 3\) 的随机矩阵:

x = torch.rand(5, 3)
print(x)
tensor([[0.2496, 0.8405, 0.7555],
[0.9820, 0.9988, 0.5419],
[0.6570, 0.4990, 0.4165],
[0.6985, 0.9972, 0.4234],
[0.0096, 0.6374, 0.8520]])

给定数据类型为 long 的 \(5\times 3\) 的全零矩阵:

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

直接从 list 中创建张量:

x = torch.tensor([5.5, 3])  # list
print(x)
tensor([5.5000, 3.0000])

直接从 Numpy 中创建张量:

import numpy as np
a = np.array([2, 3, 5], dtype='B')
x = torch.tensor(a) # numpy
print(x)
x.numel() # Tensor 中元素的个数
tensor([2, 3, 5], dtype=torch.uint8)

3
x = torch.rand(5, 3)
size = x.size()
print(size)
h, w = size
h, w
torch.Size([5, 3])

(5, 3)

Operations(运算)

Tensor 的运算大都与 Numpy 相同,下面仅仅介绍一些特殊的运算方式:

x = x.new_ones(5, 3, dtype=torch.double)      # new_* methods take in sizes
print(x) x = torch.randn_like(x, dtype=torch.float) # override dtype!
print(x)
tensor([[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]], dtype=torch.float64)
tensor([[-0.9367, -0.1121, 1.9103],
[ 0.2284, 0.3823, 1.0877],
[-0.2797, 0.7217, -0.7032],
[ 0.9047, 1.7789, 0.4215],
[-1.0368, -0.2644, -0.7948]])
result = torch.empty(5, 3)  # 创建一个为初始化的矩阵
y = torch.rand(5, 3) torch.add(x, y, out=result) # 计算 x + y 并将结果赋值给 result
print(result)
tensor([[-0.0202,  0.6110,  2.8150],
[ 1.0288, 1.2454, 1.7464],
[-0.1786, 0.8212, -0.2493],
[ 1.5294, 2.2713, 0.8383],
[-0.9292, 0.5749, -0.1146]])

任何一个 可变的 tensor 所对应的运算在其适当的位置后加上 _, 便会修改原 tensor 的值:

x = torch.tensor([7])
y = torch.tensor([2])
print(y, y.add(x))
print(y, y.add_(x))
y
tensor([2]) tensor([9])
tensor([9]) tensor([9]) tensor([9])
x = torch.tensor(7)
x.item() # 转换为 python 的 number
7

reshape tensor:veiw()

x = torch.randn(4, 4)
y = x.view(16)
z = x.view(-1, 8) # the size -1 is inferred from other dimensions
print(x.size(), y.size(), z.size())
torch.Size([4, 4]) torch.Size([16]) torch.Size([2, 8])

NumPy Bridge(与 Numpy 交互)

Tensor 转换为 Numpy

a = torch.ones(5)
print(a)
tensor([1., 1., 1., 1., 1.])

Tensor 转换为 Numpy

b = a.numpy()
print(b)
[1. 1. 1. 1. 1.]

_ 的作用依然存在:

a.add_(1)
print(a)
print(b)
tensor([2., 2., 2., 2., 2.])
[2. 2. 2. 2. 2.]

Numpy 转换为 Tensor

import numpy as np
a = np.ones(5)
b = torch.from_numpy(a)
np.add(a, 1, out=a)
print(a)
print(b)
[2. 2. 2. 2. 2.]
tensor([2., 2., 2., 2., 2.], dtype=torch.float64)

CUDA

使用 .to 方法,Tensors 可被移动到任何 device:

# let us run this cell only if CUDA is available
# We will use ``torch.device`` objects to move tensors in and out of GPU
if torch.cuda.is_available():
device = torch.device("cuda") # a CUDA device object
y = torch.ones_like(x, device=device) # directly create a tensor on GPU
x = x.to(device) # or just use strings ``.to("cuda")``
z = x + y
print(z)
print(z.to("cpu", torch.double)) # ``.to`` can also change dtype together!
tensor(8, device='cuda:0')
tensor(8., dtype=torch.float64)

更多内容参考:我的github: https://github.com/XNoteW/Studying/tree/master/PyTorch_beginner

Pytorch 基础的更多相关文章

  1. [人工智能]Pytorch基础

    PyTorch基础 摘抄自<深度学习之Pytorch>. Tensor(张量) PyTorch里面处理的最基本的操作对象就是Tensor,表示的是一个多维矩阵,比如零维矩阵就是一个点,一维 ...

  2. 【新生学习】第一周:深度学习及pytorch基础

    DEADLINE: 2020-07-25 22:00 写在最前面: 本课程的主要思路还是要求大家大量练习 pytorch 代码,在写代码的过程中掌握深度学习的各类算法,希望大家能够坚持练习,相信经度过 ...

  3. pytorch基础学习(二)

    在神经网络训练时,还涉及到一些tricks,如网络权重的初始化方法,优化器种类(权重更新),图片预处理等,继续填坑. 1. 神经网络初始化(Network Initialization ) 1.1 初 ...

  4. PyTorch基础——词向量(Word Vector)技术

    一.介绍 内容 将接触现代 NLP 技术的基础:词向量技术. 第一个是构建一个简单的 N-Gram 语言模型,它可以根据 N 个历史词汇预测下一个单词,从而得到每一个单词的向量表示. 第二个将接触到现 ...

  5. pytorch 基础内容

    一些基础的操作: import torch as th a=th.rand(3,4) #随机数,维度为3,4的tensor b=th.rand(4)print(a)print(b) a+b tenso ...

  6. pytorch基础教程1

    0.迅速入门:根据上一个博客先安装好,然后终端python进入,import torch ******************************************************* ...

  7. 【pytorch】pytorch基础学习

    目录 1. 前言 # 2. Deep Learning with PyTorch: A 60 Minute Blitz 2.1 base operations 2.2 train a classifi ...

  8. Pytorch基础(6)----参数初始化

    一.使用Numpy初始化:[直接对Tensor操作] 对Sequential模型的参数进行修改: import numpy as np import torch from torch import n ...

  9. pytorch基础学习(一)

    在炼丹师的路上越走越远,开始入手pytorch框架的学习,越炼越熟吧... 1. 张量的创建和操作 创建为初始化矩阵,并初始化 a = torch.empty(, ) #创建一个5*3的未初始化矩阵 ...

随机推荐

  1. AJAX请求 $.ajaxSetup方法的使用:设置AJAX请求的默认参数选项,当程序中需要发起多个AJAX请求时,则不用再为每一个请求配置请求的参数

    定义和用法ajaxSetup() 方法为将来的 AJAX 请求设置默认值.语法$.ajaxSetup({name:value, name:value, ... }) 该参数为带有一个或多个名称/值对的 ...

  2. Ngnix + Tomcat负载均衡架构

    一.nginx Nginx (发音同 engine x)是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,并在一个BSD-like 协议下发行.  其特点是占有内 ...

  3. cartographer 安装问题

    安装主要参考hitcm教程: http://www.cnblogs.com/hitcm/p/5939507.html 这里只说安装过程中遇到的问题, ceres-solver 与 eigen3 版本不 ...

  4. 用secureCRT操作ubuntu终端

    用secureCRT操作ubuntu终端 ubuntu下先安装ssh windows下win+R再输入ubuntu的ip地址   ubuntu 检测端口号的命令 netstat -antp   下载到 ...

  5. 一篇不错的CUDA入门

    鉴于自己的毕设需要使用GPU CUDA这项技术,想找一本入门的教材,选择了Jason Sanders等所著的书<CUDA By Example an Introduction to Genera ...

  6. python调用win32com.client的GetObject查找进程信息及服务信息

    为何不用wmi呢?因为执行很慢,为啥不用winreg?因为winreg在批量获取及遍历服务方面很不方便,于是采用这方法 该方法同命令行下的wmic执行 获取服务信息 #coding=utf8 from ...

  7. VS C# xamarin 开发android 调试正常 发布分发后运行闪退出错

    我强烈推荐大家如果不是很有必要就不要引用一些.NET STD的库,比如json库newtonsoft.JSON,直接引用官方的system.Json就足够了,否则会导致体积变得巨大 好了废话不多说,这 ...

  8. springboot:session集中存储到redis

    1.在web工程的基础上,在pom.xml中添加: <dependency> <groupId>org.springframework.boot</groupId> ...

  9. JDK1.5引入的concurrent包

    并发是伴随着多核处理器的诞生而产生的,为了充分利用硬件资源,诞生了多线程技术.但是多线程又存在资源竞争的问题,引发了同步和互斥,并带来线程安全的问题.于是,从jdk1.5开始,引入了concurren ...

  10. mpVue小程序全栈开发

    1.微信小程序,mpVue和wepy的对比 2. 3.es6中关于数组的一些方法 <script> let arr = [,,,] // 遍历 arr.forEach(v => { ...