前馈神经网络,输出为目标列表和相关概率

数据挖掘 Python 神经网络 分类
2022-02-17 17:42:31

我正在学习 FNN 教程,现在它输出 0-1 的 sigmoid 概率(例如 0.8956)。我自己的数据有 3+ 个可能的目标,所以我需要输出是目标列表以及用于测试新样本的相关概率。(在这种情况下,它将是:0(概率),1(概率))。我该怎么办?这是代码。谢谢。

import numpy as np

# sigmoid function
def nonlin(x,deriv=False):
    if(deriv==True):
    return x*(1-x)
    return 1/(1+np.exp(-x))

# input dataset
X = np.array([  [0,0,1],
                [0,1,1],
                [1,0,1],
                [1,1,1] ])

# output dataset            
y = np.array([[0,0,1,1]]).T

# seed random numbers to make calculation
# deterministic (just a good practice)
np.random.seed(1)

# initialize weights randomly with mean 0
syn0 = 2*np.random.random((3,1)) - 1

for iter in xrange(10000):

# forward propagation
l0 = X
l1 = nonlin(np.dot(l0,syn0))

# how much did we miss?
l1_error = y - l1

# multiply how much we missed by the 
# slope of the sigmoid at the values in l1
l1_delta = l1_error * nonlin(l1,True)

# update weights
syn0 += np.dot(l0.T,l1_delta)

print "Output After Training:"
print l1
1个回答

如果你有两个以上的目标,那么你应该使用softmax激活而不是 sigmoid。Softmax 激活为您提供与输出中每个类相关联的概率。在应用 softmax 之前,您唯一需要做的就是将目标转换为 one-hot 编码向量。如果你想详细了解 softmax,可以在这里阅读