码迷,mamicode.com
首页 > 其他好文 > 详细

TensorFlow 的MNIST程序入门

时间:2018-03-09 12:37:13      阅读:183      评论:0      收藏:0      [点我收藏+]

标签:方便   argmax   python   flow   imp   less   max   fine   use   

http://www.tensorfly.cn/tfdoc/tutorials/mnist_pros.html

 

———————————————————————————————————————————————————————————————————————————————

# -*- coding: utf-8 -*-
"""
Created on Fri Mar  9 10:16:39 2018

@author: DBSF
"""
import input_data
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
import tensorflow as tf
sess = tf.InteractiveSession()

x = tf.placeholder("float", shape=[None, 784])
y_ = tf.placeholder("float", shape=[None, 10])
W = tf.Variable(tf.zeros([784,10]))
b = tf.Variable(tf.zeros([10]))
sess.run(tf.initialize_all_variables())
y=tf.nn.softmax(tf.matmul(x,W)+b)
cross_entropy = -tf.reduce_sum(y_*tf.log(y))
train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy)
for i in range(1000):
  batch = mnist.train.next_batch(50)
  train_step.run(feed_dict={x: batch[0], y_: batch[1]})
 
correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
print (accuracy.eval(feed_dict={x: mnist.test.images, y_: mnist.test.labels}))

 

————————————————————————————————————————————————————————————————————————————————

 

这个程序需要用到input_data的文件,官网地址无法访问,贴出此文件,保存到统一文件夹下,改名为input_data.py,方便主程序调用

————————————————————————————————————————————————————————————————————————————————

 

# Copyright 2015 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================

"""Functions for downloading and reading MNIST data."""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import gzip
import os
import tempfile

import numpy
from six.moves import urllib
from six.moves import xrange  # pylint: disable=redefined-builtin
import tensorflow as tf
from tensorflow.contrib.learn.python.learn.datasets.mnist import read_data_sets

TensorFlow 的MNIST程序入门

标签:方便   argmax   python   flow   imp   less   max   fine   use   

原文地址:https://www.cnblogs.com/TheKat/p/8533200.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!