- 二分类问题中为了使用梯度下降法减小误差函数值,误差函数必须是连续的而不能是离散的。为此引入了sigmoid函数:σ(x)=1/(1+exp(x))作为激活函数替代阶跃函数,将线性score=Wx+b代入σ(x)得到概率。
- 多分类问题中采用softmax作为激活函数,可应用梯度下降法
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):
expL = np.exp(L)
sumExpL = sum(expL)
result = []
for i in expL:
result.append(i*1.0/sumExpL)
return result
# Note: The function np.divide can also be used here, as follows:
# def softmax(L):
# expL = np.exp(L)
# return np.divide (expL, expL.sum())
input_animals=[5,-3,7,-2,]
#输出结果是概率
print(softmax(input_animals))
- one-hot编码
对于多分类问题,需要对输入对象进行编码,且保证对象之间不存在相互依赖关系,这里采用one-hot编码方式: