pytorch中torch.unsqueeze()函数与np.expand_dims()

摘要:
numpy.expand_dims(a,axis

numpy.expand_dims(aaxis)

Expand the shape of an array.

Insert a new axis that will appear at the axis position in the expanded array shape.

 

Parameters:
a array_like

Input array.

axis int

Position in the expanded axes where the new axis is placed.

Returns:
res ndarray

Output array. The number of dimensions is one greater than that of the input array.

Examples

>>> x = np.array([1,2])
>>> x.shape
(2,)

The following is equivalent to x[np.newaxis,:] or x[np.newaxis]:

>>> y = np.expand_dims(x, axis=0)
>>> y
array([[1, 2]])
>>> y.shape
(1, 2)
>>> y = np.expand_dims(x, axis=1)  # Equivalent to x[:,np.newaxis]
>>> y
array([[1],
       [2]])
>>> y.shape
(2, 1)

Note that some examples may use None instead of np.newaxis. These are the same objects:

>>> np.newaxis is None
True


 

torch.unsqueeze(inputdimout=None) → Tensor

Returns a new tensor with a dimension of size one inserted at the specified position.

The returned tensor shares the same underlying data with this tensor.

dim value within the range [-input.dim() 1, input.dim() 1) can be used. Negative dimwill correspond to unsqueeze() applied at dim = dim input.dim() 1.

Parameters:
  • input (Tensor) – the input tensor
  • dim (int) – the index at which to insert the singleton dimension
  • out (Tensoroptional) – the output tensor

Example:

>>> x = torch.tensor([1, 2, 3, 4])
>>> torch.unsqueeze(x, 0)
tensor([[ 1,  2,  3,  4]])
>>> torch.unsqueeze(x, 1)
tensor([[ 1],
        [ 2],
        [ 3],
        [ 4]])
 

免责声明:文章转载自《pytorch中torch.unsqueeze()函数与np.expand_dims()》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Animation 动画详解(一)——alpha、scale、translate、rotate、set的xml属性及用法后端接收前端参数问题以及RestFul结构下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Selenium---元素定位之CSS定位

CSS 选择器: 常见符号: #表示 id选择器 .表示 class选择器 >表示子元素,层级 一个空格也表示子元素,但是是所有的后代子元素,相当于 xpath 中的相对路径 一、css:属性定位 1.css可以通过元素的id、class、标签这三个常规属性直接定位到 2.如下是百度输入框的的html代码: <input type="text"...

vue需求表单有单位(时分秒千克等等)

需求如下: 问题分析: 因为用elementui组件 el-input 相当于块级元素,后面的单位<span>分</span>会被挤下去,无法在同一水平。 解决方法: 不用它的组件,自己写个原生的,用组件的class名,替换el-input__inner 为 el-input__myinner   <el-form-it...

图片右上角添加删除

一、css /*图片适用大小*/ img { width:100%; height:100%; } .imgAll li { /*图片容器position: relative属性*/ width:100px; height:100px; bor...

mysql增删改和学生管理sql

importpymysql #2.建连 conn = pymysql.connect("localhost","root",'root','李森') print(conn) #3.获取游标 cur =conn.cursor() #4.增 sql="insert into student_1 values(default,%s,%s,%s,%s)"cur.e...

JQuery 之 获取 radio选中值,select选中值

以下Jquery代码适query1.4版本以上。 Radio 1.获取选中值,三种方法都可以: $('input:radio:checked').val(); $("input[type='radio']:checked").val(); $("input[name='rd']:checked").val(); 2.设置第一个Radio为选中值: $...

Android系统--输入系统(六)模拟输入驱动程序

Android系统--输入系统(六)模拟输入驱动程序 1. 回顾输入子系统 简单字符设备驱动:应用程序通过调用驱动所实现的函数使能硬件。 输入子系统:由于有多个应用程序使用输入子系统,故肯定使用的是早已规定好驱动接口,我们所需要实现的这是实现硬件相关的操作。 2. 输入子系统特性 有多套open/read/write接口 当应用程序调用这些接口,驱...