Python下的Softmax回归函数的实现方法(推荐)

您所在的位置:网站首页 pythonmax函数 Python下的Softmax回归函数的实现方法(推荐)

Python下的Softmax回归函数的实现方法(推荐)

#Python下的Softmax回归函数的实现方法(推荐)| 来源: 网络整理| 查看: 265

Python下的Softmax回归函数的实现方法(推荐)

2020-09-19 14:43脚本之家 Python

下面小编就为大家带来一篇Python下的Softmax回归函数的实现方法(推荐)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧

Softmax回归函数是用于将分类结果归一化。但它不同于一般的按照比例归一化的方法,它通过对数变换来进行归一化,这样实现了较大的值在归一化过程中收益更多的情况。

Softmax公式

Python下的Softmax回归函数的实现方法(推荐)

Softmax实现方法1

? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 import numpy as np def softmax(x):  """Compute softmax values for each sets of scores in x."""  pass # TODO: Compute and return softmax(x)  x = np.array(x)  x = np.exp(x)  x.astype('float32')  if x.ndim == 1:   sumcol = sum(x)   for i in range(x.size):    x[i] = x[i]/float(sumcol)  if x.ndim > 1:   sumcol = x.sum(axis = 0)   for row in x:    for i in range(row.size):     row[i] = row[i]/float(sumcol[i])  return x #测试结果 scores = [3.0,1.0, 0.2] print softmax(scores)

其计算结果如下:

? 1 [ 0.8360188 0.11314284 0.05083836]

Softmax实现方法2

? 1 2 3 4 5 6 7 import numpy as np def softmax(x):  return np.exp(x)/np.sum(np.exp(x),axis=0)   #测试结果 scores = [3.0,1.0, 0.2] print softmax(scores)

以上这篇Python下的Softmax回归函数的实现方法(推荐)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3