如何用Python实现softmax和交叉熵函数的实例代码?

2026-05-24 20:510阅读0评论SEO资讯
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计433个文字,预计阅读时间需要2分钟。

如何用Python实现softmax和交叉熵函数的实例代码?

Python代码示例如下:pythonimport numpy as np

python代码如下:

import numpy as np # Write a function that takes as input a list of numbers, and returns # the list of values given by the softmax function. def softmax(L): pass expL = np.exp(L) sumExpL = sum(expL) result = [] for i in expL: result.append(i*1.0/sumExpL) return result

python编写交叉熵公式:

import numpy as np def cross_entropy(Y, P): Y = np.float_(Y) P = np.float_(P) return -np.sum(Y * np.log(P) + (1 - Y) * np.log(1 - P))

补充知识:分类时,为什么不使用均方误差而是使用交叉熵作为损失函数

MSE(均方误差)对于每一个输出的结果都非常看重,而交叉熵只对正确分类的结果看重。

阅读全文

本文共计433个文字,预计阅读时间需要2分钟。

如何用Python实现softmax和交叉熵函数的实例代码?

Python代码示例如下:pythonimport numpy as np

python代码如下:

import numpy as np # Write a function that takes as input a list of numbers, and returns # the list of values given by the softmax function. def softmax(L): pass expL = np.exp(L) sumExpL = sum(expL) result = [] for i in expL: result.append(i*1.0/sumExpL) return result

python编写交叉熵公式:

import numpy as np def cross_entropy(Y, P): Y = np.float_(Y) P = np.float_(P) return -np.sum(Y * np.log(P) + (1 - Y) * np.log(1 - P))

补充知识:分类时,为什么不使用均方误差而是使用交叉熵作为损失函数

MSE(均方误差)对于每一个输出的结果都非常看重,而交叉熵只对正确分类的结果看重。

阅读全文