【问题标题】:Tensorflow doesn't converge for XORTensorflow 不会针对 XOR 收敛
【发布时间】:2017-02-11 05:14:12
【问题描述】:

我尝试编写一个简单的 XOR 神经网络,但它永远不会收敛

我创建了一个具有 2 个输入、2 个隐藏节点和 1 个输出的 NN。

我在第一个隐藏层使用 relu,最后使用 softmax 来获取输出。

理论上它应该学习如何解决它并收敛?

import tensorflow as tf

sess = tf.InteractiveSession()

# define placeholder for input and output
x_ = tf.placeholder(tf.float32, shape=[4,2], name="x-input")
y_ = tf.placeholder(tf.float32, shape=[4,1], name="y-input")

# Configure weights and layers
W = tf.Variable(tf.random_uniform([2, 2], -.01, .01))
b = tf.Variable(tf.random_uniform([2], -.01, .01))
hidden  = tf.nn.relu(tf.matmul(x_,W) + b) # first layer.

W2 = tf.Variable(tf.random_uniform([2,1], -.1, .1))
b2 = tf.Variable(tf.zeros([1]))
hidden2 = tf.matmul(hidden, W2 + b2)
y = tf.nn.softmax(hidden2)

# Training function
cross_entropy = -tf.reduce_sum(y_*tf.log(hidden2))
train_step = tf.train.GradientDescentOptimizer(0.2).minimize(cross_entropy)

XOR_X = [[0,0],[0,1],[1,0],[1,1]]
XOR_Y = [[0],[1],[1],[0]]

init = tf.global_variables_initializer()
sess.run(init)
# Train on the input data
for i in range(100):
    sess.run([cross_entropy, train_step], feed_dict={x_: XOR_X, y_: XOR_Y})
    print ('W1', sess.run(W))
    print('Output ', sess.run(y, feed_dict={x_: XOR_X, y_: XOR_Y}))

【问题讨论】:

    标签: python machine-learning tensorflow neural-network artificial-intelligence


    【解决方案1】:

    错误...

    1. W2 权重应该在 -1 和 1 之间,因为它们没有使用 ReLu。第一层的权重也使用了 ReLu,所以我将它们设置为正值以避免死神经元。

    2. Softmax 没有意义,除非它是 1 个热向量层。 Sigmoid 更有意义。阅读 Softmax 的工作原理会有所帮助。

    3. 应在 y not hidden2 上减少总和

    4. hidden2 = tf.matmul(hidden, W2 + b2) 的括号不正确。应该是hidden2 = tf.matmul(hidden, W2) + b2

    5. -日志作为错误函数仅在您尝试将输出设为 1 而不是 0 时才有效。这是因为 -log(1) = 0,而当 -log(0) 时为无穷大。这会鼓励输出变为 1,而不是 0。如果您试图将一个输入的输出推到 0,另一个输入推到 1,则对 1 个热向量有好处。

    6. 隐藏层中的 2 个神经元确实有效。但它很容易受到初始化随机性的影响。使用额外的神经元(10 个而不是 2 个)可以减少初始化错误的影响。

    下面的代码有效。它使用成本函数来帮助不同输入收敛到 0 和 1。

    import tensorflow as tf
    sess = tf.InteractiveSession()
    
    # define placeholder for input, None as first argument means tensor can be any length
    x_ = tf.placeholder(tf.float32, shape=[4,2], name="x-input")
    y_ = tf.placeholder(tf.float32, shape=[4,1], name="y-input")
    
    # Configure weights and layers
    W = tf.Variable(tf.random_uniform([2, 10], 0.001, .01))
    b = tf.Variable(tf.zeros([10]))
    hidden  = tf.nn.relu(tf.matmul(x_,W) + b) # first layer.
    
    W2 = tf.Variable(tf.random_uniform([10,1], -1, 1))
    b2 = tf.Variable(tf.zeros([1]))
    hidden2 = tf.matmul(hidden, W2) + b2
    y = tf.nn.sigmoid(hidden2)
    
    # Training function + data
    cost = tf.reduce_mean(( (y_ * tf.log(y)) +
    ((1 - y_) * tf.log(1.0 - y)) ) * -1)
    train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cost)
    
    XOR_X = [[0,0],[0,1],[1,0],[1,1]]
    XOR_Y = [[0],[1],[1],[0]]
    
    init = tf.global_variables_initializer()
    sess.run(init)
    # Train on the input data
    for i in range(100000):
        sess.run(train_step, feed_dict={x_: XOR_X, y_: XOR_Y})
        if i % 2000 == 0:
            print ('W1', sess.run(W))
            print('Output ', sess.run(y, feed_dict={x_: XOR_X, y_: XOR_Y}))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-01-08
      • 2016-05-13
      • 2017-12-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-04
      相关资源
      最近更新 更多