之前在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. Linux上rsync配置

    一.服务器端配置1.rsyncd.conf文件说明uid = rsync     #用户,用来控制用户访问模块目录的读写权限gid = rsync     #组,用来控制组访问模块目录的读写权限use ...

  2. scrapy 爬虫框架之持久化存储

    scrapy  持久化存储 一.主要过程: 以爬取校花网为例 : http://www.xiaohuar.com/hua/ 1.  spider    回调函数     返回item 时    要用y ...

  3. 网络管理命令ping和arping

    ping ping        向目标主机发送icmp请求包 常用来测试当前主机与目标主机网络连接状况 常见选项 -c              设置发包的个数 -s               设 ...

  4. MyBatis动态sql之${}和#{}区别

    前言 ​ 接触mybatis也是在今年步入社会之后,想想也半年多了,缺没时间去系统的学习,只知道大概,也是惭愧. ​ 不知道有多少刚毕业的同学和我一样,到现在还没仔仔细细去了解你每天都会见到使用到的框 ...

  5. hash、hashchange事件

    1.hash即URL中"#"字符后面的部分. ①使用浏览器访问网页时,如果网页URL中带有hash,页面就会定位到id(或name)与hash值一样的元素的位置: ②hash还有另 ...

  6. Wannafly挑战赛22-A/B/C

    链接:https://www.nowcoder.com/acm/contest/160/A来源:牛客网 题目描述 有一个计数器,计数器的初始值为0,每次操作你可以把计数器的值加上a1,a2,...,a ...

  7. Leetcode 117

    if(root == NULL) return; queue<TreeLinkNode *> que; que.push(root); while(!empty(que)){ int le ...

  8. JedisClusterMaxRedirectionsException: Too many Cluster redirections

    发生环境 当时的redis集群配置:redis-trib.rb 127.0.0.1 .... redis.conf的bind是默认# 解决方案 将redis.conf的bind为局域网真实ip red ...

  9. 【LeetCode】成对交换节点

    e.g. 给定链表 1->2->3->4,返回 2->1->4->3 的头节点. 我写了个常见的从头节点遍历,少量的奇数个或偶数个数据都能成功重新排列.但链表过长时 ...

  10. springmvc 自定义view支持json和jsonp格式数据返回

    1.如果controlloer上用@ResponseBody注解,则用<mvc:message-converter>里面配置的json解析器进行解析 <mvc:annotation- ...