神经网络:避免在任何方向产生偏差 [英] Neural networks: avoid bias in any direction for output
问题描述
我在遇到CartPole问题时遇到困难.
I'm having difficulties with the CartPole problem.
购物车的输入采用0 or 1
作为输入;左右移动.
The input to the Cart takes either 0 or 1
as input; Either move left or right.
假设我们有一个包含4 inputs
加bias
,3 hidden layers
且每个1 neuron
和1 output
的网络;在哪里
所有权重均在0 and 1
之间随机化floats
,输入也将在-10 and 10
之间随机化floats
.
Lets say we have a net with 4 inputs
plus bias
, 3 hidden layers
with 1 neuron
each and 1 output
; where
all weights are randomized floats
between 0 and 1
, and the inputs will also be randomized floats
between -10 and 10
.
因为我是随机选择所有东西,所以我固有地期望输出平均大约为0.5
,并且购物车向右走与向左走一样.
Because i chose everything random, I inherently expect the output to be approximately 0.5
on average, and that the cart will go as much right as it goes left.
情况并非如此;我大约平均得到0.63
.这会导致大问题,因为推车永远不会决定向左走. 这似乎取决于每个隐藏层的神经元数量.
This is not the case; i approximately get 0.63
on average. This leads to big problems, because the cart never decides to go to the left. This seems to be dependent on the amounts of neurons per hidden layer.
class NeuralNetwork(object):
def __init__(self):
self.inputLayerSize = 4
self.hiddenLayerCount = 3
self.hiddenLayerSize = 1
self.outputLayerSize = 1
#Initialize weights
self.W = []
self.W.append(np.random.rand(self.inputLayerSize + 1, self.hiddenLayerSize))
for _ in range(self.hiddenLayerCount - 1):
self.W.append( np.random.rand(self.hiddenLayerSize, self.hiddenLayerSize))
self.W.append( np.random.rand(self.hiddenLayerSize, self.outputLayerSize))
def forward(self, data):
layers = []
data = np.append(data, [1]) #ADD BIAS
layers.append(data)
for h in range(self.hiddenLayerCount + 1):
z = np.dot( layers[h], self.W[h] )
a = sigmoid(z)
layers.append(a)
return sigmoid( layers[self.hiddenLayerCount + 1] )
我通过用0.1
减去output
来解决此问题,但这显然是作弊;我认为没有数学上的理由将0.1
用作某种幻数.
I fix the problem by subtracting the output
with 0.1
, but this is obviously cheating; I see no mathematical reason to use 0.1
as some sort of magic number.
我认为我正在错误地解决问题,或者弄乱了我的一些代码.任何帮助将不胜感激!
I believe I'm approaching the problem wrong, or got some of my code messed up. Any help would be appreciated!
推荐答案
您的神经网络至少存在一个问题,该问题会使您的结果概率产生偏差:模型输出是最后一层的sigmoid
本身是
There's at least one problem with your neural network that skews your result probabilities: the model output is the sigmoid
of the last layer which itself is a sigmoid
.
这意味着您的登录信息(即原始分数)在[0, 1]
中,因此最终概率是在[0, 1]
范围而不是[-inf, inf]
上计算的.
This means that your logit (i.e., the raw score) is in [0, 1]
, so the final probability is computed on a [0, 1]
range, not [-inf, inf]
.
如上图所示,这使得结果概率为 大于0.5.
As you can see from the graph above, this makes the results probability to be greater than 0.5.
解决方案:尝试使用没有最后一个sigmoid
的同一网络.
Solution: try the same network without the last sigmoid
.
这篇关于神经网络:避免在任何方向产生偏差的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!