tensorflow学习笔记(MNIST)

# -*- coding:utf-8 -*-
# "-*- coding:utf-8 -*-" 定义源代码的编码格式,#
from tensorflow.python.platform import gfile
from tensorflow.contrib.learn.python.learn.datasets.mnist import extract_images
from tensorflow.contrib.learn.python.learn.datasets.mnist import extract_labels
from tensorflow.contrib.learn.python.learn.datasets.mnist import DataSet
from tensorflow.contrib.learn.python.learn.datasets import base

import numpy as np
import pandas as pd
import tensorflow as tf

def input_data_from_local():
    
    # train_x,train_y,test_x,test_y的文件路径
    TRAIN_IMAGES = 'train-images-idx3-ubyte.gz'
    TRAIN_LABELS = 'train-labels-idx1-ubyte.gz'
    TEST_IMAGES = 't10k-images-idx3-ubyte.gz'
    TEST_LABELS = 't10k-labels-idx1-ubyte.gz'
    
    # 打开文件使用with操作,使用结束后自动关闭文件
    with gfile.Open(TRAIN_IMAGES,'rb') as f:
        train_images=extract_images(f)
    #print (train_images.shape)
    
    # 读取labels时one-hot=True,可以将[1,2,3]转化为[[0,1,0,0,0,0,0,0,0,0],[0,0,1,0,0,0,0,0,0,0,0],[0,0,0,1,0,0,0,0,0,0]]
    with gfile.Open(TRAIN_LABELS,'rb') as f:
        train_labels=extract_labels(f,one_hot=True)
    #print (train_labels.shape)
    
    with gfile.Open(TEST_IMAGES,'rb') as f:
        test_images=extract_images(f)
    #print (test_images.shape)
    
    with gfile.Open(TEST_LABELS,'rb') as f:
        test_labels=extract_labels(f,one_hot=True)
    #print (test_labels.shape)
    
    validation_size=5000
    validation_images=train_images[:validation_size]
    validation_labels=train_labels[:validation_size]
    train_images=train_images[validation_size:]
    train_labels=train_labels[validation_size:]
    
    dtype = np.float32
    # options有什么作用?
    options=dict(dtype=dtype,reshape=True,seed=None)
    
    train=DataSet(train_images, train_labels, **options)
    validation=DataSet(validation_images, validation_labels, **options)
    test = DataSet(test_images, test_labels, **options)
    
    return base.Datasets(train=train, validation=validation, test=test)
    
def solve(mnist):
    
    # x不是一个特定的值,而是一个占位符placeholder,张量的形状为[None,784],None表示可以是任意值
    # x = tf.placeholder('float', [None, 784])
    x = tf.placeholder(tf.float32, [None, 784])
    
    # W,b 模型参数,用可修改的张量Variable来表示
    W = tf.Variable(tf.zeros([784, 10]))
    
    b = tf.Variable(tf.zeros([10]))
    
    # y = w * x + b,然后再softmax计算出预测值
    y = tf.nn.softmax(tf.matmul(x, W) + b)
    
    #真实值
    y_ = tf.placeholder('float', [None, 10])
    
    # 损失函数
    cross_entropy  = -tf.reduce_sum(y_*tf.log(y))
    
    # 梯度下降,步长为0.01,最小化损失函数
    train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy)
    
    # 初始化变量
    init = tf.initialize_all_variables()
    
    # sess启动,并初始化
    sess = tf.Session()
    
    sess.run(init)
    
    # 训练模型,
    for i in range(5000):
        batch_xs, batch_ys = mnist.train.next_batch(100)
        #print (batch_xs)
        #print (batch_ys)
        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}))
    
if __name__ == '__main__':
    #print('begin')

    mnist=input_data_from_local()
    solve(mnist)
    #print('end')

你可能感兴趣的:(python)