Theano学习与门

我编写了一个简单的神经网络来学习与门。我试图理解为什么我的成本从未降低,并且预测值始终是0.5:

import numpy as npimport theanoimport theano.tensor as Tinputs = [[0,0], [1,1], [0,1], [1,0]]outputs = [[0], [1], [0], [0]]x = theano.shared(value=np.asarray(inputs), name='x')y = theano.shared(value=np.asarray(outputs), name='y')alpha = 0.1w_array = np.asarray(np.random.uniform(low=-1, high=1, size=(2, 1)), dtype=theano.config.floatX)w = theano.shared(value=w_array, name='w', borrow=True)output = T.nnet.sigmoid(T.dot(x, w))cost = T.sum((y - output) ** 2)updates = [(w, w - alpha * T.grad(cost, w))]train = theano.function(inputs=[], outputs=[], updates=updates)test = theano.function(inputs=[], outputs=[output])calc_cost = theano.function(inputs=[], outputs=[cost])for i in range(60000):    if (i+1) % 10000 == 0:        print(i+1)        print(calc_cost())    train()    print(test())

输出始终是相同的:

10000[array(1.0)]20000[array(1.0)]30000[array(1.0)]40000[array(1.0)]50000[array(1.0)]60000[array(1.0)][array([[ 0.5],       [ 0.5],       [ 0.5],       [ 0.5]])]

无论输入如何,它似乎总是预测0.5,因为在学习过程中成本始终未偏离1

如果我将输出改为[[0], [1], [1], [1]]来学习或门,我会得到正确的预测,并且成本正确地降低


回答:

你的模型形式为

<w, x>

因此它无法构建任何不经过原点的分离。这类方程只能表示经过点(0,0)的线,而显然分离与门((1, 1)与其他点的线)不经过原点。你需要添加偏置项,因此你的模型应为

<w, x> + b

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中创建了一个多类分类项目。该项目可以对…

发表回复

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