为什么我的 XOR tensorflow 网络不学习?

Why isn't my XOR tensorflow network learning?

我无法弄清楚这个 XOR 神经网络做错了什么。也许我没有正确计算损失?损失一开始略有改善,然后准确率很快收敛到50%。有人可以指出我做错了什么吗?

这是一个最小的独立示例:

import numpy as np
import tensorflow as tf

n_inputs = 2
n_hidden = 3
n_outputs = 1

X = tf.placeholder(tf.float32, shape=(None, n_inputs), name='X')
y = tf.placeholder(tf.float32, shape=(None), name='y')

def neuron_layer(X, n_neurons, name, activation=None):
    with tf.name_scope(name):
        n_inputs = int(X.get_shape()[1])
        stddev = 2 / np.sqrt(n_inputs)
        init = tf.truncated_normal((n_inputs, n_neurons), stddev=stddev)
        W = tf.Variable(init, name="weights")
        b = tf.Variable(tf.zeros([n_neurons]), name="bias")
        Z = tf.matmul(X, W) + b
        if activation is not None:
            return activation(Z)
        else: return Z

with tf.name_scope('nn'):
    hidden = neuron_layer(X, n_hidden, name='hidden', activation=tf.nn.sigmoid)
    prediction_probabilities = neuron_layer(hidden, n_outputs, name='outputs', activation=tf.nn.sigmoid)

with tf.name_scope('loss'):
    mse_loss = tf.reduce_mean(tf.squared_difference(y, prediction_probabilities), name='loss')    

learning_rate = 0.1

with tf.name_scope('train'):
    optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate)
    training_op = optimizer.minimize(mse_loss)

with tf.name_scope('eval'):
    correct = tf.equal(tf.greater_equal(prediction_probabilities,0.5), tf.cast(y,tf.bool))
    accuracy = tf.reduce_mean(tf.cast(correct, tf.float32))

init = tf.global_variables_initializer()

X_train = [
    (0, 0),
    (0, 1),
    (1, 0),
    (1, 1)
]
y_train = [0,1,1,0]

with tf.Session() as sess:
    init.run()
    for epoch in range(500):
        _, mse, acc = sess.run([training_op, mse_loss, accuracy], 
                               feed_dict={X: np.array(X_train), y: np.array(y_train)})
        print("mse: %.4f, accuracy: %.2f" % (mse, acc))

您的代码完全没问题。问题出在您的输入上。您需要传递二维列表,而不是一维列表。每个内部列表都是一个单一维度的输入,这就是 tensorflow 解析它们的方式。

y_train = [[0],[1],[1],[0]]

您的代码之后运行良好。

...
mse: 0.0002, accuracy: 1.00