ncnn 算子操作描述

ncnn 算子操作描述,具体查询见

ncnn/docs/developer-guide/operators.md at master · Tencent/ncnn · GitHub

都是从上述地方copy过来的,做备份。

具体如下:

1.AbsVal: 计算输入张量中的每个元素的绝对值。

y = abs(x)
  • one_blob_only  只支持一个blob
  • support_inplace  支持替换输入的blob 就 y=abs(y)
import torch

input_tensor = torch.tensor([-1, 2, -3, 4, -5])
output_tensor = torch.abs(input_tensor)
print(output_tensor)
# tensor([1, 2, 3, 4, 5])


2.ArgMax: 计算输入张量中元素的最大值,并返回其位置索引。

y = argmax(x, out_max_val, topk)
  • one_blob_only  支持一个blob
param idnametypedefaultdescription
0out_max_valint0
1topkint1

import torch

input_tensor = torch.tensor([10, 5, 8, 20, 15])
output_index = torch.argmax(input_tensor)
print(output_index)
# tensor(3)

3.BatchNorm: 对神经网络的每一层进行归一化操作。

y = (x - mean) / sqrt(var + eps) * slope + bias
  • one_blob_only  支持一个参数
  • support_inplace  支持替换
param idnametypedefaultdescription
0channelsint0
1epsfloat0.f
weighttypeshape
slope_datafloat[channels]
mean_datafloat[channels]
var_datafloat[channels]
bias_datafloat[channels]
import torch
import torch.nn as nn

batch_norm_layer = nn.BatchNorm1d(3)
input_tensor = torch.randn(2, 3, 4)  # Batch size为2,特征维度为3,序列长度为4
output_tensor = batch_norm_layer(input_tensor)
print(output_tensor)

# tensor([[[-0.5624,  0.9015, -0.9183,  0.3030],
#          [ 0.4668,  1.0430, -2.0182,  0.7149],
#          [-1.5960,  0.5437,  0.8771, -0.1269]],
# 
#         [[-0.1101, -1.4983,  1.9178, -0.0333],
#          [-0.1873, -1.1687,  0.7301,  0.4194],
#          [ 1.2667,  0.7976, -1.4188, -0.3434]]],
#        grad_fn=<NativeBatchNormBackward0>)


4.Bias: 为神经网络的神经元或层添加偏置项。

y = x + bias
  • one_blob_only
  • support_inplace
param idnametypedefaultdescription
0bias_data_sizeint0
weighttypeshape
bias_datafloat[channels]
import torch

input_tensor = torch.randn(3, 4)
bias = torch.randn(4)
output_tensor = input_tensor + bias
print('output_tensor:',output_tensor,'\nshape:',output_tensor.shape)

# tensor([[-0.1874,  1.2358,  1.9006,  0.4483],
#         [-1.1005,  1.6844, -0.3991, -0.4538],
#         [ 0.4519,  2.2752,  1.6041, -1.2463]])
# shape: torch.Size([3, 4])

5.BinaryOp: 二元操作

对两个输入执行特定的二元操作,如加法.减法等

This operation is used for binary computation, and the calculation rule depends on the broadcasting rule.(这个操作用于二进制计算,计算规则取决于广播规则。)

C = binaryop(A, B)

if with_scalar = 1:

  • one_blob_only
  • support_inplace
param idnametypedefaultdescription
0op_typeint0Operation type as follows
1with_scalarint0with_scalar=0 B is a matrix, with_scalar=1 B is a scalar
2bfloat0.fWhen B is a scalar, B = b

Operation type:

  • 0 = ADD(加法)
  • 1 = SUB(减法)
  • 2 = MUL(乘法)
  • 3 = DIV(除法)
  • 4 = MAX(取最大值)
  • 5 = MIN(取最小值)
  • 6 = POW(幂运算)
  • 7 = RSUB(右操作数减去左操作数)
  • 8 = RDIV(右操作数除以左操作数)
  • 9 = RPOW(右操作数的左操作数次幂)
  • 10 = ATAN2(反正切运算)
  • 11 = RATAN2(右操作数以左操作数为底的反正切运算)

6.BNLL: 对输入应用 BNLL 激活函数

激活函数中的双极性 Sigmoid 函数

f(x)=log(1 + exp(x))

y = log(1 + e^(-x)) , x > 0
y = log(1 + e^x),     x < 0
  • one_blob_only
  • support_inplace

7.Cast: 类型转换

将输入数据从一种数据类型转换为另一种数据类型

y = cast(x)
  • one_blob_only
  • support_packing
param idnametypedefaultdescription
0type_fromint0
1type_toint0

Element type:

  • 0 = auto
  • 1 = float32
  • 2 = float16
  • 3 = int8
  • 4 = bfloat16
  • import torch
    
    input_tensor = torch.tensor([1.5, 2.3, 3.7])
    output_tensor = input_tensor.type(torch.int)
    print(output_tensor)
    # tensor([1, 2, 3], dtype=torch.int32)

8.CELU: 应用 CELU 激活函数。

if x < 0    y = (exp(x / alpha) - 1.f) * alpha
else        y = x
  • one_blob_only
  • support_inplace
param idnametypedefaultdescription
0alphafloat1.f
import torch
import torch.nn.functional as F

input_tensor = torch.randn(3, 4)
output_tensor = F.elu(input_tensor)
print('output_tensor:',output_tensor,'\nshape:',output_tensor.shape)
# output_tensor: tensor([[-0.5924,  0.7810,  1.1752,  0.8274],
#         [-0.6871,  0.0466,  0.9411, -0.7082],
#         [-0.8632, -0.1801, -0.8730,  0.9515]]) 
# shape: torch.Size([3, 4])


9.Clip: 将输入张量中的元素限制在指定范围内。

y = clamp(x, min, max)
  • one_blob_only
  • support_inplace
param idnametypedefaultdescription
0minfloat-FLT_MAX
1maxfloatFLT_MAX

import torch

input_tensor = torch.randn(2, 3)
output_tensor = torch.clamp(input_tensor, min=-0.5, max=0.5)
print(output_tensor)

# tensor([[-0.5000, -0.5000, -0.5000],
#         [ 0.5000, -0.4091, -0.5000]])

10.Concat: 沿指定轴连接多个输入张量。

y = concat(x0, x1, x2, ...) by axis
param idnametypedefaultdescription
0axisint0

import torch

input_tensor1 = torch.randn(2, 3)
input_tensor2 = torch.randn(2, 3)
output_tensor = torch.cat((input_tensor1, input_tensor2), dim=1)
print('output_tensor:',output_tensor,'\nshape:',output_tensor.shape)

# output_tensor: tensor([[-2.4431, -0.6428,  0.4434,  1.2216, -1.1874, -1.1327],
#         [-0.8082, -0.3552,  0.9945, -0.7679,  0.6547, -1.0401]]) 
# shape: torch.Size([2, 6])

11.Convolution: 卷积操作

通过卷积操作提取输入数据的特征。

x2 = pad(x, pads, pad_value)
x3 = conv(x2, weight, kernel, stride, dilation) + bias
y = activation(x3, act_type, act_params)
  • one_blob_only
param idnametypedefaultdescription
0num_outputint0
1kernel_wint0
2dilation_wint1
3stride_wint1
4pad_leftint0
5bias_termint0
6weight_data_sizeint0
8int8_scale_termint0
9activation_typeint0
10activation_paramsarray[ ]
11kernel_hintkernel_w
12dilation_hintdilation_w
13stride_hintstride_w
14pad_topintpad_left
15pad_rightintpad_left
16pad_bottomintpad_top
18pad_valuefloat0.f
19dynamic_weightint0
weighttypeshape
weight_datafloat/fp16/int8[kernel_w, kernel_h, num_input, num_output]
bias_datafloat[num_output]
weight_data_int8_scalesfloat[num_output]
bottom_blob_int8_scalesfloat[1]
top_blob_int8_scalesfloat[1]

import torch
import torch.nn as nn

conv_layer = nn.Conv2d(in_channels=3, out_channels=16, kernel_size=3, stride=1, padding=1)
input_tensor = torch.randn(1, 3, 32, 32)
output_tensor = conv_layer(input_tensor)
print(output_tensor.shape)
# torch.Size([1, 16, 32, 32])

12.Convolution1D:一维卷积

在一维数据上应用卷积操作。

x2 = pad(x, pads, pad_value)
x3 = conv1d(x2, weight, kernel, stride, dilation) + bias
y = activation(x3, act_type, act_params)
  • one_blob_only
param idnametypedefaultdescription
0num_outputint0
1kernel_wint0
2dilation_wint1
3stride_wint1
4pad_leftint0
5bias_termint0
6weight_data_sizeint0
9activation_typeint0
10activation_paramsarray[ ]
15pad_rightintpad_left
18pad_valuefloat0.f
19dynamic_weightint0
weighttypeshape
weight_datafloat/fp16/int8[kernel_w, num_input, num_output]
bias_datafloat[num_output]

import torch
import torch.nn as nn

conv_layer = nn.Conv1d(in_channels=3, out_channels=16, kernel_size=3, stride=1, padding=1)
input_tensor = torch.randn(1, 3, 32)
output_tensor = conv_layer(input_tensor)
print(output_tensor.shape)
# torch.Size([1, 16, 32])

13.Convolution3D:三维卷积

在三维数据上应用卷积操作。

x2 = pad(x, pads, pad_value)
x3 = conv3d(x2, weight, kernel, stride, dilation) + bias
y = activation(x3, act_type, act_params)
  • one_blob_only
param idnametypedefaultdescription
0num_outputint0
1kernel_wint0
2dilation_wint1
3stride_wint1
4pad_leftint0
5bias_termint0
6weight_data_sizeint0
9activation_typeint0
10activation_paramsarray[ ]
11kernel_hintkernel_w
12dilation_hintdilation_w
13stride_hintstride_w
14pad_topintpad_left
15pad_rightintpad_left
16pad_bottomintpad_top
17pad_behindintpad_front
18pad_valuefloat0.f
21kernel_dintkernel_w
22dilation_dintdilation_w
23stride_dintstride_w
24pad_frontintpad_left
weighttypeshape
weight_datafloat/fp16/int8[kernel_w, kernel_h, kernel_d, num_input, num_output]
bias_datafloat[num_output]
import torch
import torch.nn as nn

conv_layer = nn.Conv3d(in_channels=3, out_channels=16, kernel_size=3, stride=1, padding=1)
input_tensor = torch.randn(1, 3, 32, 32, 32)
output_tensor = conv_layer(input_tensor)
print(output_tensor.shape)
# torch.Size([1, 16, 32, 32, 32])


14.ConvolutionDepthWise: 深度可分离卷积

对每个输入通道应用独立卷积核。

