# -*- coding: utf-8 -*-
# @Time : 2018/12/14 11:09
# @Author : WenZhao
# @Email : [email protected]
# @File : mnistSoftmax.py
# @Software: PyCharm
'''
Mnist手写数字数据集softmax识别
'''
import numpy as np
import tensorflow as tf
# 下载数据集
from tensorflow.examples.tutorials.mnist import input_data
mnist=input_data.read_data_sets("./data/MNIST_data/",one_hot=True)
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]))
# softmax回归
y=tf.nn.softmax(tf.matmul(x,W)+b)
# 交叉熵
loss=-tf.reduce_sum(y_*tf.log(y))
train_step=tf.train.GradientDescentOptimizer(0.01).minimize(loss)
sess=tf.Session()
sess.run(tf.global_variables_initializer())
for i in range(1100):
# 构建批
batch=mnist.train.next_batch(50)
sess.run(train_step,feed_dict={x:batch[0],y_:batch[1]})
if i%50==0:
print(sess.run(loss,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"))
# 测试集准确率
acc=sess.run(accuracy,feed_dict={x:mnist.test.images,y_:mnist.test.labels})
print(acc)
Mnist手写数字数据集softmax识别
猜你喜欢
转载自blog.csdn.net/agjllxchjy/article/details/85002288
今日推荐
周排行