前言:实现了mnist数据集手写数字识别
代码中包含讲解、重要函数的解释!
今天学的函数:
tf.reduce_mean 函数
用于计算张量tensor沿着指定的数轴(tensor的某一维度)上的的平均值,主要用作降维或者计算tensor(图像)的平均值。
#第一个参数input_tensor: 输入的待降维的tensor;
# 第二个参数axis: 指定的轴,如果不指定,则计算所有元素的均值;
# 第三个参数keep_dims:是否降维度,设置为True,输出的结果保持输入tensor的形状,设置为False,输出结果会降低维度;
# 第四个参数name: 操作的名称;第五个参数 reduction_indices:在以前版本中用来指定轴,已弃用;
#argmax(actv,1) 求行里面最大值的索引 ,并与正确标签比较,返回TRUE 、 FALSE
#cast() 把pred得到的TRUE FALSE 转化成 1,0 ,并计算平均值,求得此次分类的精确度
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
import input_data
mnist = input_data.read_data_sets('data/', one_hot=True)
trainimg = mnist.train.images
trainlabel = mnist.train.labels
testimg = mnist.test.images
testlabel = mnist.test.labels
print ("MNIST loaded")
print (trainimg.shape)
print (trainlabel.shape)
print (testimg.shape)
print (testlabel.shape)
#print (trainimg)
print (trainlabel[0])# 行向量[0,0,0,0,0,0,0,1,0,0]
x = tf.placeholder("float", [None, 784]) # 占存储空间 无数*784
y = tf.placeholder("float", [None, 10]) # None is for infinite
W = tf.Variable(tf.zeros([784, 10]))
b = tf.Variable(tf.zeros([10]))
# LOGISTIC REGRESSION MODEL
actv = tf.nn.softmax(tf.matmul(x, W) + b) #计算得分*10个 ei/sum(ej)
# COST FUNCTION
cost = tf.reduce_mean(-tf.reduce_sum(y*tf.log(actv),axis=1))# y*tf.log(actv)只留下标签对应的(正确类别的)log得分,再取每行的和(降维成55000*1),再取平均降维成1个数
# OPTIMIZER
#tf.reduce_mean 函数用于计算张量tensor沿着指定的数轴(tensor的某一维度)上的的平均值,主要用作降维或者计算tensor(图像)的平均值。
#第一个参数input_tensor: 输入的待降维的tensor;
# 第二个参数axis: 指定的轴,如果不指定,则计算所有元素的均值;
# 第三个参数keep_dims:是否降维度,设置为True,输出的结果保持输入tensor的形状,设置为False,输出结果会降低维度;
# 第四个参数name: 操作的名称;第五个参数 reduction_indices:在以前版本中用来指定轴,已弃用;
learning_rate = 0.01
optm = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)#采用梯度下降法使cost减小
# PREDICTION
#argmax(actv,1) 求行里面最大值的索引 ,并与正确标签比较,返回TRUE 、 FALSE 55000*1
pred = tf.equal(tf.argmax(actv, 1), tf.argmax(y, 1))
# ACCURACY
#cast() 把pred得到的TRUE FALSE 转化成 1,0 ,并计算平均值,求得此次分类的精确度
accr = tf.reduce_mean(tf.cast(pred, "float"))
# INITIALIZER
init = tf.global_variables_initializer()
training_epochs = 50 # 所有样本迭代50次
batch_size = 100 #每次迭代100张
display_step = 5 #展示
# SESSION
sess = tf.Session()
sess.run(init)
# MINI-BATCH LEARNING
for epoch in range(training_epochs):
avg_cost = 0.
num_batch = int(mnist.train.num_examples/batch_size)
for i in range(num_batch):
batch_xs, batch_ys = mnist.train.next_batch(batch_size)
#batch_xs, 值data batch_ys 标签 next_batch(batch_size):mnist自带函数,获得下一batch
sess.run(optm, feed_dict={x: batch_xs, y: batch_ys})
# 前面的placeholdr占存储空间,现在feed_dict赋值
feeds = {x: batch_xs, y: batch_ys}
avg_cost += sess.run(cost, feed_dict=feeds)/num_batch
#求1次迭代的平均cost
# DISPLAY
if epoch % display_step == 0: #每5个epoch打印一次 训练和测试的数据
feeds_train = {x: batch_xs, y: batch_ys}
feeds_test = {x: mnist.test.images, y: mnist.test.labels}
train_acc = sess.run(accr, feed_dict=feeds_train)
test_acc = sess.run(accr, feed_dict=feeds_test)
print ("Epoch: %03d/%03d cost: %.9f train_acc: %.3f test_acc: %.3f"
% (epoch, training_epochs, avg_cost, train_acc, test_acc))
print ("DONE")
input_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
大概20s,结果: