Pytorch 基础
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 基础的更多相关文章
- [人工智能]Pytorch基础
PyTorch基础 摘抄自<深度学习之Pytorch>. Tensor(张量) PyTorch里面处理的最基本的操作对象就是Tensor,表示的是一个多维矩阵,比如零维矩阵就是一个点,一维 ...
- 【新生学习】第一周:深度学习及pytorch基础
DEADLINE: 2020-07-25 22:00 写在最前面: 本课程的主要思路还是要求大家大量练习 pytorch 代码,在写代码的过程中掌握深度学习的各类算法,希望大家能够坚持练习,相信经度过 ...
- pytorch基础学习(二)
在神经网络训练时,还涉及到一些tricks,如网络权重的初始化方法,优化器种类(权重更新),图片预处理等,继续填坑. 1. 神经网络初始化(Network Initialization ) 1.1 初 ...
- PyTorch基础——词向量(Word Vector)技术
一.介绍 内容 将接触现代 NLP 技术的基础:词向量技术. 第一个是构建一个简单的 N-Gram 语言模型,它可以根据 N 个历史词汇预测下一个单词,从而得到每一个单词的向量表示. 第二个将接触到现 ...
- pytorch 基础内容
一些基础的操作: import torch as th a=th.rand(3,4) #随机数,维度为3,4的tensor b=th.rand(4)print(a)print(b) a+b tenso ...
- pytorch基础教程1
0.迅速入门:根据上一个博客先安装好,然后终端python进入,import torch ******************************************************* ...
- 【pytorch】pytorch基础学习
目录 1. 前言 # 2. Deep Learning with PyTorch: A 60 Minute Blitz 2.1 base operations 2.2 train a classifi ...
- Pytorch基础(6)----参数初始化
一.使用Numpy初始化:[直接对Tensor操作] 对Sequential模型的参数进行修改: import numpy as np import torch from torch import n ...
- pytorch基础学习(一)
在炼丹师的路上越走越远,开始入手pytorch框架的学习,越炼越熟吧... 1. 张量的创建和操作 创建为初始化矩阵,并初始化 a = torch.empty(, ) #创建一个5*3的未初始化矩阵 ...
随机推荐
- saltstack系列~第三篇
一 简介:今天来聊聊sls文件的编写二 目的:通过编写特定的sls文件实现环境的初始化和固定软件的安装三 目录: /srv/salt/ 可以构造deploy文件夹四 基本构造:配置基本以yaml规范. ...
- Ajax跨域访问解决方案
No 'Access-Control-Allow-Origin' header is present on the requested resource. 当使用ajax访问远程服务器时,请求失败,浏 ...
- android fragment解析
1.fragment加载到Activity (1).添加fragment到Activity的布局文件 (2).动态在activity中添加fragment 例子: // 步骤1:获取FragmentM ...
- 原生JS给元素添加class属性
有下面这三种简单语句. document.getElementsByTagName('body')[0].className = 'snow-container'; //设置为新的 document ...
- IAR KEIL ECLIPSE使用JlinkScript文件进行调试
转载自:https://wiki.segger.com/Using_J-Link_Script_Files Using J-Link Script Files Contents [hide] ...
- UML和模式应用4:初始阶段(7)--其它需求
1.前言 本文主要讲述除用例的其它需求制品. 2.TODO
- malloc 函数详解【转】
转自:https://www.cnblogs.com/Commence/p/5785912.html 很多学过C的人对malloc都不是很了解,知道使用malloc要加头文件,知道malloc是分配一 ...
- 找到多个与名为“Home”的控制器匹配的类型的解决方案
主地址:http://localhost:3412/Home/Index 区域地址:http://localhost:3412/T200/Home/Index 解决方法: 注册路由添加命名空间(nam ...
- 记录entityframework生成的sql语句
Interceptors (EF6.1 Onwards) Starting with EF6.1 you can register interceptors in the config file. I ...
- CentOS 6.5自动化运维之基于DHCP和TFTP服务的PXE自动化安装centos操作系统详解
前言 如果要给很多台客户端主机安装操作系统,要是每一台都拿张安装光盘一台一台主机的去装系统那就太浪费时间和精力了.在生产环境中也不实际,要实现为多台主机自动安装操作系统,那我们怎么实现自动化安装 ...