【问题标题】:Tensorflow learning XOR with linear function even though it shouldn'tTensorflow 学习 XOR 与线性函数,即使它不应该
【发布时间】:2017-02-13 02:56:24
【问题描述】:

我正在 tensorflow 中实现一个简单的网络,出于教学目的,我试图证明线性变换:

yhat = w(Wx + c) + b

无法学习 XOR。但现在的问题是,就我目前的实现而言,确实如此!这表明代码中存在错误。请解释一下?

############################################################
'''
    dummy data
'''
x_data = [[0.,0.],[0.,1.],[1.,0.],[1.,1.]]
y_data = [[0],[1],[1],[0]]

############################################################
'''
    Input and output
'''
X = tf.placeholder(tf.float32, shape = [4,2], name = 'x')
Y = tf.placeholder(tf.float32, shape = [4,1], name = 'y')

'''
    Network parameters
'''
W = tf.Variable(tf.random_uniform([2,2],-1,1), name = 'W')
c = tf.Variable(tf.zeros([2])                , name = 'c')
w = tf.Variable(tf.random_uniform([2,1],-1,1), name = 'w')
b = tf.Variable(tf.zeros([1])                , name = 'b')

############################################################
'''
    Network 1:

    function: Yhat = (w (x'W + c) + b)
    loss    : \sum_i Y * log Yhat
''' 
H1    = tf.matmul(X,  W) + c
Yhat1 = tf.matmul(H1, w) + b


cross_entropy1 = -tf.reduce_sum(
                Y*tf.log(
                        tf.clip_by_value(Yhat1,1e-10,1.0)
                        )
                )

step1 = tf.train.AdamOptimizer(0.01).minimize(cross_entropy1)

'''
    Train
'''

writer = tf.train.SummaryWriter("./logs/xor_logs.graph_def")
graph1 = tf.initialize_all_variables()
sess1  = tf.Session()
sess1.run(graph1)

for i in range(100):
    sess1.run(step1, feed_dict={X: x_data, Y: y_data})


'''
    Evaluation
''' 
corrects = tf.equal(tf.argmax(Y,1), tf.argmax(Yhat1,1))
accuracy = tf.reduce_mean(tf.cast(corrects, tf.float32))
r        = sess1.run(accuracy, feed_dict={X: x_data, Y: y_data})
print ('accuracy: ' + str(r * 100) + '%')

目前准确度为100%,尽管它应该为75%。

【问题讨论】:

    标签: tensorflow neural-network


    【解决方案1】:

    tf.argmax(Y,1) 将返回 [0,0,0,0]。这不是你想要的。

    【讨论】:

    • 您能提出一个解决方案吗?
    • 为什么 argmax(Y,1) 给我 [0,0,0,0]?
    • argmax 为您提供沿轴的最大值的索引。所以我们在我们的目标是 1 热时使用它,例如(例如 10 个数字中的 1 个)。你的 Y 值不是一个热的,它们只是长度为 1 的目标,所以 argmax 只返回 0。因此,要么使它们成为一个热点(即 [[1,0]、[0,1]、[0,1]、[1,0]]),要么修复您的正确性函数以仅针对 Y 而不是 argmax( Y,1)
    • 但是当我这样做corrects2 = tf.equal(tf.cast(Y,tf.int64), tf.argmax(Yhat2,1))时,现在即使是非线性函数也能输出50%的准确率。
    • 太棒了!所以现在想想(或只是打印出) tf.argmax(YHat2, 1) 返回的内容。那是你真正想要的吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-03-15
    • 1970-01-01
    • 2019-01-19
    • 2017-12-09
    • 2016-11-28
    • 2017-08-03
    • 1970-01-01
    相关资源
    最近更新 更多