Pytroch nn.Unfold() 与 nn.Fold()图码详解

这篇具有很好参考价值的文章主要介绍了Pytroch nn.Unfold() 与 nn.Fold()图码详解。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

Unfold()与Fold()的用途

Unfold()Fold()一般成对出现。常用用途有:

  1. 代替卷积计算,Unfold()Fold()不互逆(参数不一样)(卷积本来就不可逆)
  2. 图片patch化,Unfold()Fold()互逆(参数一样,且滑动窗不重叠)

nn.Unfold()

Extracts sliding local blocks from a batched input tensor.
在各滑动窗中按行展开(行向量化),然后转置成列向量, im2col 的批量形式

input : (N, C, ∗)
output : (N, C × ∏(kernel_size), L)

# 滑动窗口有重叠
unfold = nn.Unfold(kernel_size=(2, 3))
input = torch.randn(2, 5, 3, 4)
print("input: \n", input)
output = unfold(input)
# each patch contains 30 values (2x3=6 vectors, each of 5 channels)
# 4 blocks (2x3 kernels) in total in the 3x4 input
# output.size()  # torch.Size([2, 30, 4])
print("output: \n", output)

fold = nn.Fold((3,4),(2,3))
fold_output = fold(output)
print("fold_output: \n", fold_output)

输出为:

input: 
 tensor([[[[ 0.4198,  1.0535,  0.1152,  0.3510],
          [ 1.1664,  0.3376,  1.2207,  0.3575],
          [-0.2174, -1.2490,  0.3432,  0.3388]],

         [[-1.4956,  0.9746, -0.5145,  0.1722],
          [ 1.7041,  0.9645, -0.6937, -1.9037],
          [-0.1961, -0.3345,  0.3565, -1.2329]],

         [[-0.9843, -0.8089,  1.8712, -0.2860],
          [ 0.0960, -1.7501, -0.1226,  0.9383],
          [-0.1675,  1.1498, -0.4958, -1.2953]],

         [[-1.2368,  0.5667,  1.4166, -2.2567],
          [ 0.9414,  0.8189,  1.5604,  0.1422],
          [-1.6414, -1.5594,  0.6718,  1.2319]],

         [[-0.4093,  0.6691,  1.4003,  0.7444],
          [-0.2858, -0.4375, -1.1301,  0.7377],
          [-0.0956, -0.1844,  0.7697, -0.3077]]],


        [[[ 0.4264, -0.0700, -1.5600, -0.0491],
          [ 1.5027,  3.1625,  0.6080, -1.8794],
          [-0.3148,  0.6377, -0.7242,  0.1692]],

         [[ 0.2757, -0.5403,  0.7748, -1.1795],
          [ 0.1504, -0.4671,  0.9355,  1.3050],
          [-0.4920, -0.8581,  0.0559, -0.0446]],

         [[ 2.1627,  0.6758, -0.0968,  1.3401],
          [-0.1105,  0.8299, -0.3827, -1.0687],
          [-0.2234, -1.0423,  1.2436, -0.6514]],

         [[ 0.8085, -0.4159,  0.2022,  0.5747],
          [-0.1265,  0.2828, -1.3530,  0.2831],
          [-0.1571,  0.9005,  0.4556, -1.4360]],

         [[-1.2417,  0.1829,  0.3825, -0.8555],
          [-2.0170,  0.7537,  2.3406,  0.5866],
          [-1.1704, -1.8986, -0.7958,  0.2652]]]])
