ncnn 算子操作描述

ncnn 算子操作描述,具體查詢見

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

都是從上述地方copy過來的,做備份。

具體如下:(針對有些算子 用pytorch 實現了用例,可以對比學習,如有錯誤歡迎指出)

內容索引

1.AbsVal: 計算輸入張量中的每個元素的絕對值

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

3.BatchNorm: 對神經網絡的每一層進行歸一化操作

4.Bias: 為神經網絡的神經元或層添加偏置項

5.BinaryOp: 二元操作

6.BNLL: 對輸入應用 BNLL 激活函數

7.Cast: 類型轉換

8.CELU: 應用 CELU 激活函數。

9.Clip: 將輸入張量中的元素限制在指定范圍內。

10.Concat: 沿指定軸連接多個輸入張量。

11.Convolution: 卷積操作

12.Convolution1D:一維卷積

13.Convolution3D:三維卷積

14.ConvolutionDepthWise: 深度可分離卷積

15.ConvolutionDepthWise1D: 在一維數據上應用深度可分離卷積

16.ConvolutionDepthWise3D: 在三維數據上應用深度可分離卷積

17.CopyTo: 將輸入數據復制到指定位置

18.Crop: 裁剪操作

19.CumulativeSum: 對輸入數據進行累積求和操作。

20.Deconvolution: 反卷積操作

21.Deconvolution1D: 一維反卷積操作

22.Deconvolution3D: 三維反卷積操作

23.DeconvolutionDepthWise: 深度可分離反卷積

24.DeconvolutionDepthWise1D: 在一維數據上應用深度可分離反卷積

25.DeconvolutionDepthWise3D: 三維深度可分離反卷積

26.DeformableConv2D: 可變形卷積,允許卷積核在空間上變形

27.Dequantize: 對量化后的數據進行反量化操作

28.Diag: 創建一個對角陣

29.Dropout: 隨機失活

30.Eltwise: 逐元素操作

31.ELU: 應用指數線性單元(ELU)激活函數

32.Embed: 將輸入數據映射到低維空間

33.Exp: 計算輸入數據的指數

34.Flatten: 將輸入數據展平為一維

35.Fold: 折疊操作

36.GELU: 應用高斯誤差線性單元(GELU)激活函數

37.GLU: 應用門控線性單元(GLU)激活函數

38.Gemm: 執行矩陣乘法操作

39.GridSample: 在輸入的網格上進行采樣操作

40.GroupNorm: 對神經網絡中的特征圖執行分組歸一化

41.GRU: 門控循環單元(GRU)神經網絡層

42.HardSigmoid: 應用硬Sigmoid激活函數

43.HardSwish: 應用硬Swish激活函數

44.InnerProduct: 執行全連接操作

45.Input: 神經網絡的輸入層

46.InstanceNorm: 歸一化操作

47.Interp: 執行插值操作

48.LayerNorm: 對神經網絡中的層執行歸一化操作

49.Log: 計算輸入數據的自然對數

50.LRN: 局部響應歸一化層

51.LSTM: 長短期記憶(LSTM)神經網絡層

52.MemoryData: 用于存儲數據并生成數據迭代器

53.Mish: 應用Mish激活函數

54.MultiHeadAttention: 多頭注意力機制

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

56.Noop: 空操作

57.Normalize: 歸一化操作

58.Packing: 打包操作

59.Padding: 填充操作

60.Permute: 置換操作

61.PixelShuffle: 像素重組

62.Pooling: 池化操作

63.Pooling1D: 一維池化操作

64.Pooling3D: 三維池化操作

65.Power: 冪運算

66.PReLU: 參數化修正線性單元

67.Quantize: 量化操作

68.Reduction: 執行張量的降維操作

69.ReLU: 應用修正線性單元(ReLU)激活函數。

70.Reorg: 通道重排操作

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

72.Reshape: 形狀重塑操作

73.RNN: 循環神經網絡(RNN)層。

74.Scale: 縮放操作

75.SELU: 應用自歸一化激活函數

76.Shrink: 對輸入數據進行收縮操作

77.ShuffleChannel: 通道混洗操作

78.Sigmoid: 應用Sigmoid激活函數

79.Slice: 分割操作

80.Softmax: 應用Softmax激活函數,通常用于分類任務。

81.Softplus: 應用Softplus激活函數。

82.Split: 將輸入數據分割為多個部分。

83.Swish: swish激活函數

84.TanH: TanH激活函數

85.Threshold: 閾值操作

86.Tile: 重復復制

87.UnaryOp: 對輸入執行一元操作

88.Unfold: 在輸入數據上執行展開操作



1.AbsVal: 計算輸入張量中的每個元素的絕對值。

y = abs(x)
  • one_blob_only? 只支持一個blob
  • support_inplace? 支持替換輸入的blob 就 y=abs(y)
