首页 > 其他分享 >Nine---pytorch学习---拼接与拆分/运算统计

Nine---pytorch学习---拼接与拆分/运算统计

时间:2022-08-13 19:34:40浏览次数:64  
标签:tensor 32 torch shape --- pytorch Nine print Size

## pytorch学习(6)

### 拼接与拆分

- cat
- stack
- split
- chunk

#### cat()

- 连接给定维度中给定的张量序列
- 所有张量必须具有相同的形状(拼接维度除外)或为空
- torch.cat() 可以看作是 torch.split() 和 torch.chunk() 的反运算
- torch.cat(inputs,dim=)

```python
#正确的案例
import torch

a = torch.rand(3,32,8)
b = torch.rand(6,32,8) #b与a除拼接维度外具有相同的形状
c = torch.cat([a,b], dim=0)
print(a.shape)
print(b.shape)
print(c.shape)
-------------------------------------------------------
torch.Size([3, 32, 8])
torch.Size([6, 32, 8])
torch.Size([9, 32, 8])
```

```python
#错误的案例
import torch

a = torch.rand(3,32,8)
b = torch.rand(6,32,8) #在dim=1时 第0维3与6并不相同不符合
c = torch.cat([a,b], dim=1)
print(a.shape)
print(b.shape)
print(c.shape)
-------------------------------------------------------
RuntimeError: Sizes of tensors must match except in dimension 1. Expected size 3 but got size 6 for tensor number 1 in the list.
```

#### stack()

- create new dim
- 沿着一个新维度对输入张量进行连接,属于扩张再拼接的函数
- 序列中所有张量都要有相同的形状
- torch.stack(sequence,dim=)

```python
import torch

a = torch.rand(6,32,8)
b = torch.rand(6,32,8) #a与b序列应保持相同
c = torch.stack([a,b], dim=0)
print(a.shape)
print(b.shape)
print(c.shape)
-------------------------------------------------------
torch.Size([6, 32, 8])
torch.Size([6, 32, 8])
torch.Size([2, 6, 32, 8])
```

#### split()

- by len 根据块内数据长度拆分
- 将输入张量分割成相等形状的块结构
- 如果沿指定维的张量形状大小不能被 split_size 整分,则最后一个分块会小于其他分块
- torch.split(tensor,split_size,dim)

```python
#案例一
import torch

a = torch.rand(6,32,8)
b = torch.split(a,3,0)
print(a.shape)
print(len(b))
print(b[0].shape)
print(b[1].shape)
-------------------------------------------------------
torch.Size([6, 32, 8])
2
torch.Size([3, 32, 8])
torch.Size([3, 32, 8])
```

```python
#案例二
import torch

a = torch.rand(7,32,8)
b = torch.split(a,3,0)
print(a.shape)
print(len(b))
print(b[0].shape)
print(b[1].shape)
print(b[2].shape)
-------------------------------------------------------
torch.Size([7, 32, 8])
3
torch.Size([3, 32, 8])
torch.Size([3, 32, 8])
torch.Size([1, 32, 8])
```

#### chunk()

- by num 根据块个数来拆分
- 将 tensor 在指定维度dim上进行分块(个数chunks)
- 如果沿指定维的张量形状大小不能被 chunks 整分,则最后一个分块会小于其它分块
- torch.chunk(tensor,chunks,dim)

```python
import torch

a = torch.rand(6,32,8)
b = torch.chunk(a,3,0)
print(a.shape)
print(len(b))
print(b[0].shape)
print(b[1].shape)
print(b[2].shape)
-------------------------------------------------------
torch.Size([6, 32, 8])
3
torch.Size([2, 32, 8])
torch.Size([2, 32, 8])
torch.Size([2, 32, 8])
```

### 运算与统计

- 基础四则运算
- 平方与开方
- 矩阵相乘
- 近似函数
- 数据裁剪函数

#### 基础四则运算(需要满足广播机制)

- add 加法
- sub 减法
- mul 乘法
- div 除法

##### torch.add()

```python
import torch

a = torch.rand(3,4)
b = torch.rand(4)

c = a+b
d = torch.add(a,b) #两种方式输出结果相同

print(c)
print(d)
-------------------------------------------------------
tensor([[1.3796, 0.4727, 1.7063, 1.4197],
[0.5088, 0.2003, 1.6186, 0.9607],
[0.7644, 0.2571, 1.4409, 0.9180]])
tensor([[1.3796, 0.4727, 1.7063, 1.4197],
[0.5088, 0.2003, 1.6186, 0.9607],
[0.7644, 0.2571, 1.4409, 0.9180]])
```

##### torch.sub()

```python
import torch

a = torch.rand(3,4)
b = torch.rand(4)

c = a - b
d = torch.sub(a,b)

print(c)
print(d)
-------------------------------------------------------
tensor([[ 0.3418, -0.0872, -0.4209, -0.1290],
[ 0.3348, -0.0099, -0.2430, -0.3949],
[-0.1601, -0.1792, 0.1060, -0.5435]])
tensor([[ 0.3418, -0.0872, -0.4209, -0.1290],
[ 0.3348, -0.0099, -0.2430, -0.3949],
[-0.1601, -0.1792, 0.1060, -0.5435]])
```

torch.mul(input, value, out=None)

- 对输入张量 input 逐元素乘以 标量值/张量(value),并返回一个新的张量tensor

```python
import torch

a = torch.rand(3,3)
b = torch.eye(3,3)

c = torch.mul(a,b)

print(a)
print(b)
print(c)
-------------------------------------------------------
tensor([[0.2426, 0.2934, 0.9999],
[0.3949, 0.5847, 0.8023],
[0.7302, 0.4891, 0.8976]])
tensor([[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.]])
tensor([[0.2426, 0.0000, 0.0000],
[0.0000, 0.5847, 0.0000],
[0.0000, 0.0000, 0.8976]])
```

##### torch.div() & /

```python
import torch

a = torch.full([2,2],2) #创建一个二行二列的tensor并填充2
b = 2
c = torch.div(a,b)
d = a / b #div()效果与/一样

print(a)
print(c)
print(d)
-------------------------------------------------------
tensor([[2, 2],
[2, 2]])
tensor([[1., 1.],
[1., 1.]])
tensor([[1., 1.],
[1., 1.]])
```

#### 平方与开方

- pow 函数 & **
- sqrt 函数(平方) & rsqrt 函数

##### pow 函数 & **

```python
import torch

a = torch.full([2,2],2)
b = a.pow(2)
c = a**2
d = b.pow(0.5)

print(b)
print(c)
print(d)
-------------------------------------------------------
tensor([[4, 4],
[4, 4]])
tensor([[4, 4],
[4, 4]])
tensor([[2., 2.],
[2., 2.]])
```

##### sqrt 函数(平方) & rsqrt 函数

- torch.sqrt(input, out=None) 返回一个新张量,包含输入input张量每个元素的平方根
- torch.rsqrt(input, out=None) 返回一个新张量,包含输入input张量每个元素的平方根倒数

```python
import torch

a = torch.full([2,2],2)
b = torch.sqrt(a)
c = torch.rsqrt(a)

print(a)
print(b)
print(c)
-------------------------------------------------------
tensor([[2, 2],
[2, 2]])
tensor([[1.4142, 1.4142],
[1.4142, 1.4142]])
tensor([[0.7071, 0.7071],
[0.7071, 0.7071]])
```

#### 矩阵相乘

- matmul()
- torch.mm(mat1,mat2,out=None)
- torch.bmm(batch1,batch2,out=None)
- torch.matmul(tensor1,tensor2,out=None)

##### torch.mm() --- 二维矩阵乘法

- mm只能进行矩阵乘法,也就是输入的两个tensor维度只能是 (n x m)和 (m x p)
- (n x m)和(m x p)通过矩阵乘法得到(n x p)

```python
import torch

a = torch.rand(3,4)
b = torch.rand(4,5)

c = torch.mm(a,b)

print(a.shape)
print(b.shape)
print(c.shape)
-------------------------------------------------------
torch.Size([3, 4])
torch.Size([4, 5])
torch.Size([3, 5])
```

##### torch.bmm() --- 三维带batch的矩阵乘法

- bmm是两个三维张量相乘,两个输入tensor维度是 (b x n x m) 和 (b x m x p)
- 第一维是batch维度,输出是 (b x n x p)

```python
import torch

a = torch.rand(3,2,4)
b = torch.rand(3,4,5)

c = torch.bmm(a,b)

print(a.shape)
print(b.shape)
print(c.shape)
-------------------------------------------------------
torch.Size([3, 2, 4])
torch.Size([3, 4, 5])
torch.Size([3, 2, 5])
```

##### torch.matmul()

- matmul可以进行张量乘法,输入可以是高维
- 对矩阵mat1和mat2进行相乘
- @符号与matmul效果相同
- 例如:tensor1维度是(j x 1 x n x m),tensor2维度是(k x m x p),输出为(j x k x n x p)