output: 
 tensor([[[ 0.4198,  1.0535,  1.1664,  0.3376],
         [ 1.0535,  0.1152,  0.3376,  1.2207],
         [ 0.1152,  0.3510,  1.2207,  0.3575],
         [ 1.1664,  0.3376, -0.2174, -1.2490],
         [ 0.3376,  1.2207, -1.2490,  0.3432],
         [ 1.2207,  0.3575,  0.3432,  0.3388],
         [-1.4956,  0.9746,  1.7041,  0.9645],
         [ 0.9746, -0.5145,  0.9645, -0.6937],
         [-0.5145,  0.1722, -0.6937, -1.9037],
         [ 1.7041,  0.9645, -0.1961, -0.3345],
         [ 0.9645, -0.6937, -0.3345,  0.3565],
         [-0.6937, -1.9037,  0.3565, -1.2329],
         [-0.9843, -0.8089,  0.0960, -1.7501],
         [-0.8089,  1.8712, -1.7501, -0.1226],
         [ 1.8712, -0.2860, -0.1226,  0.9383],
         [ 0.0960, -1.7501, -0.1675,  1.1498],
         [-1.7501, -0.1226,  1.1498, -0.4958],
         [-0.1226,  0.9383, -0.4958, -1.2953],
         [-1.2368,  0.5667,  0.9414,  0.8189],
         [ 0.5667,  1.4166,  0.8189,  1.5604],
         [ 1.4166, -2.2567,  1.5604,  0.1422],
         [ 0.9414,  0.8189, -1.6414, -1.5594],
         [ 0.8189,  1.5604, -1.5594,  0.6718],
         [ 1.5604,  0.1422,  0.6718,  1.2319],
         [-0.4093,  0.6691, -0.2858, -0.4375],
         [ 0.6691,  1.4003, -0.4375, -1.1301],
         [ 1.4003,  0.7444, -1.1301,  0.7377],
         [-0.2858, -0.4375, -0.0956, -0.1844],
         [-0.4375, -1.1301, -0.1844,  0.7697],
         [-1.1301,  0.7377,  0.7697, -0.3077]],

        [[ 0.4264, -0.0700,  1.5027,  3.1625],
         [-0.0700, -1.5600,  3.1625,  0.6080],
         [-1.5600, -0.0491,  0.6080, -1.8794],
         [ 1.5027,  3.1625, -0.3148,  0.6377],
         [ 3.1625,  0.6080,  0.6377, -0.7242],
         [ 0.6080, -1.8794, -0.7242,  0.1692],
         [ 0.2757, -0.5403,  0.1504, -0.4671],
         [-0.5403,  0.7748, -0.4671,  0.9355],
         [ 0.7748, -1.1795,  0.9355,  1.3050],
         [ 0.1504, -0.4671, -0.4920, -0.8581],
         [-0.4671,  0.9355, -0.8581,  0.0559],
         [ 0.9355,  1.3050,  0.0559, -0.0446],
         [ 2.1627,  0.6758, -0.1105,  0.8299],
         [ 0.6758, -0.0968,  0.8299, -0.3827],
         [-0.0968,  1.3401, -0.3827, -1.0687],
         [-0.1105,  0.8299, -0.2234, -1.0423],
         [ 0.8299, -0.3827, -1.0423,  1.2436],
         [-0.3827, -1.0687,  1.2436, -0.6514],
         [ 0.8085, -0.4159, -0.1265,  0.2828],
         [-0.4159,  0.2022,  0.2828, -1.3530],
         [ 0.2022,  0.5747, -1.3530,  0.2831],
         [-0.1265,  0.2828, -0.1571,  0.9005],
         [ 0.2828, -1.3530,  0.9005,  0.4556],
         [-1.3530,  0.2831,  0.4556, -1.4360],
         [-1.2417,  0.1829, -2.0170,  0.7537],
         [ 0.1829,  0.3825,  0.7537,  2.3406],
         [ 0.3825, -0.8555,  2.3406,  0.5866],
         [-2.0170,  0.7537, -1.1704, -1.8986],
         [ 0.7537,  2.3406, -1.8986, -0.7958],
         [ 2.3406,  0.5866, -0.7958,  0.2652]]])
