之前在softmax多分类中讲到多用交叉熵作为损失函数,这里顺便写个例子,tensorlflow练手。

# encoding:utf-8
import tensorflow as tf
import input_data ### softmax 回归 # 自动下载安装数据集
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True) # 图片 28 * 28 = 784
x=tf.placeholder('float',[None,784]) # 特征数 784
# 初始化参数
w=tf.Variable(tf.zeros([784,10])) # 10是输出维度,0-9数字的独热编码
b=tf.Variable(tf.zeros([10])) # 模型
y=tf.nn.softmax(tf.matmul(x,w)+b) ### 训练模型
y_=tf.placeholder('float',[None,10]) # 10维 # 损失函数 交叉熵
cross_entropy=-tf.reduce_sum(y_*tf.log(y)) # reduce_sum 计算张量的所有元素之和 所有图片的交叉熵综合 # 优化算法 梯度下降
train_step=tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy) # 0.01 学习率 最小化 损失函数 # 初始化变量
init=tf.initialize_all_variables() # 启动图 会话
sess=tf.Session()
sess.run(init) # 初始化变量 # 迭代,训练参数
for i in range(1000):
batch_xs, batch_ys = mnist.train.next_batch(100) # 训练集
sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys}) # 模型评估 准确率
correct_predict = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1)) # 输出布尔值,即预测与真实是否一样
accuracy = tf.reduce_mean(tf.cast(correct_predict, 'float')) # 将布尔值转化成浮点数,然后求平均, 正确/总数
print(sess.run(accuracy, feed_dict={x: mnist.test.images, y_: mnist.test.labels})) # 测试集 0.9194

tensorflow-softmax的更多相关文章

  1. tensorflow softmax应用

    ---恢复内容开始--- 1.softmax函数 2.tensorflow实现例子 #!/usr/bin/env python # -*- coding: utf-8 -*- import tenso ...

  2. 2.tensorflow——Softmax回归

    import numpy as np import tensorflow as tf import matplotlib.pyplot as plt from tensorflow.examples. ...

  3. TensorFlow softmax的互熵损失

    函数:tf.nn.softmax_cross_entropy_with_logits(logits, labels, name=None) 功能:这个函数的作用是计算 logits 经 softmax ...

  4. TensorFlow构建卷积神经网络/模型保存与加载/正则化

    TensorFlow 官方文档:https://www.tensorflow.org/api_guides/python/math_ops # Arithmetic Operators import ...

  5. Softmax回归(使用tensorflow)

    # coding:utf8 import numpy as np import cPickle import os import tensorflow as tf class SoftMax: def ...

  6. TensorFlow 入门之手写识别(MNIST) softmax算法

    TensorFlow 入门之手写识别(MNIST) softmax算法 MNIST flyu6 softmax回归 softmax回归算法 TensorFlow实现softmax softmax回归算 ...

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

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

  8. TensorFlow MNIST(手写识别 softmax)实例运行

    TensorFlow MNIST(手写识别 softmax)实例运行 首先要有编译环境,并且已经正确的编译安装,关于环境配置参考:http://www.cnblogs.com/dyufei/p/802 ...

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

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

  10. 【TensorFlow篇】--Tensorflow框架实现SoftMax模型识别手写数字集

    一.前述 本文讲述用Tensorflow框架实现SoftMax模型识别手写数字集,来实现多分类. 同时对模型的保存和恢复做下示例. 二.具体原理 代码一:实现代码 #!/usr/bin/python ...

随机推荐

  1. 安卓中使用OkHttp发送数据请求的两种方式(同、异步的GET、POST) 示例-- Android基础

    1.首先看一下最终效果的截图,看看是不是你想要的,这个年代大家都很忙,开门见山很重要! 简要说下,点击不同按钮可以实现通过不同的方式发送OkHttp请求,并返回数据,这里请求的是网页,所以返回的都是些 ...

  2. 【消息队列】kafka是如何保证高可用的

    一.kafka一个最基本的架构认识 由多个broker组成,每个broker就是一个节点:创建一个topic,这个topic可以划分为多个partition,每个partition可以存在于不同的br ...

  3. 如何阻止div中的子div触发div的事件

    <div class="sideFrame" v-on:click="hideside"> <div class="sideFram ...

  4. Integer to English words leetcode java

    问题描述: Convert a non-negative integer to its english words representation. Given input is guaranteed ...

  5. HDU 5710 Digit Sum

    Let S(N)S(N) be digit-sum of NN, i.e S(109)=10,S(6)=6S(109)=10,S(6)=6. If two positive integers a,ba ...

  6. 漏洞复现——apache文件解析漏洞

    漏洞描述: 我们可以上传一个文件名末尾包含换行符的文件,以此绕过它的黑名单 影响版本: apache 2.4.0-2.4.29 漏洞分析: <FilesMath "\.(?i:php| ...

  7. 剑指offer-调整数组内奇偶数顺序

    题目描述 输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有的奇数位于数组的前半部分,所有的偶数位于位于数组的后半部分,并保证奇数和奇数,偶数和偶数之间的相对位置不变. 解题思路 时间换 ...

  8. SQLServer清空表

    TRUNCATE TABLE TABLE_NAME 这个不记日志. delete table table_name 这个记日志 drop table table_name 删除表 TRUNCATE 语 ...

  9. 依赖注入 IOC

    首先,明确下IoC是什么东西: 控制反转(Inversion of Control,英文缩写为IoC)是一个重要的面向对象编程的法则来削减计算机程序的耦合问题,也是轻量级的Spring框架的核心. 控 ...

  10. Jupyter 快速入门——写python项目博客非常有用!!!

    from:https://blog.csdn.net/m0_37338590/article/details/78862488 一.简介: Jupyter Notebook(此前被称为 IPython ...