如何在PyTorch中完全连接批量规范?

pat*_*_ai 9 python neural-network deep-learning pytorch batch-normalization

torch.nn有班BatchNorm1d,BatchNorm2d,BatchNorm3d,但它并没有完全连接BatchNorm类?在PyTorch中执行普通Batch Norm的标准方法是什么?

pat*_*_ai 20

好.我想到了.BatchNorm1d也可以处理Rank-2张量,因此可以BatchNorm1d用于普通的完全连接的情况.

例如:

import torch.nn as nn


class Policy(nn.Module):
def __init__(self, num_inputs, action_space, hidden_size1=256, hidden_size2=128):
    super(Policy2, self).__init__()
    self.action_space = action_space
    num_outputs = action_space

    self.linear1 = nn.Linear(num_inputs, hidden_size1)
    self.linear2 = nn.Linear(hidden_size1, hidden_size2)
    self.linear3 = nn.Linear(hidden_size2, num_outputs)
    self.bn1 = nn.BatchNorm1d(hidden_size1)
    self.bn2 = nn.BatchNorm1d(hidden_size2)

def forward(self, inputs):
    x = inputs
    x = self.bn1(F.relu(self.linear1(x)))
    x = self.bn2(F.relu(self.linear2(x)))
    out = self.linear3(x)


    return out
Run Code Online (Sandbox Code Playgroud)


小智 8

BatchNorm1d 通常出现在 ReLU 之前,并且偏差是多余的,所以

import torch.nn as nn

class Policy(nn.Module):
def __init__(self, num_inputs, action_space, hidden_size1=256, hidden_size2=128):
    super(Policy2, self).__init__()
    self.action_space = action_space
    num_outputs = action_space

    self.linear1 = nn.Linear(num_inputs, hidden_size1, bias=False)
    self.linear2 = nn.Linear(hidden_size1, hidden_size2, bias=False)
    self.linear3 = nn.Linear(hidden_size2, num_outputs)
    self.bn1 = nn.BatchNorm1d(hidden_size1)
    self.bn2 = nn.BatchNorm1d(hidden_size2)

def forward(self, inputs):
    x = inputs
    x = F.relu(self.bn1(self.linear1(x)))
    x = F.relu(self.bn2(self.linear2(x)))
    out = self.linear3(x)

    return out
Run Code Online (Sandbox Code Playgroud)