fold_output: 
 tensor([[[[ 0.4198,  2.1070,  0.2304,  0.3510],
          [ 2.3328,  1.3502,  4.8828,  0.7150],
          [-0.2174, -2.4979,  0.6865,  0.3388]],

         [[-1.4956,  1.9493, -1.0290,  0.1722],
          [ 3.4083,  3.8582, -2.7747, -3.8074],
          [-0.1961, -0.6690,  0.7129, -1.2329]],

         [[-0.9843, -1.6178,  3.7424, -0.2860],
          [ 0.1920, -7.0003, -0.4904,  1.8766],
          [-0.1675,  2.2995, -0.9917, -1.2953]],

         [[-1.2368,  1.1334,  2.8331, -2.2567],
          [ 1.8829,  3.2758,  6.2418,  0.2843],
          [-1.6414, -3.1189,  1.3435,  1.2319]],

         [[-0.4093,  1.3382,  2.8006,  0.7444],
          [-0.5717, -1.7500, -4.5204,  1.4754],
          [-0.0956, -0.3688,  1.5395, -0.3077]]],


        [[[ 0.4264, -0.1399, -3.1201, -0.0491],
          [ 3.0053, 12.6500,  2.4319, -3.7589],
          [-0.3148,  1.2753, -1.4484,  0.1692]],

         [[ 0.2757, -1.0806,  1.5497, -1.1795],
          [ 0.3007, -1.8684,  3.7421,  2.6100],
          [-0.4920, -1.7162,  0.1119, -0.0446]],

         [[ 2.1627,  1.3515, -0.1936,  1.3401],
          [-0.2210,  3.3198, -1.5307, -2.1373],
          [-0.2234, -2.0846,  2.4872, -0.6514]],

         [[ 0.8085, -0.8318,  0.4044,  0.5747],
          [-0.2529,  1.1310, -5.4121,  0.5662],
          [-0.1571,  1.8010,  0.9112, -1.4360]],

         [[-1.2417,  0.3659,  0.7650, -0.8555],
          [-4.0339,  3.0146,  9.3626,  1.1733],
          [-1.1704, -3.7972, -1.5917,  0.2652]]]])

Unfold()与Fold() 变化模式图解

以上面代码输出为例,其实是以如下的格式对原数据进行组织排列的:
在各滑动窗中按行展开(行向量化),然后转置成列向量, 是im2col的批量形式。
Pytroch nn.Unfold() 与 nn.Fold()图码详解
Pytroch nn.Unfold() 与 nn.Fold()图码详解
然后,对Unfold()的结果以相同参数运用Fold()后(Fold()的讲解在下面,这里先给出结果),结果如下:
Pytroch nn.Unfold() 与 nn.Fold()图码详解

nn.Fold()

nn.Fold() 是 nn.Unfold() 函数的逆操作。 (参数相同、滑动窗口没有重叠的情况下,可以完全恢复【真互逆】。滑动窗口有重叠情况下不能恢复到Unfold的输入)

需要注意的是,如果滑动窗口有重叠,那么重叠部分相加【倍数关系】。同时,如果原来的图像不够划分的话就会舍去。在恢复时就会以 0 填充

单通道 滑动窗口无重叠

# 单通道  滑动窗口无重叠
import torch.nn as nn
import torch
 
batches_img = torch.rand(1,1,6,6)
print("batches_img: ",batches_img)
 
unfold = nn.Unfold(kernel_size=(3,3),stride=3)
patche_img = unfold(batches_img)
print("patche_img.shape: ",patche_img.shape)
print(patche_img)
 
fold = torch.nn.Fold(output_size=(6, 6), kernel_size=(3, 3), stride=3)
inputs_restore = fold(patche_img)
print("inputs_restore:", inputs_restore)

输出:

batches_img:  tensor([[[[0.0174, 0.3919, 0.0073, 0.4660, 0.6537, 0.0584],
          [0.9763, 0.9982, 0.6250, 0.1332, 0.2123, 0.9500],
          [0.5482, 0.4291, 0.9430, 0.6837, 0.6975, 0.1992],
          [0.5275, 0.6800, 0.0490, 0.0350, 0.8571, 0.2449],
          [0.3719, 0.7484, 0.7677, 0.4164, 0.2151, 0.8875],
          [0.0784, 0.3839, 0.7567, 0.4217, 0.3208, 0.3025]]]])
