首页 > 解决方案 > 如何在 Pytorch 的 CNN 中访问卷积层的权重和 L2 范数?

问题描述

是否有 PyTorch 功能可以访问这些功能?

标签: pythondeep-learningpytorchconv-neural-network

解决方案


您可以使用

torch.div(model[i].weight, torch.norm(model[i].weight), out=model[i].weight)

玩具示例(内联记录)。

import torch
from torch.nn import Linear, ReLU, CrossEntropyLoss, Sequential, Conv2d, MaxPool2d, Module, Softmax, BatchNorm2d, Dropout
from torch.optim import Adam

# Define model
model = Sequential(
            Conv2d(1, 4, kernel_size=3, stride=1, padding=1),
            BatchNorm2d(4),
            ReLU(inplace=True),
            MaxPool2d(kernel_size=2, stride=2),
            # Defining another 2D convolution layer
            Conv2d(4, 4, kernel_size=3, stride=1, padding=1),
            BatchNorm2d(4),
            ReLU(inplace=True),
            MaxPool2d(kernel_size=2, stride=2),
        )

optimizer = Adam(model.parameters(), lr=0.07)
criterion = CrossEntropyLoss()

# Train loop
for epoch in range(10):
    optimizer.zero_grad()

    # Forward
    # y_hat = model(X_train)
    # loss = criterion(y_train, y_hat)

    # Backward
    # loss.backward()
    #  optimizer.step()

    # Now maunually update the weights
    for i in range(len(model)):
      with torch.no_grad():
        if hasattr(model[i], 'weight'):
          torch.div(model[i].weight, torch.norm(model[i].weight), out=model[i].weight)

推荐阅读