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. Nuxt开发搭建博客系统

    nuxt.js第三方插件的使用?路由的配置pages目录自动生成路由layoutsdefault.vueerror.vueVuex的使用权限篇Mysqladvice nuxt.js 追求完美,相信大家 ...

  2. IO练习--按字节截取字符串

    * 在Java中字符串“abcd”和字符串“ab你好”都是4个字符, * 但是字节数不同,因为GBK中一个汉字占两个字节 * 定义一个方法用来按字节数截取字符串. * 如:对于“ab你好”,取3个字节 ...

  3. ThinkPHP3.2.3整合smarty模板(三)

    在smarty模板中使用thinkphp框架的U方法时要主要的问题: 1.不能直接使用{:U('Index/index')}: 2.正确的使用方法为:<!--{U("Login/log ...

  4. C# 处理DateTime算法,取某月第1天及最后一天

    代码如下所示: /// <summary> /// 取得某月的第一天 /// </summary> /// <param name="datetime" ...

  5. 堆优化的dij【模板】

    #include<iostream> #include<cstdio> #include<cstring> #include<queue> using ...

  6. harbor helm 仓库使用

    harbor 已经支持helm 私服仓库了,还是比较方便的 安装 下载在线安装包 wget https://storage.googleapis.com/harbor-releases/release ...

  7. 构建一个dbt 数据库适配器

    脚手架新的适配器 首先,将odbc适配器模板复制到同一目录中的新文件. 更新dbt / adapters / factory.py以将新适配器包含为类型.还要将类型添加到dbt / contracts ...

  8. MySQL · 特性分析 · 优化器 MRR & BKA【转】

    MySQL · 特性分析 · 优化器 MRR & BKA 上一篇文章咱们对 ICP 进行了一次全面的分析,本篇文章小编继续为大家分析优化器的另外两个选项: MRR & batched_ ...

  9. C语言面试题4

    第二部分:程序代码评价或者找错 1.下面的代码输出是什么,为什么?void foo(void){    unsigned int a = 6;    int b = -20;    (a+b > ...

  10. Mac 上 java 究竟在哪里,本文彻底让你搞清楚!

    Mac下当你在[终端]输入java -version时,是执行的哪里的java呢,which java命令可以看到,就是[/usr/bin/java] [/usr/bin/java]只是个替身,实际指 ...