patche_img.shape:  torch.Size([1, 9, 4])
tensor([[[0.0174, 0.4660, 0.5275, 0.0350],
         [0.3919, 0.6537, 0.6800, 0.8571],
         [0.0073, 0.0584, 0.0490, 0.2449],
         [0.9763, 0.1332, 0.3719, 0.4164],
         [0.9982, 0.2123, 0.7484, 0.2151],
         [0.6250, 0.9500, 0.7677, 0.8875],
         [0.5482, 0.6837, 0.0784, 0.4217],
         [0.4291, 0.6975, 0.3839, 0.3208],
         [0.9430, 0.1992, 0.7567, 0.3025]]])
inputs_restore: tensor([[[[0.0174, 0.3919, 0.0073, 0.4660, 0.6537, 0.0584],
          [0.9763, 0.9982, 0.6250, 0.1332, 0.2123, 0.9500],
          [0.5482, 0.4291, 0.9430, 0.6837, 0.6975, 0.1992],
          [0.5275, 0.6800, 0.0490, 0.0350, 0.8571, 0.2449],
          [0.3719, 0.7484, 0.7677, 0.4164, 0.2151, 0.8875],
          [0.0784, 0.3839, 0.7567, 0.4217, 0.3208, 0.3025]]]])

模拟图片数据(b,3,9,9),通道数 C 为3,滑动窗口无重叠。

相较于上面的代码,变化仅此

# 模拟图片数据(b,3,9,9),通道数 C 为3,滑动窗口无重叠。 相较于上面的代码,变化仅此
import torch.nn as nn
import torch
 
batches_img = torch.rand(1,3,6,6)
print("batches_img: ",batches_img)
 
unfold = nn.Unfold(kernel_size=(3,3),stride=3)
patche_img = unfold(batches_img)
print("patche_img.shape: ",patche_img.shape)
print(patche_img)
 
fold = torch.nn.Fold(output_size=(6, 6), kernel_size=(3, 3), stride=3)
inputs_restore = fold(patche_img)
print("inputs_restore:", inputs_restore)

输出为:

batches_img:  tensor([[[[0.6072, 0.9496, 0.4149, 0.1085, 0.6808, 0.3949],
          [0.9770, 0.4831, 0.3964, 0.6597, 0.1749, 0.7326],
          [0.4379, 0.0159, 0.2946, 0.4129, 0.1445, 0.5479],
          [0.1664, 0.6725, 0.5104, 0.4171, 0.6656, 0.3146],
          [0.5126, 0.2331, 0.8167, 0.2695, 0.6420, 0.8591],
          [0.2282, 0.6300, 0.9205, 0.6741, 0.6085, 0.7866]],

         [[0.7943, 0.8348, 0.5379, 0.1951, 0.2629, 0.7281],
          [0.5726, 0.4912, 0.5636, 0.7816, 0.9746, 0.3764],
          [0.5440, 0.3434, 0.5914, 0.5925, 0.9556, 0.0455],
          [0.0810, 0.0730, 0.2580, 0.0785, 0.2483, 0.3810],
          [0.4182, 0.7024, 0.4904, 0.6935, 0.1789, 0.1015],
          [0.2571, 0.9138, 0.1987, 0.6266, 0.0760, 0.4618]],

         [[0.3554, 0.2476, 0.3415, 0.5014, 0.1018, 0.3563],
          [0.2180, 0.5690, 0.9975, 0.8152, 0.5812, 0.2704],
          [0.5717, 0.9419, 0.4398, 0.5708, 0.2666, 0.3507],
          [0.3868, 0.6889, 0.0326, 0.7873, 0.7444, 0.8057],
          [0.1440, 0.9667, 0.2522, 0.9718, 0.6078, 0.2911],
          [0.1442, 0.3061, 0.4116, 0.4190, 0.2343, 0.2608]]]])
