我试着写一个简单的XOR的神经网络,但它从来没有收敛Tensorflow不收敛的XOR
我创建了一个与NN 2个输入,2个隐藏节点和1个输出。
我在第一个隐藏层上使用了relu,最后使用了softmax来获得输出。
理论上它应该学会如何解决它并收敛?因为他们没有使用RELU
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}))