是一个识别手写数字图片的计算机视觉集,它包含各类手写数字图片和每张图片对应的标签函数
softmax回归是logistic回归的一种,它是多元分类(包含二分类)。测试
sotfmax回归能够把多分类任务多输出转换为各类类别的可能几率,把最大几率值对应的类别做为输入样本的输出类别预测atom
sotfmax激活函数公式:spa
z[l]=W[l]a[l]+b[l]code
g(z[l])=e(z[l])blog
理解sotfmax:索引
sotfmax的loss function:图片
单个样本 L(ŷ ,y)=∑yjlogŷj, j=[1,...,x] y为标签值,ŷ为预测几率,x为输入特征数量
input
样本集J(w[1],b[1],…)=1/m∑L(ŷ (i),y(i)) it
sotfmax梯度降低公式: ∂J/∂z[L]=dz[L]=ŷ −y
训练样本:60000个(mnist.train),其中55000个用于训练,5000个用于验证
测试样本:10000个(mnist.test)
每个MNIST数据单元包含两部分:图片(mnist.train.images)和标签(mnist.train.labels),每张图片包含28像素X28像素,用向量表示长度是28X28=784,所以图片(mnist.train.images)为[60000, 784]的张量,第一个维度用了索引图片,第二个维度用来索引每张图片的像素点,标签介于0-9的数字,向量化表示为[1,0,0,0,0,0,0,0,0],所以标签mnist.train.labels)为[60000, 10]的张量
import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data mnist = input_data.read_data_sets('MNIST_data/', one_hot=True) # 输入特征占位 x = tf.placeholder('float', [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() with tf.Session() as sess: sess.run(init) # 开始训练模型 for _ 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}))