栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

pytorch和tensorlfow的交叉熵对比

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

pytorch和tensorlfow的交叉熵对比

pytorch交叉熵

import torch

# CrossEntropyLoss由LogSoftmax和Nllloss组成。

# 输入和输出
input_x = torch.tensor([[0.05, 0.95, 0], [0.1, 0.8, 0.1]])
target_y = torch.tensor([1, 2])
logsoftmax = torch.nn.LogSoftmax(dim = 1)
logsoftmax_output = logsoftmax(input_x)
nllloss = torch.nn.NLLLoss()
nllloss_output = nllloss(logsoftmax_output, target_y)
#
crossentropy = torch.nn.CrossEntropyLoss()
crossentropy_output = crossentropy(input_x, target_y)
print("logsoftmax_nllloss: ", nllloss_output)

keras交叉熵:

import tensorflow as tf
import numpy as np


y_true_np = np.array([1, 2])

flag = 1
if flag == 1:
    y_true = tf.constant([1, 2])
    y_pred = tf.constant([[0.05, 0.95, 0], [0.1, 0.8, 0.1]])
    # sparse_categorical_crossentropy等价于pytorch中的torch.nn.Nllloss()(torch.log(input_x), target)
    loss_total = tf.keras.losses.sparse_categorical_crossentropy(y_true, y_pred)
    loss = tf.reduce_mean(loss_total)
    
    y_true_np = np.array([1, 2])
    y_pred_np = np.array([[0.05, 0.95, 0], [0.1, 0.8, 0.1]])
    np_crossentropy = -(np.log(y_pred_np[0][y_true_np[0]]) + np.log(np.log(y_pred_np[1][y_true_np[1]])))
    
    print(loss.numpy())
    print(np_crossentropy)

总结:

在keras中使用了sparse_categorical_crossentropy,则输出需要使用softmax(x);

在pytorch中达到相同的数值,使用CrossEntropyLoss时,网络输出不需要softmax(x)/log_softmax();或者使用Nllloss(),网络输出则为torch.nn.log_softmax()

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/330979.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号