patche_img.shape:  torch.Size([1, 27, 4])
tensor([[[0.6072, 0.1085, 0.1664, 0.4171],
         [0.9496, 0.6808, 0.6725, 0.6656],
         [0.4149, 0.3949, 0.5104, 0.3146],
         [0.9770, 0.6597, 0.5126, 0.2695],
         [0.4831, 0.1749, 0.2331, 0.6420],
         [0.3964, 0.7326, 0.8167, 0.8591],
         [0.4379, 0.4129, 0.2282, 0.6741],
         [0.0159, 0.1445, 0.6300, 0.6085],
         [0.2946, 0.5479, 0.9205, 0.7866],
         [0.7943, 0.1951, 0.0810, 0.0785],
         [0.8348, 0.2629, 0.0730, 0.2483],
         [0.5379, 0.7281, 0.2580, 0.3810],
         [0.5726, 0.7816, 0.4182, 0.6935],
         [0.4912, 0.9746, 0.7024, 0.1789],
         [0.5636, 0.3764, 0.4904, 0.1015],
         [0.5440, 0.5925, 0.2571, 0.6266],
         [0.3434, 0.9556, 0.9138, 0.0760],
         [0.5914, 0.0455, 0.1987, 0.4618],
         [0.3554, 0.5014, 0.3868, 0.7873],
         [0.2476, 0.1018, 0.6889, 0.7444],
         [0.3415, 0.3563, 0.0326, 0.8057],
         [0.2180, 0.8152, 0.1440, 0.9718],
         [0.5690, 0.5812, 0.9667, 0.6078],
         [0.9975, 0.2704, 0.2522, 0.2911],
         [0.5717, 0.5708, 0.1442, 0.4190],
         [0.9419, 0.2666, 0.3061, 0.2343],
         [0.4398, 0.3507, 0.4116, 0.2608]]])
inputs_restore: tensor([[[[0.6072, 0.9496, 0.4149, 0.1085, 0.6808, 0.3949],
          [0.9770, 0.4831, 0.3964, 0.6597, 0.1749, 0.7326],
          [0.4379, 0.0159, 0.2946, 0.4129, 0.1445, 0.5479],
          [0.1664, 0.6725, 0.5104, 0.4171, 0.6656, 0.3146],
          [0.5126, 0.2331, 0.8167, 0.2695, 0.6420, 0.8591],
          [0.2282, 0.6300, 0.9205, 0.6741, 0.6085, 0.7866]],

         [[0.7943, 0.8348, 0.5379, 0.1951, 0.2629, 0.7281],
          [0.5726, 0.4912, 0.5636, 0.7816, 0.9746, 0.3764],
          [0.5440, 0.3434, 0.5914, 0.5925, 0.9556, 0.0455],
          [0.0810, 0.0730, 0.2580, 0.0785, 0.2483, 0.3810],
          [0.4182, 0.7024, 0.4904, 0.6935, 0.1789, 0.1015],
          [0.2571, 0.9138, 0.1987, 0.6266, 0.0760, 0.4618]],

         [[0.3554, 0.2476, 0.3415, 0.5014, 0.1018, 0.3563],
          [0.2180, 0.5690, 0.9975, 0.8152, 0.5812, 0.2704],
          [0.5717, 0.9419, 0.4398, 0.5708, 0.2666, 0.3507],
          [0.3868, 0.6889, 0.0326, 0.7873, 0.7444, 0.8057],
          [0.1440, 0.9667, 0.2522, 0.9718, 0.6078, 0.2911],
          [0.1442, 0.3061, 0.4116, 0.4190, 0.2343, 0.2608]]]])

单通道 滑动窗口有重叠。

kernel_size=(3,3),stride=2

# 单通道 滑动窗口有重叠。  kernel_size=(3,3),stride=2
import torch.nn as nn
import torch
 
batches_img = torch.rand(1,1,6,6)
print("batches_img: \n",batches_img)
 
unfold = nn.Unfold(kernel_size=(3,3),stride=2)
patche_img = unfold(batches_img)
print("patche_img.shape: ",patche_img.shape)
print(patche_img)
 
fold = torch.nn.Fold(output_size=(6, 6), kernel_size=(3, 3), stride=2)
inputs_restore = fold(patche_img)
print("inputs_restore: \n", inputs_restore)

