XOR神经网络不会收敛

时间:2018-06-07 08:11:05

标签: machine-learning neural-network xor backpropagation

我一直在尝试复制学习XOR门的[2,2,1]神经网络,但我不能让我的模型收敛。我不确定我哪里出错了,我真的很感激一些反馈。

这是我最初在Jupyter笔记本中运行的类的代码:

# Define activation function and derivative
def sigmoid(x):
    return 1 / (1 + np.exp(-x))
def dsigmoid(x):
    return sigmoid(x) * (1 - sigmoid(x))

# Define loss function and derivative
def mse(targets, predictions):
    return (1 / (2 * len(targets))) * (targets - predictions) ** 2
def dmse(targets, predictions):
    return targets - predictions

# Feedforward function
def feedforward(X):

    z1 = np.dot(l1_weights, X.T) + l1_biases
    a1 = sigmoid(z1)
    z2 = np.dot(l2_weights, a1) + l2_biases
    a2 = sigmoid(z2)

    return z1, a1, z2, a2

# Backpropogation function
def backprop(x, y):

    z1, a1, z2, a2 = feedforward(x)

    delta_l2 = dmse(y.T, a2) * dsigmoid(z2)
    delta_l1 = np.dot(l2_weights.T, delta_l2) * dsigmoid(z1)

    l2_dw = np.dot(delta_l2, a1.T)
    l2_db = delta_l2

    l1_dw = np.dot(delta_l1, x)
    l1_db = delta_l1

    return l1_dw, l1_db, l2_dw, l2_db

# Input data and labels
X = np.array([[1,0],[0,1],[1,1],[0,0]])
Y = np.array([[1],[1],[0],[0]])

# Create the data set
data = [(np.array(x), np.array(y)) for x, y in zip(X, Y)]

# Randomly initialize weights and biases
np.random.seed(1)
l1_weights = np.random.randn(2,2) 
l2_weights = np.random.randn(1,2)
l1_biases = np.random.randn(2,1)  
l2_biases = np.random.randn(1,1) 

# Train the model
epochs = 100000
eta = 0.05
batch_size = 4

# Batch the data
batches = [data[i:i + batch_size] for i in range(0, len(data), batch_size)]

for batch in batches:

    feature_batch = np.array([x[0] for x in batch])
    label_batch = np.array([x[1] for x in batch])

    # Update network weights with stochastic gradient descent
    l1_dw, l1_db, l2_dw, l2_db = backprop(feature_batch, label_batch)

    l1_weights = l1_weights - (eta / batch_size) * l1_dw
    l2_weights = l2_weights - (eta / batch_size) * l2_dw

    l1_biases = l1_biases - (eta / batch_size) * l1_db
    l2_biases = l2_biases - (eta / batch_size) * l2_db

以下是培训后的示例输出:

训练时期= 100000

学习率= 0.5

记录期= 10

批量大小= 4

Error for period 1: 0.135619
Error for period 2: 0.249879
Error for period 3: 0.249941
Error for period 4: 0.249961
Error for period 5: 0.249956
Error for period 6: 0.249963
Error for period 7: 0.249972
Error for period 8: 0.249983
Error for period 9: 0.249986
Error for period 10: 0.249981

# OUTPUT
print(feedforward(X)[-1])

>>> array([[0.99997653, 0.99995257, 0.99997791, 0.99995534]])

请帮忙!

0 个答案:

没有答案