x2 = pad(x, pads, pad_value)
x3 = conv(x2, weight, kernel, stride, dilation, group) + bias
y = activation(x3, act_type, act_params)
  • one_blob_only
param idnametypedefaultdescription
0num_outputint0
1kernel_wint0
2dilation_wint1
3stride_wint1
4pad_leftint0
5bias_termint0
6weight_data_sizeint0
7groupint1
8int8_scale_termint0
9activation_typeint0
10activation_paramsarray[ ]
11kernel_hintkernel_w
12dilation_hintdilation_w
13stride_hintstride_w
14pad_topintpad_left
15pad_rightintpad_left
16pad_bottomintpad_top
18pad_valuefloat0.f
19dynamic_weightint0
weighttypeshape
weight_datafloat/fp16/int8[kernel_w, kernel_h, num_input / group, num_output / group, group]
bias_datafloat[num_output]
weight_data_int8_scalesfloat[group]
bottom_blob_int8_scalesfloat[1]
top_blob_int8_scalesfloat[1]
import torch
import torch.nn as nn

conv_dw_layer = nn.Conv2d(in_channels=3, out_channels=3, kernel_size=3, groups=3)
input_tensor = torch.randn(1, 3, 32, 32)
output_tensor = conv_dw_layer(input_tensor)
print(output_tensor.shape)
# torch.Size([1, 3, 30, 30])


15.ConvolutionDepthWise1D: 在一维数据上应用深度可分离卷积。

x2 = pad(x, pads, pad_value)
x3 = conv1d(x2, weight, kernel, stride, dilation, group) + bias
y = activation(x3, act_type, act_params)
  • one_blob_only
param idnametypedefaultdescription
0num_outputint0
1kernel_wint0
2dilation_wint1
3stride_wint1
4pad_leftint0
5bias_termint0
6weight_data_sizeint0
7groupint1
9activation_typeint0
10activation_paramsarray[ ]
15pad_rightintpad_left
18pad_valuefloat0.f
19dynamic_weightint0
weighttypeshape
weight_datafloat/fp16/int8[kernel_w, num_input / group, num_output / group, group]
bias_datafloat[num_output]
import torch
import torch.nn as nn

# 定义一个一维的深度可分离卷积层
conv_dw_layer = nn.Conv1d(in_channels=3, out_channels=3, kernel_size=3, groups=3)

# 创建一个随机输入张量
input_tensor = torch.randn(1, 3, 10)  # 输入张量的形状为 (batch_size, channels, sequence_length)

# 将输入张量传递给深度可分离卷积层
output_tensor = conv_dw_layer(input_tensor)

print(output_tensor.shape)
# torch.Size([1, 3, 8])


16.ConvolutionDepthWise3D: 在三维数据上应用深度可分离卷积。

x2 = pad(x, pads, pad_value)
x3 = conv1d(x2, weight, kernel, stride, dilation, group) + bias
y = activation(x3, act_type, act_params)
  • one_blob_only
param idnametypedefaultdescription
0num_outputint0
1kernel_wint0
2dilation_wint1
3stride_wint1
4pad_leftint0
5bias_termint0
6weight_data_sizeint0
7groupint1
9activation_typeint0
10activation_paramsarray[ ]
15pad_rightintpad_left
18pad_valuefloat0.f
19dynamic_weightint0
weighttypeshape
weight_datafloat/fp16/int8[kernel_w, num_input / group, num_output / group, group]
bias_datafloat[num_output]

17.CopyTo: 将输入数据复制到指定位置

self[offset] = src
  • one_blob_only
param idnametypedefaultdescription
0woffsetint0
1hoffsetint0
13doffsetint0
2coffsetint0
9startsarray[ ]
11axesarray[ ]


18.Crop: 裁剪操作

对输入数据进行裁剪操作,保留感兴趣的部分。

y = crop(x)
  • one_blob_only
param idnametypedefaultdescription
0woffsetint0
1hoffsetint0
13doffsetint0
2coffsetint0
3outwint0
4outhint0
14outdint0
5outcint0
6woffset2int0
7hoffset2int0
15doffset2int0
8coffset2int0
9startsarray[ ]
10endsarray[ ]
11axesarray[ ]

import torch

# 创建一个3x3的张量
tensor = torch.tensor([[1, 2, 3],
                        [4, 5, 6],
                        [7, 8, 9]])

# 进行裁剪,选取其中部分区域
cropped_tensor = tensor[1:, 1:]

print(cropped_tensor)
# tensor([[5, 6],
#         [8, 9]])

19.CumulativeSum: 对输入数据进行累积求和操作。

If axis < 0, we use axis = x.dims + axis

It implements torch.cumsum — PyTorch 2.3 documentation

  • one_blob_only
  • support_inplace
param idnametypedefaultdescription
0axisint0

20.Deconvolution: 反卷积操作

用于图像生成和语义分割任务等。

x2 = deconv(x, weight, kernel, stride, dilation) + bias
x3 = depad(x2, pads, pad_value)
y = activation(x3, act_type, act_params)
  • one_blob_only
param idnametypedefaultdescription
0num_outputint0
1kernel_wint0
2dilation_wint1
3stride_wint1
4pad_leftint0
5bias_termint0
6weight_data_sizeint0
9activation_typeint0
10activation_paramsarray[ ]
11kernel_hintkernel_w
12dilation_hintdilation_w
13stride_hintstride_w
14pad_topintpad_left
15pad_rightintpad_left
16pad_bottomintpad_top
18output_pad_rightint0
19output_pad_bottomintoutput_pad_right
20output_wint0
21output_hintoutput_w
28dynamic_weightint0
weighttypeshape
weight_datafloat/fp16[kernel_w, kernel_h, num_input, num_output]
bias_datafloat[num_output]

21.Deconvolution1D: 一维反卷积操作

在一维数据上应用反卷积操作。

x2 = deconv1d(x, weight, kernel, stride, dilation) + bias
x3 = depad(x2, pads, pad_value)
y = activation(x3, act_type, act_params)
  • one_blob_only
param idnametypedefaultdescription
0num_outputint0
1kernel_wint0
2dilation_wint1
3stride_wint1
4pad_leftint0
5bias_termint0
6weight_data_sizeint0
9activation_typeint0
10activation_paramsarray[ ]
15pad_rightintpad_left
18output_pad_rightint0
20output_wint0
28dynamic_weightint0
weighttypeshape
weight_datafloat/fp16[kernel_w, num_input, num_output]
bias_datafloat[num_output]

22.Deconvolution3D: 三维反卷积操作

在三维数据上应用反卷积操作。

x2 = deconv3d(x, weight, kernel, stride, dilation) + bias
x3 = depad(x2, pads, pad_value)
y = activation(x3, act_type, act_params)
  • one_blob_only
param idnametypedefaultdescription
0num_outputint0
1kernel_wint0
2dilation_wint1
3stride_wint1
4pad_leftint0
5bias_termint0
6weight_data_sizeint0
9activation_typeint0
10activation_paramsarray[ ]
11kernel_hintkernel_w
12dilation_hintdilation_w
13stride_hintstride_w
14pad_topintpad_left
15pad_rightintpad_left
16pad_bottomintpad_top
17pad_behindintpad_front
18output_pad_rightint0
19output_pad_bottomintoutput_pad_right
20output_pad_behindintoutput_pad_right
21kernel_dintkernel_w
22dilation_dintdilation_w
23stride_dintstride_w
24pad_frontintpad_left
25output_wint0
26output_hintoutput_w
27output_dintoutput_w
weighttypeshape
weight_datafloat/fp16[kernel_w, kernel_h, kernel_d, num_input, num_output]
bias_datafloat[num_output]


23.DeconvolutionDepthWise: 深度可分离反卷积。

x2 = deconv(x, weight, kernel, stride, dilation, group) + bias
x3 = depad(x2, pads, pad_value)
y = activation(x3, act_type, act_params)
  • one_blob_only
param idnametypedefaultdescription
0num_outputint0
1kernel_wint0
2dilation_wint1
3stride_wint1
4pad_leftint0
5bias_termint0
6weight_data_sizeint0
7groupint1
9activation_typeint0
10activation_paramsarray[ ]
11kernel_hintkernel_w
12dilation_hintdilation_w
13stride_hintstride_w
14pad_topintpad_left
15pad_rightintpad_left
16pad_bottomintpad_top
18output_pad_rightint0
19output_pad_bottomintoutput_pad_right
20output_wint0
21output_hintoutput_w
28dynamic_weightint0
weighttypeshape
weight_datafloat/fp16[kernel_w, kernel_h, num_input / group, num_output / group, group]
bias_datafloat[num_output]

24.DeconvolutionDepthWise1D: 在一维数据上应用深度可分离反卷积。

x2 = deconv1d(x, weight, kernel, stride, dilation, group) + bias
x3 = depad(x2, pads, pad_value)
y = activation(x3, act_type, act_params)
  • one_blob_only
param idnametypedefaultdescription
0num_outputint0
1kernel_wint0
2dilation_wint1
3stride_wint1
4pad_leftint0
5bias_termint0
6weight_data_sizeint0
7groupint1
9activation_typeint0
10activation_paramsarray[ ]
15pad_rightintpad_left
18output_pad_rightint0
20output_wint0
28dynamic_weightint0
weighttypeshape
weight_datafloat/fp16[kernel_w, num_input / group, num_output / group, group]
bias_datafloat[num_output]

25.DeconvolutionDepthWise3D: 三维深度可分离反卷积

在三维数据上应用深度可分离反卷积。

x2 = deconv3d(x, weight, kernel, stride, dilation, group) + bias
x3 = depad(x2, pads, pad_value)
y = activation(x3, act_type, act_params)
  • one_blob_only
param idnametypedefaultdescription
0num_outputint0
1kernel_wint0
2dilation_wint1
3stride_wint1
4pad_leftint0
5bias_termint0
6weight_data_sizeint0
7groupint1
9activation_typeint0
10activation_paramsarray[ ]
11kernel_hintkernel_w
12dilation_hintdilation_w
13stride_hintstride_w
14pad_topintpad_left
15pad_rightintpad_left
16pad_bottomintpad_top
17pad_behindintpad_front
18output_pad_rightint0
19output_pad_bottomintoutput_pad_right
20output_pad_behindintoutput_pad_right
21kernel_dintkernel_w
22dilation_dintdilation_w
23stride_dintstride_w
24pad_frontintpad_left
25output_wint0
26output_hintoutput_w
27output_dintoutput_w
weighttypeshape
weight_datafloat/fp16[kernel_w, kernel_h, kernel_d, num_input / group, num_output / group, group]
bias_datafloat[num_output]


26.DeformableConv2D: 可变形卷积,允许卷积核在空间上变形。

