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的未初始化矩阵 ...
随机推荐
- Android性能优化系列之Bitmap图片优化
https://blog.csdn.net/u012124438/article/details/66087785 在Android开发过程中,Bitmap往往会给开发者带来一些困扰,因为对Bitma ...
- 20165230田坤烨《网络对抗》Exp1 PC平台逆向破解
实践目标 本次实践的对象是一个名为pwn1的linux可执行文件. 该程序正常执行流程是:main调用foo函数,foo函数会简单回显任何用户输入的字符串. 该程序同时包含另一个代码片段,getShe ...
- echarts地图使用
在使用echarts3当中,地图需要的数据时分开的,需要自己下载,而在echarts3官网上的地图数据只有全国.中国.各省的地图 如果我们需要使用更详细的地图,需要在echarts2的生成需要的地级市 ...
- 16-client、offset、scroll系列
1.client系列 代码如下: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"& ...
- PYTHON-匿名函数,递归与二分法,面向过程编程-练习
# 四 声明式编程练习题 # 1.将names=['egon','alex_sb','wupeiqi','yuanhao']中的名字全部变大写names = ['egon', 'alex_sb', ' ...
- Java多线程中wait语句的具体使用技巧
Java多线程在使用的时候会有很多语句需要我们具体的学习,在这其中wait()就是其中的一个.当然我们需要不断的努力学习才能掌握这一个语句的应用,下面的代码会对你学习Java多线程有所帮助. clas ...
- 【功能】返回数据类型、字节长度和在内部的存储位置.DUMP(w[,x[,y[,z]]])
DUMP(w[,x[,y[,z]]]) [功能]返回数据类型.字节长度和在内部的存储位置. [参数] w为各种类型的字符串(如字符型.数值型.日期型--) x为返回位置用什么方式表达,可为:8,10, ...
- keras2.0的一些变化
keras 变化太快了https://github.com/fchollet/keras/wiki/Keras-2.0-release-notes
- ubuntu 窗口操作快捷键
alt + space 打开窗口菜单SUPER + A: 搜索或浏览程序 SUPER/alt + tab: 切换窗口 SUPER + L: 进入睡眠状态 SUPER + M:查看日历
- poj2528贴海报,,
对于区间段的离散化需要注意一下,和点离散化不同 离散后如何识别一段区间还是一段区间,而不是两个顶点,就是如果两个点的距离大于1,就往离散的数据里插入一个中间值,即用三个点来表示一段区间 /* 离散化长 ...