torch.ones_like(),expand_as(),expend()等torch.repeat
https://blog.csdn.net/Arthur_Holmes/article/details/104267662
https://blog.csdn.net/weixin_39568781/article/details/110066146
https://blog.csdn.net/qq_40263477/article/details/107054675
https://blog.csdn.net/whitesilence/article/details/119575617


pytorch中的expand()和expand_as()函数
1.expand()函数:
(1)函数功能:
expand()函数的功能是用来扩展张量中某维数据的尺寸,它返回输入张量在某维扩展为更大尺寸后的张量。
扩展张量不会分配新的内存,只是在存在的张量上创建一个新的视图(关于张量的视图可以参考博文:由浅入深地分析张量),而且原始tensor和处理后的tensor是不共享内存的。
expand()函数括号中的输入参数为指定经过维度尺寸扩展后的张量的size。
(2)应用举例:

RuntimeError: Can’t call numpy() on Tensor that requires grad. Use tensor.detach().numpy() instead.
待转换类型的PyTorch Tensor变量带有梯度,直接将其转换为numpy数据将破坏计算图,因此numpy拒绝进行数据转换,实际上这是对开发者的一种提醒。如果自己在转换数据时不需要保留梯度信息,可以在变量转换之前添加detach()调用。


expand的含义:为1的维度可以变大维度或者维度数增多
tensor_1.expand(size):把tensor_1扩展成size的形状
tensor_1.expand_as(tensor_2) :把tensor_1扩展成和tensor_2一样的形状

TORCH.ONES_LIKE
torch.ones_like(input, *, dtype=None, layout=None, device=None, requires_grad=False, memory_format=torch.preserve_format) → Tensor-
Returns a tensor filled with the scalar value 1, with the same size as
input.torch.ones_like(input)is equivalent totorch.ones(input.size(), dtype=input.dtype, layout=input.layout, device=input.device).

torch.ones_like(),expand_as(),expend()等torch.repeat的更多相关文章
- 一探torch.nn究竟“What is torch.nn really?”
来自: https://pytorch.org/tutorials/beginner/nn_tutorial.html <What is torch.nn really?>这文章看起来不能 ...
- 安装pytorch后import torch显示no module named 'torch'
问题描述:在pycharm终端里通过pip指令安装pytorch,显示成功安装但是python程序和终端都无法使用pytorch,显示no module named 'torch'. 起因:电脑里有多 ...
- pytorch中的torch.repeat()函数与numpy.tile()
repeat(*sizes) → Tensor Repeats this tensor along the specified dimensions. Unlike expand(), this fu ...
- PyTorch官方中文文档:torch.Tensor
torch.Tensor torch.Tensor是一种包含单一数据类型元素的多维矩阵. Torch定义了七种CPU tensor类型和八种GPU tensor类型: Data tyoe CPU te ...
- 学习笔记CB012: LSTM 简单实现、完整实现、torch、小说训练word2vec lstm机器人
真正掌握一种算法,最实际的方法,完全手写出来. LSTM(Long Short Tem Memory)特殊递归神经网络,神经元保存历史记忆,解决自然语言处理统计方法只能考虑最近n个词语而忽略更久前词语 ...
- 100+torch的基础操作
官网: torch 各种操作,做个翻译,以后查阅 Tensors torch.is_tensor 如果 obj 是 pytorch 张量,则返回 True. torch.is_storage ...
- torch
1.从数据直接构建tensor x = torch.tensor([5.5,3]) 2.从已有的tensor构建一个tensor.这些方法会重用原来tensor的特征. x = x.new_ones( ...
- torch文档学习笔记
下面为官方文档学习笔记 http://pytorch.org/docs/0.3.0/index.html 1.torch.Tensor from __future__ import print_ ...
- L0 torch 构建网络初步
L0 pytorch 构建简单网络 本文是L0, 目的是把pytorch构建感知器的程序,仔细剖析理解. import torch from torch import nn torch.__versi ...
随机推荐
- vConsole移动端调试利器
图示: , 简单的几步操作: 1. 引入cdn 可以从https://www.bootcdn.cn/vConsole/下载,也可以下载保存在本地,直接引用 <!DOCTYPE html ...
- Mybatis实现批量删除数据
Mybatis实现批量删除操作 学习内容: 1. 使用 2. 代码实现 2.1 UserMapper.java 接口 2.2 UserMapper.xml 总结: 学习内容: 1. 使用 这里通过动态 ...
- vue引入swiper
https://github.com/surmon-china/vue-awesome-swiper/blob/master/examples/03-pagination.vue https://su ...
- springcloud报错:org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'armeriaServer' defined in class path resource
spring boot配置zipkin 无法启动 加入 Zipkin Server 由于需要收集 Spring Cloud 系统的跟踪信息,以便及时地发现系统中出现的延迟升高问题并找出系统性能瓶颈的根 ...
- 6.Docker容器底层实现了解与安全机制
原文地址: 点击直达 0x00 底层实现 我们以 Docker 基础架构来探究Docke底层的核心技术,简单的包括: Linux 上的命名空间(Namespaces) 控制组(Control grou ...
- Java语言学习day17--7月23日
1.面向对象思想2.类与对象的关系3.局部变量和成员变量的关系4.封装思想5.private,this关键字6.随机点名器 ###01面向对象和面向过程的思想 * A: 面向过程与面向对象都是我们编程 ...
- jsp第一周作业
环境搭建,运行出来一个JSP页面,显式hello 英文字母表 <%@ page language="java" import="java.util.*" ...
- javascript中的Ajax基础(一)
一.手写一个ajax 1 const xhr = new xmlHttpRequest() 2 3 xhr.open(请求方式:post get, 请求地址, 同步或者异步) 4 5 xhr.onre ...
- python基础练习题(题目 将一个整数分解质因数。例如:输入90,打印出90=2*3*3*5)
day9 --------------------------------------------------------------- 实例014:分解质因数 题目 将一个整数分解质因数.例如:输入 ...
- Ranchar中PostgreSQL容器异常 53100: could not resize shared memory segment ... bytes: No space left on device
问题: 客户查报表时描述查询一天的报表能出来,查询一个月的报表不能出来 分析原因: 从下图的异常中分析是PostgreSQL 的共享内存过小,容器默认的/dev/shm大小为64M 解决方案:调整ra ...