当前位置: 首页 > news >正文

扬州广陵城乡建设局网站深圳品牌网站建设服务

扬州广陵城乡建设局网站,深圳品牌网站建设服务,承接做网站,wordpress rpc利用 扫描一、说明 在本文中#xff0c;讨论了深度学习中使用的所有常见损失函数#xff0c;并在NumPy#xff0c;PyTorch和TensorFlow中实现了它们。 二、内容提要 我们本文所谈的代价函数如下所列#xff1a; 均方误差 #xff08;MSE#xff09; 损失二进制交叉熵损失加权二进… 一、说明 在本文中讨论了深度学习中使用的所有常见损失函数并在NumPyPyTorch和TensorFlow中实现了它们。 二、内容提要  我们本文所谈的代价函数如下所列 均方误差 MSE 损失二进制交叉熵损失加权二进制交叉熵损失分类交叉熵损失稀疏分类交叉熵损失骰子损失吉隆坡背离损失平均绝对误差 MAE / L1 损耗胡贝尔损失 在下文我们将逐一演示其不同实现办法。  三、均方误差 MSE 损失         均方误差 MSE 损失是回归问题中常用的损失函数其目标是预测连续变量。损失计算为预测值和真实值之间的平方差的平均值。MSE 损失的公式为 MSE loss (1/n) * sum((y_pred — y_true)²)         这里 n 是数据集中的样本数目标变量的预测值y_predy_true是目标变量的真实值         MSE损失对异常值很敏感并且会严重惩罚大误差这在某些情况下可能是不可取的。在这种情况下可以使用其他损失函数如平均绝对误差MAE或Huber损失。         在 NumPy 中的实现 import numpy as npdef mse_loss(y_pred, y_true):Calculates the mean squared error (MSE) loss between predicted and true values.Args:- y_pred: predicted values- y_true: true valuesReturns:- mse_loss: mean squared error lossn len(y_true)mse_loss np.sum((y_pred - y_true) ** 2) / nreturn mse_loss         在此实现中和 是分别包含预测值和真值的 NumPy 数组。该函数首先计算 和 之间的平方差然后取这些值的平均值来获得 MSE 损失。该变量表示数据集中的样本数用于规范化损失。y_predy_truey_predy_truen TensorFlow 中的实现 import tensorflow as tfdef mse_loss(y_pred, y_true):Calculates the mean squared error (MSE) loss between predicted and true values.Args:- y_pred: predicted values- y_true: true valuesReturns:- mse_loss: mean squared error lossmse tf.keras.losses.MeanSquaredError()mse_loss mse(y_true, y_pred)return mse_loss 在此实现中和是分别包含预测值和真值的 TensorFlow 张量。该函数计算 和 之间的 MSE 损耗。该变量包含计算出的损失。y_predy_truetf.keras.losses.MeanSquaredError()y_predy_truemse_loss 在 PyTorch 中的实现 import torchdef mse_loss(y_pred, y_true):Calculates the mean squared error (MSE) loss between predicted and true values.Args:- y_pred: predicted values- y_true: true valuesReturns:- mse_loss: mean squared error lossmse torch.nn.MSELoss()mse_loss mse(y_pred, y_true)return mse_loss 在此实现中和 是分别包含预测值和真值的 PyTorch 张量。该函数计算 和 之间的 MSE 损耗。该变量包含计算出的损失。y_predy_truetorch.nn.MSELoss()y_predy_truemse_loss 四、二进制交叉熵损失         二进制交叉熵损失也称为对数损失是二元分类问题中使用的常见损失函数。它测量预测概率分布与实际二进制标签分布之间的差异。         二进制交叉熵损失的公式如下         Ly ŷ -[y * logŷ 1 — y * log1 — ŷ]         其中 y 是真正的二进制标签0 或 1ŷ 是预测概率范围从 0 到 1log 是自然对数。         等式的第一项计算真实标签为 1 时的损失第二项计算真实标签为 0 时的损失。总损失是两个项的总和。         当预测概率接近真实标签时损失较低当预测概率远离真实标签时损失较高。此损失函数通常用于在输出层中使用 sigmoid 激活函数来预测二进制标签的神经网络模型。 4.1 在 NumPy 中的实现         在numpy中二进制交叉熵损失可以使用我们前面描述的公式来实现。下面是如何计算它的示例 # define true labels and predicted probabilities y_true np.array([0, 1, 1, 0]) y_pred np.array([0.1, 0.9, 0.8, 0.3])# calculate the binary cross-entropy loss loss -(y_true * np.log(y_pred) (1 - y_true) * np.log(1 - y_pred)).mean()# print the loss print(loss) 4.2 TensorFlow 中的实现         在 TensorFlow 中二进制交叉熵损失可以使用 tf.keras.loss.BinaryCrossentropy 函数实现。下面是如何使用它的示例 import tensorflow as tf# define true labels and predicted probabilities y_true tf.constant([0, 1, 1, 0]) y_pred tf.constant([0.1, 0.9, 0.8, 0.3])# define the loss function bce_loss tf.keras.losses.BinaryCrossentropy()# calculate the loss loss bce_loss(y_true, y_pred)# print the loss print(loss) 4.3 在 PyTorch 中的实现         在 PyTorch 中二进制交叉熵损失可以使用该函数实现。下面是如何使用它的示例torch.nn.BCELoss() import torch# define true labels and predicted probabilities y_true torch.tensor([0, 1, 1, 0], dtypetorch.float32) y_pred torch.tensor([0.1, 0.9, 0.8, 0.3], dtypetorch.float32)# define the loss function bce_loss torch.nn.BCELoss()# calculate the loss loss bce_loss(y_pred, y_true)# print the loss print(loss) 4.4 加权二进制交叉熵损失         加权二元交叉熵损失是二元交叉熵损失的一种变体允许为正熵和负示例分配不同的权重。这在处理不平衡的数据集时非常有用其中一类与另一类相比明显不足。         加权二元交叉熵损失的公式如下 Ly ŷ -[w_pos * y * logŷ w_neg * 1 — y * log1 — ŷ]         其中 y 是真正的二进制标签0 或 1ŷ 是预测概率范围从 0 到 1log 是自然对数w_pos 和 w_neg 分别是正权重和负权重。         等式的第一项计算真实标签为 1 时的损失第二项计算真实标签为 0 时的损失。总损失是两个项的总和每个项按相应的权重加权。         可以根据每个类的相对重要性选择正权重和负权重。例如如果正类更重要则可以为其分配更高的权重。同样如果负类更重要则可以为其分配更高的权重。         当预测概率接近真实标签时损失较低当预测概率远离真实标签时损失较高。此损失函数通常用于在输出层中使用 sigmoid 激活函数来预测二进制标签的神经网络模型。 五、分类交叉熵损失         分类交叉熵损失是多类分类问题中使用的一种常用损失函数。它衡量每个类的真实标签和预测概率之间的差异。         分类交叉熵损失的公式为 L -1/N * sum(sum(Y * log(Y_hat)))         其中 是单热编码格式的真实标签矩阵是每个类的预测概率矩阵是样本数表示自然对数。YY_hatNlog         在此公式中形状为 其中是样本数是类数。每行 表示单个样本的真实标签分布列中的值 1 对应于真实标签0 对应于所有其他列。Y(N, C)NCY         类似地具有 的形状其中每行表示单个样本的预测概率分布每个类都有一个概率值。Y_hat(N, C)         该函数逐个应用于预测的概率矩阵。该函数使用两次来求和矩阵的两个维度。logY_hatsumY         结果值表示数据集中所有样本的平均交叉熵损失。训练神经网络的目标是最小化这种损失函数。LN         损失函数对模型的惩罚更大因为在预测低概率的类时犯了大错误。目标是最小化损失函数这意味着使预测概率尽可能接近真实标签。 5.1 在 NumPy 中的实现         在numpy中分类交叉熵损失可以使用我们前面描述的公式来实现。下面是如何计算它的示例 import numpy as np# define true labels and predicted probabilities as NumPy arrays y_true np.array([[0, 1, 0], [0, 0, 1], [1, 0, 0]]) y_pred np.array([[0.8, 0.1, 0.1], [0.2, 0.3, 0.5], [0.1, 0.6, 0.3]])# calculate the loss loss -1/len(y_true) * np.sum(np.sum(y_true * np.log(y_pred)))# print the loss print(loss)In this example, y_true represents the true labels (in integer format), and y_pred represents the predicted probabilities for each class (in a 2D array). The eye() function is used to convert the true labels to one-hot encoding, which is required for the loss calculation. The categorical cross-entropy loss is calculated using the formula we provided earlier, and the mean() function is used to average the loss over the entire dataset. Finally, the calculated loss is printed to the console.         在此示例中 以独热编码格式表示真实标签并表示每个类的预测概率两者都为 NumPy 数组。使用上述公式计算损失然后使用该函数打印到控制台。请注意该函数使用两次来对矩阵的两个维度求和。y_truey_predprintnp.sumY 5.2 TensorFlow 中的实现         在TensorFlow中分类交叉熵损失可以使用该类轻松计算。下面是如何使用它的示例tf.keras.losses.CategoricalCrossentropy import tensorflow as tf# define true labels and predicted probabilities as TensorFlow Tensors y_true tf.constant([[0, 1, 0], [0, 0, 1], [1, 0, 0]]) y_pred tf.constant([[0.8, 0.1, 0.1], [0.2, 0.3, 0.5], [0.1, 0.6, 0.3]])# create the loss object cce_loss tf.keras.losses.CategoricalCrossentropy()# calculate the loss loss cce_loss(y_true, y_pred)# print the loss print(loss.numpy())         在此示例中以独热编码格式表示真实标签并表示每个类的预测概率两者都作为 TensorFlow 张量。该类用于创建损失函数的实例然后通过将真实标签和预测概率作为参数传递来计算损失。最后使用该方法将计算出的损失打印到控制台。y_truey_predCategoricalCrossentropy.numpy() 请注意该类在内部处理将真实标签转换为独热编码因此无需显式执行此操作。如果你的真实标签已经是独热编码格式你可以将它们直接传递给损失函数没有任何问题。CategoricalCrossentropy 5.3 在 PyTorch 中的实现         在 PyTorch 中分类交叉熵损失可以使用该类轻松计算。下面是如何使用它的示例torch.nn.CrossEntropyLoss import torch# define true labels and predicted logits as PyTorch Tensors y_true torch.LongTensor([1, 2, 0]) y_logits torch.Tensor([[0.8, 0.1, 0.1], [0.2, 0.3, 0.5], [0.1, 0.6, 0.3]])# create the loss object ce_loss torch.nn.CrossEntropyLoss()# calculate the loss loss ce_loss(y_logits, y_true)# print the loss print(loss.item())         在此示例中 以整数格式表示真实标签并表示每个类的预测对数两者都作为 PyTorch 张量。该类用于创建损失函数的实例然后通过将预测的对数和 true 标签作为参数传递来计算损失。最后使用该方法将计算出的损失打印到控制台。y_truey_logitsCrossEntropyLoss.item()         请注意该类将 softmax 激活函数和分类交叉熵损失组合到一个操作中因此您无需单独应用 softmax。另请注意真正的标签应采用整数格式而不是独热编码格式。CrossEntropyLoss
http://www.hkea.cn/news/14357812/

