pytorch-批量标准化简单问题

时间:2019-11-11 07:43:43

标签: pytorch batch-normalization

我用批处理归一化实现了一个模型:

class FFNet(torch.nn.Module):
    def __init__(self, D_in, H_1, H_2, D_out):

        super(FFNet, self).__init__()
        self.linear1 = torch.nn.Linear(D_in, H_1)
        self.linear2 = torch.nn.Linear(H_1, H_2)
        self.bn2 = torch.nn.BatchNorm1d(H_2)
        self.linear4 = torch.nn.Linear(H_2, D_out)

    def forward(self, x):

        h_relu_1=F.relu(self.linear1(x))
        h_relu_2=F.relu(self.bn2(self.linear2(h_relu_1)))
        y_pred=self.linear4(h_relu_2)
        return y_pred

我还编写了训练循环:

for epoch in range(epoches):

    running_loss = 0.0
    cnt = 0
    for i, data in enumerate(train_data, 0):
        local_X, local_y = data
        y_pred = model.forward(local_X)
        loss = criterion(y_pred, local_y)
        optimizer.zero_grad()
        #loss = criterion(y_pred, Y_local_output)
        loss.backward() # back props
        optimizer.step()
        running_loss = running_loss + loss.item()
        cnt+=1


    Validation_loss = 0.0
    cnt2 = 0
    # Validation
    for i, data in enumerate(validation_data, 0):
        Val_X, Val_Y = data

        y_pred = model.forward(Val_X)

        loss=criterion(y_pred, Val_Y)
        Validation_loss = Validation_loss + loss.item()
        cnt2+=1

我有两个问题: 1.无需在此代码中使用model.train()? 2.如何使用eval评估此模型?我有一个数据样本,其大小为(1xD_in),并且批处理大小大于1。使用以下代码时,出现错误:

    test_single = torch.tensor([aa, ab, ac, ad, ae, af, ag])
    test_single = test_single.unsqueeze(0)

    model.eval()
    [bb,cc] = model.forward(test_single)

错误是“没有足够的值要解压(预期2,得到1)”

1 个答案:

答案 0 :(得分:0)

如果您具有批量归一化功能,那么在训练和评估时确实需要分别使用model.train()和model.eval()。

第二部分(压缩代码)没有错。但是,您的模型只有一个输出(请参阅模型的forward函数的return语句),这会导致错误,即您尝试解包2个值,而只有一个。因此,您不能

[bb,cc] = model.forward(test_single)

您必须

out = model.forward(test_single)

我尝试过了,而且可行。