x2 = deformableconv2d(x, offset, mask, weight, kernel, stride, dilation) + bias
y = activation(x2, act_type, act_params)
param idnametypedefaultdescription
0num_outputint0
1kernel_wint0
2dilation_wint1
3stride_wint1
4pad_leftint0
5bias_termint0
6weight_data_sizeint0
9activation_typeint0
10activation_paramsarray[ ]
11kernel_hintkernel_w
12dilation_hintdilation_w
13stride_hintstride_w
14pad_topintpad_left
15pad_rightintpad_left
16pad_bottomintpad_top
weighttypeshape
weight_datafloat/fp16/int8[kernel_w, kernel_h, num_input, num_output]
bias_datafloat

[num_output]

27.Dequantize: 对量化后的数据进行反量化操作。

将量化后的数据还原为原始浮点数形式的过程,通常用于将量化后的激活值或权重恢复为浮点数,以便进行后续的计算

y = x * scale + bias
  • one_blob_only
  • support_inplace
param idnametypedefaultdescription
0scale_data_sizeint1
1bias_data_sizeint0
weighttypeshape
scale_datafloat[scale_data_size]
bias_datafloat[bias_data_size]

#对激活值(Activation)进行Dequantization:
import torch

# 假设quantized_tensor为量化后的张量
quantized_tensor = torch.tensor([0, 1, 2, 3], dtype=torch.uint8)  # 假设使用8位无符号整数进行量化

# 进行Dequantization
dequantized_tensor = quantized_tensor.float()  # 将数据类型转换为float类型,即将量化后的整数数据转换为浮点数

print(dequantized_tensor)
# tensor([0., 1., 2., 3.])
#对权重(Weights)进行Dequantization
import torch

# 假设quantized_weights为量化后的权重张量
quantized_weights = torch.tensor([-1, 0, 1, 2], dtype=torch.int8)  # 假设使用8位有符号整数进行量化

# 进行Dequantization
scale = 0.01  # 量化比例
dequantized_weights = quantized_weights.float() * scale  # 将量化后的整数数据乘以比例因子以完成反量化操作

print(dequantized_weights)
# tensor([-0.0100,  0.0000,  0.0100,  0.0200])

28.Diag: 创建一个对角阵。

对角矩阵是一个主对角线以外的所有元素均为零的矩阵,而主对角线上的元素可以为零或非零。

如下:

y = diag(x, diagonal)
  • one_blob_only
param idnametypedefaultdescription
0diagonalint0
import torch

# 创建一个包含对角线元素为 [1, 2, 3] 的对角矩阵
diagonal_elements = torch.tensor([1, 2, 3])
diagonal_matrix = torch.diag(diagonal_elements)

print(diagonal_matrix)
# tensor([[1, 0, 0],
#         [0, 2, 0],
#         [0, 0, 3]])

29.Dropout: 随机失活

在训练过程中随机断开神经元连接,用于防止过拟合。

y = x * scale
  • one_blob_only
param idnametypedefaultdescription
0scalefloat1.f
import torch
import torch.nn as nn

# 创建一个包含两个全连接层和一个Dropout层的神经网络
class MyModel(nn.Module):
    def __init__(self):
        super(MyModel, self).__init__()
        self.fc1 = nn.Linear(10, 5)
        self.dropout = nn.Dropout(p=0.5)  # 创建一个保留概率为0.5的Dropout层
        self.fc2 = nn.Linear(5, 2)

    def forward(self, x):
        x = self.fc1(x)
        x = self.dropout(x)  # 在全连接层1的输出上应用Dropout
        x = torch.relu(x)
        x = self.fc2(x)
        return x

# 创建模型实例
model = MyModel()

# 在训练时,使用model.train()来开启Dropout
model.train()

# 输入数据示例
input_data = torch.randn(1, 10)  # 创建一个大小为(1, 10)的张量

# 前向传播
output = model(input_data)

print(output)
# tensor([[0.7759, 0.4466]], grad_fn=<AddmmBackward0>)

30.Eltwise: 逐元素操作

对输入执行元素级操作,如加法.乘法等。

y = elementwise_op(x0, x1, ...)
param idnametypedefaultdescription
0op_typeint0
1coeffsarray[ ]

Operation type:

  • 0 = PROD
  • 1 = SUM
  • 2 = MAX
  • import torch
    
    # 创建两个张量
    a = torch.tensor([1, 2, 3])
    b = torch.tensor([4, 5, 6])
    
    # 0 = PROD,逐元素相乘
    prod_result = torch.mul(a, b)
    print("Elementwise product result:", prod_result)
    # Elementwise product result: tensor([ 4, 10, 18])
    # 1 = SUM,逐元素相加
    sum_result = torch.add(a, b)
    print("Elementwise sum result:", sum_result)
    # Elementwise sum result: tensor([5, 7, 9])
    
    # 2 = MAX,逐元素取最大值
    max_result = torch.maximum(a, b)
    print("Elementwise max result:", max_result)
    # Elementwise max result: tensor([4, 5, 6])

31.ELU: 应用指数线性单元(ELU)激活函数。

if x < 0    y = (exp(x) - 1) * alpha
else        y = x
  • one_blob_only
  • support_inplace
param idnametypedefaultdescription
0alphafloat0.1f

32.Embed: 将输入数据映射到低维空间。

词向量啊,万物皆可embed

将高维稀疏的数据编码成低维稠密向量表示的技术,通常用于将离散的类别型数据(例如单词、产品ID等)映射到连续的实数向量空间中

y = embedding(x)
param idnametypedefaultdescription
0num_outputint0
1input_dimint0
2bias_termint0
3weight_data_sizeint0
weighttypeshape
weight_datafloat[weight_data_size]
bias_termfloat[num_output]

import torch
import torch.nn as nn

# 假设我们有10个不同的词,需要将它们映射成一个5维的稠密向量
vocab_size = 10
embedding_dim = 5

# 创建一个Embedding层
embedding = nn.Embedding(num_embeddings=vocab_size, embedding_dim=embedding_dim)

# 定义一个输入,假设我们要获取ID为3和7的词的向量表示
input_ids = torch.LongTensor([3, 7])

# 通过Embedding层获取对应词的向量表示
output = embedding(input_ids)

print(output)
# tensor([[-0.4583,  2.2385,  1.1503,  0.4575, -0.5081],
#         [ 2.1852, -1.2893,  0.6631,  0.1552,  1.6735]],
#        grad_fn=<EmbeddingBackward0>)

33.Exp: 计算输入数据的指数。

if base == -1   y = exp(shift + x * scale)
else            y = pow(base, (shift + x * scale))
  • one_blob_only
  • support_inplace
param idnametypedefaultdescription
0basefloat-1.f
1scalefloat1.f
2shiftfloat0.f

34.Flatten: 将输入数据展平为一维。

Reshape blob to 1 dimension(将其重塑为一维数组。)

  • one_blob_only
  • import torch
    
    # 创建一个3维张量,例如(2, 3, 4),表示(batch_size, channels, height, width)
    input_tensor = torch.randn(2, 3, 4)
    
    
    
    # 使用torch.flatten()将张量展平
    output_tensor1 = torch.flatten(input_tensor, start_dim=0)
    
    # 使用torch.flatten()将张量展平
    output_tensor2 = input_tensor.view(2*3*4)
    
    print("Input Tensor shape:", input_tensor.shape)
    print("Flattened Tensor shape:", output_tensor1.shape)
    print("view Tensor shape:", output_tensor2.shape)
    # Input Tensor shape: torch.Size([2, 3, 4])
    # Flattened Tensor shape: torch.Size([24])
    # view Tensor shape: torch.Size([24])

35.Fold: 折叠操作

对输入数据进行折叠操作,与展平相反。

y = fold(x)
  • one_blob_only
param idnametypedefaultdescription
0num_outputint0
1kernel_wint0
2dilation_wint1
3stride_wint1
4pad_leftint0
11kernel_hintkernel_w
12dilation_hintdilation_w
13stride_hintstride_w
14pad_topintpad_left
15pad_rightintpad_left
16pad_bottomintpad_top
20output_wint0
21output_hintoutput_w
import torch

# 创建一个4x4的张量
x = torch.arange(1, 17).view(4, 4)
print("Original tensor:")
print(x)
# Original tensor:
# tensor([[ 1,  2,  3,  4],
#         [ 5,  6,  7,  8],
#         [ 9, 10, 11, 12],
#         [13, 14, 15, 16]])
# 对张量进行fold操作 4x4 =16  分成 2x8 或者8x2 、1x16 、2x2x2x2其他 等等
folded_tensor = x.view(2,2,2,2)
print("Folded tensor:")
print(folded_tensor)
# Folded tensor:
# tensor([[[[ 1,  2],
#           [ 3,  4]],
# 
#          [[ 5,  6],
#           [ 7,  8]]],
# 
# 
#         [[[ 9, 10],
#           [11, 12]],
# 
#          [[13, 14],
#           [15, 16]]]])

36.GELU: 应用高斯误差线性单元(GELU)激活函数。

if fast_gelu == 1   y = 0.5 * x * (1 + tanh(0.79788452 * (x + 0.044715 * x * x * x)));
else                y = 0.5 * x * erfc(-0.70710678 * x)
  • one_blob_only
  • support_inplace
param idnametypedefaultdescription
0fast_geluint0use approximation

37.GLU: 应用门控线性单元(GLU)激活函数。

If axis < 0, we use axis = x.dims + axis

GLU(a,b)=a⊗σ(b)

where a is the first half of the input matrix and b is the second half.

axis specifies the dimension to split the input

a 是输入矩阵的前一半,b 是后一半。

axis 参数用于指定沿着哪个维度(dimension)对输入矩阵进行分割。

  • one_blob_only
param idnametypedefaultdescription
0axisint0

38.Gemm: 执行矩阵乘法操作。

a = transA ? transpose(x0) : x0
b = transb ? transpose(x1) : x1
c = x2
y = (gemm(a, b) + c * beta) * alpha
param idnametypedefaultdescription
0alphafloat1.f
1betafloat1.f
2transAint0
3transbint0
4constantAint0
5constantBint0
6constantCint0
7constantMint0
8constantNint0
9constantKint0
10constant_broadcast_type_Cint0
11output_N1Mint0
12output_elempackint0
13output_elemtypeint0
14output_transposeint0
20constant_TILE_Mint0
21constant_TILE_Nint0
22constant_TILE_Kint0
weighttypeshape
A_datafloat[M, K] or [K, M]
B_datafloat[N, K] or [K, N]
C_datafloat[1], [M] or [N] or [1, M] or [N,1] or [N, M]

import torch

# 创建两个矩阵
A = torch.tensor([[1, 2], [3, 4]])
B = torch.tensor([[5, 6], [7, 8]])

# 执行矩阵乘法
C = torch.matmul(A, B)

print("Matrix A:")
print(A)
print("Matrix B:")
print(B)
print("Result of Matrix Multiplication:")
print(C)
# Matrix A:
# tensor([[1, 2],
#         [3, 4]])
# Matrix B:
# tensor([[5, 6],
#         [7, 8]])
# Result of Matrix Multiplication:
# tensor([[19, 22],
#         [43, 50]])

39.GridSample: 在输入的网格上进行采样操作。

根据输入的采样网格(sampling grid)中指定的坐标,在输入张量上进行采样,输出对应的插值结果

Given an input and a flow-field grid, computes the output using input values and pixel locations from grid.

For each output location output[:, h2, w2], the size-2 vector grid[h2, w2, 2] specifies input pixel[:, h1, w1] locations x and y, 
which are used to interpolate the output value output[:, h2, w2]

This function is often used in conjunction with affine_grid() to build Spatial Transformer Networks .

给定一个输入和一个flow-field流场网格,使用输入值和来自网格的像素位置,计算输出。

对于每个输出位置 output[:, h2, w2],大小为2的向量 grid[h2, w2, 2] 指定了输入像素[:, h1, w1] 的位置 x 和 y,用于进行输出值 output[:, h2, w2] 的插值计算。

这个函数通常与 affine_grid() 一起使用,用于构建空间变换网络(Spatial Transformer Networks)。

param idnametypedefaultdescription
0sample_typeint1
1padding_modeint1
2align_cornerint0
3permute_fusionint0fuse with permute

Sample type:

  • 1 = Nearest
  • 2 = Bilinear
  • 3 = Bicubic

Padding mode:

  • 1 = zeros
  • 2 = border
  • 3 = reflection
  • #引用 https://www.cnblogs.com/yanghailin/p/17747266.html
    import torch
    from torch.nn import functional as F
    
    inp = torch.randint(10, 20, (1, 1, 20, 20)).float()
    print('inp.shape:', inp.shape)
    
    # 得到一个长宽为20的tensor
    out_h = 40
    out_w = 40
    
    # 生成grid点
    grid_h = torch.linspace(-1, 1, out_h).view(1, -1, 1).expand(1, out_h, out_w)
    grid_w = torch.linspace(-1, 1, out_w).view(1, 1, -1).expand(1, out_h, out_w)
    grid = torch.stack((grid_h, grid_w), dim=3)  # grid的形状为 [1, 20, 20, 2]
    
    outp = F.grid_sample(inp, grid=grid, mode='bilinear')
    print(outp.shape)  # torch.Size([1, 1, 20, 20])
    
    print("Input tensor:")
    print(inp)
    
    print("Output tensor after grid sampling:")
    print(outp)
    # inp.shape: torch.Size([1, 1, 20, 20])
    # torch.Size([1, 1, 40, 40])
    # Input tensor:
    # tensor([[[[16., 17., 16., 10., 16., 11., 13., 17., 16., 15., 10., 10., 13., 17.,
    #            11., 19., 12., 11., 10., 12.],
    #           [12., 15., 17., 16., 13., 13., 16., 19., 18., 10., 11., 13., 19., 14.,
    #            14., 18., 14., 11., 10., 15.],
    #           [12., 11., 18., 10., 15., 15., 17., 10., 10., 14., 18., 15., 12., 16.,
    #            10., 18., 16., 16., 10., 16.],
    #           [17., 17., 12., 11., 16., 16., 10., 16., 17., 16., 13., 10., 18., 18.,
    #            17., 17., 17., 10., 16., 19.],
    #           [14., 15., 16., 19., 12., 12., 11., 10., 16., 12., 16., 10., 17., 10.,
    #            12., 18., 19., 13., 13., 16.],
    #           [15., 19., 17., 18., 15., 16., 15., 10., 19., 15., 11., 16., 18., 14.,
    #            19., 10., 13., 16., 18., 19.],
    #           [13., 13., 14., 11., 15., 13., 18., 14., 10., 13., 13., 11., 17., 13.,
    #            17., 13., 10., 12., 14., 10.],
    #           [12., 10., 17., 16., 17., 10., 18., 15., 14., 13., 13., 10., 17., 16.,
    #            19., 13., 14., 10., 17., 12.],
    #           [12., 14., 18., 15., 16., 14., 13., 14., 13., 13., 17., 11., 15., 18.,
    #            19., 14., 12., 14., 12., 14.],
    #           [12., 13., 17., 14., 18., 16., 14., 16., 14., 15., 19., 13., 19., 17.,
    #            12., 18., 15., 12., 16., 11.],
    #           [10., 19., 12., 13., 12., 17., 14., 13., 19., 19., 12., 13., 17., 17.,
    #            14., 17., 11., 14., 18., 12.],
    #           [10., 19., 19., 11., 16., 16., 15., 17., 10., 13., 16., 10., 17., 10.,
    #            15., 11., 11., 17., 15., 17.],
    #           [13., 12., 10., 11., 11., 16., 16., 16., 10., 10., 13., 19., 14., 13.,
    #            18., 15., 12., 19., 14., 16.],
    #           [16., 13., 11., 11., 12., 16., 12., 16., 10., 16., 11., 19., 19., 12.,
    #            11., 15., 11., 15., 12., 17.],
    #           [17., 12., 17., 10., 15., 12., 13., 16., 14., 15., 19., 17., 17., 12.,
    #            10., 18., 19., 12., 15., 13.],
    #           [10., 15., 16., 10., 13., 19., 17., 19., 18., 18., 12., 14., 13., 12.,
    #            18., 17., 12., 17., 14., 17.],
    #           [13., 10., 15., 19., 19., 14., 11., 14., 11., 13., 19., 10., 10., 13.,
    #            16., 11., 15., 13., 18., 15.],
    #           [19., 10., 15., 15., 13., 13., 15., 13., 15., 18., 13., 10., 14., 10.,
    #            13., 14., 16., 12., 17., 12.],
    #           [12., 10., 17., 15., 19., 12., 19., 11., 14., 19., 16., 11., 17., 14.,
    #            15., 12., 12., 14., 18., 15.],
    #           [12., 15., 14., 18., 19., 19., 17., 11., 11., 12., 13., 19., 17., 19.,
    #            10., 17., 15., 18., 14., 10.]]]])
    # Output tensor after grid sampling:
    # tensor([[[[ 4.0000,  7.9744,  6.9487,  ...,  6.0000,  6.0000,  3.0000],
    #           [ 8.0064, 15.9619, 13.9237,  ..., 12.0048, 12.0376,  6.0192],
    #           [ 8.2628, 16.4878, 14.9757,  ..., 12.1954, 13.5432,  6.7885],
    #           ...,
    #           [ 5.4744, 10.9670, 11.6967,  ..., 14.4545, 12.1599,  6.0513],
    #           [ 5.9872, 12.0123, 13.5311,  ..., 12.6727, 10.1152,  5.0256],
    #           [ 3.0000,  6.0192,  6.7885,  ...,  6.3141,  5.0321,  2.5000]]]])

40.GroupNorm: 对神经网络中的特征图执行分组归一化。

将特征通道分为多个组,每个组包含一定数量的通道,然后对每个组内的通道进行独立的规范化操作。

split x along channel axis into group x0, x1 ...
l2 normalize for each group x0, x1 ...
y = x * gamma + beta
  • one_blob_only
  • support_inplace
param idnametypedefaultdescription
0groupint1
1channelsint0
2epsfloat0.001fx = x / sqrt(var + eps)
3affineint1
weighttypeshape
gamma_datafloat[channels]
beta_datafloat[channels]

import torch
import torch.nn as nn

# 定义一个输入张量
input_tensor = torch.randn(1, 6, 4, 4)  # (batch_size, num_channels, height, width)

# 使用GroupNorm,假设分成2组
num_groups = 2
group_norm = nn.GroupNorm(num_groups, 6)  # num_groups为组数,6为输入通道数

# 对输入张量进行GroupNorm操作
output = group_norm(input_tensor)

# 打印输入输出形状
print("Input shape:", input_tensor.shape)
print("Output shape after GroupNorm:", output.shape)
# Input shape: torch.Size([1, 6, 4, 4])
# Output shape after GroupNorm: torch.Size([1, 6, 4, 4])

41.GRU: 门控循环单元(GRU)神经网络层。

        是一种常用的递归神经网络(RNN)变体,用于处理序列数据。与标准RNN相比,GRU引入了门控机制,有助于更好地捕捉长期依赖关系

Apply a single-layer GRU to a feature sequence of T timesteps. The input blob shape is [w=input_size, h=T] and the output blob shape is [w=num_output, h=T].

y = gru(x)
y0, hidden y1 = gru(x0, hidden x1)
  • one_blob_only if bidirectional
param idnametypedefaultdescription
0num_outputint0hidden size of output
1weight_data_sizeint0total size of weight matrix
2directionint00=forward, 1=reverse, 2=bidirectional
weighttypeshape
weight_xc_datafloat/fp16/int8[input_size, num_output * 3, num_directions]
bias_c_datafloat/fp16/int8[num_output, 4, num_directions]
weight_hc_datafloat/fp16/int8[num_output, num_output * 3, num_directions]

Direction flag:

  • 0 = forward only
  • 1 = reverse only
  • 2 = bidirectional
  • import torch
    import torch.nn as nn
    
    # 假设输入维度为3,隐藏单元数为4
    input_size = 3
    hidden_size = 4
    
    # 定义一个GRU层
    gru = nn.GRU(input_size, hidden_size)  # 默认情况下,没有指定层数,默认为单层
    
    # 定义一个输入序列,假设序列长度为2,批量大小为1
    input_seq = torch.randn(2, 1, 3)  # (seq_len, batch_size, input_size)
    
    # 初始化隐藏状态
    hidden = torch.zeros(1, 1, 4)  # (num_layers, batch_size, hidden_size)
    
    # 将输入序列传递给GRU层
    output, hidden = gru(input_seq, hidden)
    
    # 打印输出和隐藏状态的形状
    print("Output shape:", output.shape)  # (seq_len, batch_size, num_directions * hidden_size)
    print("Hidden state shape:", hidden.shape)  # (num_layers * num_directions, batch, hidden_size)
    # Output shape: torch.Size([2, 1, 4])
    # Hidden state shape: torch.Size([1, 1, 4])
    

42.HardSigmoid: 应用硬Sigmoid激活函数。

在神经网络中通常用于限制神经元的激活范围。与标准的 Sigmoid 函数相比,HardSigmoid 是一种更简单和高效的近似函数,通常用于加速模型的训练过程

