TensorFlow-mnist-学习

TensorFlow-mnist-学习
装好TensorFlow环境后,开始学习mnist手写数字识别

打开终端:输入:source activate 环境名字,如下

source activate dataLibrary

然后输入:
ipython
终端就会出现如下的信息

Python 2.7.13 |Continuum Analytics, Inc.| (default, Dec 20 2016, 23:05:08) 

Type "copyright", "credits" or "license" for more information.


IPython 5.3.0 -- An enhanced Interactive Python.

?         -> Introduction and overview of IPython's features.

%quickref -> Quick reference.

help      -> Python's own help system.

object?   -> Details about 'object', use 'object??' for extra details.


In [1]:


看到上面的In [1]:,现在要下载mnist的资源,拷贝下面的代码到终端并回车运行:最好加as input_data

import tensorflow.examples.tutorials.mnist.input_data as input_data
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)

之后等一段时间会提示下面的完成信息:

In [1]:importtensorflow.examples.tutorials.mnist.input_data

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

   ...: 


Successfully downloaded train-images-idx3-ubyte.gz 9912422 bytes.

Extracting MNIST_data/train-images-idx3-ubyte.gz

Successfully downloaded train-labels-idx1-ubyte.gz 28881 bytes.

Extracting MNIST_data/train-labels-idx1-ubyte.gz

Successfully downloaded t10k-images-idx3-ubyte.gz 1648877 bytes.

Extracting MNIST_data/t10k-images-idx3-ubyte.gz

Successfully downloaded t10k-labels-idx1-ubyte.gz 4542 bytes.

Extracting MNIST_data/t10k-labels-idx1-ubyte.gz


或者打开notebook,在notebook里编辑,终端输入:
source activate dataweekends   然后输入下面:

ipython notebook

之后会打开一个网页,从里面新建一个Python

然后拷贝下面代码,就可以了


import tensorflow as tf
import tensorflow.examples.tutorials.mnist.input_data as input_data 
mnist = input_data.read_data_sets("/Users/用户名/Downloads/mnist", 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("float", [None,10])
cross_entropy = -tf.reduce_sum(y_*tf.log(y))
train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy)


init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init)
for i in range(1000):
  batch_xs, batch_ys = mnist.train.next_batch(100)
  sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})


correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
print sess.run(accuracy, feed_dict={x: mnist.test.images, y_: mnist.test.labels})


结果是:0.9159



TensorFlow官方教程:虹膜花识别

运行官方的代码:https://github.com/tensorflow/models/tree/master/samples/core/get_started

下载代码运行,会发现报一堆的错误:
 比如可能是tensorflow版本太低导致参数问题。
 或者没有安装某个包比如numpy或者keras等等。
 或者dataset返回的数据不是格式不是需要的格式:features should be a dictionary of `Tensor`s. Given type
所以,了解dataset很有必要,不说了,先看,然后再更新



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值