python - 无法用 Python 中的两个隐藏神经元解决 XOR 问题

标签 python numpy neural-network

我有一个小型的 3 层神经网络,其中包含两个输入神经元、两个隐藏神经元和一个输出神经元。我试图坚持以下仅使用 2 个隐藏神经元的格式。

enter image description here

我试图展示如何将其用作 XOR 逻辑门,但是只有两个隐藏的神经元在 1,000,000 次迭代后得到以下糟糕的输出!

Input: 0 0   Output:  [0.01039096]
Input: 1 0   Output:  [0.93708829]
Input: 0 1   Output:  [0.93599738]
Input: 1 1   Output:  [0.51917667]

如果我使用三个隐藏的神经元,我将通过 100,000 次迭代获得更好的输出:

Input: 0 0   Output:  [0.01831612]
Input: 1 0   Output:  [0.98558057]
Input: 0 1   Output:  [0.98567602]
Input: 1 1   Output:  [0.02007876]

我在隐藏层中有 3 个神经元但在隐藏层中有两个神经元时得到了不错的输出。为什么?

根据下面的评论,这 repo包含使用两个隐藏神经元解决 XOR 问题的 high 代码。

我不知道我做错了什么。任何建议表示赞赏! 附上我的代码:

import numpy as np
import matplotlib
from matplotlib import pyplot as plt


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


alpha = [0.7]

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

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

# seed random numbers to make calculation deterministic
np.random.seed(1)

# initialise weights randomly with mean 0
syn0 = 2 * np.random.random((2, 3)) - 1  # 1st layer of weights synapse 0 connecting L0 to L1
syn1 = 2 * np.random.random((3, 1)) - 1  # 2nd layer of weights synapse 0 connecting L1 to L2

# Randomize inputs for stochastic gradient descent
data = np.hstack((X, y))    # append Input and output dataset
np.random.shuffle(data)     # shuffle
x, y = np.array_split(data, 2, 1)    # Split along vertical(1) axis

for iter in range(100000):
    for i in range(4):
        # forward prop
        layer0 = x[i]  # Input layer
        layer1 = sigmoid(np.dot(layer0, syn0))  # Prediction step for layer 1
        layer2 = sigmoid(np.dot(layer1, syn1))  # Prediction step for layer 2

        layer2_error = y[i] - layer2  # Compare how well layer2's guess was with input

        layer2_delta = layer2_error * sigmoid(layer2, deriv=True)  # Error weighted derivative step

        if iter % 10000 == 0:
            print("Error: ", str(np.mean(np.abs(layer2_error))))
            plt.plot(iter, layer2_error, 'ro')


        # Uses "confidence weighted error" from l2 to establish an error for l1
        layer1_error = layer2_delta.dot(syn1.T)

        layer1_delta = layer1_error * sigmoid(layer1, deriv=True)  # Error weighted derivative step

        # Since SGD we need to dot product two 1D arrays. This is how.
        syn1 += (alpha * np.dot(layer1[:, None], layer2_delta[None, :]))  # Update weights
        syn0 += (alpha * np.dot(layer0[:, None], layer1_delta[None, :]))

    # Training was done above, below we re run to test algorithm

    layer0 = X  # Input layer
    layer1 = sigmoid(np.dot(layer0, syn0))  # Prediction step for layer 1
    layer2 = sigmoid(np.dot(layer1, syn1))  # Prediction step for layer 2


plt.show()
print("output after training: \n")
print("Input: 0 0 \t Output: ", layer2[0])
print("Input: 1 0 \t Output: ", layer2[1])
print("Input: 0 1 \t Output: ", layer2[2])
print("Input: 1 1 \t Output: ", layer2[3])

最佳答案

这是因为您没有考虑神经元的任何偏差。 您仅使用权重来尝试拟合 XOR 模型。

在隐藏层中有 2 个神经元的情况下,网络欠拟合,因为它无法补偿偏差。

当您在隐藏层中使用 3 个神经元时,额外的神经元会抵消由于缺乏偏差而造成的影响。

这是异或门的网络示例。您会注意到添加到隐藏层的 theta(偏差)。这为网络提供了一个额外的参数来进行调整。

enter image description here

Additional resources

关于python - 无法用 Python 中的两个隐藏神经元解决 XOR 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56308413/

相关文章:

python - 遍历numpy中的矩阵

python - python中的3d sobel算法?

python - NumPy 数组中的矢量化字符串处理

tensorflow - tensorflow 中的 Pytorch 等效功能?

python - 使用 Keras 编程多输入神经网络架构

haskell - fmap 有两个功能

Python TKinter 下拉菜单问题

python 2.7 : reload(sys) disables error messages and print in Windows

python - 使用 Resnet 的图像生成器

python - 在并行程序中播种随机数生成器