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的未初始化矩阵 ...
随机推荐
- tr 设置margin、padding无效
tr.td设置margin 无效 tr 设置padding无效.td设置padding有效
- proxysql 系列 ~ 总揽概括
一 简介: proxysql相关知识汇总 二 proxysql 相关报错 1 proxysql 报错 too many connections 分析 proxysql关于连接池的参数 ...
- 2018-2019-2 网络对抗技术 20165320 Exp2 后门原理与实践
后门原理与实践 windows获取Linux操作Shell 获取本机的IP netcat介绍:一个进行基本的TCP.UDP数据收发的工具 相关的参数与具体用法 windows打开监听: ncat.ex ...
- Freemarker list 的简单使用
freemarker list (长度,遍历,下标,嵌套,排序) 1. freemarker获取list的size : Java ArrayList<String> list = new ...
- Linux内核驱动--mmap设备方法【原创】
mmap系统调用(功能) void *mmap(void *addr, size_t len, int prot, int flags, int fd, off_t offset) 内存映射函数mma ...
- c或c++的网络库
Asio C++ Library: Asio is a cross-platform C++ library for network and low-level I/O programming tha ...
- xargs -i和-I的区别【转】
xargs与find经常结合来进行文件操作,平时删日志的时候只是习惯的去删除,比如 # find . -type f -name "*.log" | xargs rm -rf * ...
- springboot系列十二、springboot集成RestTemplate及常见用法
一.背景介绍 在微服务都是以HTTP接口的形式暴露自身服务的,因此在调用远程服务时就必须使用HTTP客户端.我们可以使用JDK原生的URLConnection.Apache的Http Client.N ...
- 【转】通过xml处理sql语句时对小于号与大于号的处理转换
当我们需要通过xml格式处理sql语句时,经常会用到< ,<=,>,>=等符号,但是很容易引起xml格式的错误,这样会导致后台将xml字符串转换为xml文档时报错,从而导致程序 ...
- mysql caching_sha2_password异常分析
使用navicat连接mysql报错 解决办法: 通过命令行登录mysql后,输入: alter user 'root'@'localhost' IDENTIFIED WITH mysql_nativ ...