Pytorch torch.sparse_coo_tensor()
Pytorch
构造稀疏Tensor
torch.sparse_coo_tensor(indices, values,size=None, *,dtype=None,device=None,requires_grad=False)
Constructs a sparse tensor in COO(rdinate) format with specified values at the given indices.
This function returns an uncoalesced tensor.
1. COO Style Matrix
COO 格式的矩阵可以通过一个三元组表示,如上图右侧所示,从上到下依次为:
- ① 矩阵非零元素所在的行;
- ② 矩阵非零元素所在的列(与行位置匹配);
- ③ 矩阵非零元素对应的值。
有了该基础接下来再看 sparse_coo_tensor()
2. sparse_coo_tensor()示例
接下来看一下参数说明,知道了COO格式,下面可以先略过。
Parameters
indices (array_like) – Initial data for the tensor. Can be a list, tuple, NumPy ndarray, scalar, and other types. Will be cast to a torch.LongTensor internally. The indices are the coordinates of the non-zero values in the matrix, and thus should be two-dimensional where the first dimension is the number of tensor dimensions and the second dimension is the number of non-zero values.
values (array_like) – Initial values for the tensor. Can be a list, tuple, NumPy ndarray, scalar, and other types.
size (list, tuple, or torch.Size, optional) – Size of the sparse tensor. If not provided the size will be inferred as the minimum size big enough to hold all non-zero elements.
Keyword Arguments
dtype (torch.dtype, optional) – the desired data type of returned tensor. Default: if None, infers data type from values.
device (torch.device, optional) – the desired device of returned tensor. Default: if None, uses the current device for the default tensor type (see torch.set_default_tensor_type()). device will be the CPU for CPU tensor types and the current CUDA device for CUDA tensor types.
requires_grad (bool, optional) – If autograd should record operations on the returned tensor. Default:
False
.
# setting valid pixel location
indice = torch.tensor([[0, 1, 1],[2, 0, 2]])
# setting match values
values = torch.tensor([3, 4, 5], dtype=torch.float32)
print('COO tensor: \n{}'.format(torch.sparse_coo_tensor(indice, values, [2, 4])))
print('COO tensor: \n{}'.format(torch.sparse_coo_tensor(indice, values))) # inferred as the minimum size
print('COO tensor: \n{}'.format(torch.sparse_coo_tensor(indice, values, [2, 4], dtype=torch.float64)))
# Create a empty sparse tensor.
print('COO tensor: \n{}'.format(torch.sparse_coo_tensor(torch.empty([1, 0]), [], [1])))
print('COO tensor: \n{}'.format(torch.sparse_coo_tensor(torch.empty([1, 0]), torch.empty([0, 2]), [1, 2])))
测试结果:
COO tensor:
tensor(indices=tensor([[0, 1, 1],[2, 0, 2]]),values=tensor([3., 4., 5.]),size=(2, 4), nnz=3, dtype=torch.float32, layout=torch.sparse_coo)
COO tensor:
tensor(indices=tensor([[0, 1, 1],[2, 0, 2]]),values=tensor([3., 4., 5.]),size=(2, 3), nnz=3, dtype=torch.float32, layout=torch.sparse_coo)
COO tensor:
tensor(indices=tensor([[0, 1, 1],[2, 0, 2]]),values=tensor([3., 4., 5.]),size=(2, 4), nnz=3, layout=torch.sparse_coo)
COO tensor:
tensor(indices=tensor([], size=(1, 0)),values=tensor([], size=(0,)),size=(1,), nnz=0, layout=torch.sparse_coo)
COO tensor:
tensor(indices=tensor([], size=(1, 0)),values=tensor([], size=(0, 2)),size=(1, 2), nnz=0, layout=torch.sparse_coo)
Pytorch torch.sparse_coo_tensor()相关推荐
- PyTorch : torch.nn.xxx 和 torch.nn.functional.xxx
PyTorch : torch.nn.xxx 和 torch.nn.functional.xxx 在写 PyTorch 代码时,我们会发现在 torch.nn.xxx 和 torch.nn.funct ...
- PyTorch | torch.linspace()创建均分数列张量 | torch.linspace()如何使用?| torch.linspace()使用方法 | torch.linspace例子
公众号[计算机视觉联盟]后台回复[PyTorch]可以获得独家PyTorch学习教程pdf版 通过torch.linspace创建均分数列 张量 步长=(Start - end)/(Steps - 1 ...
- PyTorch | torch.full()使用方法 | torch.full()如何使用? torch.full()例子说明 | 通过torch.full创建全相同的张量
公众号[计算机视觉联盟]后台回复[PyTorch]可以获得独家PyTorch学习教程pdf版 举例子说明torch.full()使用方法: t = torch.full((3,3),10)print( ...
- PyTorch | torch.zeros()如何使用?torch.zeros使用方法 | torch.zeros()例子
公众号[计算机视觉联盟]后台回复[PyTorch]可以获得独家PyTorch学习教程pdf版 还是用实际例子比较明显 out_t = torch.tensor(([1]))t = torch.zero ...
- PyTorch | torch.randperm()使用方法
公众号[计算机视觉联盟]后台回复[PyTorch]可以获得PyTorch学习教程pdf版 返回一个0~n-1的数组,随机打散的 t = torch.randperm(8) 结果: tensor([5, ...
- PytorchRuntimeError: inconsistent tensor sizes at /pytorch/torch/lib/TH/generic/THTensorMath.c:2709
1.pytorch在得到dataset时没有问题,得到DataLoader时也没有问题,以batch size = 512遍历数据时,出现了: RuntimeError: inconsistent t ...
- 如何使用PyTorch torch.max()
In this article, we'll take a look at using the PyTorch torch.max() function. 在本文中,我们将介绍如何使用PyTorch ...
- pytorch torch.Tensor.numpy()(从张量创建一个numpy数组,数组和张量共享相同内存)
https://pytorch.org/docs/1.1.0/tensors.html?highlight=numpy#torch.Tensor.numpy numpy() → numpy.ndarr ...
- pytorch torch.from_numpy()(从numpy数组创建一个张量,数组和张量共享相同内存)
https://pytorch.org/docs/1.1.0/torch.html?highlight=numpy#torch.from_numpy torch.from_numpy(ndarray) ...
最新文章
- 路由器运行python脚本_写个Python脚本来登录小米路由器
- smokeping安装部署最佳实践
- 面试总结-百度(2)
- Android 手势解锁 GestureLock的使用和简单修复
- 域名商2014年度报告:35互联域名总量增至33.4万
- 标准化(Normalization)和归一化实现
- Android 开发之旅:深入分析布局文件又是“Hello World!”
- python 矩阵元素如何表示_python 怎么给矩阵里的每一个元素赋值
- 云计算和主机托管有哪几点不同?
- java 快排_秋招|字节跳动Java后台已上岸,发个面经回馈牛油
- windows环境elasticsearch安装IK分词器
- 高等数学同济七版课后习题答案
- 千年3步法外挂 千年3脚本 千年3外挂 千年3最新外挂
- Proxyee-down – 百度网盘全平台满速下载神器,还带有IDM的全网嗅探功能!(替代PanDownload)
- iOS字体大小适配机型的几种方法
- 手机QQ山寨微信 3万网友愤愤不平
- “≡”3个横杠的等号的意思
- 1:2000比例尺测图
- 【复习笔记】软件项目管理
- 车用高速音视频传输串行总线技术简介(APIX、FPD-LINK、GMSL、ClockLessLink)