Softmax Regression
发布日期:2021-07-01 05:04:59
浏览次数:2
分类:技术文章
本文共 2821 字,大约阅读时间需要 9 分钟。
Softmax Regression是Logistic回归的推广,Logistic回归是处理二分类问题的,而Softmax Regression是处理多分类问题的。
下面就是TensorFlow使用Softmax回归的例子:
# Import MINST dataimport input_datamnist = input_data.read_data_sets("/tmp/data/", one_hot=True)import tensorflow as tf# Set parameterslearning_rate = 0.01training_iteration = 30batch_size = 100display_step = 2# TF graph inputx = tf.placeholder("float", [None, 784]) # mnist data image of shape 28*28=784y = tf.placeholder("float", [None, 10]) # 0-9 digits recognition => 10 classes# Create a model# Set model weightsW = tf.Variable(tf.zeros([784, 10]))b = tf.Variable(tf.zeros([10]))# Construct a linear modelmodel = tf.nn.softmax(tf.matmul(x, W) + b) # Softmax# Minimize error using cross entropy# Cross entropycost_function = -tf.reduce_sum(y*tf.log(model)) # Gradient Descentoptimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost_function)# Initializing the variablesinit = tf.initialize_all_variables()# Launch the graphwith tf.Session() as sess: sess.run(init) # Training cycle for iteration in range(training_iteration): avg_cost = 0. total_batch = int(mnist.train.num_examples/batch_size) # Loop over all batches for i in range(total_batch): batch_xs, batch_ys = mnist.train.next_batch(batch_size) # Fit training using batch data sess.run(optimizer, feed_dict={x: batch_xs, y: batch_ys}) # Compute average loss avg_cost += sess.run(cost_function, feed_dict={x: batch_xs, y: batch_ys})/total_batch # Display logs per eiteration step if iteration % display_step == 0: print "Iteration:", '%04d' % (iteration + 1), "cost=", "{:.9f}".format(avg_cost) print "Tuning completed!" # Test the model predictions = tf.equal(tf.argmax(model, 1), tf.argmax(y, 1)) # Calculate accuracy accuracy = tf.reduce_mean(tf.cast(predictions, "float")) print "Accuracy:", accuracy.eval({x: mnist.test.images, y: mnist.test.labels})Softmax回归介绍 我们知道MNIST的每一张图片都表示一个数字,从0到9。我们希望得到给定图片代表每个数字的概率。比如说,我们的模型可能推测一张包含9的图片代表数字9的概率是80%但是判断它是8的概率是5%(因为8和9都有上半部分的小圆),然后给予它代表其他数字的概率更小的值。 这是一个使用softmax回归(softmax regression)模型的经典案例。softmax模型可以用来给不同的对象分配概率。即使在之后,我们训练更加精细的模型时,最后一步也需要用softmax来分配概率。 softmax回归(softmax regression)分两步:第一步为了得到一张给定图片属于某个特定数字类的证据(evidence),我们对图片像素值进行加权求和。如果这个像素具有很强的证据说明这张图片不属于该类,那么相应的权值为负数,相反如果这个像素拥有有利的证据支持这张图片属于这个类,那么权值是正数。
但是更多的时候把softmax模型函数定义为前一种形式:把输入值当成幂指数求值,再正则化这些结果值。这个幂运算表示,更大的证据对应更大的假设模型(hypothesis)里面的乘数权重值。反之,拥有更少的证据意味着在假设模型里面拥有更小的乘数系数。假设模型里的权值不可以是0值或者负值。Softmax然后会正则化这些权重值,使它们的总和等于1,以此构造一个有效的概率分布。(更多的关于Softmax函数的信息,可以参考Michael Nieslen的书里面的这个部分,其中有关于softmax的可交互式的可视化解释。)
1. C++标准模板库从入门到精通
2.跟老菜鸟学C++
3. 跟老菜鸟学python
4. 在VC2015里学会使用tinyxml库
5. 在Windows下SVN的版本管理与实战
6.Visual Studio 2015开发C++程序的基本使用
7.在VC2015里使用protobuf协议
8.在VC2015里学会使用MySQL数据库
转载地址:https://mysoft.blog.csdn.net/article/details/60578247 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
留言是一种美德,欢迎回访!
[***.207.175.100]2024年05月01日 02时57分27秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
教会徒弟饿死师傅吗
2019-05-04
便利贴女孩_百度百科
2019-05-04
计算机内加减法的溢出处理
2019-05-04
DAO sql 传参
2019-05-04
级联删除
2019-05-04
HTML 一行显示
2019-05-04
上传图片 服务器保存图片
2019-05-04
JS 获取图片、文件数据并封装在json中
2019-05-04
Servlet init()
2019-05-04
JS 加载页面
2019-05-04
button 标签
2019-05-04
VSCode-linux-x64 无法正常安装组件
2019-05-04
Eclipse 使用
2019-05-04
Win10 快捷键
2019-05-04
Chrome 快捷键
2019-05-04
Sublime Text 快捷键
2019-05-04
Spring BeanFactory、Bean和Context
2019-05-04
Vue 解决跨域问题
2019-05-04
C++的文件操作
2019-05-04