【发布时间】: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%。
【问题讨论】: