试图学习tensorflow的人啊。

来源:互联网 发布:ab测试 算法 编辑:程序博客网 时间:2024/06/05 01:03

试图学习tensorflow

光看(看的是 《TensorFlow实战_黄文坚》)是不行的,写了好几遍,终于可以闭着眼睛写出来了。
有几处是死记硬背,希望以后能理解吧。

编辑器是anaconda-spyder,简单无脑,不用搞环境还是好啊。
以下就是tensorflow 的”hello world“

代码块

# y = x * W + b #解释一下,x是输入 y是输出 目标是寻找出参数W和b,使得该等式成立(绝大多数情况下)# x [50000, 784]     y [50000, 10]  import tensorflow as tffrom tensorflow.examples.tutorials.mnist import input_datamnist = input_data.read_data_sets("MNIST_data", one_hot=True) #获取数据x = tf.placeholder(tf.float32, [None, 784])W = tf.Variable(tf.zeros([784, 10]))b = tf.Variable(tf.zeros([10]))y = tf.nn.softmax(tf.matmul(x, W) + b)  #现实y_ = tf.placeholder(tf.float32, [None, 10])  #理想cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y), reduction_indices=[1]))  #判断理想与现实的差距的train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)sess = tf.InteractiveSession()tf.global_variables_initializer().run()for i in range(1000):    bat_x, bat_y_ = mnist.train.next_batch(100)    train_step.run(feed_dict={x:bat_x, y_:bat_y_})accuracy = tf.reduce_mean(tf.cast(tf.equal(tf.arg_max(y, 1), tf.arg_max(y_, 1)), tf.float32))print(accuracy.eval(feed_dict={x:mnist.test.images, y_:mnist.test.labels}))