为什么这个与门神经网络没有向最优权重移动?

我有一个简单的两输入一输出的神经网络,没有隐藏层。即,

[input1][weight1 weight2] = z [input2]output = sigmoid(z)

权重似乎没有移动到最优值。我尽我所知检查了梯度,我能看到权重根据成本函数的导数上升或下降,但网络并没有向最优值移动。

这是代码:

import numpy as npimport random as rimport sysdef sigmoid(ip, derivate=False):    if derivate:        return ip*(1-ip)    return 1.0/(1+np.exp(-1*ip))class NeuralNet:    global sigmoid     def __init__(self):        self.inputLayers = 2        self.outputLayer = 1    def setup(self):        self.i = np.array([r.random(), r.random()], dtype=float).reshape(2,)        self.w = np.array([r.random(), r.random()], dtype=float).reshape(2,)    def forward_propogate(self):        self.z = self.w*self.i        self.o = sigmoid(sum(self.z))    def optimize_cost(self, desired):        i=0        current_cost = pow(desired - self.o, 2)        for weight in self.w:            dpdw = -1 * (desired-self.o) * (sigmoid(self.o, derivate=True)) * self.i[i]            print(dpdw)            self.w[i] = self.w[i] + 500*dpdw            i+=1        self.forward_propogate()    def train(self, ip, op):        self.i = np.array(ip).reshape(2,)        self.forward_propogate()        print("before:{}".format(self.o))        self.optimize_cost(op[0])        # print(self.i,self.w)n = NeuralNet()n.setup()# while sys.stdin.read(1):while True:    a = r.random()    b = r.random()    if a>0.5 and b>0.5:        c = 0.9    else:        c = 0.1    print(c)    n.train([a,b],[c])    print(n.i, n.w)    print("after: {}".format(n.o))

回答:

回答我自己的问题。我所需要的只是一个偏置(BIAS)。没有偏置,sigmoid函数无法从0偏离。

这是一个偏置为2的sigmoid函数。现在sigmoid(0)更接近0.1

enter image description here

在网络中加入一个偏置节点后,我能够得到结果。

成功率: 99.00000042272556% 网络已训练,耗时:2365601次尝试
网络权重:[14.0435016 14.04351048]
偏置: 21.861074330808844


输入输入值:
0
1
网络输出:0.00040243926180320134

输入输入值:
1
1
网络输出:0.9980264340845117

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注