神经网络:用于猜测点相对于函数的位置的感知器
Neural Network: A Perceptron for guessing the position of a point relative to a function
我正在构建一个具有 3 个输入的简单感知器 (x, y, bias=1)
他必须猜测给定的点(x,y)是在之下还是下面给定函数。
基本上,它的灵感来自 this article
使用监督学习模型来训练网络,公式如下:
learningConst = 0.01
error = desired - neuralAnswer
new_weights[i] = old_weights[i] + error * inputs[i] * learningConst
尽管如此,在 100000 次训练测试后,即使是简单的函数 (2x+1)
也会出错
代码如下:
import numpy as np
import matplotlib.pyplot as plt
class Perceptron:
def __init__(self, n):
self.n = n #n is 2 in this case. 2 inputs [ x, y ]
self.weights = [np.random.uniform(-1, 1) for x in range(n)]
self.learningConstant = 0.05
# 1 added to the sum is the bias input
def feedForward(self, inputs):
return 1 + sum([self.weights[i]*inputs[i] for i in range(self.n)])
def activate(self, result):
if result >= 0:
return 1
elif result < 0:
return -1
def train(self, inputs, expected):
prediction = self.feedForward(inputs)
answer = self.activate(prediction)
error = expected - answer
self.weights = [
self.weights[i] + error * inputs[i] * self.learningConstant
for i in range(self.n)
]
#print(self.weights)
def predict(self, inputs):
prediction = self.feedForward(inputs)
return self.activate(prediction)
你可以在这里看到结果。绿色表示感知器猜对了,红色表示错误。
有趣的是 - 它往往在线下的点上出错。
我应该如何改进程序?
完整代码:CLICK
解决方案
我的问题是使用偏差输入作为一个常量(完整代码的第 14 行),而不允许算法在其上学习。
所以,我的输入现在是 [bias, x, y],权重是 [w1, w3, w3] - 偏置输入现在有它的权重。
另一个好主意是将权重保存在其他地方,这样算法就不必在每次测试程序时都重新开始。
2x + 1
x^2 - 2x + 1
确保您希望分类的数据是线性可分的,否则感知器学习算法永远不会收敛。
您的解决方案的主要问题是您的偏差始终为 1。它不是参数 - 它是常量。所以这可能是个问题,因为你的模型比经典感知器模型弱得多。
我正在构建一个具有 3 个输入的简单感知器 (x, y, bias=1)
他必须猜测给定的点(x,y)是在之下还是下面给定函数。
基本上,它的灵感来自 this article
使用监督学习模型来训练网络,公式如下:
learningConst = 0.01
error = desired - neuralAnswer
new_weights[i] = old_weights[i] + error * inputs[i] * learningConst
尽管如此,在 100000 次训练测试后,即使是简单的函数 (2x+1)
也会出错代码如下:
import numpy as np
import matplotlib.pyplot as plt
class Perceptron:
def __init__(self, n):
self.n = n #n is 2 in this case. 2 inputs [ x, y ]
self.weights = [np.random.uniform(-1, 1) for x in range(n)]
self.learningConstant = 0.05
# 1 added to the sum is the bias input
def feedForward(self, inputs):
return 1 + sum([self.weights[i]*inputs[i] for i in range(self.n)])
def activate(self, result):
if result >= 0:
return 1
elif result < 0:
return -1
def train(self, inputs, expected):
prediction = self.feedForward(inputs)
answer = self.activate(prediction)
error = expected - answer
self.weights = [
self.weights[i] + error * inputs[i] * self.learningConstant
for i in range(self.n)
]
#print(self.weights)
def predict(self, inputs):
prediction = self.feedForward(inputs)
return self.activate(prediction)
你可以在这里看到结果。绿色表示感知器猜对了,红色表示错误。 有趣的是 - 它往往在线下的点上出错。
我应该如何改进程序?
完整代码:CLICK
解决方案
我的问题是使用偏差输入作为一个常量(完整代码的第 14 行),而不允许算法在其上学习。 所以,我的输入现在是 [bias, x, y],权重是 [w1, w3, w3] - 偏置输入现在有它的权重。
另一个好主意是将权重保存在其他地方,这样算法就不必在每次测试程序时都重新开始。
2x + 1
x^2 - 2x + 1
确保您希望分类的数据是线性可分的,否则感知器学习算法永远不会收敛。
您的解决方案的主要问题是您的偏差始终为 1。它不是参数 - 它是常量。所以这可能是个问题,因为你的模型比经典感知器模型弱得多。