hello world

1.安装

sudo pip  install --index http://pypi.douban.com/simple --trusted-host pypi.douban.com tensorflow

2.mnist_softmax.py脚本

#!/usr/local/python

from tensorflow.examples.tutorials.mnist import input_data

mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)

import tensorflow as tf

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])

#train

cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y), reduction_indices=[1]))

train_step = tf.train.GradientDescentOptimizer(0.05).minimize(cross_entropy)

sess = tf.InteractiveSession()

tf.global_variables_initializer().run()

for _ in range(1000):

      batch_xs, batch_ys = mnist.train.next_batch(100)

      sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})

#Evaluating

correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1))

accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

print(sess.run(accuracy, feed_dict={x: mnist.test.images, y_: mnist.test.labels}))

3.运行demo

从http://yann.lecun.com/exdb/mnist/下载mnist数据放到tensorflow/examples/tutorials/mnist/MNIST_data;在tensorflow/examples/tutorials/mnist/运行mnist_softmax.py

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 简单线性回归 import tensorflow as tf import numpy # 创造数据 x_dat...
    CAICAI0阅读 3,564评论 0 49
  • 一.目的 类似学习开发语言的第一个代码,Hello World! 机器学习中,我们通过MNIST来学习手写输入法的...
    Coming0524阅读 6,530评论 2 8
  • 自编码器和多层感知机 整个神经网络的流程: 定义算法公式,也就是神经网络的forward时的计算定义loss,选定...
    阿面a阅读 2,289评论 0 4
  • 所谓偶遇不是真的遇到那个一见钟情的人,而是遇到那些能让你生活热情指数飙升或是开阔视野的人事物。 按照预约时间到医院...
    一岁喵咪阅读 695评论 4 1
  • 第二曲 江霄云和黎鸾分别以各自中学的第一名考取了振坤中学。 江霄云遇见黎鸾是在高一新生的欢迎典礼...
    孤怪_阅读 1,209评论 0 2