python - 为什么我的 TensorFlow 神经网络的 XOR 精度只有 0.5 左右?

标签 python machine-learning tensorflow neural-network

我在 TensorFlow 中为 XOR 输入编写了一个神经网络。我使用了 1 个隐藏层和 2 个单元以及 softmax 分类。输入的形式为 <1, x_1, x_2, Zero, one> ,其中

  • 1 是偏差
  • 对于所有组合 {00, 01, 10, 11},x_1 和 x_2 均介于 0 和 1 之间。选择在 0 或 1 附近呈正态分布
  • 零:如果输出为零,则为 1
  • one:如果输出为一,则为 1

准确度始终在 0.5 左右。出了什么问题?是神经网络的架构错误,还是代码有问题?

import tensorflow as tf
import numpy as np
from random import randint

DEBUG=True

def init_weights(shape):
    return tf.Variable(tf.random_normal(shape, stddev=0.01))


def model(X, weight_hidden, weight_output):
    # [1,3] x [3,n_hiddent_units] = [1,n_hiddent_units]
    hiddern_units_output = tf.nn.sigmoid(tf.matmul(X, weight_hidden))

    # [1,n_hiddent_units] x [n_hiddent_units, 2] = [1,2]
    return hiddern_units_output
    #return tf.matmul(hiddern_units_output, weight_output)


def getHiddenLayerOutput(X, weight_hidden):
    hiddern_units_output = tf.nn.sigmoid(tf.matmul(X, weight_hidden))
    return hiddern_units_output

total_inputs = 100
zeros = tf.zeros([total_inputs,1])
ones = tf.ones([total_inputs,1])
around_zeros = tf.random_normal([total_inputs,1], mean=0, stddev=0.01)
around_ones = tf.random_normal([total_inputs,1], mean=1, stddev=0.01)

batch_size = 10
n_hiddent_units = 2
X = tf.placeholder("float", [None, 3])
Y = tf.placeholder("float", [None, 2])

weight_hidden = init_weights([3, n_hiddent_units])
weight_output = init_weights([n_hiddent_units, 2])

hiddern_units_output = getHiddenLayerOutput(X, weight_hidden)
py_x = model(X, weight_hidden, weight_output)

#cost = tf.square(Y - py_x)
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=py_x, labels=Y))
train_op = tf.train.GradientDescentOptimizer(0.05).minimize(cost)

with tf.Session() as sess:
    tf.global_variables_initializer().run()

    trX_0_0 = sess.run(tf.concat([ones, around_zeros, around_zeros, ones, zeros], axis=1))
    trX_0_1 = sess.run(tf.concat([ones, around_zeros, around_ones, zeros, ones], axis=1))
    trX_1_0 = sess.run(tf.concat([ones, around_ones, around_zeros, zeros, ones], axis=1))
    trX_1_1 = sess.run(tf.concat([ones, around_ones, around_ones, ones, zeros], axis=1))
    trX = sess.run(tf.concat([trX_0_0, trX_0_1, trX_1_0, trX_1_1], axis=0))
    trX = sess.run(tf.random_shuffle(trX))
    print(trX)

    for i in range(10):
        for start, end in zip(range(0, len(trX), batch_size), range(batch_size, len(trX) + 1, batch_size)):
            trY = tf.identity(trX[start:end,3:5])
            trY = sess.run(tf.reshape(trY,[batch_size, 2]))
            sess.run(train_op, feed_dict={ X: trX[start:end,0:3], Y: trY })

        start_index = randint(0, (total_inputs*4)-batch_size)
        y_0 = sess.run(py_x, feed_dict={X: trX[start_index:start_index+batch_size,0:3]})
        print("iteration :",i, " accuracy :", np.mean(np.absolute(trX[start_index:start_index+batch_size,3:5]-y_0)),"\n")

查看评论部分以获取更新的代码

最佳答案

问题出在随机分配的权重上。 Here是经过一系列试错后得到的修改版本。

关于python - 为什么我的 TensorFlow 神经网络的 XOR 精度只有 0.5 左右?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42516484/

相关文章:

numpy - 如何从距离矩阵中获取点坐标?

machine-learning - 尽管存在 -f 标志,Vowpal Wabbit 仍不保存模型

python - 在 sklearn 中训练后是否必须再次使用 fit() ?

python - Tensorflow 保护程序似乎覆盖了现有的已保存变量文件

python - python 3.4.3 上的 pydoop 安装

python - 从 settings.py 更改设置变量

python - setText() 在 Python 中不起作用

python - 失败前提条件错误: GetNext() failed after loading a Tensorflow Saved_Model

python-3.x - session 图为空

python - 在 Google Colab 中访问 '.pickle' 文件