关于numpy和torch.tensor的张量的操作
作者:comli_cn 发布时间:2023-12-30 23:35:25
1. 张量的拼接
(1) numpy.concatenate
np.concatenate((a1,a2,a3,…), axis=0)
张量的拼接要用np.concatenate这个方法的,其中 a1,a2,a3,…是拼接的子张量,axis是维数,axis=0表示按照第一维进行拼接。
例如将两个二维的张量按照第一维拼接成一个二维的张量:
import numpy as np
a=np.array([[1,2,3]])
b=np.array([[4,5,6]])
c=np.concatenate((a,b),axis=0)
print(c)
d=np.concatenate((c,a),axis=0)
print(d)
e=np.concatenate((c,c),axis=1)
print(e)
结果
array([[1, 2, 3],
[4, 5, 6]])
array([[1, 2, 3],
[4, 5, 6],
[1, 2, 3]])
array([[1, 2, 3, 1, 2, 3],
[4, 5, 6, 4, 5, 6]])
对于axis选择的更简单直接的理解是我们可以从将被拼接的两个矩阵的形状上来看,比如
a.shape=(3,1,2), b.shape=(6,1,2),则我们对其进行拼接的话目的是让拼接之后的shape=(9,1,2),那么我们就选择axis=0,即代表对第0维的进行相加。
代码如下:
import numpy as np
a = np.zeros((3, 1, 2))
b = np.zeros((6, 1, 2))
c = np.concatenate((a, b), axis=0)
print(c.shape)
结果为:
(9, 1, 2)
(2) torch.cat
这里的拼接和上面介绍的numpy的拼接功能是一样的
C = torch.cat( (A,B),0 ) #按维数0拼接(竖着拼)
C = torch.cat( (A,B),1 ) #按维数1拼接(横着拼)
例:
import torch
A=torch.ones(2,3) #2x3的张量(矩阵)
B=2*torch.ones(4,3) #4x3的张量(矩阵)
C=torch.cat((A,B),0) #按维数0(行)拼接
print(C)
结果:
tensor([[ 2., 2., 2.],
[ 2., 2., 2.],
[ 2., 2., 2.],
[ 2., 2., 2.]])
接着上面
D=2*torch.ones(2,4) #2x4的张量(矩阵)
C=torch.cat((A,D),1)#按维数1(列)拼接
print(C)
结果:
tensor([[ 1., 1., 1., 2., 2., 2., 2.],
[ 1., 1., 1., 2., 2., 2., 2.]])
2. 张量的重构
(1) np.reshape
>>> import numpy as np
>>> a = np.array([[1,2,3],[4,5,6]])
>>> a
array([[1, 2, 3],
[4, 5, 6]])
>>> b = np.reshape(a, (2,3,1))
>>> b
array([[[1],
[2],
[3]],
[[4],
[5],
[6]]])
>>> b.shape
(2, 3, 1)
(2) array.shape
>>> import numpy as np
>>> a = np.array([1,2,3,4,5,6,7,8])
>>> a.shape = (2, 4)
>>> a
array([[1, 2, 3, 4],
[5, 6, 7, 8]])
(3) torch.view
在pytorch中view函数的作用为重构张量的维度,相当于numpy中resize()的功能,但是用法可能不太一样。
1.torch.view(参数a,参数b,…)
例如:
import torch
tt1=torch.tensor([-0.3623, -0.6115, 0.7283, 0.4699, 2.3261, 0.1599])
result=tt1.view(3,2)
print(result)
结果
tensor([[-0.3623, -0.6115],
[ 0.7283, 0.4699],
[ 2.3261, 0.1599]])
在上面例子中参数a=3和参数b=2决定了将一维的tt1重构成3x2维的张量。
2.有的时候会出现torch.view(-1)或者torch.view(参数a,-1)这种情况。
例:
import torch
tt2=torch.tensor([[-0.3623, -0.6115],
[ 0.7283, 0.4699],
[ 2.3261, 0.1599]])
result=tt2.view(-1)
print(result)
结果:
tensor([-0.3623, -0.6115, 0.7283, 0.4699, 2.3261, 0.1599])
由上面的案例可以看到,如果是torch.view(-1),则原张量会变成一维的结构。
例:
import torch
tt3=torch.tensor([[-0.3623, -0.6115],
[ 0.7283, 0.4699],
[ 2.3261, 0.1599]])
>>> result=tt3.view(2,-1)
结果:
tensor([[-0.3623, -0.6115, 0.7283],
[ 0.4699, 2.3261, 0.1599]])
由上面的案例可以看到,如果是torch.view(参数a,-1),则表示在参数b未知,参数a已知的情况下自动补齐列向量长度,在这个例子中a=2,tt3总共由6个元素,则b=6/2=3。
例:
import torch
inputs = torch.randn(1,3)
print(inputs)
print(inputs.view(1, 1, -1))
结果:
tensor([[-0.5525, 0.6355, -0.3968]])
tensor([[[-0.5525, 0.6355, -0.3968]]])
将二维变为三维,a=1,b=1,c=3/(1*1)
3. 张量的形状
(1) torch.size
import torch
inputs = torch.randn(1,3)
print(inputs.size())
结果:
torch.Size([1, 3])
4. 张量的扩展
(1) torch.tensor扩展方法
用unsqueeze方法将原张量进行维度扩张,unsqueeze后面括号里的数字代表在哪个维度扩张
import torch
a = torch.tensor([[1, 2, 3], [4, 5, 6]])
b = torch.tensor([[7, 8, 9], [4, 5, 6]])
print(a)
print(b)
a = a.unsqueeze(0)
b = b.unsqueeze(0)
print(a)
print(b)
c = torch.cat((a, b), 0)
print(c)
print(c.shape)
结果为
tensor([[1, 2, 3],
[4, 5, 6]])
tensor([[7, 8, 9],
[4, 5, 6]])
tensor([[[1, 2, 3],
[4, 5, 6]]])
tensor([[[7, 8, 9],
[4, 5, 6]]])
tensor([[[1, 2, 3],
[4, 5, 6]],[[7, 8, 9],
[4, 5, 6]]])
torch.Size([2, 2, 3])
用squeeze方法将原张量进行维度缩减,squeeze后面括号里的数字代表在哪个维度缩减
import torch
a = torch.tensor([[1, 2, 3], [4, 5, 6]])
b = torch.tensor([[7, 8, 9], [4, 5, 6]])
print(a)
print(b)
a = a.unsqueeze(0)
b = b.unsqueeze(0)
print(a)
print(b)
a = a.squeeze(0)
b = b.squeeze(0)
print(a)
print(b)
结果为
tensor([[1, 2, 3],
[4, 5, 6]])
tensor([[7, 8, 9],
[4, 5, 6]])
tensor([[[1, 2, 3],
[4, 5, 6]]])
tensor([[[7, 8, 9],
[4, 5, 6]]])
tensor([[1, 2, 3],
[4, 5, 6]])
tensor([[7, 8, 9],
[4, 5, 6]])
(2) np.array扩展方法
np.expand_dims:用于扩展数组的形状
原始数组:
import numpy as np
In [12]:
a = np.array([[[1,2,3],[4,5,6]]])
a.shape
Out[12]:
(1, 2, 3)
np.expand_dims(a, axis=0)表示在0位置添加数据,转换结果如下:
In [13]:
b = np.expand_dims(a, axis=0)
b
Out[13]:
array([[[[1, 2, 3],
[4, 5, 6]]]])
In [14]:
b.shape
Out[14]:
(1, 1, 2, 3)
来源:https://blog.csdn.net/comli_cn/article/details/104797320


猜你喜欢
- 作者:丁仪来源:https://chengxuzhixin.com/blog/post/mysql_zhong_yao_ri_zhi_wen
- PDO::inTransactionPDO::inTransaction — 检查是否在一个事务内(PHP 5 >= 5.3.3, B
- 1.元组的创建元组(tuple):元组本身是不可变数据类型,没有增删改查元组内可以存储任意数据类型t = (1,2.3,True,'
- 代码编辑环境Win10+(Pycharmm or Vscode)+PyQt 5.14.2功能实现静态作图:数据作图,取决于作图函数,可自行修
- 先装 MYSQL 的 ODBC 驱动然后'connect to MySQL server&n
- 和很多语言一样,Python中也分为简单赋值、浅拷贝、深拷贝这几种“拷贝”方式。在学习过程中,一开始对浅拷贝理解很模糊。不过经过一系列的实验
- 概述今天主要分享下mysql数据库应该如何正确的删除binlog日志,这里要注意不要强制使用rm命令进行清除。否则mysq-bin.inde
- 本文分享的实例主要实现的是Python+matplotlib绘制一个有阴影和没有阴影的3D条形图,具体如下。首先看看演示效果:完整代码如下:
- MySQL存储过程SAVEPOINT ROLLBACK to示例如下:DELIMITER $$DROP PROCEDURE IF EXIST
- 用python编表白程序的方法:1、创建GUI窗口,实现代码的调用。2、编写点击触发函数,实现表白程序。具体代码如下:from tkinte
- 学生表:create table student( id number(8) primary key, name var
- 情景互动广告是指需要广告画面外的物体来参与的广告,例如:ps:这是我收集的,大家慢慢看吧!其中有大多数收集http://www.netnoe
- 1、执行环境说明python版本3.7直接使用pip进行安装pywin32、pyinstallerpip install pywin32pi
- 这篇文章给大家介绍Django中使用 Closure Table 储存无限分级数据,具体内容如下所述:起步对于数据量大的情况(比如用户之间有
- Django cors跨域问题前后端分离项目中的跨域问题 即同源策略同源策略:同源策略/SOP(Same origin policy)是一种
- 函数 0. 显示当前时间命令:select now()。作用: 显示当前时间。应用场景: 创建时间,修改时间等默认值。例子:mys
- TCP 客户端一个使用TCP协议实现可连续对话的客户端示例代码:import socket# 客户端配置HOST = 'localh
- 有的时候我们希望生成一段时间返回,比如从 2022-01-01 00:00:00 后面的 10 天,这么 10 个 datetime 对象,
- 1. 前言数组和矩阵是数值计算的基础元素。目前为止,我们都是使用NumPy的ndarray数据结构来表示数组,这是一种同构的容器,用于存储数
- 前言我们经常会与文件和目录打交道,对于这些操作python提供了一个os模块,里面包含了很多操作文件和目录的函数。在写一些系统脚本或者自动化