import torchinput_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 torchinput_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 nnbatch_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 torchinput_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 torchinput_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 Finput_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 torchinput_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 torchinput_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 nnconv_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 nnconv_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 nnconv_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 nnconv_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的輸出上應用Dropoutx = 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 Finp = 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 nnclass 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?雙三次插值?雙三次插值是一種更復雜的插值方法,它會在目標圖像的像素周圍選擇16個像素進行加權平均,以生成新像素的值。這種方法在保留圖像細節的同時,也會增加計算復雜度
  • 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 nnclass LRN(nn.Module):def __init__(self, size=5, alpha=1e-4, beta=0.75, k=1.0):super(LRN, self).__init__()self.size = sizeself.alpha = alphaself.beta = betaself.k = kdef 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 * pooloutput = 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 partxq = affine(q) / (embed_dim / num_head)xk = affine(k)xv = affine(v)xqk = xq * xkxqk = xqk + attn_mask if attn_mask existssoftmax_inplace(xqk)xqkv = xqk * xvmerge 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-次數)

默認:NCHW 格式? DCHW

如下:WH代表二維,WHC代表三維替換,WHDC代表4維,最高支持4維,最低2維

  • 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.pswp.cn/web/10110.shtml
繁體地址,請注明出處:http://hk.pswp.cn/web/10110.shtml
英文地址,請注明出處:http://en.pswp.cn/web/10110.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Java學習筆記網站技術博客匯總

江南一點雨網站 https://www.javaboy.org/ 個人博客 https://yanbingzn.github.io/ CS-Notes 面試筆記 https://www.cyc2018.xyz/ JavaGuide&#xff08;Java學習&面試指南&#xff09; https://javaguide.cn/home.html SpringMVC 教程 https://springmvc.javaboy…

C++運算符重載(操作符重載)

運算符重載 1. 運算符重載基礎1.1 運算符重載語法1.2 運算符重載細節補充1.3 更多的運算符重載 2. 重載單目運算符3. 如何直接輸入輸出對象類型——重載運算符 << 和 >>3.1 單個對象實現 cou <<3.2 多個對象實現 cout<<3.3 右移運算符 輸入 cin >&g…

Excel-VBA報錯01-解決方法

【已刪除的部件:部件/xl/vbaProject.bin。(Visual Basic for Applications(VBA))】 1.問題復現&#xff1a; Win10 &#xff1b;64位 &#xff1b;Office Excel 2016 打開帶有宏的Excel文件&#xff0c;報錯&#xff1a;【已刪除的部件&#xff1a;部件/xl/vbaProject.bin。…

KBU1010-ASEMI新能源專用KBU1010

編輯&#xff1a;ll KBU1010-ASEMI新能源專用KBU1010 型號&#xff1a;KBU1010 品牌&#xff1a;ASEMI 封裝&#xff1a;KBU-4 最大重復峰值反向電壓&#xff1a;1000V 最大正向平均整流電流(Vdss)&#xff1a;10A 功率(Pd)&#xff1a;中小功率 芯片個數&#xff1a;4…

pandas數據清洗和統計實例

步驟&#xff1a; 統計每一個列的標簽個數去除或者填充某一列NaN值遍歷某一列分組統計在DataFrame中插入行在DataFrame中追加行 pandas讀取Json數據或csv數據 以一個json數據為例&#xff0c;只要json每一個object都一致就可以&#xff1a; # 讀取json或csv df_f pd.read_…

抖店選品都怎么選品?什么樣的產品更吸引人,更具有購買力?

大家好&#xff0c;我是電商花花。 抖店選品一直都是我們無貨源商家的核心問題&#xff0c;不管是出單、還是爆單&#xff0c;店鋪想要有銷量的前提下都是選品。 很多人一上來就是就是選品&#xff0c;沒有選品經驗還瞎選品&#xff0c;結果到最后選了一堆出單的產品&#xf…

回聲消除原理

回聲消除原理 回聲消除是一種音頻處理技術&#xff0c;用于消除聲學空間中發生的回聲。其基本原理如下&#xff1a; 1. 遠端信號估計 捕獲遠端揚聲器發出的信號&#xff08;通常通過麥克風&#xff09;。使用自適應濾波器估計遠端信號&#xff0c;即回聲信號。 2. 回聲信號…

用wordpress建外貿獨立站的是主流的外貿建站方式

WordPress因其易用性、靈活性和強大的功能支持&#xff0c;成為了外貿企業首選的網站建設平臺。 從技術和功能角度來看&#xff0c;WordPress提供了豐富的主題和插件&#xff0c;這些都是構建專業外貿網站所必需的。例如&#xff0c;有專門為外貿網站設計的主題和插件&#xf…

【棧】Leetcode 驗證棧序列

