TensorFlow 实现Softmax Regression识别手写数字

来源:互联网 发布:怎么在淘宝雇佣水军 编辑:程序博客网 时间:2024/04/28 14:10
使用TensorFlow实现一个简单的机器学习算法Softmax Regression,一个没有隐含层的最浅的神经网络。环境:Ubuntu + Anaconda(Spyder) + TensorFlow数据:MNIST(Mixed National Institute of Standards and Technology database)步骤:1)定义算法公司,也就是神经网络forward时的计算。2)定义loss,选定优化器,并指定优化器优化loss。3)迭代地对数据进行训练。4)在测试集或验证集上对准确率进行评测。

太多的理论赛不过代码的理解,下面见整个代码和注释。
#!/usr/bin/env python3# -*- coding: utf-8 -*-"""Created on Thu Sep 14 16:33:11 2017Softmax Regression using TensorFlow@author: z"""##import dataimport tensorflow as tffrom tensorflow.examples.tutorials.mnist import input_data  mnist =input_data.read_data_sets("MNIST_data/", one_hot=True) #Print the shape of mistprint('\n','mnist.train.images.shape,mnist.train.labels.shape')print(mnist.train.images.shape,mnist.train.labels.shape)#(55000, 784) (55000, 10)     ##28*28=784print('mnist.test.images.shape, mnist.train.labels.shape')print(mnist.test.images.shape, mnist.train.labels.shape)#(10000, 784) (55000, 10)print('mnist.validation.images.shape, mnist.validation.labels.shape')print(mnist.validation.images.shape, mnist.validation.labels.shape)#(5000, 784) (5000, 10)## 2.Create the model#create sesssess=tf.InteractiveSession()#put-data , first var is datatype, second is tensor shapex=tf.placeholder(tf.float32,[None,784])#Weights and bias are initialized to zeroW=tf.Variable(tf.zeros([784,10]))  b=tf.Variable(tf.zeros([10]))#realize Softmax Regression: y=sotfmax(Wx+b)y=tf.nn.softmax(tf.matmul(x,W)+b)# Define lossy_ = tf.placeholder(tf.float32, [None, 10])cross_entropy = tf.reduce_mean(    -tf.reduce_sum(y_ * tf.log(y), reduction_indices=[1]))#Define optimizertrain_step=tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy) tf.global_variables_initializer().run()  ## 3.Trainfor i in range(1000):    batch_xs, batch_ys = mnist.train.next_batch(100)    train_step.run({x: batch_xs, y_: batch_ys})## 4.Test trained modelcorrect_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))print(accuracy.eval({x: mnist.test.images,y_: mnist.test.labels}))
阅读全文
0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 店里生意不好怎么办?解决方案 淘宝店铺没有生意怎么办 淘宝店做大了应该怎么办 汽车维修没生意怎么办 淘宝买家具安装怎么办 投标时未记主材费结算时怎么办 不敢买自慰棒怎么办 车被扎了个洞怎么办 企业欠税交不起怎么办 组织代码查不到怎么办 u盾电量不足怎么办 对公账户拍照怎么办 个人怎么办对公账户 车辆超过年检日期怎么办 手机cpu负载过高怎么办 移动数据上网慢怎么办 服务器密码忘记了怎么办 网吧电脑卡死了怎么办 局域网连接不上怎么办 电脑没有dns地址怎么办 无法连接版本服务器怎么办 登录游戏就死机怎么办 亿企薪税保没有绑定企业怎么办 众筹如果不成功怎么办 淘宝被投诉商标权怎么办 茅台贴标褶皱怎么办 ins取不了名字怎么办 ins密码忘了怎么办 ins不记得密码怎么办 ins账号被停用怎么办 电脑登录不上怎么办 电脑桌面密码忘记了怎么办 苹果电脑用户名忘记了怎么办 w7电脑忘记密码怎么办 电脑win10忘密码怎么办 win7账号被停用怎么办 淘宝号忘了怎么办 xp忘记系统密码怎么办 u盘忘记密码怎么办 mac系统密码忘记怎么办 电脑用户名密码忘记怎么办