pytorch中的tf.nn.softmax_cross_entropy_with_logits等效吗?

时间:2019-04-08 16:07:52

标签: tensorflow deep-learning pytorch

我试图用pytorch复制用tensorflow编写的代码。我在张量流中遇到了一个损失函数softmax_cross_entropy_with_logits。我在pytorch中寻找了一个损失函数,我发现了torch.nn.MultiLabelSoftMarginLoss,尽管我不太确定这是正确的函数。我也不知道如何测量准确性当我使用此损失函数并且网络末端没有relu层时,模型的代码是我的代码:


# GRADED FUNCTION: compute_cost 

def compute_cost(Z3, Y):

    loss = torch.nn.MultiLabelSoftMarginLoss()    
    return loss(Z3,Y)


def model(net,X_train, y_train, X_test, y_test, learning_rate = 0.009,
          num_epochs = 100, minibatch_size = 64, print_cost = True):

    optimizer = torch.optim.Adam(net.parameters(), lr=learning_rate)
    optimizer.zero_grad()

    total_train_acc=0

    for epoch in range(num_epochs):
        for i, data in enumerate(train_loader, 0):
            running_loss = 0.0

            inputs, labels = data

            inputs, labels = Variable(inputs), Variable(labels)

            Z3 = net(inputs)

            # Cost function
            cost = compute_cost(Z3, labels)

            # Backpropagation: Define the optimizer. 
            # Use an AdamOptimizer that minimizes the cost.

            cost.backward()
            optimizer.step()             

            running_loss += cost.item()

            # Measuring the accuracy of minibatch
            acc = (labels==Z3).sum()
            total_train_acc += acc.item()
            #Print every 10th batch of an epoch
            if epoch%1 == 0:
            print("Cost after epoch {} : 
            {:.3f}".format(epoch,running_loss/len(train_loader)))

1 个答案:

答案 0 :(得分:1)

使用torch.nn.CrossEntropyLoss()。它结合了softmax和交叉熵。来自文档:

  

此标准将nn.LogSoftmax()和nn.NLLLoss()合并到一个类中。

示例:

# define loss function
loss_fn = torch.nn.CrossEntropyLoss(reduction='mean')

# during training
for (x, y) in train_loader:
    model.train()
    y_pred = model(x) # your input `torch.FloatTensor`
    loss_val = loss_fn(y_pred, y)
    print(loss_val.item()) # prints numpy value

    optimizer.zero_grad()
    loss_val.backward()
    optimizer.step()

确保xy的类型正确。通常,转换是这样完成的:loss_fn(y_pred.type(torch.FloatTensor), y.type(torch.LongTensor))

要测量准确性,您可以定义一个自定义函数:

def compute_accuracy(y_pred, y):
   if list(y_pred.size()) != list(y.size()):
      raise ValueError('Inputs have different shapes.',
                       list(y_pred.size()), 'and', list(y.size()))

  result = [1 if y1==y2 else 0 for y1, y2 in zip(y_pred, y)]

  return sum(result) / len(result)

并同时使用两者:

model.train()
y_pred = model(x)

loss_val = loss_fn(y_pred.type(torch.FloatTensor), y.type(torch.LongTensor))
_, y_pred = torch.max(y_pred, 1)
accuracy_val = compute_accuracy(y_pred, y)
print(loss_val.item()) # print loss value
print(accuracy_val) # print accuracy value
# update step e.t.c

如果您的输入数据是一次性编码,则可以在使用loss_fn之前将其转换为常规编码:

_, targets = y.max(dim=1)
y_pred = model(x)
loss_val = loss_fn(y_pred, targets)