输出为:

batches_img: 
 tensor([[[[0.4171, 0.0129, 0.2183, 0.0610, 0.5242, 0.9530],
          [0.7112, 0.7892, 0.2548, 0.4604, 0.7200, 0.0294],
          [0.0754, 0.0451, 0.2892, 0.6765, 0.8671, 0.5574],
          [0.4220, 0.4499, 0.8946, 0.0149, 0.6790, 0.0719],
          [0.1529, 0.2815, 0.8502, 0.5781, 0.0339, 0.9916],
          [0.6900, 0.4843, 0.3190, 0.0676, 0.8558, 0.0060]]]])
patche_img.shape:  torch.Size([1, 9, 4])
tensor([[[0.4171, 0.2183, 0.0754, 0.2892],
         [0.0129, 0.0610, 0.0451, 0.6765],
         [0.2183, 0.5242, 0.2892, 0.8671],
         [0.7112, 0.2548, 0.4220, 0.8946],
         [0.7892, 0.4604, 0.4499, 0.0149],
         [0.2548, 0.7200, 0.8946, 0.6790],
         [0.0754, 0.2892, 0.1529, 0.8502],
         [0.0451, 0.6765, 0.2815, 0.5781],
         [0.2892, 0.8671, 0.8502, 0.0339]]])
inputs_restore: 
 tensor([[[[0.4171, 0.0129, 0.4365, 0.0610, 0.5242, 0.0000],
          [0.7112, 0.7892, 0.5095, 0.4604, 0.7200, 0.0000],
          [0.1507, 0.0902, 1.1567, 1.3530, 1.7342, 0.0000],
          [0.4220, 0.4499, 1.7892, 0.0149, 0.6790, 0.0000],
          [0.1529, 0.2815, 1.7005, 0.5781, 0.0339, 0.0000],
          [0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000]]]])
# 重复累加次数最多的元素:
batches_img[0,0,2,2]*4  
# 输出:tensor(1.1567)

卷积等价于:Unfold + Matrix Multiplication + Fold (或view()到卷积输出形状)

注: 使用 Unfold + Matrix Multiplication + Fold 来代替卷积时,Fold 中的 kernel size 需要为 (1,1)文章来源地址https://www.toymoban.com/news/detail-457507.html

inp = torch.randn(1, 3, 10, 12)
w = torch.randn(2, 3, 4, 5)
inp_unf = torch.nn.functional.unfold(inp, (4, 5))
out_unf = inp_unf.transpose(1, 2).matmul(w.view(w.size(0), -1).t()).transpose(1, 2)  # 下面自己实现的更简洁
out = torch.nn.functional.fold(out_unf, (7, 8), (1, 1))
# or equivalently (and avoiding a copy),
# out = out_unf.view(1, 2, 7, 8)
print((torch.nn.functional.conv2d(inp, w) - out).abs().max())  # tensor(1.9073e-06)

out_unf_jason = w.view(w.size(0), -1).matmul(inp_unf)
out_jason = out_unf_jason.view(1, 2, 7, 8)
print((torch.nn.functional.conv2d(inp, w) - out_jason).abs().max())  # tensor(1.9073e-06)

