自定义分类器pytorch,我想添加softmax

afdcj2ne  于 2022-11-09  发布在  其他
关注(0)|答案(1)|浏览(139)

我有这样的分类器:

input_dim = 25088
h1_dim = 4096
h2_dim = 2048
h3_dim = 1024
h4_dim = 512
output_dim = len(cat_to_name) # 102
drop_prob = 0.2

model.classifier = nn.Sequential(nn.Linear(input_dim, h1_dim),
                                 nn.ReLU(),
                                 nn.Dropout(drop_prob),
                                 nn.Linear(h1_dim, h2_dim),
                                 nn.ReLU(),
                                 nn.Dropout(drop_prob),
                                 nn.Linear(h2_dim, h3_dim),
                                 nn.ReLU(),
                                 nn.Dropout(drop_prob),
                                 nn.Linear(h3_dim, h4_dim),
                                 nn.ReLU(),
                                 nn.Dropout(drop_prob),
                                 nn.Linear(h4_dim, output_dim),
                                 )

我用CrossEntropyLoss作为标准。在验证和测试中,我如何添加Softmax?这是验证循环:

model.eval()
            with torch.no_grad():
                for images, labels in valid_loader:
                    images, labels = images.to(device), labels.to(device)
                    images.requires_grad = True

                    logits = model.forward(images)
                    batch_loss = criterion(logits, labels)
                    valid_loss += batch_loss.item()

                    ps = torch.exp(logits)
                    top_p, top_class = ps.topk(1, dim=1)
                    equals = top_class == labels.view(*top_class.shape)
8zzbczxx

8zzbczxx1#

  • CrossEntropyLoss已经应用了softmax函数。

请注意,这种情况相当于LogSoftmax和NLLLoss的组合。
因此,如果你只是想使用交叉熵损失,没有必要事先应用SoftMax

  • 如果确实想使用SoftMax函数,可以执行以下操作:
m = nn.Softmax(dim=1)
output = m(logits)

假设你的对数的形状为(batch_size, number_classes)
您可以检查:https://pytorch.org/docs/stable/generated/torch.nn.Softmax.htmlhttps://pytorch.org/docs/stable/generated/torch.nn.CrossEntropyLoss.html显示器

相关问题