torch实现clip by tensor操作
tf的clip_by_value不仅可以clip by number还能clip by tensor,到了torch暂时没有找到,只有clamp,其中max和min必须是number。
自己实现了一下。
def clip_by_tensor(t,t_min,t_max):"""clip_by_tensor:param t: tensor:param t_min: min:param t_max: max:return: cliped tensor"""t=t.float()t_min=t_min.float()t_max=t_max.float()result = (t >= t_min).float() * t + (t < t_min).float() * t_minresult = (result <= t_max).float() * result + (result > t_max).float() * t_maxreturn result
使用方法:
b=torch.randint(0,10,(3,3)) print(b) min=torch.randint(0,5,(3,3)) print(min) max=torch.randint(6,10,(3,3)) print(max) print(clip_by_tensor(b,min,max))
输出结果:
tensor([[2, 8, 8],
[3, 1, 6],
[2, 4, 4]])
tensor([[3, 0, 3],
[2, 4, 0],
[0, 3, 2]])
tensor([[9, 8, 7],
[9, 8, 6],
[9, 7, 9]])
tensor([[3., 8., 7.],
[3., 4., 6.],
[2., 4., 4.]])
torch实现clip by tensor操作相关推荐
- pytorch tensor操作:tensor与numpy转换
tensor转numpy t = torch.ones(5) print(f"t: {t}") n = t.numpy() print(f"n: {n}") 输 ...
- PyTorch: tensor操作(一) contiguous
本文目录 tensor在内存中的存储 信息区和存储区 shape && stride contiguous 什么时候用contiguous呢? 为什么要用contiguous 为什么c ...
- pytorch Tensor操作(二)
高级索引 PyTorch在0.2版本中完善了索引操作,目前已经支持绝大多数numpy的高级索引.高级索引可以看成是普通索引操作的扩展,但是高级索引操作的结果一般不和原始的Tensor贡献内出. x = ...
- python选取tensor某一维_Pytorch的Tensor操作(1)
类型推断 torch.randn():随机初始化 a.type():返回类型 type():返回基本类型 isinstance() :检查类型 cuda会影响数据类型 标量 维度(dimention) ...
- torch将多个tensor张量合并为一个张量,只提高迷你批次的纬度
在自己写pytorch的数据集加载函数时,会遇到一个问题,如何将多张图片张量合并到一起,提升迷你批次batch的纬度,但是不影响图片的大小和RGB通道数 解决方法: 函数torch.cat(input ...
- pytorch之tensor操作expand
import torch# expand 将tensor按照某一个维度扩大或者将tensor整体扩大 # expand 不会额外分配空间,只是原tensor的一个视图x = torch.tensor( ...
- torch对于tensor的常规操作
前言 使用pytorch框架,会常操作tensor,以下则是对tensor常规操作的汇总. import torch torch.Tensor会继承某些torch的某些数学运算,例如sort, min ...
- torch.Tensor常用数据操作汇总与自动求梯度
在深度学习中,通常会频繁地对数据进行操作,在PyTorch中,torch.Tensor是存储和变换数据的主要工具(Tensor和NumPy的多维数组非常类似,Tensor提供GPU计算和自动求梯度等更 ...
- 【PyTorch系例】torch.Tensor详解和常用操作
学习教材: 动手学深度学习 PYTORCH 版(DEMO) (https://github.com/ShusenTang/Dive-into-DL-PyTorch) PDF 制作by [Marcus ...
最新文章
- ARC指南 strong和weak指针
- CentOS7 安装chrome浏览器和ChromeDriver 及 python脚本调用chrome浏览器
- 笔记1——C++多态与Java多态的异同
- 确定Java等价性的新时代?
- 牛客国庆集训派对Day2: E. 数据排序(状压DP+记忆化搜索)
- 计算机无线网络怎么连接,怎么连接无线网络 电脑怎么添加无线网络连接
- oracle把字段选为候选键,Oracle数据库试题及答案
- qpython3h数字雨代码,html开发数字雨特效代码
- 一台计算机连接两个投影,Win10系统电脑外接双显示器(投影仪)设置图文教程
- python中断输入_在 Python 中接管键盘中断信号的实现方法
- Windows 7 64位无法在状态码为0xc0000225的VirtualBox / Linux上安装
- Bootstrap+web+Idea实现登录页面(含验证码)
- 校招java实习生基础知识面试题
- 关于Linux中的apt-get的相关操作及原理
- 带大家读python数据分析一书(三)
- CSP-J/S初赛考点总结
- 图片验证码和短信验证码开发
- 白羊狮子:爱在烈火中永生
- 我是如何”黑掉”91Ri的
- 金蝶eas税率更新为16%相关功能点