我不确定 PyTorch 如何管理将损失函数链接到我想要计算它的模型。损失和模型之间从来没有明确的参考,例如模型参数和优化器之间的参考。
比如说我想在同一个数据集上训练 2 个网络,所以我想利用数据集的单次传递。PyTorch 如何将适当的损失函数链接到适当的模型。下面是代码供参考:
import torch
from torch import nn, optim
import torch.nn.functional as F
from torchvision import datasets, transforms
import shap
# Define a transform to normalize the data
transform = transforms.Compose([transforms.ToTensor(),
transforms.Normalize((0.5,), (0.5,)),
])
# Download and load the training data
trainset = datasets.MNIST('~/.pytorch/MNIST_data/', download=True, train=True, transform=transform)
trainloader = torch.utils.data.DataLoader(trainset, batch_size=64, shuffle=True)
model = nn.Sequential(nn.Linear(784, 128),
nn.ReLU(),
nn.Linear(128, 64),
nn.ReLU(),
nn.Linear(64, 10),
nn.LogSoftmax(dim=1))
model2 = nn.Sequential(nn.Linear(784, 128),
nn.ReLU(),
nn.Linear(128, 10),
nn.LogSoftmax(dim=1))
# Define the loss …Run Code Online (Sandbox Code Playgroud) 好的,所以我试图使用 Bootstrap 重新创建这个页面,但我在这部分被卡住了。

我的两个问题是:
代码示例:
<div class="container" style="margin-top: 3%; ">
<div class="row">
<div class="col-xs-6 col-xs-offset-3">
<div class="row">
<div class="col-xs-6">
<div class="form-group">
<input type="text" class='form-control' placeholder="First Name">
</div>
</div>
<div class="col-xs-6">
<div class="form-group">
<input type="text" class='form-control' placeholder="Last Name">
</div>
</div>
</div>
<div class="row">
<div class="col-xs-12">
<div class="form-group">
<input type="text" class='form-control' placeholder="Display name">
</div>
</div>
</div>
<div class="row">
<div class="col-xs-12">
<div class="form-group">
<input type="text" class='form-control' placeholder="Email Address">
</div>
</div>
</div>
<div class="row">
<div class="col-xs-6"> …Run Code Online (Sandbox Code Playgroud)