y = clamp(x * alpha + beta, 0, 1)
  • one_blob_only
  • support_inplace
param idnametypedefaultdescription
0alphafloat0.2f
1betafloat0.5f

import torch
import torch.nn.functional as F

# 定义输入张量
input_tensor = torch.randn(3, 4)  # 假设输入张量大小为3x4

# 使用HardSigmoid激活函数
output = F.hardsigmoid(input_tensor)  # HardSigmoid(x) = clip(0.2*x + 0.5, 0, 1)

# 打印输入和输出张量
print("Input tensor:")
print(input_tensor)
# Input tensor:
# tensor([[ 0.5026,  0.6612, -0.0961,  1.9332],
#         [-0.8780, -0.4930, -0.2804, -0.0440],
#         [ 1.2866, -1.9575,  0.7738, -0.8340]])
print("\nOutput tensor after HardSigmoid:")
print(output)
# Output tensor after HardSigmoid:
# tensor([[0.5838, 0.6102, 0.4840, 0.8222],
#         [0.3537, 0.4178, 0.4533, 0.4927],
#         [0.7144, 0.1738, 0.6290, 0.3610]])

43.HardSwish: 应用硬Swish激活函数。

y = x * clamp(x * alpha + beta, 0, 1)
  • one_blob_only
  • support_inplace
param idnametypedefaultdescription
0alphafloat0.2f
1betafloat0.5f

import torch
import torch.nn.functional as F

# 定义 HardSwish 激活函数
def hardswish(x):
    return x * F.hardsigmoid(x + 3, inplace=True)

# 创建一个张量作为输入
input_tensor = torch.randn(3, 4)  # 假设输入张量大小为 3x4

# 应用 HardSwish 激活函数
output = hardswish(input_tensor)

# 打印输入张量和输出张量
print("Input tensor:")
print(input_tensor)
print("\nOutput tensor after HardSwish:")
print(output)
# Input tensor:
# tensor([[ 0.4330, -1.9232,  1.9127,  0.6024],
#         [-0.2073,  0.1116, -0.6153,  0.5362],
#         [-1.4893,  0.0764, -0.1484, -0.0945]])
# 
# Output tensor after HardSwish:
# tensor([[ 0.4330, -1.3068,  1.9127,  0.6024],
#         [-0.2001,  0.1116, -0.5522,  0.5362],
#         [-1.1197,  0.0764, -0.1447, -0.0930]])

44.InnerProduct: 执行全连接操作。

将输入的所有特征连接到输出层的每个神经元,实现了每个神经元与前一层的所有神经元之间的连接

x2 = innerproduct(x, weight) + bias
y = activation(x2, act_type, act_params)
  • one_blob_only
param idnametypedefaultdescription
0num_outputint0
1bias_termint0
2weight_data_sizeint0
8int8_scale_termint0
9activation_typeint0
10activation_paramsarray[ ]
weighttypeshape
weight_datafloat/fp16/int8[num_input, num_output]
bias_datafloat[num_output]
weight_data_int8_scalesfloat[num_output]
bottom_blob_int8_scalesfloat[1]

import torch
import torch.nn as nn

class InnerProduct(nn.Module):
    def __init__(self, in_features, out_features):
        super(InnerProduct, self).__init__()
        self.fc = nn.Linear(in_features, out_features)

    def forward(self, x):
        return self.fc(x)

# 创建一个 InnerProduct 层
inner_product_layer = InnerProduct(100, 200)  # 假设输入特征维度为 100,输出特征维度为 200

# 定义输入数据
input_data = torch.randn(1, 100)  # 假设输入数据为 1 组,每组包含 100 个特征

# 运行 InnerProduct 层
output = inner_product_layer(input_data)
print(output.shape)  # 输出特征的形状
# torch.Size([1, 200])

45.Input: 神经网络的输入层

y = input
  • support_inplace
param idnametypedefaultdescription
0wint0
1hint0
11dint0
2cint0

46.InstanceNorm: 归一化操作

一种归一化技术,通常用于神经网络中的层级操作。实例归一化独立地标准化每个样本的特征,而不是整个批次的特征。这种归一化方式可以帮助模型更好地学习特征表示,提高收敛速度并加速训练

split x along channel axis into instance x0, x1 ...
l2 normalize for each channel instance x0, x1 ...
y = x * gamma + beta
  • one_blob_only
  • support_inplace
param idnametypedefaultdescription
0channelsint0
1epsfloat0.001fx = x / sqrt(var + eps)
2affineint1
weighttypeshape
gamma_datafloat[channels]
beta_datafloat[channels]

import torch
import torch.nn as nn

# 创建一个实例归一化层
instance_norm_layer = nn.InstanceNorm2d(3)  # 通道数为 3

# 随机生成一组特征图作为输入数据
input_data = torch.randn(1, 3, 224, 224)  # 假设输入数据为 1 组,通道数为 3,图像尺寸为 224x224

# 运行实例归一化层
output = instance_norm_layer(input_data)

print(output.shape)  # 输出特征的形状
# torch.Size([1, 3, 224, 224])

47.Interp: 执行插值操作

在计算机视觉领域,插值通常用于调整图像的大小,从而实现图像的放大、缩小或者调整分辨率等操作

if dynamic_target_size == 0     y = resize(x) by fixed size or scale
else                            y = resize(x0, size(x1))
  • one_blob_only if dynamic_target_size == 0
param idnametypedefaultdescription
0resize_typeint0
1height_scalefloat1.f
2width_scalefloat1.f
3output_heightint0
4output_widthint0
5dynamic_target_sizeint0
6align_cornerint0

Resize type:

  • 1 = Nearest
  • 2 = Bilinear
  • 3 = Bicubic
  • import torch
    import torch.nn.functional as F
    
    # 创建一个随机的特征图作为输入数据
    input_data = torch.randn(1, 3, 224, 224)  # 假设输入数据为 1 组,通道数为 3,图像尺寸为 224x224
    
    # 执行双线性插值将图像大小调整到 300x300
    output = F.interpolate(input_data, size=(300, 300), mode='bilinear', align_corners=False)
    
    print(output.shape)  # 输出特征的形状
    # torch.Size([1, 3, 300, 300])

48.LayerNorm: 对神经网络中的层执行归一化操作

是一种用于神经网络中的归一化技术,与 Batch Normalization 不同,Layer Normalization 是对单个样本的特征进行标准化,而不是对整个批次。层归一化有助于减少内部协变量偏移,从而加速网络训练过程并提高泛化性能

split x along outmost axis into part x0, x1 ...
l2 normalize for each part x0, x1 ...
y = x * gamma + beta by elementwise
  • one_blob_only
  • support_inplace
param idnametypedefaultdescription
0affine_sizeint0
1epsfloat0.001fx = x / sqrt(var + eps)
2affineint1
weighttypeshape
gamma_datafloat[affine_size]
beta_datafloat[affine_size]
import torch
import torch.nn as nn

# 创建一个层归一化模块
layer_norm = nn.LayerNorm(256)  # 输入特征的尺寸为 256

# 随机生成一组特征作为输入数据
input_data = torch.randn(4, 256)  # 假设输入数据为 4 组,每组特征的尺寸为 256

# 运行层归一化模块
output = layer_norm(input_data)

print(output.shape)  # 输出特征的形状
# torch.Size([4, 256])

49.Log: 计算输入数据的自然对数。

if base == -1   y = log(shift + x * scale)
else            y = log(shift + x * scale) / log(base)
  • one_blob_only
  • support_inplace
param idnametypedefaultdescription
0basefloat-1.f
1scalefloat1.f
2shiftfloat0.f

50.LRN: 局部响应归一化层。

一种局部归一化的方法,用于一些深度学习模型中,旨在模拟生物神经元系统中的侧抑制机制。LRN 主要用于提升模型的泛化能力,防止模型过拟合

if region_type == ACROSS_CHANNELS   square_sum = sum of channel window of local_size
if region_type == WITHIN_CHANNEL    square_sum = sum of spatial window of local_size
y = x * pow(bias + alpha * square_sum / (local_size * local_size), -beta)
  • one_blob_only
  • support_inplace
param idnametypedefaultdescription
0region_typeint0
1local_sizeint5
2alphafloat1.f
3betafloat0.75f
4biasfloat1.f