```python
import torch

a = torch.rand(3,1,2,4)
b = torch.rand(5,4,6)

c = torch.matmul(a,b)
d = a @ b
print(a.shape)
print(b.shape)
print(c.shape)
print(d.shape)
-------------------------------------------------------
torch.Size([3, 1, 2, 4])
torch.Size([5, 4, 6])
torch.Size([3, 5, 2, 6])
torch.Size([3, 5, 2, 6])
```

#### 近似函数

- .floor() 向下取整
- .ceil() 向上取整
- .round() 四舍五入
- .trunc() 裁剪整数部分
- .frac() 裁剪小数部分

```python
import torch

a = torch.tensor(3.1415926)

b = a.floor()
c = a.ceil()
d = a.round()
e = a.trunc()
f = a.frac()

print(a)
print(b)
print(c)
print(d)
print(e)
print(f)
-------------------------------------------------------
tensor(3.1416)
tensor(3.)
tensor(4.)
tensor(3.)
tensor(3.)
tensor(0.1416)
```

#### 数据裁剪函数

- clamp函数 将输入input张量每个元素值约束到区间[min,max],并返回结果到一个新的tensor,也可以只设定min或只设定max
- torch.clamp(input,min,max)

```python
import torch

a = torch.rand(3,3)*20
b = torch.clamp(a,0,10)
c = torch.clamp(a,7,14)

print(a)
print(b)
print(c)
-------------------------------------------------------
tensor([[ 1.8016, 14.4178, 10.5252],
[ 8.3026, 0.1275, 19.7785],
[ 2.6293, 13.7800, 18.9552]])
tensor([[ 1.8016, 10.0000, 10.0000],
[ 8.3026, 0.1275, 10.0000],
[ 2.6293, 10.0000, 10.0000]])
tensor([[ 7.0000, 14.0000, 10.5252],
[ 8.3026, 7.0000, 14.0000],
[ 7.0000, 13.7800, 14.0000]])
```

 

标签:tensor,32,torch,shape,---,pytorch,Nine,print,Size
From: https://www.cnblogs.com/311dih/p/16583856.html

相关文章

  • C#并发编程-4 同步
    如果程序用到了并发技术,那就要特别留意这种情况:一段代码需要修改数据,同时其他代码需要访问同一个数据。这种情况就需要考虑同步地访问数据。如果下面三个条件都满足,就必......
  • Three---面向对象与面向过程/属性和变量/关于self/一些魔法方法的使用/继承/super方法
    python的面向对象面向对象与面向过程面向过程面向过程思想:需要实现一个功能的时候,看重的是开发的步骤和过程,每一个步骤都需要自己亲力亲为,需要自己编写代码(自己来做)......
  • AICA第6期-学习笔记汇总
    AICA第6期-学习笔记汇总AICA第六期|预科班课程1.《跨上AI的战车》2.《产业中NLP任务的技术选型与落地》3.《计算机视觉产业落地挑战与应对》4.《搭建适合企业的AI中......
  • djnago-filter用法
    django-filter用法集成drf自定义filter文件内fromdjango_filtersimportrest_frameworkasrs_filtersfrom.modelsimport*classTestFilter(rs_filters.Filter......
  • 常见docker命令(二)-容器生命周期相关
    dockerrun命令主要参数-d后台运行,返回容器id-i以交互模式运行,通常与-t连用-t为容器重新分配一个伪输入终端,通常与-i连用-P(大写)随机端口映射,容器内部端口随机映射到......
  • 常见docker命令(三)-容器操作相关
    dockerexec-在容器中执行命令以交互模式进入mynginx容器的bash控制台dockerexec-itmynginxbash以交互模式在mynginx容器中执行/test.shdockerexec-itmynginx/bin/......
  • 【SpringBoot】学习笔记-静态资源导入探究
    获取静态资源路径1    如图所示,当我们访问localhost:8080/webjars目录下面的静态资源,都会被映射到classpath:/META-INF/resources/webjars/去进行查找  ......
  • 随身wifi - debian篇
    前言开始准备工作9008模式miko刷第三方包fastboot模式debian刷机包刷入debian在base目录下运行flash.bat在debian目录下运行安装驱动......
  • css画矩形的凹陷及突出效果box-shadow
    外边框颜色渐变 background:linear-gradient(toright,white,#f0f6fe,#e5eff7); border:solid2px#d3def2; margin:10px0; padding:12px16px18px;......
  • VUE学习-mixin混入
    mixin混入混入(mixin)提供了一种非常灵活的方式,来分发Vue组件中的可复用功能。组件式混入//定义一个混入对象varmyMixin={ created:function(){this.hel......