【问题标题】:Pruning using Pytorch on a complicated model在复杂模型上使用 Pytorch 进行修剪
【发布时间】:2020-11-10 01:41:11
【问题描述】:

所以我尝试使用torch.nn.utils.prune.global_unstructured

我在一个简单的模型上做了这件事,而且效果很好。 model.cov2 或其他层并且有效。我正在尝试在(嵌套)的模型上执行此操作?我收到以下错误:

AttributeError: 'CNN' object has no attribute 'conv1'

和其他错误。我尝试了一切来访问这个深度 cov1,但我不能。

您可以在下面找到型号代码:

class CNN(nn.Module):
def __init__(self):
    """CNN Builder."""
    super(CNN, self).__init__()

    self.conv_layer = nn.Sequential(

        # Conv Layer block 1
        nn.Conv2d(in_channels=3, out_channels=32, kernel_size=3, padding=1),
        nn.BatchNorm2d(32),
        nn.ReLU(inplace=True),
        nn.Conv2d(in_channels=32, out_channels=64, kernel_size=3, padding=1),
        nn.ReLU(inplace=True),
        nn.MaxPool2d(kernel_size=2, stride=2),

        # Conv Layer block 2
        nn.Conv2d(in_channels=64, out_channels=128, kernel_size=3, padding=1),
        nn.BatchNorm2d(128),
        nn.ReLU(inplace=True),
        nn.Conv2d(in_channels=128, out_channels=128, kernel_size=3, padding=1),
        nn.ReLU(inplace=True),
        nn.MaxPool2d(kernel_size=2, stride=2),
        nn.Dropout2d(p=0.05),

        # Conv Layer block 3
        nn.Conv2d(in_channels=128, out_channels=256, kernel_size=3, padding=1),
        nn.BatchNorm2d(256),
        nn.ReLU(inplace=True),
        nn.Conv2d(in_channels=256, out_channels=256, kernel_size=3, padding=1),
        nn.ReLU(inplace=True),
        nn.MaxPool2d(kernel_size=2, stride=2),
    )


    self.fc_layer = nn.Sequential(
        nn.Dropout(p=0.1),
        nn.Linear(4096, 1024),
        nn.ReLU(inplace=True),
        nn.Linear(1024, 512),
        nn.ReLU(inplace=True),
        nn.Dropout(p=0.1),
        nn.Linear(512, 100)
    )


def forward(self, x):
    """Perform forward."""
    # conv layers
    x = self.conv_layer(x)
    # flatten
    x = x.view(x.size(0), -1)
    # fc layer
    x = self.fc_layer(x)
    return x

如何在这个模型上应用剪枝?

【问题讨论】:

  • 需要更多信息和错误回溯才能正确理解此处的问题。
  • @PranayModukuru prune.random_unstructured(module, name="weight", amount=0.3) - 我需要把这条线放在哪里进行修剪? - 就在 CNN 课程之后,还是我需要将其放入训练循环中?
  • 它应该在训练循环中——如果你想在每次迭代后修剪它。或者,如果您只想执行一次 - 然后在培训之后或培训之前调用一次。

标签: python machine-learning pytorch google-colaboratory pruning


【解决方案1】:

使用此方法查看图层名称

for layer_name, param in model.named_parameters():
    print(f"layer name: {layer_name} has {param.shape}")

并将这些名称传递给prune 方法

例如,prune.random_unstructured(module_name, name="weight", amount=0.3)

【讨论】:

    【解决方案2】:

    您的模块不是名称“conv1”或“conv2”,您可以使用 named_modules 生成器查看名称。从上面,您有一个“conv_stem”,可以索引为 model.conv_stem[0] 来访问。你可以遍历模块来创建一个像这样的字典:

    parameters_to_prune = (
        (model.conv1, 'weight'),
        (model.conv2, 'weight'),
        (model.fc1, 'weight'),
        (model.fc2, 'weight'),
        (model.fc3, 'weight'), )
    

    并将其传入。请参阅:https://colab.research.google.com/github/pytorch/tutorials/blob/gh-pages/_downloads/f40ae04715cdb214ecba048c12f8dddf/pruning_tutorial.ipynb#scrollTo=UVFjM079F0Oi

    【讨论】:

      猜你喜欢
      • 2021-06-09
      • 2021-06-08
      • 1970-01-01
      • 1970-01-01
      • 2021-01-09
      • 1970-01-01
      • 2021-07-01
      • 2012-12-08
      • 2022-11-15
      相关资源
      最近更新 更多