【问题标题】:Solving XOR with 3 data points using Multi-Layered Perceptron使用多层感知器解决具有 3 个数据点的 XOR
【发布时间】:2018-07-14 20:18:15
【问题描述】:

已知 XOR 问题可以通过多层感知器解决,给定所有 4 个布尔输入和输出,它训练并记住重现 I/O 所需的权重。例如

import numpy as np
np.random.seed(0)

def sigmoid(x): # Returns values that sums to one.
    return 1 / (1 + np.exp(-x))

def sigmoid_derivative(sx):
    # See https://math.stackexchange.com/a/1225116
    return sx * (1 - sx)

# Cost functions.
def cost(predicted, truth):
    return truth - predicted

xor_input = np.array([[0,0], [0,1], [1,0], [1,1]])
xor_output = np.array([[0,1,1,0]]).T

X = xor_input
Y = xor_output

# Define the shape of the weight vector.
num_data, input_dim = X.shape
# Lets set the dimensions for the intermediate layer.
hidden_dim = 5
# Initialize weights between the input layers and the hidden layer.
W1 = np.random.random((input_dim, hidden_dim))

# Define the shape of the output vector. 
output_dim = len(Y.T)
# Initialize weights between the hidden layers and the output layer.
W2 = np.random.random((hidden_dim, output_dim))

num_epochs = 10000
learning_rate = 1.0

for epoch_n in range(num_epochs):
    layer0 = X
    # Forward propagation.

    # Inside the perceptron, Step 2. 
    layer1 = sigmoid(np.dot(layer0, W1))
    layer2 = sigmoid(np.dot(layer1, W2))

    # Back propagation (Y -> layer2)

    # How much did we miss in the predictions?
    layer2_error = cost(layer2, Y)
    # In what direction is the target value?
    # Were we really close? If so, don't change too much.
    layer2_delta = layer2_error * sigmoid_derivative(layer2)


    # Back propagation (layer2 -> layer1)
    # How much did each layer1 value contribute to the layer2 error (according to the weights)?
    layer1_error = np.dot(layer2_delta, W2.T)
    layer1_delta = layer1_error * sigmoid_derivative(layer1)

    # update weights
    W2 +=  learning_rate * np.dot(layer1.T, layer2_delta)
    W1 +=  learning_rate * np.dot(layer0.T, layer1_delta)

我们看到我们已经完全训练了网络来记忆 XOR 的输出:

# On the training data
[int(prediction > 0.5) for prediction in layer2] 

[出]:

[0, 1, 1, 0]

如果我们重新输入相同的输入,我们会得到相同的输出:

for x, y in zip(X, Y):
    layer1_prediction = sigmoid(np.dot(W1.T, x)) # Feed the unseen input into trained W.
    prediction = layer2_prediction = sigmoid(np.dot(W2.T, layer1_prediction)) # Feed the unseen input into trained W.
    print(int(prediction > 0.5), y)

[出]:

0 [0]
1 [1]
1 [1]
0 [0]

但是如果我们在没有数据点之一的情况下重新训练参数(W1 和 W2),即

xor_input = np.array([[0,0], [0,1], [1,0], [1,1]])
xor_output = np.array([[0,1,1,0]]).T

让我们删除最后一行数据并将其用作看不见的测试。

X = xor_input[:-1]
Y = xor_output[:-1]

对于其余相同的代码,无论我如何更改超参数,它都无法学习 XOR 函数并重现 I/O。

for x, y in zip(xor_input, xor_output):
    layer1_prediction = sigmoid(np.dot(W1.T, x)) # Feed the unseen input into trained W.
    prediction = layer2_prediction = sigmoid(np.dot(W2.T, layer1_prediction)) # Feed the unseen input into trained W.
    print(int(prediction > 0.5), y)

[出]:

0 [0]
1 [1]
1 [1]
1 [0]

即使我们打乱输入/输出:

# Shuffle the order of the inputs
_temp = list(zip(X, Y))
random.shuffle(_temp)
xor_input_shuff, xor_output_shuff = map(np.array, zip(*_temp))

我们无法完全训练 XOR 函数:'

for x, y in zip(xor_input, xor_output):
    layer1_prediction = sigmoid(np.dot(W1.T, x)) # Feed the unseen input into trained W.
    prediction = layer2_prediction = sigmoid(np.dot(W2.T, layer1_prediction)) # Feed the unseen input into trained W.
    print(x, int(prediction > 0.5), y)

[出]:

[0 0] 1 [0]
[0 1] 1 [1]
[1 0] 1 [1]
[1 1] 0 [0]

因此,当文献指出多层感知器(也称为基本深度学习)解决 XOR 时,是否意味着它可以在给定完整的输入/输出集的情况下完全学习和记忆权重,但不能假设缺少一个数据点,推广 XOR 问题?

这是回答者可以自己测试网络的 Kaggle 数据集的链接:https://www.kaggle.com/alvations/xor-with-mlp/

【问题讨论】:

标签: numpy neural-network deep-learning xor perceptron


【解决方案1】:

我认为学习(概括)异或和记忆异或是不同的事情。

如您所见,两层感知器可以记住 XOR,即存在损失最小且等于 0(绝对最小值)的权重组合。

如果权重是随机初始化的,您最终可能会遇到实际学习 XOR 而不仅仅是记住的情况。

请注意,多层感知器是非凸函数,因此可能存在多个最小值(甚至多个全局最小值)。当数据缺少一个输入时,会有多个最小值(并且所有最小值都相等),并且存在可以正确分类缺失点的最小值。因此,MLP 可以学习 XOR。 (尽管发现权重组合可能很难缺少点)。

经常有人认为神经网络是通用函数逼近器,甚至可以逼近无意义的标签。鉴于此,您可能想看看这项工作https://arxiv.org/abs/1611.03530

【讨论】:

    猜你喜欢
    • 2015-08-05
    • 2017-08-29
    • 2017-02-15
    • 2018-07-15
    • 2015-10-14
    • 2015-09-27
    • 1970-01-01
    • 2023-03-03
    • 2013-07-14
    相关资源
    最近更新 更多