相关文章:

  • 为什么建设旅游网站底湘西网站建设
  • 一台云服务器做多个网站中文网站建设哪家好
  • 网站导航栏全屏怎么做济南建设信息网官网
  • 泰兴网站建设价格官方网站下载免费app
  • 为什么要给企业建设网站百度推广助手app
  • 在哪些网站能接到活做装修公司网站开发
  • 网站建设最关键的两个素材pico笔克品牌介绍
  • 绵阳企业品牌网站建设深圳市南山区住房和建设局
  • 个人网站怎么做详情页软文写作500字
  • 网站设计合同附件珠市口网站建设
  • 商务网站建设的第一步个人电子商务网站建设方案
  • 怎么对网站的数据库做管理wordpress优化图片分离
  • 网站建设网络推广公司一键生成器
  • 公司专业设计网站怎么做会员积分网站
  • 做网站的费用入账厦门市建设局网站首页
  • 网站策划哪里找企业邮箱注册申请需要付费吗
  • 代理 网站前置审批网站当地备案
  • 企业管理10大系统seo推广代理
  • 诸城做网站的公司影视公司招聘
  • 做家乡网站源代码wordpress采集英文
  • 网站开发中如何设计验证码广东网站制作多少钱
  • 如何建网站服务器jspajax网站开发典型实例
  • 网站开发与设计难嘛北京做网站比较有名的公司有哪些
  • 菏泽机关建设网站wordpress 附件显示设置
  • 有网站加金币的做弊器吗专门做app的网站
  • 网站建设推广好做吗seo刷关键词排名软件
  • 志愿海南网站新乡手机网站建设哪家好
  • 麻花星空影视传媒制作公司网站四川省建设厅官网信息查询平台
  • 免费微网站系统源码设置引擎营销是用户主导的网络营销方式
  • zencart网站打不开牧风 wordpress