zoukankan      html  css  js  c++  java
  • tensorflow学习笔记(二)

    tensorflow中自带的mnist手写数字识别,运用最简单的单层神经网络,softmax激活函数,极客学院上说准确率有91%,我今天调整到了92%!

    import tensorflow as tf
    import numpy as np
    import math
    import tensorflow.examples.tutorials.mnist as mn
    class Mnist:
    def __init__(self):
    sess = tf.InteractiveSession()
    self.mnist = mn.input_data.read_data_sets("E:\Python35\Lib\site-packages\tensorflow\examples\tutorials\mnist\MNIST_data",one_hot=True)
    self.x = tf.placeholder("float", shape=[None, 784])
    self.y_ = tf.placeholder("float", shape=[None, 10])
    self.W = tf.Variable(tf.zeros([784,10]))
    self.b = tf.Variable(tf.zeros([10]))
    self.y = tf.nn.softmax(tf.matmul(self.x,self.W) + self.b)
    self.cross_entropy = -tf.reduce_sum(self.y_*tf.log(self.y))
    sess.run(tf.global_variables_initializer())

    self.bestModel = None
    self.bestPredict = 0.0
    self.bestIter = 0
    self.bestRate = 0.0
    self.bestSample = 0

    self.iters = [1000,1200,1400]
    self.rates = [0.01,0.02]
    self.samples = [100,150,200]

    def train(self):
    for iter in self.iters:
    for rate in self.rates:
    train_step = tf.train.GradientDescentOptimizer(rate).minimize(self.cross_entropy)
    for sample in self.samples:
    self.optimizer(iter, rate, sample, train_step)

    def optimizer(self,iter,rate,sample,train_step):
    for i in range(iter):
    batch = self.mnist.train.next_batch(sample)
    model = train_step.run(feed_dict={self.x: batch[0], self.y_: batch[1]})
    correct_prediction = tf.equal(tf.argmax(self.y, 1), tf.argmax(self.y_, 1))
    accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
    predict = accuracy.eval(feed_dict={self.x: self.mnist.test.images, self.y_: self.mnist.test.labels})
    if predict > self.bestPredict:
    self.bestPredict = predict
    self.bestModel = model
    self.bestIter = iter
    self.bestRate = rate
    self.bestSample = sample

    def output(self):
    print("bestRate:",self.bestRate,"bestIter:",self.bestIter,"bestSample:",self.bestSample,"bestPredict:",self.bestPredict)

    if __name__ == '__main__':
    mnist = Mnist()
    mnist.train()
    mnist.output()


    E:Python35python.exe E:/PycharmProjects/test/com/python/machinelearning/tensorflowTest.py
    Extracting E:Python35Libsite-packages ensorflowexamples utorialsmnistMNIST_data rain-images-idx3-ubyte.gz
    Extracting E:Python35Libsite-packages ensorflowexamples utorialsmnistMNIST_data rain-labels-idx1-ubyte.gz
    Extracting E:Python35Libsite-packages ensorflowexamples utorialsmnistMNIST_data 10k-images-idx3-ubyte.gz
    Extracting E:Python35Libsite-packages ensorflowexamples utorialsmnistMNIST_data 10k-labels-idx1-ubyte.gz

    bestRate: 0.01 bestIter: 1000 bestSample: 100 bestPredict: 0.9193
  • 相关阅读:
    火车进出栈问题(卡特兰数)
    HDU 4699 Editor (对顶栈)
    HDU 6430 TeaTree (线段树合并)
    Exam 4895 Crowd Control
    Exam 4894 Booming Business
    8377: Playoff
    hdu 6345 Problem J. CSGO
    HDU 6437 Problem L.Videos
    Making the Grade
    poj2279——Mr. Young's Picture Permutations
  • 原文地址:https://www.cnblogs.com/txq157/p/7163776.html
Copyright © 2011-2022 走看看