pytorch学习笔记一之张量
1. 张量¶
1.1. 概述¶
张量(tensor)是pytorch中的一种较为基础的数据结构,类比于numpy中的ndarrays,在pytorch中,张量可以在GPU中进行运算
通过以下命令,我们导入pytorch和numpy:
import torch
import numpy as np
1.2. 张量初始化¶
1.2.1. 直接生成张量¶
data = [[1, 2], [3, 4]]
x_data = torch.tensor(data)
x_data
tensor([[1, 2],
[3, 4]])
1.2.2. ndarrays转化¶
np_array = np.array(data)
x_np = torch.from_numpy(np_array)
x_np
tensor([[1, 2],
[3, 4]])
1.2.3. 通过已有张量生成¶
继承结构与数据类型:
x_ones = torch.ones_like(x_data)
x_ones
tensor([[1, 1],
[1, 1]])
继承结构,改变数据类型:
x_rand = torch.rand_like(x_data, dtype=torch.float)
x_rand
tensor([[0.9849, 0.3644],
[0.0800, 0.2939]])
1.2.4. 指定维数生成张量¶
用元组类型的数据指定维数:
shape = (2, 3)
生成张量:
torch.ones(shape)
tensor([[1., 1., 1.],
[1., 1., 1.]])
torch.zeros(shape)
tensor([[0., 0., 0.],
[0., 0., 0.]])
torch.rand(shape)
tensor([[0.1744, 0.3771, 0.7969],
[0.7098, 0.9853, 0.3950]])
1.3. 张量属性¶
维数:
x_data.shape
torch.Size([2, 2])
数据类型:
x_data.dtype
torch.int64
存储设备:
x_data.device
device(type='cpu')
1.4. 张量计算¶
GPU对于张量的计算更快,检测GPU是否可用:
torch.cuda.is_available()
False
显然,对于笔者设备来说,由于没有显卡,GPU加速是不可用的,如果设备GPU可用,可以将CPU中的数据导入GPU:
if torch.cuda.is_available():
tensor = x_data.to('cuda')
1.4.1. 索引和切片¶
tensor = torch.ones((3, 4))
tensor
tensor([[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.]])
类比于ndarrays,tensor也可理解为是一个多维数组,以下表示将tensor变量的第一行、第一列变为0:
tensor[1, 1] = 0
tensor
tensor([[1., 1., 1., 1.],
[1., 0., 1., 1.],
[1., 1., 1., 1.]])
以下表示将tensor变量的第三列变为0:
tensor[:, 3] = 0
tensor
tensor([[1., 1., 1., 0.],
[1., 0., 1., 0.],
[1., 1., 1., 0.]])
1.4.2. 张量的拼接¶
tensor1 = torch.ones((3, 4))
tensor2 = torch.zeros((3, 4))
使用torch.cat()方法,指定维数进行拼接:
torch.cat([tensor1, tensor2], dim=1)
tensor([[1., 1., 1., 1., 0., 0., 0., 0.],
[1., 1., 1., 1., 0., 0., 0., 0.],
[1., 1., 1., 1., 0., 0., 0., 0.]])
torch.cat([tensor1, tensor2], dim=0)
tensor([[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.],
[0., 0., 0., 0.],
[0., 0., 0., 0.],
[0., 0., 0., 0.]])
torch.cat([tensor1, tensor2], dim=-1)
tensor([[1., 1., 1., 1., 0., 0., 0., 0.],
[1., 1., 1., 1., 0., 0., 0., 0.],
[1., 1., 1., 1., 0., 0., 0., 0.]])
torch.cat([tensor1, tensor2], dim=-2)
tensor([[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.],
[0., 0., 0., 0.],
[0., 0., 0., 0.],
[0., 0., 0., 0.]])
torch.cat([tensor1, tensor2, tensor], dim=-2)
tensor([[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.],
[0., 0., 0., 0.],
[0., 0., 0., 0.],
[0., 0., 0., 0.],
[1., 1., 1., 0.],
[1., 0., 1., 0.],
[1., 1., 1., 0.]])
此处实验 dim = 2 时,有:
torch.cat([tensor1, tensor2], dim=2)
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-31-dc57fe12e880> in <module>
----> 1 torch.cat([tensor1, tensor2], dim=2)
IndexError: Dimension out of range (expected to be in range of [-2, 1], but got 2)
根据官网示例,此处dim的取值主要是0和1:
x = torch.randn(2, 3)
torch.cat((x, x, x), 0)
torch.cat((x, x, x), 1)
综上,dim的取值有 -2、-1、0、1,然而-2、-1与0、1的意思似乎是一样的
1.4.3. 张量的乘积与矩阵乘法¶
逐个元素相乘:
tensor.mul(tensor)
tensor([[1., 1., 1., 0.],
[1., 0., 1., 0.],
[1., 1., 1., 0.]])
等价于:
tensor * tensor
tensor([[1., 1., 1., 0.],
[1., 0., 1., 0.],
[1., 1., 1., 0.]])
张量与张量的矩阵乘法:
tensor.matmul(tensor.T)
tensor([[3., 2., 3.],
[2., 2., 2.],
[3., 2., 3.]])
等价于:
tensor @ tensor.T
tensor([[3., 2., 3.],
[2., 2., 2.],
[3., 2., 3.]])
1.4.4. 自动赋值运算¶
自增运算:
tensor
tensor([[1., 1., 1., 0.],
[1., 0., 1., 0.],
[1., 1., 1., 0.]])
tensor.add_(5)
tensor([[6., 6., 6., 5.],
[6., 5., 6., 5.],
[6., 6., 6., 5.]])
tensor
tensor([[6., 6., 6., 5.],
[6., 5., 6., 5.],
[6., 6., 6., 5.]])
复制运算:
tensor.copy_(tensor1)
tensor([[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.]])
tensor
tensor([[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.]])
注意:自动赋值运算可以节省内存,但是会导致一些中间过程的问题
1.5. Tensor与Numpy的转换¶
1.5.1. Tensor转换为Numpy¶
tensor
tensor([[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.]])
np_t = tensor.numpy()
np_t
array([[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.]], dtype=float32)
tensor.add_(5)
tensor([[6., 6., 6., 6.],
[6., 6., 6., 6.],
[6., 6., 6., 6.]])
np_t
array([[6., 6., 6., 6.],
[6., 6., 6., 6.],
[6., 6., 6., 6.]], dtype=float32)
可见:Tensor和Numpy共用内存,一个改变时另一个也改变
1.5.2. Numpy转Tensor¶
np_t
array([[6., 6., 6., 6.],
[6., 6., 6., 6.],
[6., 6., 6., 6.]], dtype=float32)
tensor
tensor([[6., 6., 6., 6.],
[6., 6., 6., 6.],
[6., 6., 6., 6.]])
t_np = torch.from_numpy(np_t)
t_np
tensor([[6., 6., 6., 6.],
[6., 6., 6., 6.],
[6., 6., 6., 6.]])
np.add(np_t, 1, out=np_t)
array([[7., 7., 7., 7.],
[7., 7., 7., 7.],
[7., 7., 7., 7.]], dtype=float32)
t_np
tensor([[7., 7., 7., 7.],
[7., 7., 7., 7.],
[7., 7., 7., 7.]])
np.add(np_t, 1)
array([[8., 8., 8., 8.],
[8., 8., 8., 8.],
[8., 8., 8., 8.]], dtype=float32)
t_np
tensor([[7., 7., 7., 7.],
[7., 7., 7., 7.],
[7., 7., 7., 7.]])
可见:np.add()指定out=时才会重新赋值
1.6. 参考资料:¶
pytorch学习笔记一之张量的更多相关文章
- [PyTorch 学习笔记] 1.3 张量操作与线性回归
本章代码:https://github.com/zhangxiann/PyTorch_Practice/blob/master/lesson1/linear_regression.py 张量的操作 拼 ...
- [PyTorch 学习笔记] 1.2 Tensor(张量)介绍
本章代码: https://github.com/zhangxiann/PyTorch_Practice/blob/master/lesson1/tensor_introduce1.py https: ...
- Pytorch学习笔记(二)---- 神经网络搭建
记录如何用Pytorch搭建LeNet-5,大体步骤包括:网络的搭建->前向传播->定义Loss和Optimizer->训练 # -*- coding: utf-8 -*- # Al ...
- Pytorch学习笔记(一)---- 基础语法
书上内容太多太杂,看完容易忘记,特此记录方便日后查看,所有基础语法以代码形式呈现,代码和注释均来源与书本和案例的整理. # -*- coding: utf-8 -*- # All codes and ...
- 【pytorch】pytorch学习笔记(一)
原文地址:https://pytorch.org/tutorials/beginner/deep_learning_60min_blitz.html 什么是pytorch? pytorch是一个基于p ...
- [PyTorch 学习笔记] 1.4 计算图与动态图机制
本章代码:https://github.com/zhangxiann/PyTorch_Practice/blob/master/lesson1/computational_graph.py 计算图 深 ...
- [PyTorch 学习笔记] 2.2 图片预处理 transforms 模块机制
PyTorch 的数据增强 我们在安装PyTorch时,还安装了torchvision,这是一个计算机视觉工具包.有 3 个主要的模块: torchvision.transforms: 里面包括常用的 ...
- [PyTorch 学习笔记] 4.3 优化器
本章代码: https://github.com/zhangxiann/PyTorch_Practice/blob/master/lesson4/optimizer_methods.py https: ...
- 【深度学习】Pytorch 学习笔记
目录 Pytorch Leture 05: Linear Rregression in the Pytorch Way Logistic Regression 逻辑回归 - 二分类 Lecture07 ...
- Pytorch学习笔记(二)——Tensor
一.对Tensor的操作 从接口的角度讲,对Tensor的操作可以分为两类: (1)torch.function (2)tensor.function 比如torch.sum(a, b)实际上和a.s ...
随机推荐
- 【大数据面试】【框架】Hive:架构、计算引擎、比较、内外部表、by、函数、优化、数据倾斜、动静态分区
一.组成 1.架构 源数据原本是存在dubby数据库,存在MySQL可以支持多个客户端 客户端.数据存储(HDFS).MR计算引擎 2.计算引擎的选择 MR引擎:基于磁盘,计算时间长,但一定能算出结果 ...
- 全网最全的linux上docker安装oracle的详细文档,遇到了n个问题,查了几十篇文章,最终汇总版,再有解决不了的,私聊我,我帮你解决
目录 全网最全的linux上docker安装oracle的详细文档,遇到了n个问题,查了几十篇文章,最终汇总版,再有解决不了的,私聊我,我帮你解决 1. 拉取阿里镜像oracle 2. 创建初始化数据 ...
- Django视图层/FBV与CBV/jsonResponse对象/form表单上传文件/request对象获取文件
目录 虚拟环境 视图层views 三板斧 jsonResponse对象 form如何上传文件/request对象获取文件 request对象方法 FBV与CBV CBV源码解析 虚拟环境 每创建一个虚 ...
- java的基本数据类型自动转换
本文主要内容阐明java的自动数据类型转换 首先明确,数据总是由精度低到精度高方向转换 public class AutoConvert { public static void main(Strin ...
- uniapp详细入门教程
链接:https://www.ruletree.club/archives/2071/ 点击链接查看,内容详细,一学就会哦~! /******** * * .-~~~~~~~~~-._ _.-~~~~ ...
- [OpenCV实战]45 基于OpenCV实现图像哈希算法
目前有许多算法来衡量两幅图像的相似性,本文主要介绍在工程领域最常用的图像相似性算法评价算法:图像哈希算法(img hash).图像哈希算法通过获取图像的哈希值并比较两幅图像的哈希值的汉明距离来衡量两幅 ...
- vulnhub靶场之HACKER KID: 1.0.1
准备: 攻击机:虚拟机kali.本机win10. 靶机:Hacker kid: 1.0.1,下载地址:https://download.vulnhub.com/hackerkid/Hacker_Kid ...
- PowerUsageSummary.java源码分析
在在线网站http://androidxref.com/上对Android版本6.0.1_r10源码进行分析 官方手机的应用耗电排行具体实现位置在:/packages/apps/Settings/sr ...
- 大数据分析新玩法之Kusto宝典 - 新书发布,免费发行
我很高兴地跟大家分享,我在元旦期间编写的一本新书今天上线,并且免费发行,大家可以随时通过 https://kusto.book.xizhang.com 这个地址访问,也可以下载 PDF 的版本, 这本 ...
- BC2-小飞机
题目描述 KiKi 学会了 printf 在屏幕输出信息,他想输出一架小飞机.请帮他编写程序输出这架小飞机. 输入描述 无 输出描述 解题思路 方案一: 将小飞机的图形按照行进行划分,总共可以分成 6 ...