classification-softmax
softmax分类
import tensorflow as tf
import numpy as npfrom input_data import read_data_sets mnist = read_data_sets('MNIST_data', one_hot=True) def add_layer(inputs, in_size, out_size, active_function=None):
"""
:param inputs:
:param in_size: 行
:param out_size: 列 , [行, 列] =矩阵
:param active_function:
:return:
"""
with tf.name_scope('layer'):
with tf.name_scope('weights'):
W = tf.Variable(tf.random_normal([in_size, out_size]), name='W') #
with tf.name_scope('bias'):
b = tf.Variable(tf.zeros([1, out_size]) + 0.1) # b是代表每一行数据,对应out_size列个数据
with tf.name_scope('Wx_plus_b'):
Wx_plus_b = tf.matmul(inputs, W) + b
if active_function is None:
outputs = Wx_plus_b
else:
outputs = active_function(Wx_plus_b)
return outputs def compute_accuracy(v_xs, v_ys):
""" 计算的准确率 """
global prediction # prediction value
y_pre = sess.run(prediction, feed_dict={xs: v_xs})
# 与期望的值比较 bool
correct_pre = tf.equal(tf.argmax(y_pre, 1), tf.argmax(ys, 1))
# 将bools转化为数字
accuracy = tf.reduce_mean(tf.cast(correct_pre, tf.float32))
result = sess.run(accuracy, feed_dict={xs: v_xs, ys: v_ys})
return result # define placeholder for inputs to network
xs = tf.placeholder(tf.float32, [None, 784])
ys = tf.placeholder(tf.float32, [None, 10]) # softmax + cross_entropy = classification
# add output layer
prediction = add_layer(xs, 784, 10, active_function=tf.nn.softmax) # softmax分类 # the loss between prediction and really
cross_entropy = tf.reduce_mean(-tf.reduce_sum(ys*tf.log(prediction),
reduction_indices=[1])) # training
train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy) sess = tf.Session()
sess.run(tf.initialize_all_variables()) # start training
for i in range(1000):
batch_x, batch_y = mnist.train.next_batch(100)
sess.run(train_step, feed_dict={xs: batch_x, ys: batch_y})
if i % 50 == 0:
print(compute_accuracy(mnist.test.images, mnist.test.labels))
classification-softmax的更多相关文章
- 如何入门Pytorch之三:如何优化神经网络
在上一节中,我们介绍了如何使用Pytorch来搭建一个经典的分类神经网络.一般情况下,搭建完模型后训练不会一次就能达到比较好的效果,这样,就需要不断的调整和优化模型的各个部分.从而引出了本文的主旨:如 ...
- [DeeplearningAI笔记]Multi-class classification多类别分类Softmax regression_02_3.8-3.9
Multi-class classification多类别分类 觉得有用的话,欢迎一起讨论相互学习~Follow Me 3.8 Softmax regression 原有课程我们主要介绍的是二分分类( ...
- 用matlab训练数字分类的深度神经网络Training a Deep Neural Network for Digit Classification
This example shows how to use Neural Network Toolbox™ to train a deep neural network to classify ima ...
- TensorFlow 深度学习笔记 Logistic Classification
Logistic Classification Github工程地址:https://github.com/ahangchen/GDLnotes 欢迎star,有问题可以到Issue区讨论 官方教程地 ...
- CS231n-lecture2-Image Classification pipeline 课堂笔记
---恢复内容开始--- 相关资源 Event Type Date Description Course Materials Lecture 2 Thursday April 6 Image ...
- R︱Softmax Regression建模 (MNIST 手写体识别和文档多分类应用)
本文转载自经管之家论坛, R语言中的Softmax Regression建模 (MNIST 手写体识别和文档多分类应用) R中的softmaxreg包,发自2016-09-09,链接:https:// ...
- AI - TensorFlow - 分类与回归(Classification vs Regression)
分类与回归 分类(Classification)与回归(Regression)的区别在于输出变量的类型.通俗理解,定量输出称为回归,或者说是连续变量预测:定性输出称为分类,或者说是离散变量预测. 回归 ...
- Derivative of Softmax Loss Function
Derivative of Softmax Loss Function A softmax classifier: \[ p_j = \frac{\exp{o_j}}{\sum_{k}\exp{o_k ...
- multi-label image classification:多标签图像分类总结
多标签图像分类总结 目录 1.简介 2.现有数据集和评价指标 3.学习算法 4.总结(现在存在的问题,研究发展的方向) 简介 传统监督学习主要是单标签学习,而现实生活中目标样本往往比较复杂,具有多个语 ...
- tensorflow学习之(九)classification 分类问题之分类手写数字0-9
#classification 分类问题 #例子 分类手写数字0-9 import tensorflow as tf from tensorflow.examples.tutorials.mnist ...
随机推荐
- 【opencv基础】cv::Point类型与行列的关系
关系 row == height == Point.y col == width == Point.x Mat::at(Point(x, y)) == Mat::at(y,x) 参考 1.博客: 完
- android中传统的创建数据库
1.在Android工程中建立一个class类,且继承与SQLiteOpenHelper. 2.然后到Mainactivity中去new一个MyOpenHelper来找到它 3.第一次创建数据库的时候 ...
- pta 奇数值结点链表&&单链表结点删除
本题要求实现两个函数,分别将读入的数据存储为单链表.将链表中奇数值的结点重新组成一个新的链表.链表结点定义如下: struct ListNode { int data; ListNode *next; ...
- idea快捷键 好的网址收藏
http://blog.csdn.net/u010800804/article/details/48491395http://blog.csdn.net/wei83523408/article/det ...
- AllJoyn 了解
AllJoyn是一个中性平台系统,旨在简化邻近异构分布式移动通信网络系统.这里的异构性不仅表示不同的设备,而且可以是具有不同操作系统和不同类型的设备(例如个人电脑.手机.平板电脑和消费性电子产品),并 ...
- Bloom Filter(布隆过滤器)的概念和原理
Bloom filter 适用范围:可以用来实现数据字典,进行数据的判重,或者集合求交集 基本原理及要点: 对于原理来说很简单,位数组+k个独立hash函数.将hash函数对应的值的位数组置1,查找时 ...
- coffeescript学习
test2.coffeestdin = process.openStdin()stdin.setEncoding 'utf8' stdin.on 'data', (input) -> n ...
- java 搭建新项目,最佳组合:spring boot + mybatis generator
java 搭建新项目,最佳组合:spring boot + mybatis generator
- RK3399 Android 7.1 删除repo后编译报错
CPU:RK3399 系统:Android 7.1 瑞芯微使用的是 repo 来进行代码管理,但我们需要用 git 来管理,所以就删除了 repo,但是编译就报错,如下:Server is alrea ...
- [C++ Primer] : 第11章: 关联容器
目录 使用关联容器 关联容器概述 关联容器操作 无序容器 使用关联容器 关联容器与顺序容器有着根本的不同: 关联容器中的元素是按关键字来保存和访问的, 按顺序容器中的元素是按它们在容器中的位置来顺序保 ...