Region type:

  • 0 = ACROSS_CHANNELS
  • 1 = WITHIN_CHANNEL
  • import torch
    import torch.nn as nn
    
    class LRN(nn.Module):
        def __init__(self, size=5, alpha=1e-4, beta=0.75, k=1.0):
            super(LRN, self).__init__()
            self.size = size
            self.alpha = alpha
            self.beta = beta
            self.k = k
    
        def forward(self, x):
            squared = x.pow(2)
            pool = nn.functional.avg_pool2d(squared, self.size, stride=1, padding=self.size//2)
            denom = self.k + self.alpha * pool
            output = x / denom.pow(self.beta)
            return output
    
    # 创建一个 LRN 模块实例
    lrn = LRN(size=3, alpha=1e-4, beta=0.75, k=1.0)
    
    # 随机生成一组特征作为输入数据
    input_data = torch.randn(1, 3, 224, 224)  # 假设输入数据为 1 组,通道数为 3,图像尺寸为 224x224
    
    # 运行 LRN 模块
    output = lrn(input_data)
    
    print(output.shape)  # 输出特征的形状
    # torch.Size([1, 3, 224, 224])

51.LSTM: 长短期记忆(LSTM)神经网络层。

        是一种常用的循环神经网络(RNN)变体,专门设计用来解决传统 RNN 中遇到的长期依赖问题。LSTM 的设计使其能够更好地捕捉和利用长期序列中的依赖关系,适用于处理时间序列数据、自然语言处理等任务。

Apply a single-layer LSTM to a feature sequence of T timesteps. The input blob shape is [w=input_size, h=T] and the output blob shape is [w=num_output, h=T].

y = lstm(x)
y0, hidden y1, cell y2 = lstm(x0, hidden x1, cell x2)
  • one_blob_only if bidirectional
param idnametypedefaultdescription
0num_outputint0output size of output
1weight_data_sizeint0total size of IFOG weight matrix
2directionint00=forward, 1=reverse, 2=bidirectional
3hidden_sizeintnum_outputhidden size
weighttypeshape
weight_xc_datafloat/fp16/int8[input_size, hidden_size * 4, num_directions]
bias_c_datafloat/fp16/int8[hidden_size, 4, num_directions]
weight_hc_datafloat/fp16/int8[num_output, hidden_size * 4, num_directions]
weight_hr_datafloat/fp16/int8[hidden_size, num_output, num_directions]

Direction flag:

  • 0 = forward only
  • 1 = reverse only
  • 2 = bidirectional

52.MemoryData: 用于存储数据并生成数据迭代器。

用于在模型中定义一个固定大小的内存数据块。MemoryData 层通常用于存储一些固定的参数或中间数据,以便在模型前向推理过程中进行使用。

y = data
param idnametypedefaultdescription
0wint0
1hint0
11dint0
2cint0
21load_typeint11=fp32
weighttypeshape
datafloat[w, h, d, c]

53.Mish: 应用Mish激活函数。

Mish 激活函数的形式相对简单,但由于其使用了双曲正切函数和软加函数的组合,可以在一定程度上克服一些常见激活函数的问题,如梯度消失和梯度爆炸。

y = x * tanh(log(exp(x) + 1))
  • one_blob_only
  • support_inplace

54.MultiHeadAttention: 多头注意力机制。

多头注意力机制是注意力机制的一种扩展形式,旨在充分利用不同“头”(独立的子空间)来对输入的序列进行不同方式的关注和表示。每个“头”都学习关注输入序列中不同的部分,从而能够更好地捕捉序列中的不同特征和关系。

split q k v into num_head part q0, k0, v0, q1, k1, v1 ...
for each num_head part
    xq = affine(q) / (embed_dim / num_head)
    xk = affine(k)
    xv = affine(v)
    xqk = xq * xk
    xqk = xqk + attn_mask if attn_mask exists
    softmax_inplace(xqk)
    xqkv = xqk * xv
    merge xqkv to out
y = affine(out)
param idnametypedefaultdescription
0embed_dimint0
1num_headsint1
2weight_data_sizeint0
3kdimintembed_dim
4vdimintembed_dim
5attn_maskint0
weighttypeshape
q_weight_datafloat/fp16/int8[weight_data_size]
q_bias_datafloat[embed_dim]
k_weight_datafloat/fp16/int8[embed_dim * kdim]
k_bias_datafloat[embed_dim]
v_weight_datafloat/fp16/int8[embed_dim * vdim]
v_bias_datafloat[embed_dim]
out_weight_datafloat/fp16/int8[weight_data_size]
out_bias_datafloat[embed_dim]

55.MVN: 均值方差归一化操作。

if normalize_variance == 1 && across_channels == 1      y = (x - mean) / (sqrt(var) + eps) of whole blob
if normalize_variance == 1 && across_channels == 0      y = (x - mean) / (sqrt(var) + eps) of each channel
if normalize_variance == 0 && across_channels == 1      y = x - mean of whole blob
if normalize_variance == 0 && across_channels == 0      y = x - mean of each channel
  • one_blob_only
param idnametypedefaultdescription
0normalize_varianceint0
1across_channelsint0
2epsfloat0.0001fx = x / (sqrt(var) + eps)

56.Noop: 空操作

空操作,不对输入做任何操作

y = x

57.Normalize: 归一化操作

对输入数据进行归一化操作

if across_spatial == 1 && across_channel == 1      x2 = normalize(x) of whole blob
if across_spatial == 1 && across_channel == 0      x2 = normalize(x) of each channel
if across_spatial == 0 && across_channel == 1      x2 = normalize(x) of each position
y = x2 * scale
  • one_blob_only
  • support_inplace
param idnametypedefaultdescription
0across_spatialint0
1channel_sharedint0
2epsfloat0.0001fsee eps mode
3scale_data_sizeint0
4across_channelint0
9eps_modeint0
weighttypeshape
scale_datafloat[scale_data_size]

Eps Mode:

  • 0 = caffe/mxnet x = x / sqrt(var + eps)
  • 1 = pytorch x = x / max(sqrt(var), eps)
  • 2 = tensorflow x = x / sqrt(max(var, eps))

58.Packing: 打包操作

用于高效处理图像张量数据

y = wrap_packing(x)
  • one_blob_only
param idnametypedefaultdescription
0out_elempackint1
1use_paddingint0
2cast_type_fromint0
3cast_type_toint0
4storage_type_fromint0
5storage_type_toint0

59.Padding: 填充操作

对输入数据进行填充操作

y = pad(x, pads)
param idnametypedefaultdescription
0topint0
1bottomint0
2leftint0
3rightint0
4typeint0
5valuefloat0
6per_channel_pad_data_sizeint0
7frontintstride_w
8behindintpad_left
weighttypeshape
per_channel_pad_datafloat[per_channel_pad_data_size]

Padding type:

  • 0 = CONSTANT
  • 1 = REPLICATE
  • 2 = REFLECT

60.Permute: 置换操作

对输入数据的维度进行排列操作

指的是重新排列数据或张量中的维度,以改变数据的排列顺序或维度顺序。这样的操作可以对数据进行重构以适应不同的模型或算法的需求,也可以在处理序列数据时对特定维度进行调整。

y = reorder(x)
param idnametypedefaultdescription
0order_typeint0

Order Type:排列类型如下( W-宽 H-高 C-通道  D-深度)

  • 0 = WH WHC WHDC 
  • 1 = HW HWC HWDC
  • 2 = WCH WDHC
  • 3 = CWH DWHC
  • 4 = HCW HDWC
  • 5 = CHW DHWC
  • 6 = WHCD
  • 7 = HWCD
  • 8 = WCHD
  • 9 = CWHD
  • 10 = HCWD
  • 11 = CHWD
  • 12 = WDCH
  • 13 = DWCH
  • 14 = WCDH
  • 15 = CWDH
  • 16 = DCWH
  • 17 = CDWH
  • 18 = HDCW
  • 19 = DHCW
  • 20 = HCDW
  • 21 = CHDW
  • 22 = DCHW
  • 23 = CDHW

61.PixelShuffle: 像素重组

执行像素重排操作,用于实现像素重排。这种操作通常用于超分辨率重建或者图像生成领域

if mode == 0    y = depth_to_space(x) where x channel order is sw-sh-outc
if mode == 1    y = depth_to_space(x) where x channel order is outc-sw-sh
  • one_blob_only
param idnametypedefaultdescription
0upscale_factorint1
1modeint0

PixelShuffle 操作将输入张量中的通道分组,然后对每个分组内的像素进行重排,从而增加图像的分辨率。在每个分组内部,PixelShuffle 操作会将多个低分辨率通道重组成一个高分辨率通道。

PixelShuffle 的主要优点是可以在不引入额外参数的情况下增加图像的分辨率,这使得神经网络在图像超分辨率重建等任务上表现更加出色

62.Pooling: 池化操作

执行池化操作,降低特征图维度

x2 = pad(x, pads)
x3 = pooling(x2, kernel, stride)
param idnametypedefaultdescription
0pooling_typeint0
1kernel_wint0
2stride_wint1
3pad_leftint0
4global_poolingint0
5pad_modeint0
6avgpool_count_include_padint0
7adaptive_poolingint0
8out_wint0
11kernel_hintkernel_w
12stride_hintstride_w
13pad_topintpad_left
14pad_rightintpad_left
15pad_bottomintpad_top
18out_hintout_w

Pooling type:

  • 0 = MAX
  • 1 = AVG

Pad mode:

  • 0 = full padding
  • 1 = valid padding
  • 2 = tensorflow padding=SAME or onnx padding=SAME_UPPER
  • 3 = onnx padding=SAME_LOWER

63.Pooling1D: 一维池化操作

在一维数据上执行池化操作

x2 = pad(x, pads)
x3 = pooling1d(x2, kernel, stride)
param idnametypedefaultdescription
0pooling_typeint0
1kernel_wint0
2stride_wint1
3pad_leftint0
4global_poolingint0
5pad_modeint0
6avgpool_count_include_padint0
7adaptive_poolingint0
8out_wint0
14pad_rightintpad_left

Pooling type:

  • 0 = MAX
  • 1 = AVG

Pad mode:

  • 0 = full padding
  • 1 = valid padding
  • 2 = tensorflow padding=SAME or onnx padding=SAME_UPPER
  • 3 = onnx padding=SAME_LOWER

64.Pooling3D: 三维池化操作

在三维数据上执行池化操作

x2 = pad(x, pads)
x3 = pooling3d(x2, kernel, stride)
param idnametypedefaultdescription
0pooling_typeint0
1kernel_wint0
2stride_wint1
3pad_leftint0
4global_poolingint0
5pad_modeint0
6avgpool_count_include_padint0
7adaptive_poolingint0
8out_wint0
11kernel_hintkernel_w
12stride_hintstride_w
13pad_topintpad_left
14pad_rightintpad_left
15pad_bottomintpad_top
16pad_behindintpad_front
18out_hintout_w
21kernel_dintkernel_w
22stride_dintstride_w
23pad_frontintpad_left
28out_dintout_w

Pooling type:

  • 0 = MAX
  • 1 = AVG

Pad mode:

  • 0 = full padding
  • 1 = valid padding
  • 2 = tensorflow padding=SAME or onnx padding=SAME_UPPER
  • 3 = onnx padding=SAME_LOWER

65.Power: 幂运算

对输入数据执行幂运算

y = pow((shift + x * scale), power)
  • one_blob_only
  • support_inplace
param idnametypedefaultdescription
0powerfloat1.f
1scalefloat1.f
2shiftfloat0.f

66.PReLU: 参数化修正线性单元

        在传统的ReLU中,当输入值小于0时,激活函数的输出始终为0。而在PReLU中,当输入值小于0时,激活函数的输出不再是固定的0,而是一个小的线性函数,其斜率是可学习的参数,即一个非零值

if x < 0    y = x * slope
else        y = x
  • one_blob_only
  • support_inplace
param idnametypedefaultdescription
0num_slopeint0
weighttypeshape
slope_datafloat[num_slope]

67.Quantize: 量化操作

 量化是将神经网络中的参数和/或激活值从较高精度(比如32位浮点数)转换为较低精度(比如8位整数)的过程。这一过程有助于减少模型的存储消耗和计算成本,并且在一定程度上可以提高模型的运行速度

y = float2int8(x * scale)
  • one_blob_only
param idnametypedefaultdescription
0scale_data_sizeint1
weighttypeshape
scale_datafloat[scale_data_size]

68.Reduction: 执行张量的降维操作

进行聚合操作或降维操作

y = reduce_op(x * coeff)
  • one_blob_only
param idnametypedefaultdescription
0operationint0
1reduce_allint1
2coefffloat1.f
3axesarray[ ]
4keepdimsint0
5fixbug0int0hack for bug fix, should be 1

Operation type:

  • 0 = SUM (求和):将张量中所有元素相加,得到一个标量值。
  • 1 = ASUM(绝对值求和): 将张量中所有元素的绝对值相加,得到一个标量值。
  • 2 = SUMSQ (平方和): 将张量中所有元素的平方相加,得到一个标量值。
  • 3 = MEAN (均值): 计算张量中所有元素的平均值,得到一个标量值
  • 4 = MAX (最大值): 找出张量中的最大值,并返回一个标量值。
  • 5 = MIN(最小值): 找出张量中的最小值,并返回一个标量值。
  • 6 = PROD(乘积):  计算张量中所有元素的乘积,得到一个标量值。
  • 7 = L1 (L1范数):计算张量中所有元素的L1范数(绝对值的和),得到一个标量值。
  • 8 = L2(L2范数): 计算张量中所有元素的L2范数(平方和后开根号),得到一个标量值。
  • 9 = LogSum(对数求和): 对张量中的元素取对数后相加,得到一个标量值。
  • 10 = LogSumExp对数指数求和): 对张量中的元素先分别取指数,再取对数后相加,得到一个标量值。