到了这里,关于Pytroch nn.Unfold() 与 nn.Fold()图码详解的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 【PyTorch API】 nn.RNN 和 nn.LSTM 介绍和代码详解

    torch.nn.RNN 的 PyTorch 链接:torch.nn.RNN(*args, **kwargs) nn.RNN 的用法和输入输出参数的介绍直接看代码: 需要特别注意的是 nn.RNN 的第二个输出 hn 表示所有掩藏层的在最后一个 time step 隐状态,听起来很难理解,看下面的红色方框内的数据就懂了。即 output[:, -1, :] = hn[-1, : , :] 这里

    2024年02月12日
    浏览(39)
  • torch.nn.Linear详解

    在学习transformer时,遇到过非常频繁的nn.Linear()函数,这里对nn.Linear进行一个详解。 参考:https://pytorch.org/docs/stable/_modules/torch/nn/modules/linear.html 从名称就可以看出来,nn.Linear表示的是线性变换,原型就是初级数学里学到的线性函数: y=kx+b 不过在深度学习中,变量都是多维张

    2023年04月09日
    浏览(36)
  • nn.Conv2d详解

    nn.Conv2d  是 PyTorch 中的一个卷积层,用于实现二维卷积操作。其主要参数有: in_channels :表示输入图像的通道数,也就是输入特征图的深度。 out_channels :表示输出特征图的通道数,也就是卷积核的个数。 kernel_size :表示卷积核的大小;可以是一个整数,表示正方形卷积核的

    2024年02月05日
    浏览(38)
  • PyTorch中的torch.nn.Parameter() 详解

    今天来聊一下PyTorch中的torch.nn.Parameter()这个函数,笔者第一次见的时候也是大概能理解函数的用途,但是具体实现原理细节也是云里雾里,在参考了几篇博文,做过几个实验之后算是清晰了,本文在记录的同时希望给后来人一个参考,欢迎留言讨论。 先看其名,parameter,中文

    2023年04月08日
    浏览(85)
  • Pytorch:torch.nn.Module.apply用法详解

    torch.nn.Module.apply 是 PyTorch 中用于递归地应用函数到模型的所有子模块的方法。它允许对模型中的每个子模块进行操作,比如初始化权重、改变参数类型等。 以下是关于 torch.nn.Module.apply 的示例: 1. 语法 Module:PyTorch 中的神经网络模块,例如 torch.nn.Module 的子类。 fn:要应用到

    2024年01月15日
    浏览(49)
  • pytorch之nn.Conv1d详解

    1、Conv1d 定义 class torch.nn.Conv1d(in_channels, out_channels, kernel_size, stride=1, padding=0, dilation=1, groups=1, bias=True) 自然语言处理中一个句子序列,一维的,所以使用Conv1d,此时卷积核(没有batch_size,参数是共享的)除去chanel,也是一维的。  2、参数  in_channels(int) – 输入信号的通道。在

    2024年02月16日
    浏览(43)
  • 【PyTorch】nn.Conv2d函数详解

    CONV2D官方链接 in_channels:输入的通道数,RGB 图像的输入通道数为 3 out_channels:输出的通道数 kernel_size:卷积核的大小,一般我们会使用 5x5、3x3 这种左右两个数相同的卷积核,因此这种情况只需要写 kernel_size = 5这样的就行了。如果左右两个数不同,比如3x5的卷积核,那么写作

    2024年01月22日
    浏览(36)
  • MAE论文笔记+Pytroch实现

    近期在梳理Transformer在CV领域的相关论文,落脚点在于如何去使用Pytroch实现如ViT和MAE等。通过阅读源码,发现不少论文的源码都直接调用timm来实现ViT。故在此需要简单介绍一下timm这个库中ViT相关部分。此外小破站上的李沐大神讲的贼棒,这里也顺带记录一下相关笔记。 可以

    2023年04月09日
    浏览(41)
  • C++中size_t类型详解:探究其特性与用途,附带源代码示例

    C++中size_t类型详解:探究其特性与用途,附带源代码示例 在C++编程语言中,\\\"size_t\\\"是一个常见的数据类型,用于表示内存大小或对象大小。在本文中,我们将深入了解size_t类型的特性和用途,并提供相应的源代码示例。 一、size_t类型概述 size_t是C/C++中定义的一个无符号整数类

    2024年02月06日
    浏览(39)
  • 详解Pytorch中的torch.nn.MSELoss函,包括对每个参数的分析!

    一、函数介绍 Pytorch中MSELoss函数的接口声明如下,具体网址可以点这里。 torch.nn.MSELoss(size_average=None, reduce=None, reduction=‘mean’) 该函数 默认用于计算两个输入对应元素差值平方和的均值 。具体地,在深度学习中,可以使用该函数用来计算两个特征图的相似性。 二、使用方式

    2023年04月19日
    浏览(44)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包