Tensorflow - Implement for a Softmax Regression Model on MNIST.
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.的更多相关文章
- Tensorflow - Implement for a Convolutional Neural Network on MNIST.
Coding according to TensorFlow 官方文档中文版 中文注释源于:tf.truncated_normal与tf.random_normal TF-卷积函数 tf.nn.con ...
- 基于MNIST数据的softmax regression
跟着tensorflow上mnist基本机器学习教程联系 首先了解sklearn接口: sklearn.linear_model.LogisticRegression In the multiclas ...
- TensorFlow(2)Softmax Regression
Softmax Regression Chapter Basics generate random Tensors Three usual activation function in Neural ...
- 学习笔记TF024:TensorFlow实现Softmax Regression(回归)识别手写数字
TensorFlow实现Softmax Regression(回归)识别手写数字.MNIST(Mixed National Institute of Standards and Technology ...
- TensorFlow实战之Softmax Regression识别手写数字
关于本文说明,本人原博客地址位于http://blog.csdn.net/qq_37608890,本文来自笔者于2018年02月21日 23:10:04所撰写内容(http://blog.c ...
- TensorFlow实现Softmax Regression识别手写数字
本章已机器学习领域的Hello World任务----MNIST手写识别做为TensorFlow的开始.MNIST是一个非常简单的机器视觉数据集,是由几万张28像素*28像素的手写数字组成,这些图片只 ...
- (六)6.10 Neurons Networks implements of softmax regression
softmax可以看做只有输入和输出的Neurons Networks,如下图: 其参数数量为k*(n+1) ,但在本实现中没有加入截距项,所以参数为k*n的矩阵. 对损失函数J(θ)的形式有: 算法 ...
- CS229 6.10 Neurons Networks implements of softmax regression
softmax可以看做只有输入和输出的Neurons Networks,如下图: 其参数数量为k*(n+1) ,但在本实现中没有加入截距项,所以参数为k*n的矩阵. 对损失函数J(θ)的形式有: 算法 ...
- Exercise : Softmax Regression
Step 0: Initialize constants and parameters Step 1: Load data Step 2: Implement softmaxCost Implemen ...
随机推荐
- LeetCode41.缺失的第一个正数 JavaScript
给定一个未排序的整数数组,找出其中没有出现的最小的正整数. 示例 1: 输入: [1,2,0] 输出: 3 示例 2: 输入: [3,4,-1,1] 输出: 2 示例 3: 输入: [7,8,9,11 ...
- 国产开源JavaWeb应用程序框架——XWAF(1)
XWAF是一个基于java反射和Servlet 技术的国产开源Web应用程序框架.其英文全称为“eXtensible Web Application Framework”,意即“可扩展的网络应用程序框 ...
- QueryRunner cannot be resolved to a type:关于包不能正常导入的问题
在操作一个功能模块的时候,出现一个问题: 我原则是按着项目指导一步一步走的,但却出现, QueryRunner cannot be resolved to a type,这个问题应该属于Xxx can ...
- 【leetcode】867 - Transpose Matrix
[题干描述] Given a matrix A, return the transpose of A. The transpose of a matrix is the matrix flipped ...
- PHP绘制验证码
<?php //使用PHP绘图技术,画出自己的验证码 $checkCode=""; for($i=0;$i<4;$i++){ ...
- L2-006 树的遍历 (后序中序求层序)
题目: 给定一棵二叉树的后序遍历和中序遍历,请你输出其层序遍历的序列.这里假设键值都是互不相等的正整数. 输入格式: 输入第一行给出一个正整数N(≤30),是二叉树中结点的个数.第二行给出其后序遍历序 ...
- mysql-8.0.15允许外网访问
1.进MySQL之后, 2.输入以下语句,进入mysql库: use mysql3.更新域属性,'%'表示允许外部访问: update user set host='%' where user ='r ...
- ElasticSearch优化系列三:机器设置(内存)
heap参数设置优化 命令行修改 ./bin/elasticsearch -Xmx10g -Xms10g xmx-JVM最大允许分配的堆内存,按需分配 xms-JVM初始分配的堆内存 此值设置与-Xm ...
- Mysql双向同步热备份设置
1.环境描述. 主机:103.241.49.137(A) 主机:103.240.182.191(B) MYSQL 版本为5.1.112.授权用户.(本人比较懒,直接用的root 跳过这一步)A:mys ...
- MQTT入门2 -- “Error: Invalid password hash for user nick.”和“Connection Refused: not authorised.”
原文地址:https://www.cnblogs.com/NickQ/p/9277315.html 问题描述: 搭建好mosqitto环境后,利用无密码验证方式,成功通过测试. 但修改配置文件将匿名访 ...