題目講解 946. 驗證棧序列 算法講解 在這里就只需要模擬一下這個棧的出棧順序即可&#xff1a;使用一個stack&#xff0c;每次讓pushed里面的元素入棧&#xff0c;如果當前棧頂的元素等于poped容器中的當前元素&#xff0c;因此就需要讓棧頂元素出棧&#xff0c;poped的遍歷…

一篇文章幫你搞定微軟云計算證書Renew

IT證書都有過期的時間&#xff0c;像AWS是3年有效期&#xff0c;谷歌是2年&#xff0c;微軟是1年&#xff0c;那這些證書到期該怎么Renew更新呢&#xff1f; 小李哥最近的微軟AZ-204證書要到期了&#xff0c;到期前半年就會收到Microsoft發來提醒郵件。大家在這半年內任何時間都…

500行代碼實現貪吃蛇(2)

文章目錄 3. 貪吃蛇的具體實現 3. 貪吃蛇的具體實現 首先&#xff0c;我們要讓整個程序適應本地化 int main() {//修改適配本地中文環境setlocale(LC_ALL, "");return 0; }蛇身節點的創建 //蛇身結點的定義 typedef struct SnakeNode {int x; int y;struct SnakeN…

Unity 性能優化之LOD技術(十)

提示&#xff1a;僅供參考&#xff0c;有誤之處&#xff0c;麻煩大佬指出&#xff0c;不勝感激&#xff01; 文章目錄 LOD技術效果一、LOD技術是什么&#xff1f;二、LODGroup組件介紹三、LODGroup組件使用步驟添加組件添加模型 四、Project Settings中與LOD組件相關參數總結 L…

【DevOps】Linux 內核網絡子系統全面指南與性能調優

目錄 一、Linux 內核網絡子系統 1. Netfilter 主要特性 工作流程 2. Traffic Control (TC) 主要特性 工作流程 3. Socket 主要特性 工作流程 二、內核參數優化 1. net.ipv4.tcp_window_scaling 2. net.core.netdev_max_backlog 3. net.ipv4.tcp_rmem 和 net.ipv4…

第33次CSP認證Q1:詞頻統計

&#x1f344;題目描述 在學習了文本處理后&#xff0c;小 P 對英語書中的 &#x1d45b;n 篇文章進行了初步整理。 具體來說&#xff0c;小 P 將所有的英文單詞都轉化為了整數編號。假設這 &#x1d45b;n 篇文章中共出現了 &#x1d45a;m 個不同的單詞&#xff0c;則把它們…

網絡編程套接字 (二)---udosocket

本專欄內容為&#xff1a;Linux學習專欄&#xff0c;分為系統和網絡兩部分。 通過本專欄的深入學習&#xff0c;你可以了解并掌握Linux。 &#x1f493;博主csdn個人主頁&#xff1a;小小unicorn ?專欄分類&#xff1a;網絡 &#x1f69a;代碼倉庫&#xff1a;小小unicorn的代…

C++學習第二十九課:C++ 輸入輸出流詳解:從基礎到高級應用

在 C 中&#xff0c;流&#xff08;stream&#xff09;是一種用于實現輸入輸出操作的抽象概念。流可以看作是字節的流動&#xff0c;這些字節可以從一個地方流向另一個地方&#xff0c;例如從鍵盤輸入到程序中&#xff0c;或者從程序輸出到屏幕。C 提供了一套完整的流庫來處理各…

Qt中正則表達式的用法

一.基本功能介紹&#xff1a; 在Qt中&#xff0c;可以使用QRegularExpression類來處理正則表達式。以下是一些常用的正則表達式用法&#xff1a; 1.創建正則表達式對象&#xff1a; QRegularExpression regex("pattern"); 2.匹配字符串&#xff1a; QString str …

LeetCode 2960.統計已測試設備:看測試了多少次

【LetMeFly】2960.統計已測試設備&#xff1a;看測試了多少次 力扣題目鏈接&#xff1a;https://leetcode.cn/problems/count-tested-devices-after-test-operations/ 給你一個長度為 n 、下標從 0 開始的整數數組 batteryPercentages &#xff0c;表示 n 個設備的電池百分比…

【QEMU系統分析之實例篇(十七)】

系列文章目錄 第十七章 QEMU系統仿真的機器創建分析實例 文章目錄 系列文章目錄第十七章 QEMU系統仿真的機器創建分析實例 前言一、QEMU是什么&#xff1f;二、QEMU系統仿真的機器創建分析實例1.系統仿真的命令行參數2.配置加速器configure_accelerators()do_configure_icount…

藍橋杯13屆JAVA A組 國賽

??????? package 藍橋杯國賽; // 貪心選個數最少的進行擺 // 2:1 ,3:1, 4:1,5 : 3,6:3,7:1 // 選 1&#xff0c;7&#xff0c;4&#xff0c;2&#xff0c;3&#xff0c;5&#xff0c;9 // 然后都選滿10個 public class 火彩棒數字 {public static void main(String[] a…