【发布时间】: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/
【问题讨论】:
-
我喜欢 yoav 的观点。这有点不可能
标签: numpy neural-network deep-learning xor perceptron