Coding according to TensorFlow 官方文档中文版

 import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True) ''' Intro. for this python file.
Objective:
Implement for a Softmax Regression Model on MNIST.
Operating Environment:
python = 3.6.4
tensorflow = 1.5.0
''' # Set a placeholder. We hope arbitrary number of images could be input to this model.
x = tf.placeholder("float", [None, 784]) # Set weight/bias variables. Their initial values could be set Randomly.
W = tf.Variable(tf.zeros([784, 10]))
b = tf.Variable(tf.zeros([10])) # Model implementation
y = tf.nn.softmax(tf.matmul(x, W) + b) # Set a placeholder 'y_' to accept the ground-truth values.
y_ = tf.placeholder("float", [None, 10]) # Calculate cross-entropy
cross_entropy = -tf.reduce_sum(y_ * tf.log(y)) # Train Softmax Regression Model
train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy) # Initialize variables
# init = tf.initialize_all_variables() # Warning
init = tf.global_variables_initializer() # Launch the graph in a session.
sess = tf.Session()
sess.run(init) for i in range(1000):
batch_xs, batch_ys = mnist.train.next_batch(100) # Grabbing 100 batch data points from training data randomly.
sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys}) # Model Evaluation
''' tf.argmax(input, axis=None, name=None, dimension=None, output_type=tf.int64)
Explanation:
Returns the index with the largest value across axes of a tensor.
test = np.array([[1, 2, 3], [2, 3, 4], [5, 4, 3], [8, 7, 2]])
np.argmax(test, 0) # output:array([3, 3, 1])
np.argmax(test, 1) # output:array([2, 2, 0, 0])
Returns:
A Tensor of type output_type.
''' # correct_prediction = tf.equal(tf.arg_max(y, 1), tf.arg_max(y_, 1)) # Warning
correct_prediction = tf.equal(tf.argmax(y, axis=1), tf.argmax(y_, axis=1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
print(sess.run(accuracy, feed_dict={x: mnist.test.images, y_: mnist.test.labels})) # The result is around 0.91.

Tensorflow - Implement for a Softmax Regression Model on MNIST.的更多相关文章

  1. Tensorflow - Implement for a Convolutional Neural Network on MNIST.

    Coding according to TensorFlow 官方文档中文版 中文注释源于:tf.truncated_normal与tf.random_normal TF-卷积函数 tf.nn.con ...

  2. 基于MNIST数据的softmax regression

    跟着tensorflow上mnist基本机器学习教程联系 首先了解sklearn接口: sklearn.linear_model.LogisticRegression In the multiclas ...

  3. TensorFlow(2)Softmax Regression

    Softmax Regression Chapter Basics generate random Tensors Three usual activation function in Neural ...

  4. 学习笔记TF024:TensorFlow实现Softmax Regression(回归)识别手写数字

    TensorFlow实现Softmax Regression(回归)识别手写数字.MNIST(Mixed National Institute of Standards and Technology ...

  5. TensorFlow实战之Softmax Regression识别手写数字

         关于本文说明,本人原博客地址位于http://blog.csdn.net/qq_37608890,本文来自笔者于2018年02月21日 23:10:04所撰写内容(http://blog.c ...

  6. TensorFlow实现Softmax Regression识别手写数字

    本章已机器学习领域的Hello World任务----MNIST手写识别做为TensorFlow的开始.MNIST是一个非常简单的机器视觉数据集,是由几万张28像素*28像素的手写数字组成,这些图片只 ...

  7. (六)6.10 Neurons Networks implements of softmax regression

    softmax可以看做只有输入和输出的Neurons Networks,如下图: 其参数数量为k*(n+1) ,但在本实现中没有加入截距项,所以参数为k*n的矩阵. 对损失函数J(θ)的形式有: 算法 ...

  8. CS229 6.10 Neurons Networks implements of softmax regression

    softmax可以看做只有输入和输出的Neurons Networks,如下图: 其参数数量为k*(n+1) ,但在本实现中没有加入截距项,所以参数为k*n的矩阵. 对损失函数J(θ)的形式有: 算法 ...

  9. Exercise : Softmax Regression

    Step 0: Initialize constants and parameters Step 1: Load data Step 2: Implement softmaxCost Implemen ...

随机推荐

  1. VS Code 的常用快捷键及插件(前端)

    一.vs code 的常用快捷键 1.注释: a) 单行注释:[ctrl+k,ctrl+c] 或 ctrl+/ b) 取消单行注释:[ctrl+k,ctrl+u] (按下ctrl不放,再按k + u) ...

  2. 【原创】CRM 2015/2016,SSRS 生成PDF文件,幷以附件的形式发送邮件

    主要步骤如下: 生成一条邮件记录 生成一条ActivityParty记录 生成PDF文件,并以Base64添加到ActivityMimeAttachment 中去 打开发送邮件窗口,以便编辑及发送邮件 ...

  3. Java实例 Part3:流程控制

    目录 Example01:判断某一年是否为闰年 Example02:验证登录信息的合法性 Example03:判断用户输入月份的季节 Example04:使用while循环语句与自增运算符循环遍历数组 ...

  4. java-spark的各种常用算子的写法

    通常写spark的程序用scala比较方便,毕竟spark的源码就是用scala写的.然而,目前java开发者特别多,尤其进行数据对接.上线服务的时候,这时候,就需要掌握一些spark在java中的使 ...

  5. Qt的checkbox风格修改

    环境: HelperA64开发板 Linux3.10内核 时间:2019.01.12 目标:修改Qt中checkbox图片太小的BUG 问题: 在从Qt4移植为Qt5时遇到很多问题,这次是移植到开发板 ...

  6. Python面向对象总结及类与正则表达式

    Python3 面向对象 一丶面向对象技术简介 类(Class): 用来描述具有相同的属性和方法的对象的集合.它定义了该集合中每个对象所共有的属性和方法.对象是类的实例. 方法:类中定义的函数. 类变 ...

  7. pycharm社区版新建django文件不友好操作

    一.cmd操作 1.django-admin startproject (新建project名称) 2.在pycharm打开project,运行终端输入:python manage.py starta ...

  8. Linux入门第二天——基本命令入门(中)

    一.文件搜索命令 1.文件搜索命令:locate 速度很快(具体见Linux工具网址的对比),注意无法找到新建的文件(原理暂不展开) locate命令其实是“find -name”的另一种写法,但是要 ...

  9. 20155210 潘滢昊 Java第一次实验---凯撒密码

    Java第一次实验---凯撒密码 实验内容 实现凯撒密码,并进行测试. 实验代码 import java.io.*; import java.util.Scanner; public class ks ...

  10. 20155233 《Java程序设计》第十二周课堂练习总结

    20155233 <Java程序设计> 第十二周课堂练习总结 测试题目 1.修改教材P98 Score2.java, 让执行结果数组填充是自己的学号:提交在IDEA或命令行中运行结查截图, ...