PyTorch自定义损失函数计算图外

2024-03-28 15:31:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我对pytorch有点陌生,我正试着去了解它。 我读过关于自定义损失函数的书,就我所见,它们不能与内部计算图解耦。这意味着损失函数消耗张量,对张量执行操作(在pytorch中实现),并输出张量。有没有办法将损耗计算解耦并以某种方式将其插回

用例

我试图训练一个编码器,在那里潜在的空间将被优化到某种统计质量。这意味着我不分批训练,而是为整个历元和整个数据集计算单个损失值。这样教网络有可能吗

class Encoder(nn.Module):
    def __init__(self, genome_size: int):
        super(Encoder, self).__init__()

        self.fc1 = nn.Linear(genome_size, genome_size)
        self.fc2 = nn.Linear(genome_size, genome_size)
        self.fc3 = nn.Linear(genome_size, genome_size)

        self.genome_size = genome_size

    def forward(self, x):
        x = self.fc1(x)
        x = self.fc2(x)
        x = self.fc3(x)

        return x
def train_encoder(
    net: nn.Module,
    optimizer: Optimizer,
    epochs: int,
    population: Tensor,
    fitness: Tensor,
):
    running_loss = 0.0

    for epoch in range(epochs):

        optimizer.zero_grad()
        outputs = net(population)

        # encoder_loss is computationally heavy and cannot be done only on tensors
        # I need to unwrap those tensors to numpy arrays and use them as an input to another model
        loss = encoder_loss(outputs, fitness)
        running_loss += loss

        running_loss.backward()
        optimizer.step()

        print('Encoder loss:', loss)

我已经看到了一些累积running_loss的例子,但是我的编码器无法学习任何东西。收敛图只是到处跳跃

感谢您抽出时间<;三,


Tags: to函数selfencodersizegenomedefnn