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的更多相关文章

  1. 如何入门Pytorch之三:如何优化神经网络

    在上一节中,我们介绍了如何使用Pytorch来搭建一个经典的分类神经网络.一般情况下,搭建完模型后训练不会一次就能达到比较好的效果,这样,就需要不断的调整和优化模型的各个部分.从而引出了本文的主旨:如 ...

  2. [DeeplearningAI笔记]Multi-class classification多类别分类Softmax regression_02_3.8-3.9

    Multi-class classification多类别分类 觉得有用的话,欢迎一起讨论相互学习~Follow Me 3.8 Softmax regression 原有课程我们主要介绍的是二分分类( ...

  3. 用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 ...

  4. TensorFlow 深度学习笔记 Logistic Classification

    Logistic Classification Github工程地址:https://github.com/ahangchen/GDLnotes 欢迎star,有问题可以到Issue区讨论 官方教程地 ...

  5. CS231n-lecture2-Image Classification pipeline 课堂笔记

    ---恢复内容开始--- 相关资源  Event Type  Date  Description  Course Materials Lecture 2 Thursday April 6 Image ...

  6. R︱Softmax Regression建模 (MNIST 手写体识别和文档多分类应用)

    本文转载自经管之家论坛, R语言中的Softmax Regression建模 (MNIST 手写体识别和文档多分类应用) R中的softmaxreg包,发自2016-09-09,链接:https:// ...

  7. AI - TensorFlow - 分类与回归(Classification vs Regression)

    分类与回归 分类(Classification)与回归(Regression)的区别在于输出变量的类型.通俗理解,定量输出称为回归,或者说是连续变量预测:定性输出称为分类,或者说是离散变量预测. 回归 ...

  8. Derivative of Softmax Loss Function

    Derivative of Softmax Loss Function A softmax classifier: \[ p_j = \frac{\exp{o_j}}{\sum_{k}\exp{o_k ...

  9. multi-label image classification:多标签图像分类总结

    多标签图像分类总结 目录 1.简介 2.现有数据集和评价指标 3.学习算法 4.总结(现在存在的问题,研究发展的方向) 简介 传统监督学习主要是单标签学习,而现实生活中目标样本往往比较复杂,具有多个语 ...

  10. tensorflow学习之(九)classification 分类问题之分类手写数字0-9

    #classification 分类问题 #例子 分类手写数字0-9 import tensorflow as tf from tensorflow.examples.tutorials.mnist ...

随机推荐

  1. 在 Ubuntu 18.0-10上安装 MySQL8

    直接使用apt install mysql-server安装,那么恭喜你踩坑. sudo apt install mysql-server默认会安装MySQL 5.7,将会出现一些莫名的问题,例如:安 ...

  2. FZU OJ:2230 翻翻棋

    Problem 2230 翻翻棋 Accept: 872    Submit: 2132Time Limit: 1000 mSec    Memory Limit : 32768 KB  Proble ...

  3. 【spring源码分析】spring AspectJ的Execution表达式

    在使用spring框架配置AOP的时候,不管是通过XML配置文件还是注解的方式都需要定义pointcut"切入点" 例如定义切入点表达式  execution (* com.sam ...

  4. python pandas Timestamp 转为 datetime 类型

    In [11]: ts = pd.Timestamp('2014-01-23 00:00:00', tz=None) In [12]: ts.to_pydatetime() Out[12]: date ...

  5. 转-java编译时error: illegal character '\ufeff' 的解决办法-https://blog.csdn.net/t518vs20s/article/details/80833061

    原文链接:https://blog.csdn.net/shixing_11/article/details/6976900 最近开发人员通过SVN提交了xxx.java文件,因发布时该包有问题需要回退 ...

  6. HBase + Solr Cloud实现HBase二级索引

    1. 执行流程 2. Solr Cloud实现 http://blog.csdn.net/u011462328/article/details/53008344 3. HBase实现 1) 自定义Ob ...

  7. STL版本号简单介绍

    说明:本文仅供学习交流.转载请标明出处.欢迎转载! 本文的參考文献为:<STL源代码剖析>侯捷        (1)HP STL:全部STL的祖先版本号,由C++之父Alexander S ...

  8. Upgrade to or Install Cinnamon 2.4 in Ubuntu

    http://www.omgubuntu.co.uk/2014/11/install-cinnamon-2-4-ubuntu-14-04-lts sudo add-apt-repository ppa ...

  9. MVC ASP.NET MVC各个版本的区别 (转)

    Net Framework4.5是不支持安装在window server 2003上,如非装请用net framework4.0; MVC1.0 publsh time:2008 IDEV:VS200 ...

  10. FineUI 修改config表属性

    此功能可用来设置系统的不同的标题 private void SelectSystem() { ConfigHelper.Title = DropDownList1.SelectedText; Conf ...