tensorflow实现感知机还是非常方便的,这里我们先实现一个简单的例子, 这里是最简单的感知机的例子, 可以做为入门的调试代码用

import tensorflow as tf

a = [[1, 1], [1, 0], [0, 1], [0, 0]]
b = [[1], [0], [0], [0]]
input = 2
h1 = 10 #这里不使用隐层
w1 = tf.Variable(tf.truncated_normal([input,1],stddev=0.1))
b1 = tf.Variable(tf.zeros([1]))
x = tf.placeholder(tf.float32,[None,input])
#hidden1 = tf.nn.relu(tf.matmul(x,w1)+b1)
y = tf.nn.softmax(tf.matmul(x,w1) + b1)
y_ = tf.placeholder(tf.float32,[None,1])
cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y),reduction_indices = [1]))
train_step = tf.train.AdagradOptimizer(0.3).minimize(cross_entropy)
tf.global_variables_initializer().run()
for i in range(30):
    batch_xs, batch_ys = a, b
    train_step.run({x: batch_xs, y_: batch_ys,keep_prob:0.75})
correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1))  
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))      
print(accuracy.eval({x:mnist.test.images,y_:mnist.test.labels,keep_prob:1.0}))


dmlllll
47 声望6 粉丝

引用和评论

0 条评论