69.ReLU: 应用修正线性单元(ReLU)激活函数。

        ReLU函数对输入值进行处理,如果输入值小于零,则输出为零;如果输入值大于零,则输出与输入相同

if x < 0    y = x * slope
else        y = x
  • one_blob_only
  • support_inplace
param idnametypedefaultdescription
0slopefloat0.f

70.Reorg: 通道重排操作

将输入张量的通道重新排列,实现通道数变化和数据重组,从而满足特定的网络结构要求。通常情况下,Reorg操作会改变张量的通道数、高度和宽度,同时保持数据不变

if mode == 0    y = space_to_depth(x) where x channel order is sw-sh-outc
if mode == 1    y = space_to_depth(x) where x channel order is outc-sw-sh
  • one_blob_only
param idnametypedefaultdescription
0strideint1
1modeint0

71.Requantize: 重新量化(再量化)

就是对量化的数据进再量化,一般Quantize从f32 到 int8 ,Requantize 从int32 到int8

x2 = x * scale_in + bias
x3 = activation(x2)
y = float2int8(x3 * scale_out)
  • one_blob_only
param idnametypedefaultdescription
0scale_in_data_sizeint1
1scale_out_data_sizeint1
2bias_data_sizeint0
3activation_typeint0
4activation_paramsint[ ]
weighttypeshape
scale_in_datafloat[scale_in_data_size]
scale_out_datafloat[scale_out_data_size]
bias_datafloat[bias_data_size]

72.Reshape: 形状重塑操作

对输入数据进行形状重塑操作

操作通常用于调整神经网络中层的输入输出张量的形状,以适应不同层之间的连接需求或更改数据的维度

if permute == 1     y = hwc2chw(reshape(chw2hwc(x)))
else                y = reshape(x)
  • one_blob_only
param idnametypedefaultdescription
0wint-233
1hint-233
11dint-233
2cint-233
3permuteint0

Reshape flag:

  • 0 = copy from bottom (当维度值为0时,表示从底部(原始维度)复制维度值。换句话说,保留原始张量的相应维度值)
  • -1 = remaining (维度值为-1时,表示保持剩余的维度不变。这意味着在进行reshape操作时,会根据其他指定的维度值,自动计算并保持剩余的维度值)
  • -233 = drop this dim(default)(维度值为-233时,表示丢弃该维度。在进行reshape操作时,将会将指定维度值设为-233,这样就会将该维度丢弃,从而改变张量的形状)

73.RNN: 循环神经网络(RNN)层。

Apply a single-layer RNN to a feature sequence of T timesteps. The input blob shape is [w=input_size, h=T] and the output blob shape is [w=num_output, h=T].

将单层 RNN 应用于一个包含 T 个时间步的特征序列。输入的数据形状为 [w=input_size, h=T],输出的数据形状为 [w=num_output, h=T]。

y = rnn(x)
y0, hidden y1 = rnn(x0, hidden x1)
  • one_blob_only if bidirectional
param idnametypedefaultdescription
0num_outputint0hidden size of output
1weight_data_sizeint0total size of weight matrix
2directionint00=forward, 1=reverse, 2=bidirectional
weighttypeshape
weight_xc_datafloat/fp16/int8[input_size, num_output, num_directions]
bias_c_datafloat/fp16/int8[num_output, 1, num_directions]
weight_hc_datafloat/fp16/int8[num_output, num_output, num_directions]

Direction flag:

  • 0 = forward only 只允许向前移动
  • 1 = reverse only 只允许向后移动
  • 2 = bidirectional 允许双向移动

74.Scale: 缩放操作

        操作通常用于调整权重、偏置或特征图等参数的数值大小,以影响模型的学习效率、性能和收敛速度

if scale_data_size == -233  y = x0 * x1
else                        y = x * scale + bias
  • one_blob_only if scale_data_size != -233
  • support_inplace
param idnametypedefaultdescription
0scale_data_sizeint0
1bias_termint0
weighttypeshape
scale_datafloat[scale_data_size]
bias_datafloat[scale_data_size]

75.SELU: 应用自归一化激活函数

是一种激活函数。SELU激活函数最初由Hochreiter等人在2017年提出,被设计用于神经网络的隐藏层,与其他激活函数(如ReLU、sigmoid、tanh)相比,SELU具有一些独特的性质和优势。

\lambda = 1.0507 和 \alpha = 1.67326

SELU激活函数具有以下特点:

  1. 自归一化性质(self-normalizing): 在一定条件下,使用SELU激活函数可以使得神经网络自我归一化,有助于缓解梯度消失或爆炸问题,提高网络训练的稳定性。

  2. 非线性特性: SELU在激活过程中引入了非线性,有助于神经网络学习复杂的数据模式和特征。

  3. 稳定性和鲁棒性: SELU对于输入值的变化相对稳定,在一定程度上增强了网络的鲁棒性。

if x < 0    y = (exp(x) - 1.f) * alpha * lambda
else        y = x * lambda
  • one_blob_only
  • support_inplace
param idnametypedefaultdescription
0alphafloat1.67326324f
1lambdafloat1.050700987f

76.Shrink: 对输入数据进行收缩操作

操作通常用于减少量化后张量数据的尺寸,以便在神经网络计算中更有效地处理数据

if x < -lambd y = x + bias
if x >  lambd y = x - bias
else          y = x
  • one_blob_only
  • support_inplace
param idnametypedefaultdescription
0biasfloat0.0f
1lambdfloat0.5f

77.ShuffleChannel: 通道混洗操作

会将输入张量的通道进行重新排列,以改变数据的通道顺

  1. 将输入张量按照一定规则分割成若干个通道组。
  2. 对这些通道组进行重新排列。
  3. 将重新排列后的通道重新组合成最终的输出张量。
if reverse == 0     y = shufflechannel(x) by group
if reverse == 1     y = shufflechannel(x) by channel / group
  • one_blob_only
param idnametypedefaultdescription
0groupint1
1reverseint0

78.Sigmoid: 应用Sigmoid激活函数

它将任意实数映射到一个取值范围在 0 到 1 之间的实数

Sigmoid函数曾经被广泛用于隐藏层的激活函数,但后来由于存在梯度消失和饱和性的问题,逐渐被ReLU等激活函数取代

y = 1 / (1 + exp(-x))
  • one_blob_only
  • support_inplace

79.Slice: 分割操作

操作通常用于从输入张量中获取指定范围内的子张量或子数组。

Slice操作可以根据用户指定的起始索引和结束索引以及步长,从输入张量中提取出一个子张量。这个子张量通常是原始张量的一个子集,用于在神经网络中的特定层或模块中进一步处理

split x along axis into slices, each part slice size is based on slices array
param idnametypedefaultdescription
0slicesarray[ ]切片数组
1axisint0
2indicesarray[ ]        

80.Softmax: 应用Softmax激活函数,通常用于分类任务。

将模型的原始输出转换为表示概率分布的形式

softmax(x, axis)
  • one_blob_only
  • support_inplace
param idnametypedefaultdescription
0axisint0
1fixbug0int0hack for bug fix, should be 1
import torch
import torch.nn.functional as F

# 定义一个示例原始输出张量
logits = torch.tensor([2.0, 1.0, 0.1])

# 使用 torch.nn.functional.softmax 进行Softmax操作
probabilities = F.softmax(logits, dim=0)

# 打印转换后的概率分布
print("Softmax输出概率分布:")
print(probabilities)
# Softmax输出概率分布:
# tensor([0.6590, 0.2424, 0.0986])

81.Softplus: 应用Softplus激活函数。

softplus(x)=log(1+ex)

Softplus函数可以将输入的任何实数映射到一个大于零的实数范围内

Softplus函数的特点是它在输入值为负数时会接近于0,而在输入值为正数时会保持增长。与 ReLU 函数类似,Softplus函数也具有非线性特性,有助于增加神经网络的表达能力

y = log(exp(x) + 1)
  • one_blob_only
  • support_inplace
  • import torch
    import torch.nn.functional as F
    
    # 定义一个示例输入张量
    x = torch.tensor([-2.0, 0.0, 2.0])
    
    # 使用 torch.nn.functional.softplus 进行Softplus操作
    output = F.softplus(x)
    
    # 打印Softplus函数的输出
    print("Softplus输出:")
    print(output)
    # Softplus输出:
    # tensor([0.1269, 0.6931, 2.1269])

82.Split: 将输入数据分割为多个部分。

直接把输入数据复制多份,此处应该直接就是指针引用

y0, y1 ... = x

83.Swish: swish激活函数

应用Swish激活函数

y = x / (1 + exp(-x))
  • one_blob_only
  • support_inplace

84.TanH: TanH激活函数

应用双曲正切(tanh)激活函数

y = tanh(x)
  • one_blob_only
  • support_inplace

85.Threshold: 阈值操作

对输入数据应用阈值操作

if x > threshold    y = 1
else                y = 0
  • one_blob_only
  • support_inplace
param idnametypedefaultdescription
0thresholdfloat0.f

86.Tile: 重复复制

        是指在张量的维度上重复其内容以扩展张量的尺寸。重复操作允许您在指定的维度上复制张量中的数据,从而增加该维度的大小。

y = repeat tiles along axis for x
  • one_blob_only
param idnametypedefaultdescription
0axisint0
1tilesint1次数
2repeatsarray[ ]        

import torch

# 创建一个示例张量
x = torch.tensor([[1, 2],
                  [3, 4]])

# 定义参数
params = {"axis": 0, "tiles": 2, "repeats": [2, 1]}

# 获取参数值
axis = params["axis"]
tiles = params["tiles"]
repeats = params["repeats"]

# 在指定的轴上重复张量内容
y = x.repeat(repeats[0] if axis == 0 else 1, repeats[1] if axis == 1 else 1)

# 输出结果
print(y)
# tensor([[1, 2],
#         [3, 4],
#         [1, 2],
#         [3, 4]])

87.UnaryOp: 对输入执行一元操作。

一元操作通常涉及对输入进行转换、变换或提取特定信息,而不涉及多个输入之间的操作

y = unaryop(x)
  • one_blob_only
  • support_inplace
param idnametypedefaultdescription
0op_typeint0Operation type as follows

Operation type:

  • 0 = ABS(绝对值):返回输入的绝对值。
  • 1 = NEG(负值):返回输入的负值。
  • 2 = FLOOR(向下取整):返回不大于输入值的最大整数。
  • 3 = CEIL(向上取整):返回不小于输入值的最小整数
  • 4 = SQUARE(平方):返回输入值的平方。
  • 5 = SQRT(平方根):返回输入的平方根。
  • 6 = RSQ(倒数平方根):返回输入值的倒数的平方根。
  • 7 = EXP(指数):返回以 e 为底的输入值的指数。
  • 8 = LOG(对数):返回输入值的自然对数。
  • 9 = SIN(正弦):返回输入值的正弦值。
  • 10 = COS(余弦):返回输入值的余弦值。
  • 11 = TAN(正切):返回输入值的正切值。
  • 12 = ASIN(反正弦):返回输入值的反正弦值
  • 13 = ACOS(反余弦):返回输入值的反余弦值。
  • 14 = ATAN(反正切):返回输入值的反正切值。
  • 15 = RECIPROCAL(倒数):返回输入值的倒数。
  • 16 = TANH(双曲正切):返回输入值的双曲正切值。
  • 17 = LOG10(以10为底的对数):返回输入值的以10为底的对数。
  • 18 = ROUND(四舍五入):返回输入值四舍五入的结果。
  • 19 = TRUNC(截断):返回输入值的整数部分。

88.Unfold: 在输入数据上执行展开操作。

从一个批次的输入张量中提取出滑动的局部区域块

y = unfold(x)
  • one_blob_only
param idnametypedefaultdescription
0num_outputint0
1kernel_wint0
2dilation_wint1
3stride_wint1
4pad_leftint0
11kernel_hintkernel_w
12dilation_hintdilation_w
13stride_hintstride_w
14pad_topintpad_left
15pad_rightintpad_left
16pad_bottomintpad_top

import torch

# 创建一个3x3的张量作为示例输入
input_tensor = torch.tensor([[1, 2, 3],
                             [4, 5, 6],
                             [7, 8, 9]])

# 在第一个维度上展开,窗口大小为2,步长为1
unfolded_tensor = input_tensor.unfold(0, 2, 1)

print('Input Tensor:\n', input_tensor)
# tensor([[1, 2, 3],
#         [4, 5, 6],
#         [7, 8, 9]])
print('Unfolded Tensor:\n', unfolded_tensor,"\nshape:",unfolded_tensor.shape)
# tensor([[[1, 4],
#          [2, 5],
#          [3, 6]],
#
#         [[4, 7],
#          [5, 8],
#          [6, 9]]])
# shape: torch.Size([2, 3, 2])

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/596704.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

Go 语言(四)【常用包使用】

1、命令行参数包 flag flag 包就是一个用来解析命令行参数的工具。 1.1、os.Args import ("fmt""os" )func main() {if len(os.Args) > 0 {for index, arg : range os.Args {fmt.Printf("args[%d]%v\n", index, arg)}} } 运行结果&#…

【Docker】docker部署lnmp和搭建wordpress网站

环境准备 docker&#xff1a;192.168.67.30 虚拟机&#xff1a;4核4G systemctl stop firewalld systemctl disable firewalld setenforce 0 安装docker #安装依赖包 yum -y install yum-utils device-mapper-persistent-data lvm2 #设置阿里云镜像 yum-config-manager --add…

MTEB - Embedding 模型排行榜

文章目录 关于 MTEBMTEB 任务和数据集概览使用 MTEB Pythont 库Installation使用 关于 MTEB MTEB : Massive Text Embedding Benchmark github : https://github.com/embeddings-benchmark/mtebhuggingface : https://huggingface.co/spaces/mteb/leaderboardpaper : https:/…

全国31省对外开放程度、经济发展水平、ZF干预程度指标数据(2000-2022年)

01、数据介绍 自2000年至2022年&#xff0c;中国的对外开放程度不断深化、经济发展水平不断提高、ZF不断探索并调整自身在经济运行中的角色和定位&#xff0c;以更好地适应国内外环境的变化&#xff0c;也取得了举世瞩目的成就。这一期间&#xff0c;中国积极融入全球经济体系…

书籍推荐|经典书籍ic书籍REUSE METHODOLOGY MANUALFOR等和verilog网站推荐(附下载)

大家好&#xff0c;今天是51过后的第一个工作日&#xff0c;想必大家都还没有完全从节假日的吃喝玩乐模式转变为勤勤恳恳的打工人模式&#xff0c;当然也包括我&#xff0c;因此这次更新主要是分享几篇书籍和verilog相关的学习网站~ 首先是一本数字电路相关的基础书籍&#xf…

JavaScript 中的 Class 类

&#x1f525; 引言 在ECMAScript 2015&#xff08;ES6&#xff09;中&#xff0c;class 关键字被引入&#xff0c;为JavaScript带来了一种更接近传统面向对象语言的语法糖。类是创建对象的模板&#xff0c;它们封装了数据&#xff08;属性&#xff09;和行为&#xff08;方法&…

【SpringMVC 】什么是SpringMVC(一)?如何创建一个简单的springMvc应用?

文章目录 SpringMVC第一章1、什么是SpringMVC2、创建第一个SpringMVC的应用1-3步第4步第5步第6步7-8步3、基本语法1、进入控制器类的方式方式1:方式2:方式3:方式4:方式5:2、在控制器类中取值的方式方式1:方式2:方式3:方式4:方式5:方式6:超链接方式7:日期方式8:aja…

第一天学习(GPT)

1.图片和语义是如何映射的&#xff1f; **Dalle2&#xff1a;**首先会对图片和语义进行预训练&#xff0c;将二者向量存储起来&#xff0c;然后将语义的vector向量转成图片的向量&#xff0c;然后基于这个图片往回反向映射&#xff08;Diffusion&#xff09;——>根据这段描…

云原生周刊:Terraform 1.8 发布 | 2024.5.6

开源项目推荐 xlskubectl 用于控制 Kubernetes 集群的电子表格。xlskubectl 将 Google Spreadsheet 与 Kubernetes 集成。你可以通过用于跟踪费用的同一电子表格来管理集群。 git-sync git-sync 是一个简单的命令&#xff0c;它将 git 存储库拉入本地目录&#xff0c;等待一…

深度神经网络中的不确定性研究综述

A.单一确定性方法 对于确定性神经网络&#xff0c;参数是确定的&#xff0c;每次向前传递的重复都会产生相同的结果。对于不确定性量化的单一确定性网络方法&#xff0c;我们总结了在确定性网络中基于单一正向传递计算预测y *的不确定性的所有方法。在文献中&#xff0c;可以找…

如何取消xhr / fetch / axios请求

如何取消xhr请求 setTimeout(() > { xhr.abort() }, 1000)如何取消fetch请求 fetch()请求发送以后&#xff0c;如果中途想要取消&#xff0c;需要使用AbortController对象。 let controller new AbortController(); let signal controller.signal;fetch(url, {signal:…

[激光原理与应用-92]:振镜的光路图原理

目录 一、振镜的光路 二、振镜的工作原理 2.1 概述 2.2 焊接头 2.3 准直聚焦头-直吹头 2.4 准直聚焦头分类——按应用分 2.4.1 准直聚焦头分类——功能分类 2.4.2 准直聚焦头镜片 2.4.3 振镜焊接头 2.4.4 振镜分类&#xff1a; 2.4.5 动态聚焦系统演示&#xff08;素…

vivado Virtex 和 Kintex UltraScale+ 比特流设置

下表所示 Virtex 和 Kintex UltraScale 器件的器件配置设置可搭配 set_property <Setting> <Value> [current_design] Vivado 工具 Tcl 命令一起使用。

Python使用割圆法求π值

三国时期刘徽提出的割圆法有多牛掰&#xff0c;看这个&#xff1a;刘徽割圆术到底做了什么&#xff1f; - 知乎 用Python实现的该算法代码如下&#xff1a; #!/usr/bin/env python """使用割圆法计算π值Usage::$ python calc_circle_pi.py 20 # 参数20是迭代…

ArthasGC日志GCeasy详解

Arthas详解 Arthas是阿里巴巴在2018年9月开源的Java诊断工具,支持JDK6,采用命令行交互模式,可以方便定位和诊断线上程序运行问题.Arthas官方文档十分详细.详见:官方文档 Arthas使用场景 Arthas使用 # github下载arthas wget https://alibaba.github.io/arthas/arthas-boot.j…

uniapp 禁止截屏(应用内,保护隐私)插件 Ba-ScreenShot

禁止截屏&#xff08;应用内&#xff0c;保护隐私&#xff09; Ba-ScreenShot 简介&#xff08;下载地址&#xff09; Ba-ScreenShot 是一款uniapp禁止应用内截屏的插件&#xff0c;保护隐私&#xff0c;支持禁止截屏、放开截屏 截图展示 也可关注博客&#xff0c;实时更新最…

嵌入式学习——C语言基础——day14

1. 共用体 1.1 定义 union 共用名 { 数据类型1 成员变量1; 数据类型2 成员变量2; 数据类型3 成员变量3; .. }; 1.2 共用体和结构体的区别 1. 结构体每个成员变量空间独立 2. 共用体每个成员变量空间共享 1.3 判断内存大小端 1. 内存大端…

Ranni: Taming Text-to-Image Diffusion for Accurate Instruction Following

Ranni: Taming Text-to-Image Diffusion for Accurate Instruction Following abstract 我们引入了一个语义面板作为解码文本到图像的中间件&#xff0c;支持生成器更好地遵循指令 Related work 最近的工作还通过包含额外的条件&#xff08;如补全掩码[15&#xff0c;45]、…

天猫商品搜索API返回值说明:关键字搜索如何精准定位商品,精准定位,一键直达!

通过天猫商品搜索API&#xff0c;关键词搜索不再是难题。精准定位&#xff0c;快速找到您心仪的商品&#xff0c;开启便捷购物新时代。掌握API返回值的奥秘&#xff0c;让您的搜索更智能、更高效&#xff01; 天猫商品搜索API&#xff08;如item_search&#xff09;的返回值设计…

xyctf(write up)

ezhttp 因为是一道http的题&#xff0c;前端代码没有什么有效信息&#xff0c;但提示说密码在某个地方&#xff0c;我们用robots建立一个robots.txt文件来看有哪个文件可以访问 补充知识&#xff1a;http请求中via字段表示从哪个网址的服务器代理而来&#xff0c;user-agent表…
最新文章