<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>machine learning &#8211; 编码无悔 /  Intent &amp; Focused</title>
	<atom:link href="https://www.codelast.com/tag/machine-learning/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.codelast.com</link>
	<description>最优化之路</description>
	<lastBuildDate>Wed, 24 May 2023 09:36:14 +0000</lastBuildDate>
	<language>zh-Hans</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>
	<item>
		<title>[原创]Machine Learning/机器学习 文章合集</title>
		<link>https://www.codelast.com/%e5%8e%9f%e5%88%9bmachine-learning%e6%9c%ba%e5%99%a8%e5%ad%a6%e4%b9%a0-%e6%96%87%e7%ab%a0%e5%90%88%e9%9b%86/</link>
					<comments>https://www.codelast.com/%e5%8e%9f%e5%88%9bmachine-learning%e6%9c%ba%e5%99%a8%e5%ad%a6%e4%b9%a0-%e6%96%87%e7%ab%a0%e5%90%88%e9%9b%86/#comments</comments>
		
		<dc:creator><![CDATA[learnhard]]></dc:creator>
		<pubDate>Sat, 21 Jan 2017 10:57:16 +0000</pubDate>
				<category><![CDATA[Algorithm]]></category>
		<category><![CDATA[Math]]></category>
		<category><![CDATA[原创]]></category>
		<category><![CDATA[machine learning]]></category>
		<category><![CDATA[机器学习]]></category>
		<guid isPermaLink="false">http://www.codelast.com/?p=9137</guid>

					<description><![CDATA[<p>注：带有 <span style="color:#ff0000;">♬♬♬♬♬</span>&#160;标志的是文章合集。</p>
<p>✍<a href="https://www.codelast.com/?p=10907" rel="noopener noreferrer" target="_blank">强化学习(Reinforcement Learning)文章合集</a>&#160;<span style="color: rgb(255, 0, 0);">♬♬♬♬♬</span></p>
<p>✍<a href="https://www.codelast.com/?p=8629" rel="noopener noreferrer" target="_blank">用人话解释机器学习中的Logistic Regression（逻辑回归）</a></p>
<p>✍<a href="https://www.codelast.com/?p=9211" rel="noopener noreferrer" target="_blank">如何防止softmax函数上溢出(overflow)和下溢出(underflow)</a></p>
<p>✍<a href="https://www.codelast.com/?p=9754" rel="noopener noreferrer" target="_blank">ELL（Embedded Learning Library，微软嵌入式学习库）文章合集</a>&#160;<span style="color: rgb(255, 0, 0);">♬♬♬♬♬</span><br />
<span id="more-9137"></span><br />
✍<a href="https://www.codelast.com/?p=9040" rel="noopener noreferrer" target="_blank">《Neural Networks and Deep Learning》读书笔记：最简单的识别MNIST的神经网络程序(1)</a></p>
<p>✍<a href="https://www.codelast.com/?p=9083" rel="noopener noreferrer" target="_blank">《Neural Networks and Deep Learning》读书笔记：最简单的识别MNIST的神经网络程序(2)</a></p>
<p>✍<a href="https://www.codelast.com/?p=9085" rel="noopener noreferrer" target="_blank">《Neural Networks and Deep Learning》读书笔记：反向传播的4个基本方程(1)</a></p>
<p>✍<a href="https://www.codelast.com/?p=9951" rel="noopener noreferrer" target="_blank">用人话解释蒙特卡罗方法/Monte Carlo method(文章合集)</a>&#160;<span style="color: rgb(255, 0, 0);">♬♬♬♬♬</span></p>
<p>✍<a href="https://www.codelast.com/?p=10475" rel="noopener noreferrer" target="_blank">一个简单的TensorFlow-Serving例子</a></p>
<p>✍<a href="https://www.codelast.com/?p=10520" rel="noopener noreferrer" target="_blank">tf.tile()</a>&#8230; <a href="https://www.codelast.com/%e5%8e%9f%e5%88%9bmachine-learning%e6%9c%ba%e5%99%a8%e5%ad%a6%e4%b9%a0-%e6%96%87%e7%ab%a0%e5%90%88%e9%9b%86/" class="read-more">Read More </a></p>]]></description>
										<content:encoded><![CDATA[<p>注：带有 <span style="color:#ff0000;">♬♬♬♬♬</span>&nbsp;标志的是文章合集。</p>
<p><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/270d.png" alt="✍" class="wp-smiley" style="height: 1em; max-height: 1em;" /><a href="https://www.codelast.com/?p=10907" rel="noopener noreferrer" target="_blank">强化学习(Reinforcement Learning)文章合集</a>&nbsp;<span style="color: rgb(255, 0, 0);">♬♬♬♬♬</span></p>
<p><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/270d.png" alt="✍" class="wp-smiley" style="height: 1em; max-height: 1em;" /><a href="https://www.codelast.com/?p=8629" rel="noopener noreferrer" target="_blank">用人话解释机器学习中的Logistic Regression（逻辑回归）</a></p>
<p><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/270d.png" alt="✍" class="wp-smiley" style="height: 1em; max-height: 1em;" /><a href="https://www.codelast.com/?p=9211" rel="noopener noreferrer" target="_blank">如何防止softmax函数上溢出(overflow)和下溢出(underflow)</a></p>
<p><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/270d.png" alt="✍" class="wp-smiley" style="height: 1em; max-height: 1em;" /><a href="https://www.codelast.com/?p=9754" rel="noopener noreferrer" target="_blank">ELL（Embedded Learning Library，微软嵌入式学习库）文章合集</a>&nbsp;<span style="color: rgb(255, 0, 0);">♬♬♬♬♬</span><br />
<span id="more-9137"></span><br />
<img src="https://s.w.org/images/core/emoji/17.0.2/72x72/270d.png" alt="✍" class="wp-smiley" style="height: 1em; max-height: 1em;" /><a href="https://www.codelast.com/?p=9040" rel="noopener noreferrer" target="_blank">《Neural Networks and Deep Learning》读书笔记：最简单的识别MNIST的神经网络程序(1)</a></p>
<p><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/270d.png" alt="✍" class="wp-smiley" style="height: 1em; max-height: 1em;" /><a href="https://www.codelast.com/?p=9083" rel="noopener noreferrer" target="_blank">《Neural Networks and Deep Learning》读书笔记：最简单的识别MNIST的神经网络程序(2)</a></p>
<p><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/270d.png" alt="✍" class="wp-smiley" style="height: 1em; max-height: 1em;" /><a href="https://www.codelast.com/?p=9085" rel="noopener noreferrer" target="_blank">《Neural Networks and Deep Learning》读书笔记：反向传播的4个基本方程(1)</a></p>
<p><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/270d.png" alt="✍" class="wp-smiley" style="height: 1em; max-height: 1em;" /><a href="https://www.codelast.com/?p=9951" rel="noopener noreferrer" target="_blank">用人话解释蒙特卡罗方法/Monte Carlo method(文章合集)</a>&nbsp;<span style="color: rgb(255, 0, 0);">♬♬♬♬♬</span></p>
<p><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/270d.png" alt="✍" class="wp-smiley" style="height: 1em; max-height: 1em;" /><a href="https://www.codelast.com/?p=10475" rel="noopener noreferrer" target="_blank">一个简单的TensorFlow-Serving例子</a></p>
<p><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/270d.png" alt="✍" class="wp-smiley" style="height: 1em; max-height: 1em;" /><a href="https://www.codelast.com/?p=10520" rel="noopener noreferrer" target="_blank">tf.tile() 用法举例(TensorFlow)</a></p>
<p><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/270d.png" alt="✍" class="wp-smiley" style="height: 1em; max-height: 1em;" /><a href="https://www.codelast.com/?p=10534">二维numpy数组保存到TFRecord并读取还原回来</a></p>
<p><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/270d.png" alt="✍" class="wp-smiley" style="height: 1em; max-height: 1em;" /><a href="https://www.codelast.com/?p=10565" rel="noopener noreferrer" target="_blank">如何打印出TensorFlow保存的checkpoint里的参数名</a></p>
<p><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/270d.png" alt="✍" class="wp-smiley" style="height: 1em; max-height: 1em;" /><a href="https://www.codelast.com/?p=10578" rel="noopener noreferrer" target="_blank">如何取出 tf.layers.dense 定义的全连接层的weight和bias参数值</a></p>
<p><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/270d.png" alt="✍" class="wp-smiley" style="height: 1em; max-height: 1em;" /><a href="https://www.codelast.com/?p=10950" rel="noopener noreferrer" target="_blank">PyTorch模型 .pt，.pth，.pkl 的区别</a></p>
<p><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/270d.png" alt="✍" class="wp-smiley" style="height: 1em; max-height: 1em;" /><a href="https://www.codelast.com/?p=10953" rel="noopener noreferrer" target="_blank">PyTorch模型的两种保存方法</a></p>
<p><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/270d.png" alt="✍" class="wp-smiley" style="height: 1em; max-height: 1em;" /><a href="https://www.codelast.com/?p=12047" rel="noopener noreferrer" target="_blank">PyTorch做inference/prediction的时候如何使用GPU</a></p>
<p><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/270d.png" alt="✍" class="wp-smiley" style="height: 1em; max-height: 1em;" /><a href="https://www.codelast.com/?p=12698" rel="noopener noreferrer" target="_blank">调戏了一番度娘&quot;最先进&quot;的PLATO-2预训练模型之后，我还是回到了和人类交谈...</a></p>
<p><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/270d.png" alt="✍" class="wp-smiley" style="height: 1em; max-height: 1em;" /><a href="https://www.codelast.com/?p=12856" rel="noopener noreferrer" target="_blank">《使用 fastText 做中文文本分类》文章合集</a><span style="color: rgb(255, 0, 0);">♬♬♬♬♬</span></p>
<p><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/270d.png" alt="✍" class="wp-smiley" style="height: 1em; max-height: 1em;" /><a href="https://www.codelast.com/?p=13912" rel="noopener noreferrer" target="_blank">怎样确认当前正在运行的TensorFlow model-serving服务加载的是哪个.pb模型</a></p>
<p><span style="color: rgb(255, 255, 255);">文章来源：</span><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">https://www.codelast.com/</span></a><br />
<span style="color:#ff0000;">➤➤</span>&nbsp;版权声明&nbsp;<span style="color: rgb(255, 0, 0);">➤➤</span>&nbsp;<br />
转载需注明出处：<u><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><em><span style="color: rgb(0, 0, 255);"><strong style="font-size: 16px;"><span style="font-family: arial, helvetica, sans-serif;">codelast.com</span></strong></span></em></a></u>&nbsp;<br />
感谢关注我的微信公众号（微信扫一扫）：</p>
<p style="border: 0px; font-size: 13px; margin: 0px 0px 9px; outline: 0px; padding: 0px; color: rgb(77, 77, 77);">
	<img decoding="async" alt="wechat qrcode of codelast" src="https://www.codelast.com/codelast_wechat_qr_code.jpg" style="width: 200px; height: 200px;" /></p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.codelast.com/%e5%8e%9f%e5%88%9bmachine-learning%e6%9c%ba%e5%99%a8%e5%ad%a6%e4%b9%a0-%e6%96%87%e7%ab%a0%e5%90%88%e9%9b%86/feed/</wfw:commentRss>
			<slash:comments>4</slash:comments>
		
		
			</item>
		<item>
		<title>[原创] 在树莓派上用TensorFlow玩深度学习(Deep Learning)</title>
		<link>https://www.codelast.com/%e5%8e%9f%e5%88%9b-%e5%9c%a8%e6%a0%91%e8%8e%93%e6%b4%be%e4%b8%8a%e7%94%a8tensorflow%e7%8e%a9%e6%b7%b1%e5%ba%a6%e5%ad%a6%e4%b9%a0deep-learning/</link>
					<comments>https://www.codelast.com/%e5%8e%9f%e5%88%9b-%e5%9c%a8%e6%a0%91%e8%8e%93%e6%b4%be%e4%b8%8a%e7%94%a8tensorflow%e7%8e%a9%e6%b7%b1%e5%ba%a6%e5%ad%a6%e4%b9%a0deep-learning/#comments</comments>
		
		<dc:creator><![CDATA[learnhard]]></dc:creator>
		<pubDate>Tue, 06 Dec 2016 17:08:58 +0000</pubDate>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Raspberry Pi/树莓派]]></category>
		<category><![CDATA[原创]]></category>
		<category><![CDATA[deep learning]]></category>
		<category><![CDATA[machine learning]]></category>
		<category><![CDATA[Raspberry Pi]]></category>
		<category><![CDATA[TensorFlow]]></category>
		<category><![CDATA[树莓派]]></category>
		<category><![CDATA[深度学习]]></category>
		<guid isPermaLink="false">http://www.codelast.com/?p=8941</guid>

					<description><![CDATA[<p>
本文软硬件环境：<br />
树莓派：3代 Model B V1.2，内存1GB<br />
OS：Arch Linux ARM</p>
<p>深度学习（Deep Learning）现在这么火，树莓派玩家们当然也不会放过，目前已经有很多树莓派项目都搭上了Deep Learning的车，纯粹出于&#8220;好玩&#8221;的目的，我在树莓派上也实验了一把，用TensorFlow来识别一张图片里的物体&#8220;是什么&#8221;。<br />
<span id="more-8941"></span><br />
<span style="background-color:#00ff00;">『1』</span>对深度学习（Deep Learning）的简单介绍<br />
以下解释来自维基百科：</p>
<blockquote>
<p>
		深度学习是机器学习拉出的分支，它试图使用包含复杂结构或由多重非线性变换构成的多个处理层对数据进行高层抽象的算法。</p>
</blockquote>
<p>深度学习的用途实在太广泛，最为普通人所熟知的，就是以下和民生相关的应用：人脸识别，语音识别，图像搜索，在线翻译，等等。<br />
目前流行的深度学习框架有TensorFlow（Google开源），MXNet（得到Amazon支持），Theano等，利用这些框架，我们只需要做比较少的工作，就能把深度学习能力带入我们自己的程序。<br />
<span style="color: rgb(255, 255, 255);">文章来源：</span><a href="http://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">http://www.codelast.com/</span></a><br />
<span style="background-color:#00ff00;">『3』</span>树莓派上的深度学习<br />
当前，主流的深度学习框架都不是<span style="color:#0000ff;">主要</span>为了移动平台／嵌入式平台而准备的&#8212;&#8212;这是由于计算能力所限，在移动平台上运行local的深度学习程序，计算速度通常会非常慢。因此，移动平台上主要还是采用向云端提交计算请求、云端计算完成后返回结果的方式来处理数据。<br />
作为一个&#8220;类嵌入式&#8221;平台，树莓派虽然是同类型里最受关注的产品，但我认为在深度学习的世界里，树莓派还没到像Android、iOS那种&#8220;开发一个App必须要支持&#8221;的程度。<br />
因此，把任何一个主流的深度学习框架，在树莓派上跑起来都将是一个耗时耗力的工作。<br />
好在TensorFlow是如此流行，并且IT界永远不缺牛人，已经有人把它成功地&#8220;移植&#8221;到了树莓派3代上（<a href="https://github.com/samjabrahams/tensorflow-on-raspberry-pi" rel="noopener noreferrer" target="_blank"><span style="background-color:#ffa07a;">看这里</span></a>），所以，在树莓派上用TensorFlow来实现深度学习应用是一个不错的选择。<br />
<span style="color: rgb(255, 255, 255);">文章来源：</span><a href="http://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">http://www.codelast.com/</span></a><br />
<span style="background-color: rgb(0, 255, 0);">『3』</span>在树莓派上安装TensorFlow<br />
按作者的<a href="https://github.com/samjabrahams/tensorflow-on-raspberry-pi" rel="noopener noreferrer" target="_blank"><span style="background-color:#ffa07a;">文档</span></a>，通过极其简单的几步操作，就可以在树莓派上把TensorFlow跑起来。如果你有兴趣，可以直接去看作者写的教程。<br />
首先要声明的是：</p>
<ul>
<li>
		作者在树莓派上使用的最流行的Linux发行版Raspbian，而我使用的OS是Arch Linux ARM，不过这无所谓，经过我的测试，没有问题（至少我没遇到）</li>
<li>
		由于在树莓派上开发其他程序的原因，我已经预先安装过了比较多的开发库／软件，类似于Protocol Buffers，NumPy，pip等，而这些软件有些可能会被TensorFlow依赖，所以，我就不需要像作者的文档里说的一样另外再去安装它们了</li>
</ul>
<p>
<span style="color: rgb(255, 255, 255);">文章来源：</span><a href="http://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">http://www.codelast.com/</span></a>&#8230; <a href="https://www.codelast.com/%e5%8e%9f%e5%88%9b-%e5%9c%a8%e6%a0%91%e8%8e%93%e6%b4%be%e4%b8%8a%e7%94%a8tensorflow%e7%8e%a9%e6%b7%b1%e5%ba%a6%e5%ad%a6%e4%b9%a0deep-learning/" class="read-more">Read More </a></p>]]></description>
										<content:encoded><![CDATA[<p>
本文软硬件环境：<br />
树莓派：3代 Model B V1.2，内存1GB<br />
OS：Arch Linux ARM</p>
<p>深度学习（Deep Learning）现在这么火，树莓派玩家们当然也不会放过，目前已经有很多树莓派项目都搭上了Deep Learning的车，纯粹出于&ldquo;好玩&rdquo;的目的，我在树莓派上也实验了一把，用TensorFlow来识别一张图片里的物体&ldquo;是什么&rdquo;。<br />
<span id="more-8941"></span><br />
<span style="background-color:#00ff00;">『1』</span>对深度学习（Deep Learning）的简单介绍<br />
以下解释来自维基百科：</p>
<blockquote>
<p>
		深度学习是机器学习拉出的分支，它试图使用包含复杂结构或由多重非线性变换构成的多个处理层对数据进行高层抽象的算法。</p>
</blockquote>
<p>深度学习的用途实在太广泛，最为普通人所熟知的，就是以下和民生相关的应用：人脸识别，语音识别，图像搜索，在线翻译，等等。<br />
目前流行的深度学习框架有TensorFlow（Google开源），MXNet（得到Amazon支持），Theano等，利用这些框架，我们只需要做比较少的工作，就能把深度学习能力带入我们自己的程序。<br />
<span style="color: rgb(255, 255, 255);">文章来源：</span><a href="http://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">http://www.codelast.com/</span></a><br />
<span style="background-color:#00ff00;">『3』</span>树莓派上的深度学习<br />
当前，主流的深度学习框架都不是<span style="color:#0000ff;">主要</span>为了移动平台／嵌入式平台而准备的&mdash;&mdash;这是由于计算能力所限，在移动平台上运行local的深度学习程序，计算速度通常会非常慢。因此，移动平台上主要还是采用向云端提交计算请求、云端计算完成后返回结果的方式来处理数据。<br />
作为一个&ldquo;类嵌入式&rdquo;平台，树莓派虽然是同类型里最受关注的产品，但我认为在深度学习的世界里，树莓派还没到像Android、iOS那种&ldquo;开发一个App必须要支持&rdquo;的程度。<br />
因此，把任何一个主流的深度学习框架，在树莓派上跑起来都将是一个耗时耗力的工作。<br />
好在TensorFlow是如此流行，并且IT界永远不缺牛人，已经有人把它成功地&ldquo;移植&rdquo;到了树莓派3代上（<a href="https://github.com/samjabrahams/tensorflow-on-raspberry-pi" rel="noopener noreferrer" target="_blank"><span style="background-color:#ffa07a;">看这里</span></a>），所以，在树莓派上用TensorFlow来实现深度学习应用是一个不错的选择。<br />
<span style="color: rgb(255, 255, 255);">文章来源：</span><a href="http://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">http://www.codelast.com/</span></a><br />
<span style="background-color: rgb(0, 255, 0);">『3』</span>在树莓派上安装TensorFlow<br />
按作者的<a href="https://github.com/samjabrahams/tensorflow-on-raspberry-pi" rel="noopener noreferrer" target="_blank"><span style="background-color:#ffa07a;">文档</span></a>，通过极其简单的几步操作，就可以在树莓派上把TensorFlow跑起来。如果你有兴趣，可以直接去看作者写的教程。<br />
首先要声明的是：</p>
<ul>
<li>
		作者在树莓派上使用的最流行的Linux发行版Raspbian，而我使用的OS是Arch Linux ARM，不过这无所谓，经过我的测试，没有问题（至少我没遇到）</li>
<li>
		由于在树莓派上开发其他程序的原因，我已经预先安装过了比较多的开发库／软件，类似于Protocol Buffers，NumPy，pip等，而这些软件有些可能会被TensorFlow依赖，所以，我就不需要像作者的文档里说的一样另外再去安装它们了</li>
</ul>
<p>
<span style="color: rgb(255, 255, 255);">文章来源：</span><a href="http://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">http://www.codelast.com/</span></a><br />
然后剩下最关键的一步就是，从GitHub下载一个wheel文件并安装。<br />
wheel是众多Python软件安装包格式中的一种，本质上是一个zip包格式，它使用.whl作为扩展名，用于安装Python模块。</p>
<p>如果你使用Python 2.7：</p>
<section class="output_wrapper" id="output_wrapper_id" style="font-size: 16px; color: rgb(62, 62, 62); line-height: 1.6; letter-spacing: 0px; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;">
<pre style="font-size: inherit; color: inherit; line-height: inherit; margin-top: 0px; margin-bottom: 0px; padding: 0px;">
<code class="bash language-bash hljs" style="margin: 0px 2px; line-height: 18px; font-size: 14px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; color: rgb(169, 183, 198); background: rgb(40, 43, 46); padding: 0.5em; overflow-wrap: normal !important; word-break: normal !important; overflow: auto !important; display: -webkit-box !important;">wget&nbsp;https://github.com/samjabrahams/tensorflow-on-raspberry-pi/releases/download/v0.11.0/tensorflow-0.11.0-cp27-none-linux_armv7l.whl
sudo&nbsp;pip&nbsp;install&nbsp;tensorflow-0.11.0-cp27-none-linux_armv7l.whl
</code></pre>
</section>
<p>
如果你使用Python 3.3+（但据作者所说，3.5以及3.6是不保证能支持的，不过，我用3.5.2还是安装成功了）：</p>
<section class="output_wrapper" id="output_wrapper_id" style="font-size: 16px; color: rgb(62, 62, 62); line-height: 1.6; letter-spacing: 0px; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;">
<pre style="font-size: inherit; color: inherit; line-height: inherit; margin-top: 0px; margin-bottom: 0px; padding: 0px;">
<code class="bash language-bash hljs" style="margin: 0px 2px; line-height: 18px; font-size: 14px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; color: rgb(169, 183, 198); background: rgb(40, 43, 46); padding: 0.5em; overflow-wrap: normal !important; word-break: normal !important; overflow: auto !important; display: -webkit-box !important;">wget&nbsp;https://github.com/samjabrahams/tensorflow-on-raspberry-pi/releases/download/v0.11.0/tensorflow-0.11.0-py3-none-any.whl
sudo&nbsp;pip3&nbsp;install&nbsp;tensorflow-0.11.0-py3-none-any.whl
</code></pre>
</section>
<p>下载whl安装包的过程可能会比较漫长，我使用的是Python 3.5.2，下面是我的命令行输出内容：</p>
<blockquote>
<div>
		Processing ./tensorflow-0.10.0-py3-none-any.whl</div>
<div>
		Collecting protobuf==3.0.0b2 (from tensorflow==0.10.0)</div>
<div>
		&nbsp; Downloading protobuf-3.0.0b2-py2.py3-none-any.whl (326kB)</div>
<div>
		&nbsp; &nbsp; 100% |████████████████████████████████| 327kB 11kB/s</div>
<div>
		Collecting wheel&gt;=0.26 (from tensorflow==0.10.0)</div>
<div>
		&nbsp; Downloading wheel-0.29.0-py2.py3-none-any.whl (66kB)</div>
<div>
		&nbsp; &nbsp; 100% |████████████████████████████████| 71kB 18kB/s</div>
<div>
		Requirement already satisfied (use --upgrade to upgrade): numpy&gt;=1.8.2 in /usr/lib/python3.5/site-packages (from tensorflow==0.10.0)</div>
<div>
		Requirement already satisfied (use --upgrade to upgrade): six&gt;=1.10.0 in /usr/lib/python3.5/site-packages (from tensorflow==0.10.0)</div>
<div>
		Requirement already satisfied (use --upgrade to upgrade): setuptools in /usr/lib/python3.5/site-packages (from protobuf==3.0.0b2-&gt;tensorflow==0.10.0)</div>
<div>
		Installing collected packages: protobuf, wheel, tensorflow</div>
<div>
		Successfully installed protobuf-3.0.0b2 tensorflow-0.10.0 wheel-0.29.0</div>
<div>
		You are using pip version 8.1.2, however version 9.0.1 is available.</div>
<div>
		You should consider upgrading via the &#39;pip install --upgrade pip&#39; command.</div>
</blockquote>
<p><span style="color: rgb(255, 255, 255);">文章来源：</span><a href="http://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">http://www.codelast.com/</span></a><br />
然后呢？然后就搞定了！就这么简单！</p>
<p>如果上面的步骤失败了，那么你就只能选择从源码来编译TensorFlow，这是一个相当麻烦的工作，只能祝你好运了，不过好消息就是，作者已经帮大家踩过很多坑了，教程在<a href="https://github.com/samjabrahams/tensorflow-on-raspberry-pi/blob/master/GUIDE.md" rel="noopener noreferrer" target="_blank"><span style="background-color:#ffa07a;">这里</span></a>。</p>
<p><span style="background-color:#00ff00;">『4』</span>在树莓派上使用TensorFlow<br />
要识别一张图片里的物体是什么，我们需要先训练一个图像分类模型，这个过程非常消耗计算资源，在树莓派上干这事是不明智的，我们可以直接使用Google已经训练好的<a href="https://www.tensorflow.org/versions/r0.12/tutorials/image_recognition/index.html" rel="noopener noreferrer" target="_blank"><span style="color:#0000ff;">Inception-v3</span>模型</a>。</p>
<blockquote>
<p>
		Inception-v3 is trained for the ImageNet Large Visual Recognition Challenge using the data from 2012. This is a standard task in computer vision, where models try to classify entire images into 1000 classes, like &quot;Zebra&quot;, &quot;Dalmatian&quot;, and &quot;Dishwasher&quot;.</p>
</blockquote>
<p>先下载Inception-V3模型到任意目录中，并解压出来：</p>
<section class="output_wrapper" id="output_wrapper_id" style="font-size: 16px; color: rgb(62, 62, 62); line-height: 1.6; letter-spacing: 0px; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;">
<pre style="font-size: inherit; color: inherit; line-height: inherit; margin-top: 0px; margin-bottom: 0px; padding: 0px;">
<code class="bash language-bash hljs" style="margin: 0px 2px; line-height: 18px; font-size: 14px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; color: rgb(169, 183, 198); background: rgb(40, 43, 46); padding: 0.5em; overflow-wrap: normal !important; word-break: normal !important; overflow: auto !important; display: -webkit-box !important;">mkdir&nbsp;~/tensorflow-related/model
<span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">cd</span>&nbsp;~/tensorflow-related/model
wget&nbsp;http://download.tensorflow.org/models/image/imagenet/inception-2015-12-05.tgz
tar&nbsp;xf&nbsp;inception-2015-12-05.tgz
</code></pre>
</section>
<p>解压出来一堆文件：</p>
<blockquote>
<div>
		classify_image_graph_def.pb</div>
<div>
		cropped_panda.jpg</div>
<div>
		imagenet_2012_challenge_label_map_proto.pbtxt</div>
<div>
		imagenet_synset_to_human_label_map.txt</div>
<div>
		LICENSE</div>
</blockquote>
<p>然后就可以开始进行图像识别啦。在这里我从网上找了一张时下很流行的摩拜单车（Mobike）的图片：<br />
<a href="http://www.codelast.com/" rel="noopener noreferrer" target="_blank"><img decoding="async" alt="mobike" src="http://www.codelast.com/wp-content/uploads/2016/12/mobike.jpg" style="width: 484px; height: 360px;" /></a><br />
<span style="color: rgb(255, 255, 255);">文章来源：</span><a href="http://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">http://www.codelast.com/</span></a><br />
用TensorFlow来识别它：</p>
<section class="output_wrapper" id="output_wrapper_id" style="font-size: 16px; color: rgb(62, 62, 62); line-height: 1.6; letter-spacing: 0px; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;">
<pre style="font-size: inherit; color: inherit; line-height: inherit; margin-top: 0px; margin-bottom: 0px; padding: 0px;">
<code class="bash language-bash hljs" style="margin: 0px 2px; line-height: 18px; font-size: 14px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; color: rgb(169, 183, 198); background: rgb(40, 43, 46); padding: 0.5em; overflow-wrap: normal !important; word-break: normal !important; overflow: auto !important; display: -webkit-box !important;"><span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">cd</span>&nbsp;/usr/lib/python3.5/site-packages/tensorflow/models/image/imagenet
python3.5&nbsp;classify_image.py&nbsp;--model_dir&nbsp;/root/tensorflow-related/model&nbsp;--image_file&nbsp;/root/tensorflow-related/<span class="hljs-built_in" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">test</span>-images/mobike.jpg
</code></pre>
</section>
<p>其中，<span style="color:#0000ff;">/usr/lib/python3.5/site-packages/tensorflow/models/image/imagenet</span> 这个路径是TensorFlow的 Python图像分类程序&nbsp;classify_image.py 所在的路径，不同的OS可能不一样。<br />
<span style="color:#0000ff;">--model_dir</span> 参数传入的是我们前面解压出来的模型文件所在的路径，<span style="color:#0000ff;">--image_file</span> 是待识别的图片的路径。<br />
<span style="color:#b22222;">注：如果找不到classify_image.py文件，请参考本文末的更新说明。</span><br />
输出如下：</p>
<blockquote>
<div>
		/usr/lib/python3.5/site-packages/tensorflow/python/ops/array_ops.py:1750: VisibleDeprecationWarning: converting an array with ndim &gt; 0 to an index will result in an error in the future</div>
<div>
		&nbsp; result_shape.insert(dim, 1)</div>
<div>
		W tensorflow/core/framework/op_def_util.cc:332] Op BatchNormWithGlobalNormalization is deprecated. It will cease to work in GraphDef version 9. Use tf.nn.batch_normalization().</div>
<div>
		bicycle-built-for-two, tandem bicycle, tandem (score = 0.33731)</div>
<div>
		tricycle, trike, velocipede (score = 0.16082)</div>
<div>
		unicycle, monocycle (score = 0.12926)</div>
<div>
		mountain bike, all-terrain bike, off-roader (score = 0.10689)</div>
<div>
		parking meter (score = 0.01563)</div>
<div>
		[root@alarmpi imagenet]# python3.5 classify_image.py --model_dir /root/tensorflow-related/model --image_file /root/tensorflow-related/test-images/mobike.jpg</div>
<div>
		/usr/lib/python3.5/site-packages/tensorflow/python/ops/array_ops.py:1750: VisibleDeprecationWarning: converting an array with ndim &gt; 0 to an index will result in an error in the future</div>
<div>
		&nbsp; result_shape.insert(dim, 1)</div>
<div>
		W tensorflow/core/framework/op_def_util.cc:332] Op BatchNormWithGlobalNormalization is deprecated. It will cease to work in GraphDef version 9. Use tf.nn.batch_normalization().</div>
<div>
		<span style="color:#b22222;">mountain bike, all-terrain bike, off-roader (score = 0.56671)</span></div>
<div>
		<span style="color:#b22222;">tricycle, trike, velocipede (score = 0.12035)</span></div>
<div>
		<span style="color:#b22222;">bicycle-built-for-two, tandem bicycle, tandem (score = 0.08768)</span></div>
<div>
		<span style="color:#b22222;">lawn mower, mower (score = 0.00651)</span></div>
<div>
		<span style="color:#b22222;">alp (score = 0.00387)</span></div>
</blockquote>
<p>
可见，TensorFlow认为图片是山地自行车（mountain bike）／全地形自行车（all-terrain bike）／越野车（off-roader）的概率是0.56671，识别结果还算可以。<br />
<span style="color: rgb(255, 255, 255);">文章来源：</span><a href="http://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">http://www.codelast.com/</span></a><br />
<span style="background-color:#00ff00;">『5』</span>计算速度问题<br />
上面的一次图像识别试验，总共花了50多秒的时间！这么慢的速度在实际应用中基本没有实用价值。<br />
但实际上，这个时间是可以大幅缩短的。</p>
<ul>
<li>
		为模型&ldquo;预热&rdquo;很有必要。根据作者等人的<a href="https://github.com/samjabrahams/tensorflow-on-raspberry-pi/tree/master/benchmarks/inceptionv3" rel="noopener noreferrer" target="_blank"><span style="background-color:#ffa07a;">性能测试结果</span></a>，在合理预热的情况下，计算时间会减少较多。<span style="color: rgb(255, 0, 0);">我测试了一下，请看<a href="http://www.codelast.com/?p=8984" rel="noopener noreferrer" target="_blank"><span style="background-color:#ffa07a;">这篇</span></a>文章</span>。</li>
<li>
		我们每运行一次程序，就重新加载一次模型，而模型文件又很大，这非常浪费时间，所以，应该把程序做成一个常驻内存的程序，只加载一次模型，每次识别一幅图像就能减少很多很多时间。<span style="color: rgb(255, 0, 0);">我也实验了一下，请看<a href="http://www.codelast.com/?p=8995" rel="noopener noreferrer" target="_blank"><span style="background-color: rgb(255, 160, 122);">这篇</span></a>文章</span>。</li>
<li>
		使用TensorFlow的C++接口来实现程序应该比Python版速度快，可以尝试。</li>
<li>
		暂时还不能让TensorFlow使用树莓派的GPU来计算（<a href="https://github.com/samjabrahams/tensorflow-on-raspberry-pi/issues/15" rel="noopener noreferrer" target="_blank"><span style="background-color:#ffa07a;">看这里</span></a>），但大家期待未来有一天这个愿望会实现。</li>
</ul>
<p>所以至少50多秒这种恐怖的数字是可以避免的。<br />
<span style="color: rgb(255, 255, 255);">文章来源：</span><a href="http://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">http://www.codelast.com/</span></a><br />
<span style="background-color:#00ff00;">『6』</span>在树莓派上跑TensorFlow有什么用<br />
我随意想到的一个可能有意义的应用就是：一个幼儿辅助学习系统。在树莓派上挂载一个摄像头，孩子在摄像头前拿着一样东西，摄像头抓拍一张图片，识别出里面的东西，朗读出概率最大的那个英文单词。<br />
当然，这里面有非常多的工程上的问题需要解决。</p>
<p>我相信，随着树莓派下一代的计算能力继续增强，以及TensorFlow每一次发布，都让性能提高一些，在不久的将来，在树莓派上跑TensorFlow应用的实用性将会非常好。<br />
<span style="color: rgb(255, 255, 255);">文章来源：</span><a href="http://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">http://www.codelast.com/</span></a><br />
<span style="background-color: rgb(0, 255, 0);">『7』</span>更新</p>
<ul>
<li>
		<span style="color:#ff0000;">2017.03.19更新</span></li>
</ul>
<p>在本文写完之后不久，作者就发布了基于TensorFlow 1.0.0的Python wheel包，我试验了一下，它无法在Python 3.6.0的环境下安装，提示错误是：</p>
<blockquote>
<p>
		tensorflow-1.0.0-cp34-cp34m-linux_armv7l.whl is not a supported wheel on this platform</p>
</blockquote>
<p>作者说这是因为暂时不支持Python 3.5+。所以为了装上新版的TensorFlow，我用回了Python 2.7，然后就发现可以正常安装了：</p>
<section class="output_wrapper" id="output_wrapper_id" style="font-size: 16px; color: rgb(62, 62, 62); line-height: 1.6; letter-spacing: 0px; font-family: &quot;Helvetica Neue&quot;, Helvetica, &quot;Hiragino Sans GB&quot;, &quot;Microsoft YaHei&quot;, Arial, sans-serif;">
<pre style="font-size: inherit; color: inherit; line-height: inherit; margin-top: 0px; margin-bottom: 0px; padding: 0px;">
<code class="bash language-bash hljs" style="margin: 0px 2px; line-height: 18px; font-size: 14px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; border-radius: 0px; color: rgb(169, 183, 198); background: rgb(40, 43, 46); padding: 0.5em; overflow-wrap: normal !important; word-break: normal !important; overflow: auto !important; display: -webkit-box !important;">pacman&nbsp;-S&nbsp;python2&nbsp;python2-pip
pip2&nbsp;install&nbsp;tensorflow-1.0.0-cp27-none-linux_armv7l.whl
</code></pre>
</section>
<p>期待以后能支持。</p>
<ul>
<li>
		<span style="color: rgb(255, 0, 0);">2017.08.19更新</span></li>
</ul>
<p>如果你安装的是1.1.0版本的<a href="https://github.com/samjabrahams/tensorflow-on-raspberry-pi" rel="noopener noreferrer" target="_blank"><span style="background-color: rgb(255, 160, 122);">TensorFlow on Raspberry Pi</span></a>，那么你会发现你找不到classify_image.py文件，这是因为此版本确实不再包含该文件，这时你可以在<span style="background-color: rgb(255, 160, 122);"><a href="https://github.com/tensorflow/models" rel="noopener noreferrer" target="_blank">TensorFlow的这个子项目</a></span>里找到该文件。</p>
<p><span style="color: rgb(255, 255, 255);">文章来源：</span><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">https://www.codelast.com/</span></a><br />
<span style="color: rgb(255, 0, 0);">➤➤</span>&nbsp;版权声明&nbsp;<span style="color: rgb(255, 0, 0);">➤➤</span>&nbsp;<br />
转载需注明出处：<u><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><em><span style="color: rgb(0, 0, 255);"><strong style="font-size: 16px;"><span style="font-family: arial, helvetica, sans-serif;">codelast.com</span></strong></span></em></a></u>&nbsp;<br />
感谢关注我的微信公众号（微信扫一扫）：</p>
<p style="border: 0px; font-size: 13px; margin: 0px 0px 9px; outline: 0px; padding: 0px; color: rgb(77, 77, 77);">
	<img decoding="async" alt="wechat qrcode of codelast" src="https://www.codelast.com/codelast_wechat_qr_code.jpg" style="width: 200px; height: 200px;" /></p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.codelast.com/%e5%8e%9f%e5%88%9b-%e5%9c%a8%e6%a0%91%e8%8e%93%e6%b4%be%e4%b8%8a%e7%94%a8tensorflow%e7%8e%a9%e6%b7%b1%e5%ba%a6%e5%ad%a6%e4%b9%a0deep-learning/feed/</wfw:commentRss>
			<slash:comments>16</slash:comments>
		
		
			</item>
		<item>
		<title>[原创] 用人话解释机器学习中的Logistic Regression（逻辑回归）</title>
		<link>https://www.codelast.com/%e5%8e%9f%e5%88%9b-%e7%94%a8%e4%ba%ba%e8%af%9d%e8%a7%a3%e9%87%8a%e6%9c%ba%e5%99%a8%e5%ad%a6%e4%b9%a0%e4%b8%ad%e7%9a%84logistic-regression%ef%bc%88%e9%80%bb%e8%be%91%e5%9b%9e%e5%bd%92%ef%bc%89/</link>
					<comments>https://www.codelast.com/%e5%8e%9f%e5%88%9b-%e7%94%a8%e4%ba%ba%e8%af%9d%e8%a7%a3%e9%87%8a%e6%9c%ba%e5%99%a8%e5%ad%a6%e4%b9%a0%e4%b8%ad%e7%9a%84logistic-regression%ef%bc%88%e9%80%bb%e8%be%91%e5%9b%9e%e5%bd%92%ef%bc%89/#comments</comments>
		
		<dc:creator><![CDATA[learnhard]]></dc:creator>
		<pubDate>Fri, 19 Feb 2016 08:47:08 +0000</pubDate>
				<category><![CDATA[Algorithm]]></category>
		<category><![CDATA[Math]]></category>
		<category><![CDATA[原创]]></category>
		<category><![CDATA[cross entropy]]></category>
		<category><![CDATA[Logistic Regression]]></category>
		<category><![CDATA[Logit Regression]]></category>
		<category><![CDATA[machine learning]]></category>
		<category><![CDATA[optimization]]></category>
		<category><![CDATA[交叉熵]]></category>
		<category><![CDATA[最优化]]></category>
		<category><![CDATA[机器学习]]></category>
		<category><![CDATA[逻辑回归]]></category>
		<guid isPermaLink="false">http://www.codelast.com/?p=8629</guid>

					<description><![CDATA[<p>友情提示：如果觉得页面中的公式显示太小，可以放大页面查看（不会失真）。</p>
<p><span style="color:#0000ff;">Logistic Regression</span>（或<span style="color:#0000ff;">Logit Regression</span>），即<span style="color:#0000ff;">逻辑回归</span>，简记为<span style="color:#0000ff;">LR</span>，是机器学习领域的一种极为常用的算法／方法／模型。<br />
你能从网上搜到十万篇讲述Logistic Regression的文章，也不多我这一篇，但是，就像我写过的<a href="http://www.codelast.com/?p=7364" rel="noopener noreferrer" target="_blank"><span style="background-color:#ffa07a;">最优化系列文章</span></a>一样，我仍然试图用&#8220;人话&#8221;来再解释一遍&#8212;&#8212;可能不专业，但是容易看得懂。那些一上来就是几页数学公式什么的最讨厌了，不是吗？<br />
所以这篇文章是写给完全没听说过Logistic Regression的人看的，我相信看完这篇文章，你差不多可以从无到有，把逻辑回归应用到实践中去。<br />
<span id="more-8629"></span><br />
Logistic Regression是一种<span style="color:#0000ff;">分类</span>算法。分类，也就是把一个群体（或问题，或数据）分为几个类别，例如，男/女/人妖；爱她的人/不爱她的人；今天会下雨/今天不会下雨。<br />
Logistic Regression最常用于处理&#8220;<span style="color:#0000ff;">二分类</span>&#8221;问题，也就是说分类只有两个，像&#8220;爱她的人/不爱她的人&#8221;就是二分类，而&#8220;男/女/人妖&#8221;就不是二分类。当然，Logistic Regression也可以用于处理多分类问题，即所谓的&#8220;<span style="color:#800080;">多分类逻辑回归</span>&#8221;（<span style="color:#b22222;">Multiclass Logistic Regression</span>），但本文并不涉及这个方面。<br />
所以，说得简单点就是，给你一条数据，用Logistic Regression可以判断出这条数据应该被分到两个类别中的哪个中去。<br />
<span style="color: rgb(255, 255, 255);">文章来源：</span><a href="http://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">http://www.codelast.com/</span></a><br />
Logistic Regression在现实世界中非常有用。例如，可以用它来判断一个用户是否会点击一个广告（会点击／不会点击），可以用Logistic Regression来判断两类人是否会相爱（会相爱／不会相爱），等等。</p>
<p>机器学习的主旨就是通过对历史数据的计算（即&#8220;学习&#8221;），得到一些未知参数的值，从而可以推断出新数据会有什么结论。例如一个非常简单的函数： <span class='MathJax_Preview'><img src='https://www.codelast.com/wp-content/plugins/latex/cache/tex_0fdb61c8abfb7bfd67516c1f8c8055b2.gif' style='vertical-align: middle; border: none; padding-bottom:2px;' class='tex' alt="y = ax + b" /></span><script type='math/tex'>y = ax + b</script> ，在已知几组  <span class='MathJax_Preview'><img src='https://www.codelast.com/wp-content/plugins/latex/cache/tex_90cbc22edf225adf8a68974f51227f05.gif' style='vertical-align: middle; border: none; padding-bottom:2px;' class='tex' alt="(x,y)" /></span><script type='math/tex'>(x,y)</script>  历史数据的情况下：</p>
<div>
<blockquote>
<div>
			(1, 5.5)<br />
			(1.5, 7)<br />
			(2, 6.5)</div>
</blockquote>
<p>	我们怎样能够预测一个未知的自变量  <span class='MathJax_Preview'><img src='https://www.codelast.com/wp-content/plugins/latex/cache/tex_4c63f2fd29fec6c20010a36949e9752d.gif' style='vertical-align: middle; border: none; padding-bottom:2px;' class='tex' alt="x = 3" /></span><script type='math/tex'>x = 3</script>  会对应什么样的因变量&#160; <span class='MathJax_Preview'><img src='https://www.codelast.com/wp-content/plugins/latex/cache/tex_415290769594460e2e485922904f345d.gif' style='vertical-align: middle; border: none; padding-bottom:2px;' class='tex' alt="y" /></span><script type='math/tex'>y</script>  呢？也就是说， <span class='MathJax_Preview'><img src='https://www.codelast.com/wp-content/plugins/latex/cache/tex_4c63f2fd29fec6c20010a36949e9752d.gif' style='vertical-align: middle; border: none; padding-bottom:2px;' class='tex' alt="x = 3" /></span><script type='math/tex'>x = 3</script>  时&#160; <span class='MathJax_Preview'><img src='https://www.codelast.com/wp-content/plugins/latex/cache/tex_232ec2a90bc0ab9fb1a405d8ffe47582.gif' style='vertical-align: middle; border: none; padding-bottom:2px;' class='tex' alt="y = ?" /></span><script type='math/tex'>y = ?</script></p></div>&#8230; <a href="https://www.codelast.com/%e5%8e%9f%e5%88%9b-%e7%94%a8%e4%ba%ba%e8%af%9d%e8%a7%a3%e9%87%8a%e6%9c%ba%e5%99%a8%e5%ad%a6%e4%b9%a0%e4%b8%ad%e7%9a%84logistic-regression%ef%bc%88%e9%80%bb%e8%be%91%e5%9b%9e%e5%bd%92%ef%bc%89/" class="read-more">Read More </a>]]></description>
										<content:encoded><![CDATA[<p>友情提示：如果觉得页面中的公式显示太小，可以放大页面查看（不会失真）。</p>
<p><span style="color:#0000ff;">Logistic Regression</span>（或<span style="color:#0000ff;">Logit Regression</span>），即<span style="color:#0000ff;">逻辑回归</span>，简记为<span style="color:#0000ff;">LR</span>，是机器学习领域的一种极为常用的算法／方法／模型。<br />
你能从网上搜到十万篇讲述Logistic Regression的文章，也不多我这一篇，但是，就像我写过的<a href="http://www.codelast.com/?p=7364" rel="noopener noreferrer" target="_blank"><span style="background-color:#ffa07a;">最优化系列文章</span></a>一样，我仍然试图用&ldquo;人话&rdquo;来再解释一遍&mdash;&mdash;可能不专业，但是容易看得懂。那些一上来就是几页数学公式什么的最讨厌了，不是吗？<br />
所以这篇文章是写给完全没听说过Logistic Regression的人看的，我相信看完这篇文章，你差不多可以从无到有，把逻辑回归应用到实践中去。<br />
<span id="more-8629"></span><br />
Logistic Regression是一种<span style="color:#0000ff;">分类</span>算法。分类，也就是把一个群体（或问题，或数据）分为几个类别，例如，男/女/人妖；爱她的人/不爱她的人；今天会下雨/今天不会下雨。<br />
Logistic Regression最常用于处理&ldquo;<span style="color:#0000ff;">二分类</span>&rdquo;问题，也就是说分类只有两个，像&ldquo;爱她的人/不爱她的人&rdquo;就是二分类，而&ldquo;男/女/人妖&rdquo;就不是二分类。当然，Logistic Regression也可以用于处理多分类问题，即所谓的&ldquo;<span style="color:#800080;">多分类逻辑回归</span>&rdquo;（<span style="color:#b22222;">Multiclass Logistic Regression</span>），但本文并不涉及这个方面。<br />
所以，说得简单点就是，给你一条数据，用Logistic Regression可以判断出这条数据应该被分到两个类别中的哪个中去。<br />
<span style="color: rgb(255, 255, 255);">文章来源：</span><a href="http://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">http://www.codelast.com/</span></a><br />
Logistic Regression在现实世界中非常有用。例如，可以用它来判断一个用户是否会点击一个广告（会点击／不会点击），可以用Logistic Regression来判断两类人是否会相爱（会相爱／不会相爱），等等。</p>
<p>机器学习的主旨就是通过对历史数据的计算（即&ldquo;学习&rdquo;），得到一些未知参数的值，从而可以推断出新数据会有什么结论。例如一个非常简单的函数： <span class='MathJax_Preview'><img src='https://www.codelast.com/wp-content/plugins/latex/cache/tex_0fdb61c8abfb7bfd67516c1f8c8055b2.gif' style='vertical-align: middle; border: none; padding-bottom:2px;' class='tex' alt="y = ax + b" /></span><script type='math/tex'>y = ax + b</script> ，在已知几组  <span class='MathJax_Preview'><img src='https://www.codelast.com/wp-content/plugins/latex/cache/tex_90cbc22edf225adf8a68974f51227f05.gif' style='vertical-align: middle; border: none; padding-bottom:2px;' class='tex' alt="(x,y)" /></span><script type='math/tex'>(x,y)</script>  历史数据的情况下：</p>
<div>
<blockquote>
<div>
			(1, 5.5)<br />
			(1.5, 7)<br />
			(2, 6.5)</div>
</blockquote>
<p>	我们怎样能够预测一个未知的自变量  <span class='MathJax_Preview'><img src='https://www.codelast.com/wp-content/plugins/latex/cache/tex_4c63f2fd29fec6c20010a36949e9752d.gif' style='vertical-align: middle; border: none; padding-bottom:2px;' class='tex' alt="x = 3" /></span><script type='math/tex'>x = 3</script>  会对应什么样的因变量&nbsp; <span class='MathJax_Preview'><img src='https://www.codelast.com/wp-content/plugins/latex/cache/tex_415290769594460e2e485922904f345d.gif' style='vertical-align: middle; border: none; padding-bottom:2px;' class='tex' alt="y" /></span><script type='math/tex'>y</script>  呢？也就是说， <span class='MathJax_Preview'><img src='https://www.codelast.com/wp-content/plugins/latex/cache/tex_4c63f2fd29fec6c20010a36949e9752d.gif' style='vertical-align: middle; border: none; padding-bottom:2px;' class='tex' alt="x = 3" /></span><script type='math/tex'>x = 3</script>  时&nbsp; <span class='MathJax_Preview'><img src='https://www.codelast.com/wp-content/plugins/latex/cache/tex_232ec2a90bc0ab9fb1a405d8ffe47582.gif' style='vertical-align: middle; border: none; padding-bottom:2px;' class='tex' alt="y = ?" /></span><script type='math/tex'>y = ?</script> <br />
	显然我们的任务就是计算出两个未知参数&nbsp; <span class='MathJax_Preview'><img src='https://www.codelast.com/wp-content/plugins/latex/cache/tex_0cc175b9c0f1b6a831c399e269772661.gif' style='vertical-align: middle; border: none; padding-bottom:2px;' class='tex' alt="a" /></span><script type='math/tex'>a</script>  和&nbsp; <span class='MathJax_Preview'><img src='https://www.codelast.com/wp-content/plugins/latex/cache/tex_92eb5ffee6ae2fec3ad71c777531578f.gif' style='vertical-align: middle; border: none; padding-bottom:2px;' class='tex' alt="b" /></span><script type='math/tex'>b</script>  的值，有了这两个值，那么任意给定一个&nbsp; <span class='MathJax_Preview'><img src='https://www.codelast.com/wp-content/plugins/latex/cache/tex_9dd4e461268c8034f5c8564e155c67a6.gif' style='vertical-align: middle; border: none; padding-bottom:2px;' class='tex' alt="x" /></span><script type='math/tex'>x</script> ，我们都能通过函数&nbsp; <span class='MathJax_Preview'><img src='https://www.codelast.com/wp-content/plugins/latex/cache/tex_0fdb61c8abfb7bfd67516c1f8c8055b2.gif' style='vertical-align: middle; border: none; padding-bottom:2px;' class='tex' alt="y = ax + b" /></span><script type='math/tex'>y = ax + b</script>  计算出&nbsp; <span class='MathJax_Preview'><img src='https://www.codelast.com/wp-content/plugins/latex/cache/tex_415290769594460e2e485922904f345d.gif' style='vertical-align: middle; border: none; padding-bottom:2px;' class='tex' alt="y" /></span><script type='math/tex'>y</script>  的值了，这就是所谓的&ldquo;预测&rdquo;。<br />
	<span style="color: rgb(255, 255, 255);">文章来源：</span><a href="http://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">http://www.codelast.com/</span></a><br />
	Logistic Regression也是类似，我们有一个函数  <span class='MathJax_Preview'><img src='https://www.codelast.com/wp-content/plugins/latex/cache/tex_d19546fa62d4242fb94dd1e54f1497ec.gif' style='vertical-align: middle; border: none; padding-bottom:2px;' class='tex' alt="y = f(X)" /></span><script type='math/tex'>y = f(X)</script> ，里面包含若干个未知参数  <span class='MathJax_Preview'><img src='https://www.codelast.com/wp-content/plugins/latex/cache/tex_600c746b4b1e96f24bf5e68ffdd17c32.gif' style='vertical-align: middle; border: none; padding-bottom:2px;' class='tex' alt="{\theta _0},{\theta _1},{\theta _2}, \cdots ,{\theta _n}" /></span><script type='math/tex'>{\theta _0},{\theta _1},{\theta _2}, \cdots ,{\theta _n}</script> 。<br />
	由于现实世界是复杂的，因变量&nbsp; <span class='MathJax_Preview'><img src='https://www.codelast.com/wp-content/plugins/latex/cache/tex_415290769594460e2e485922904f345d.gif' style='vertical-align: middle; border: none; padding-bottom:2px;' class='tex' alt="y" /></span><script type='math/tex'>y</script>  通常会跟很多因素（自变量  <span class='MathJax_Preview'><img src='https://www.codelast.com/wp-content/plugins/latex/cache/tex_9dd4e461268c8034f5c8564e155c67a6.gif' style='vertical-align: middle; border: none; padding-bottom:2px;' class='tex' alt="x" /></span><script type='math/tex'>x</script> ）有关系，即&nbsp; <span class='MathJax_Preview'><img src='https://www.codelast.com/wp-content/plugins/latex/cache/tex_51e1680e3df2c1a03394b225f5754880.gif' style='vertical-align: middle; border: none; padding-bottom:2px;' class='tex' alt="{x_0},{x_1},{x_2}, \cdots ,{x_n}" /></span><script type='math/tex'>{x_0},{x_1},{x_2}, \cdots ,{x_n}</script> ，所以这里自变量是一个<span style="color:#0000ff;">向量</span>，这里用大写的  <span class='MathJax_Preview'><img src='https://www.codelast.com/wp-content/plugins/latex/cache/tex_02129bb861061d1a052c592e2dc6b383.gif' style='vertical-align: middle; border: none; padding-bottom:2px;' class='tex' alt="X" /></span><script type='math/tex'>X</script>  来表示。同理，那一堆未知的参数也是一个向量，用一个字母  <span class='MathJax_Preview'><img src='https://www.codelast.com/wp-content/plugins/latex/cache/tex_50d91f80cbb8feda1d10e167107ad1ff.gif' style='vertical-align: middle; border: none; padding-bottom:2px;' class='tex' alt="\theta " /></span><script type='math/tex'>\theta </script>  来表示。<br />
	现在给我们一堆 <span class='MathJax_Preview'><img src='https://www.codelast.com/wp-content/plugins/latex/cache/tex_90cbc22edf225adf8a68974f51227f05.gif' style='vertical-align: middle; border: none; padding-bottom:2px;' class='tex' alt="(x,y)" /></span><script type='math/tex'>(x,y)</script> 的历史数据，我们要想办法计算出所有未知参数的值，然后就可以拿来预测新的&nbsp; <span class='MathJax_Preview'><img src='https://www.codelast.com/wp-content/plugins/latex/cache/tex_9dd4e461268c8034f5c8564e155c67a6.gif' style='vertical-align: middle; border: none; padding-bottom:2px;' class='tex' alt="x" /></span><script type='math/tex'>x</script>  值所对应的&nbsp; <span class='MathJax_Preview'><img src='https://www.codelast.com/wp-content/plugins/latex/cache/tex_415290769594460e2e485922904f345d.gif' style='vertical-align: middle; border: none; padding-bottom:2px;' class='tex' alt="y" /></span><script type='math/tex'>y</script>  值了。<br />
	但是这个函数是什么呢？如下：<br />
	<span style="color:#0000ff;"> <span class='MathJax_Preview'><img src='https://www.codelast.com/wp-content/plugins/latex/cache/tex_d75712642d3eb1dbfc0bc0718cdfb7cd.gif' style='vertical-align: middle; border: none; padding-bottom:2px;' class='tex' alt="f(X) = \frac{1}{{1 + {e^{ - {\theta ^T}X}}}}{\rm{ }}" /></span><script type='math/tex'>f(X) = \frac{1}{{1 + {e^{ - {\theta ^T}X}}}}{\rm{ }}</script> </span> <span style="color:#ff0000;">&hellip;&hellip;（1）</span><br />
	其中， <span class='MathJax_Preview'><img src='https://www.codelast.com/wp-content/plugins/latex/cache/tex_50d91f80cbb8feda1d10e167107ad1ff.gif' style='vertical-align: middle; border: none; padding-bottom:2px;' class='tex' alt="\theta " /></span><script type='math/tex'>\theta </script>  是参数向量， <span class='MathJax_Preview'><img src='https://www.codelast.com/wp-content/plugins/latex/cache/tex_02129bb861061d1a052c592e2dc6b383.gif' style='vertical-align: middle; border: none; padding-bottom:2px;' class='tex' alt="X" /></span><script type='math/tex'>X</script>  是自变量（向量）。<br />
	<span style="color: rgb(255, 255, 255);">文章来源：</span><a href="http://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">http://www.codelast.com/</span></a><br />
	那么，这个略显奇怪的函数是怎么来的呢？<br />
	首先我们看  <span class='MathJax_Preview'><img src='https://www.codelast.com/wp-content/plugins/latex/cache/tex_7214334e01f01d7c5648efcd790e4631.gif' style='vertical-align: middle; border: none; padding-bottom:2px;' class='tex' alt="{{\theta ^T}X}" /></span><script type='math/tex'>{{\theta ^T}X}</script>  这部分：这是参数向量与自变量（向量）的<a href="https://zh.wikipedia.org/wiki/%E6%95%B0%E9%87%8F%E7%A7%AF" rel="noopener noreferrer" target="_blank"><span style="background-color:#ffa07a;">点积</span></a>，这个式子想要表达的含义是：计算某个事件发生的可能性，可以把跟这个事件相关的所有特征加权求和。例如，要求今天下雨的可能性，可以把今天所有和下雨相关的概率加权求和，例如梅雨季节权重为9（每天都很可能下雨），有台风经过权重为6，等等，每一个因素都影响着&ldquo;下雨的可能性&rdquo;，即：<br />
	 <span class='MathJax_Preview'><img src='https://www.codelast.com/wp-content/plugins/latex/cache/tex_bbeb4b1d67260873773804370090ce01.gif' style='vertical-align: middle; border: none; padding-bottom:2px;' class='tex' alt="s = \sum\limits_{i = 0}^n {{\theta _i}{x_i}} = {\theta _0}{x_0} + {\theta _1}{x_1} + \cdots + {\theta _n}{x_n} = {\theta ^T}X" /></span><script type='math/tex'>s = \sum\limits_{i = 0}^n {{\theta _i}{x_i}} = {\theta _0}{x_0} + {\theta _1}{x_1} + \cdots + {\theta _n}{x_n} = {\theta ^T}X</script> <br />
	但是这个加权求和的结果是在  <span class='MathJax_Preview'><img src='https://www.codelast.com/wp-content/plugins/latex/cache/tex_d0b6f29f7312261c6da8700518a6a27e.gif' style='vertical-align: middle; border: none; padding-bottom:2px;' class='tex' alt="( - \infty , + \infty )" /></span><script type='math/tex'>( - \infty , + \infty )</script>  范围内的，为了能表示预测的概率，我们希望把输出值限制在  <span class='MathJax_Preview'><img src='https://www.codelast.com/wp-content/plugins/latex/cache/tex_b6dbc33006b907f2db1855810abfce98.gif' style='vertical-align: middle; border: none; padding-bottom:2px;' class='tex' alt="(0,1)" /></span><script type='math/tex'>(0,1)</script>  之间，而不是&nbsp; <span class='MathJax_Preview'><img src='https://www.codelast.com/wp-content/plugins/latex/cache/tex_d0b6f29f7312261c6da8700518a6a27e.gif' style='vertical-align: middle; border: none; padding-bottom:2px;' class='tex' alt="( - \infty , + \infty )" /></span><script type='math/tex'>( - \infty , + \infty )</script> 。所以，这时，<span style="color: rgb(0, 0, 255);">逻辑函数</span>就出场了。</div>
<div>
	<span style="color: rgb(255, 255, 255);">文章来源：</span><a href="http://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">http://www.codelast.com/</span></a><br />
	通过<a href="https://zh.wikipedia.org/wiki/%E9%82%8F%E8%BC%AF%E5%87%BD%E6%95%B8" rel="noopener noreferrer" target="_blank"><span style="background-color:#ffa07a;">这个WiKi页面</span></a>你可以知道，其实所谓的<span style="color:#0000ff;">逻辑函数</span>，就是这样的一个函数：<br />
	 <span class='MathJax_Preview'><img src='https://www.codelast.com/wp-content/plugins/latex/cache/tex_88b4cd1dcd65e1f12fe0d827ba791108.gif' style='vertical-align: middle; border: none; padding-bottom:2px;' class='tex' alt="P(t) = \frac{1}{{1 + {e^{ - t}}}}" /></span><script type='math/tex'>P(t) = \frac{1}{{1 + {e^{ - t}}}}</script> <br />
	这个函数是由 Pierre Fran&ccedil;ois Verhulst（皮埃尔&middot;弗朗索瓦&middot;韦吕勒）在1844～1845年的时候给它起的名字。而我们上面的函数(1)，就是这个形式。<br />
	逻辑函数的图像是这个样子的：<br />
	<img decoding="async" alt="logistic function" src="https://www.codelast.com/wp-content/uploads/2016/01/logistic_function.png" style="width: 517px; height: 273px;" /><br />
	它的函数值刚好就是在(0,1)之间。<br />
	所以，我们通过逻辑函数，就可以计算出一个事件的概率了（(0,1)之间）。但是不要忘了，我们前面说要处理二分类问题，得到一个(0,1)之间的任意值并不能归到两个分类中的一个里去，所以还要把这个概率值&ldquo;归类&rdquo;。其实这里很简单，我们可以在&nbsp; <span class='MathJax_Preview'><img src='https://www.codelast.com/wp-content/plugins/latex/cache/tex_4b8d6a6375fd790e922b90da0ecd933d.gif' style='vertical-align: middle; border: none; padding-bottom:2px;' class='tex' alt="f(X) > 0.5" /></span><script type='math/tex'>f(X) > 0.5</script> &nbsp;的时候，把它归到类别1中， <span class='MathJax_Preview'><img src='https://www.codelast.com/wp-content/plugins/latex/cache/tex_224bae9043a536776b0b33751b1b72b0.gif' style='vertical-align: middle; border: none; padding-bottom:2px;' class='tex' alt="f(X) \le 0.5" /></span><script type='math/tex'>f(X) \le 0.5</script> &nbsp;的时候，把它归到类别2中就可以了（概率值的&ldquo;分水岭&rdquo;可以根据实际情况调整）。用数学公式来表达这段话的含义就是：</p>
<div>
		 <span class='MathJax_Preview'><img src='https://www.codelast.com/wp-content/plugins/latex/cache/tex_fd236b87dd9852ea434e65a277b2398c.gif' style='vertical-align: middle; border: none; padding-bottom:2px;' class='tex' alt="y' = \left\{ {\begin{array}{*{20}{c}}{0,f(X) > 0.5}\\{1,f(X) \le 0.5}\end{array}} \right." /></span><script type='math/tex'>y' = \left\{ {\begin{array}{*{20}{c}}{0,f(X) > 0.5}\\{1,f(X) \le 0.5}\end{array}} \right.</script> </div>
<p>
	在各种机器学习的文章中，你都会看到，它们给了逻辑函数一个常用的名字：<span style="color:#0000ff;">Sigmoid函数</span>。sigmoid，意为&ldquo;S形的&rdquo;，这正符合其函数图像特点，所以大家记住就行了。<br />
	<span style="color: rgb(255, 255, 255);">文章来源：</span><a href="http://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">http://www.codelast.com/</span></a><br />
	现在，我们已经有了函数，下一步任务就是求出函数表达式中的未知参数向量  <span class='MathJax_Preview'><img src='https://www.codelast.com/wp-content/plugins/latex/cache/tex_50d91f80cbb8feda1d10e167107ad1ff.gif' style='vertical-align: middle; border: none; padding-bottom:2px;' class='tex' alt="\theta " /></span><script type='math/tex'>\theta </script>  了。这个过程是机器学习中最为核心的计算步骤。<br />
	以前面讲过的函数  <span class='MathJax_Preview'><img src='https://www.codelast.com/wp-content/plugins/latex/cache/tex_0fdb61c8abfb7bfd67516c1f8c8055b2.gif' style='vertical-align: middle; border: none; padding-bottom:2px;' class='tex' alt="y = ax + b" /></span><script type='math/tex'>y = ax + b</script> &nbsp;为例：<br />
	你会发现，当已知几组  <span class='MathJax_Preview'><img src='https://www.codelast.com/wp-content/plugins/latex/cache/tex_90cbc22edf225adf8a68974f51227f05.gif' style='vertical-align: middle; border: none; padding-bottom:2px;' class='tex' alt="(x,y)" /></span><script type='math/tex'>(x,y)</script> &nbsp;数据的情况下：</p>
<blockquote><p>
		(1, 5.5)<br />
		(1.5, 7)<br />
		(2, 6.5)</p></blockquote>
<p>	你无论如何也不可能找到一对  <span class='MathJax_Preview'><img src='https://www.codelast.com/wp-content/plugins/latex/cache/tex_0cc175b9c0f1b6a831c399e269772661.gif' style='vertical-align: middle; border: none; padding-bottom:2px;' class='tex' alt="a" /></span><script type='math/tex'>a</script>  和&nbsp; <span class='MathJax_Preview'><img src='https://www.codelast.com/wp-content/plugins/latex/cache/tex_92eb5ffee6ae2fec3ad71c777531578f.gif' style='vertical-align: middle; border: none; padding-bottom:2px;' class='tex' alt="b" /></span><script type='math/tex'>b</script>  的值，使得以上3组数据能精确地满足方程&nbsp;&nbsp; <span class='MathJax_Preview'><img src='https://www.codelast.com/wp-content/plugins/latex/cache/tex_0fdb61c8abfb7bfd67516c1f8c8055b2.gif' style='vertical-align: middle; border: none; padding-bottom:2px;' class='tex' alt="y = ax + b" /></span><script type='math/tex'>y = ax + b</script> ，正如下面的图像所示：<br />
	<img decoding="async" alt="logistic function" src="https://www.codelast.com/wp-content/uploads/2016/01/ax_b.png" style="width: 1010px; height: 234px;" /></p>
<p>	这条直线如果要精确地通过其中的两个点，那么就不能通过第三个点。所以，最终求出来的&nbsp; <span class='MathJax_Preview'><img src='https://www.codelast.com/wp-content/plugins/latex/cache/tex_0cc175b9c0f1b6a831c399e269772661.gif' style='vertical-align: middle; border: none; padding-bottom:2px;' class='tex' alt="a" /></span><script type='math/tex'>a</script>  和&nbsp; <span class='MathJax_Preview'><img src='https://www.codelast.com/wp-content/plugins/latex/cache/tex_92eb5ffee6ae2fec3ad71c777531578f.gif' style='vertical-align: middle; border: none; padding-bottom:2px;' class='tex' alt="b" /></span><script type='math/tex'>b</script>  的值，并不是方程的<a href="https://zh.wikipedia.org/wiki/%E8%A7%A3%E6%9E%90%E8%A7%A3" rel="noopener noreferrer" target="_blank"><span style="background-color:#ffa07a;">解析解</span></a>，而是&ldquo;<span style="color:#0000ff;">最优解</span>&rdquo;。<br />
	因此，问题在于，我们如何画一条直线，使得其是&ldquo;最优&rdquo;的？&ldquo;最优&rdquo;的评判标准是什么？<br />
	<span style="color: rgb(255, 255, 255);">文章来源：</span><a href="http://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">http://www.codelast.com/</span></a><br />
	为了理解&ldquo;最优&rdquo;，我们需要先了解一些概念。</p>
<ul>
<li>
			<span style="color:#0000ff;">损失函数</span>／<span style="color: rgb(0, 0, 255);">Loss Function</span>／<span style="color: rgb(0, 0, 255);">代价函数</span>／<span style="color: rgb(0, 0, 255);">Cost Function</span></li>
</ul>
</div>
<div>
	很多文章说，这几个名词的含义是一样的。但是也有文章说，Loss Function和Cost Function不是一回事，例如<a href="http://image.diku.dk/shark/sphinx_pages/build/html/rest_sources/tutorials/concepts/library_design/losses.html" rel="noopener noreferrer" target="_blank"><span style="background-color:#ffa07a;">这篇</span></a>文章。但通常认为，这二者是一回事。我觉得嘛，大家就按通常的概念来接受就好了。<br />
	按<a href="https://en.wikipedia.org/wiki/Loss_function" rel="noopener noreferrer" target="_blank"><span style="background-color:#ffa07a;">WiKi</span></a>的定义：</div>
<blockquote>
<div>
		In mathematical optimization, statistics, decision theory and machine learning, a loss function or cost function is a function that maps an event or values of one or more variables onto a real number intuitively representing some &quot;cost&quot; associated with the event. An optimization problem seeks to minimize a loss function.</div>
</blockquote>
<div>
	以及：</div>
<blockquote>
<div>
		The loss function quantifies the amount by which the prediction deviates from the actual values.</div>
</blockquote>
<div>
	我们可以知道，损失函数用于衡量预测值与实际值的偏离程度，如果预测是完全精确的，则损失函数值为0；如果损失函数值不为0，则其表示的是预测的错误有多糟糕。使得损失函数值最小的那些待求参数值，就是&ldquo;最优&rdquo;的参数值。<br />
	<span style="color: rgb(255, 255, 255);">文章来源：</span><a href="http://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">http://www.codelast.com/</span></a><br />
	所以现在问题来了，损失函数的表达式又是什么？<br />
	在探讨损失函数的表达式之前，我们先来看一下损失函数有哪些种类。<br />
	损失函数有很多种，例如下面几个：<br />
	<span style="background-color:#dda0dd;">（1）</span>0-1损失函数：可用于<span style="color:#0000ff;">分类</span>问题，即该函数用于衡量<span style="color:#0000ff;">分类错误</span>的数量，但由于此损失函数是非凸（non-convex）的，因此在做最优化计算时，难以求解，所以，正因为如此，0-1损失函数不是那么&ldquo;实用&rdquo;（如果这句话有误，请指正）。<br />
	<span style="background-color:#dda0dd;">（2）</span>平方损失函数（Square Loss）：常用于线性回归（Linear Regression）。<br />
	<span style="background-color:#dda0dd;">（3）</span>对数损失（Log Loss）函数：常用于其模型输出每一类概率的分类器（classifier），例如逻辑回归。<br />
	<span style="background-color:#dda0dd;">（4）</span>Hinge损失函数：常用于SVM（Support Vector Machine，支持向量机，一种机器学习算法）。中文名叫&ldquo;<span style="color:#0000ff;">合页</span>损失函数&rdquo;，因为hinge有&ldquo;合页&rdquo;之意。这个翻译虽然直白，但是你会发现，99％的文章都不会用它的中文名来称呼它，而是用&ldquo;Hinge损失&rdquo;之类的说法。</p>
<p>	这些都是人们的经验总结，当然，说每一种损失函数常用于什么机器学习算法，也都是有数学依据的。但是在这里，我们讲的是Logistic Regression，所以只看对数损失函数。对数损失函数通常用于衡量分类器（classifier）的精度，这里的&ldquo;分类器&rdquo;也就是指机器学习的模型，它对每一个类别输出一个概率值。从前面的文章中，我们已经知道了，逻辑回归就是这样一种分类器，所以才用对数损失函数来衡量其精度。<br />
	有时候，对数损失函数（Log Loss）也被叫作<span style="color:#0000ff;">交叉熵</span>损失函数（Cross-entropy Loss）。<a href="https://en.wikipedia.org/wiki/Cross_entropy" rel="noopener noreferrer" target="_blank"><span style="color:#0000ff;">交叉熵</span></a>这个名字比较拗口，在信息理论中，<span style="color:#0000ff;">熵</span>用于衡量某种事件的&ldquo;不可预测性&rdquo;，而<span style="color:#0000ff;">交叉熵</span>=事件的真实分布+不可预测性，所以交叉熵可以用于度量两个概率分布（真实分布&amp;预测分布）之间的差异性，即：交叉熵损失函数（对数损失函数）可以衡量一个模型对真实值带来的额外噪音，通过最小化交叉熵损失函数（对数损失函数），我们就可以最大化分类器（模型）的精度。<br />
	上面这一大段话试图用简单的描述让你相信，为什么要用Log Loss来衡量Logistic Regression的误差，但是没有给出证明。有人可能会说，为什么不能用其他的方法来衡量，例如用平方损失函数（Square Loss）。事实上，这是有数学依据的&mdash;&mdash;它会导致损失函数是一个关于参数向量 <span class='MathJax_Preview'><img src='https://www.codelast.com/wp-content/plugins/latex/cache/tex_50d91f80cbb8feda1d10e167107ad1ff.gif' style='vertical-align: middle; border: none; padding-bottom:2px;' class='tex' alt="\theta " /></span><script type='math/tex'>\theta </script> 的<span style="color:#0000ff;">非</span><a href="https://zh.wikipedia.org/wiki/%E5%87%B8%E5%87%BD%E6%95%B0" rel="noopener noreferrer" target="_blank"><span style="background-color:#ffa07a;">凸函数</span></a>，而用对数损失函数就没有这种问题。凸函数的性质为我们后面求解参数向量 <span class='MathJax_Preview'><img src='https://www.codelast.com/wp-content/plugins/latex/cache/tex_50d91f80cbb8feda1d10e167107ad1ff.gif' style='vertical-align: middle; border: none; padding-bottom:2px;' class='tex' alt="\theta " /></span><script type='math/tex'>\theta </script> 提供了极大便利，非凸函数有很多局部最优解，不利于求解 <span class='MathJax_Preview'><img src='https://www.codelast.com/wp-content/plugins/latex/cache/tex_50d91f80cbb8feda1d10e167107ad1ff.gif' style='vertical-align: middle; border: none; padding-bottom:2px;' class='tex' alt="\theta " /></span><script type='math/tex'>\theta </script> 的计算过程。<br />
	<span style="color: rgb(255, 255, 255);">文章来源：</span><a href="http://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">http://www.codelast.com/</span></a><br />
	到这里为止，我们还是没有提到损失函数的数学表达式，但是如果要计算损失函数的值，我们是回避不了的，必须要知道。所以，这里用 L 来表示损失函数（取<span style="color: rgb(255, 0, 0);">L</span>oss之意），则对数损失函数的表达式为：<br />
	<span style="color:#0000ff;"> <span class='MathJax_Preview'><img src='https://www.codelast.com/wp-content/plugins/latex/cache/tex_276a829ddba9850126edcea1af119036.gif' style='vertical-align: middle; border: none; padding-bottom:2px;' class='tex' alt="L = - \frac{1}{n}\sum\limits_{i = 1}^n {[{y_i}\log ({{\hat y}_i}) + (1 - {y_i})\log (1 - {{\hat y}_i})]} " /></span><script type='math/tex'>L = - \frac{1}{n}\sum\limits_{i = 1}^n {[{y_i}\log ({{\hat y}_i}) + (1 - {y_i})\log (1 - {{\hat y}_i})]} </script> </span> <span style="color:#ff0000;">......（2）</span><br />
	其中， <span class='MathJax_Preview'><img src='https://www.codelast.com/wp-content/plugins/latex/cache/tex_eb80f83e86467cf361778c55e850e5e0.gif' style='vertical-align: middle; border: none; padding-bottom:2px;' class='tex' alt="{{y_i}}" /></span><script type='math/tex'>{{y_i}}</script> 是第i个真实值（ <span class='MathJax_Preview'><img src='https://www.codelast.com/wp-content/plugins/latex/cache/tex_64903c82c12bd5f8d201e975ac2c3101.gif' style='vertical-align: middle; border: none; padding-bottom:2px;' class='tex' alt="{y_i} \in \{ 0,1\} " /></span><script type='math/tex'>{y_i} \in \{ 0,1\} </script> ）， <span class='MathJax_Preview'><img src='https://www.codelast.com/wp-content/plugins/latex/cache/tex_e466a7d704ae3cf2e5b7414bbae760ba.gif' style='vertical-align: middle; border: none; padding-bottom:2px;' class='tex' alt="{{{\hat y}_i}}" /></span><script type='math/tex'>{{{\hat y}_i}}</script> 是第i个预测值。<br />
	这个对数损失函数的表达式中并没有出现我们要求解的参数&nbsp; <span class='MathJax_Preview'><img src='https://www.codelast.com/wp-content/plugins/latex/cache/tex_50d91f80cbb8feda1d10e167107ad1ff.gif' style='vertical-align: middle; border: none; padding-bottom:2px;' class='tex' alt="\theta " /></span><script type='math/tex'>\theta </script> ，所以我们把&nbsp; <span class='MathJax_Preview'><img src='https://www.codelast.com/wp-content/plugins/latex/cache/tex_683296a9a6612653594c598476e669a8.gif' style='vertical-align: middle; border: none; padding-bottom:2px;' class='tex' alt="\hat y = f(X) = \frac{1}{{1 + {e^{ - {\theta ^T}X}}}}" /></span><script type='math/tex'>\hat y = f(X) = \frac{1}{{1 + {e^{ - {\theta ^T}X}}}}</script>  代到（2）式中去：<br />
	 <span class='MathJax_Preview'><img src='https://www.codelast.com/wp-content/plugins/latex/cache/tex_961181b6fd923c712b21a4cb9334697a.gif' style='vertical-align: middle; border: none; padding-bottom:2px;' class='tex' alt="L = - \frac{1}{N}\sum\limits_{i = 1}^n {\left[ {{y_i}\log \left( {\frac{1}{{1 + {e^{ - {\theta ^T}{X_i}}}}}} \right) + (1 - {y_i})\log \left( {1 - \frac{1}{{1 + {e^{ - {\theta ^T}{X_i}}}}}} \right)} \right]} " /></span><script type='math/tex'>L = - \frac{1}{N}\sum\limits_{i = 1}^n {\left[ {{y_i}\log \left( {\frac{1}{{1 + {e^{ - {\theta ^T}{X_i}}}}}} \right) + (1 - {y_i})\log \left( {1 - \frac{1}{{1 + {e^{ - {\theta ^T}{X_i}}}}}} \right)} \right]} </script> <br />
	再来仔细看一下这个式子：N 为数据集的条数（有多少组 <span class='MathJax_Preview'><img src='https://www.codelast.com/wp-content/plugins/latex/cache/tex_0340c3897ed6af331d4d59a20946c451.gif' style='vertical-align: middle; border: none; padding-bottom:2px;' class='tex' alt="(X,y)" /></span><script type='math/tex'>(X,y)</script> ，N就是多少），已知； <span class='MathJax_Preview'><img src='https://www.codelast.com/wp-content/plugins/latex/cache/tex_eb80f83e86467cf361778c55e850e5e0.gif' style='vertical-align: middle; border: none; padding-bottom:2px;' class='tex' alt="{{y_i}}" /></span><script type='math/tex'>{{y_i}}</script>  是真实值，已知； <span class='MathJax_Preview'><img src='https://www.codelast.com/wp-content/plugins/latex/cache/tex_2af46b114c169c94ea9a86ca7b36758b.gif' style='vertical-align: middle; border: none; padding-bottom:2px;' class='tex' alt="{{X_i}}" /></span><script type='math/tex'>{{X_i}}</script> 是输入的向量，也已知。所以整个式子里只有 <span class='MathJax_Preview'><img src='https://www.codelast.com/wp-content/plugins/latex/cache/tex_50d91f80cbb8feda1d10e167107ad1ff.gif' style='vertical-align: middle; border: none; padding-bottom:2px;' class='tex' alt="\theta " /></span><script type='math/tex'>\theta </script> 是未知的，可以记为 <span class='MathJax_Preview'><img src='https://www.codelast.com/wp-content/plugins/latex/cache/tex_bd346812a06f2d43be738a6f13cc17ee.gif' style='vertical-align: middle; border: none; padding-bottom:2px;' class='tex' alt="L(\theta )" /></span><script type='math/tex'>L(\theta )</script> ，称之为<span style="color:#0000ff;">目标函数</span>：<br />
	 <span class='MathJax_Preview'><img src='https://www.codelast.com/wp-content/plugins/latex/cache/tex_ea3fb6a016e9c65dd4bae8aeb9ee1f68.gif' style='vertical-align: middle; border: none; padding-bottom:2px;' class='tex' alt="L(\theta ) = - \frac{1}{N}\sum\limits_{i = 1}^n {\left[ {{y_i}\log \left( {\frac{1}{{1 + {e^{ - {\theta ^T}{X_i}}}}}} \right) + (1 - {y_i})\log \left( {1 - \frac{1}{{1 + {e^{ - {\theta ^T}{X_i}}}}}} \right)} \right]} " /></span><script type='math/tex'>L(\theta ) = - \frac{1}{N}\sum\limits_{i = 1}^n {\left[ {{y_i}\log \left( {\frac{1}{{1 + {e^{ - {\theta ^T}{X_i}}}}}} \right) + (1 - {y_i})\log \left( {1 - \frac{1}{{1 + {e^{ - {\theta ^T}{X_i}}}}}} \right)} \right]} </script> <br />
	因此，我们只要找到一个参数向量 <span class='MathJax_Preview'><img src='https://www.codelast.com/wp-content/plugins/latex/cache/tex_50d91f80cbb8feda1d10e167107ad1ff.gif' style='vertical-align: middle; border: none; padding-bottom:2px;' class='tex' alt="\theta " /></span><script type='math/tex'>\theta </script> ，能使得此式的值最小，那么这个参数向量 <span class='MathJax_Preview'><img src='https://www.codelast.com/wp-content/plugins/latex/cache/tex_50d91f80cbb8feda1d10e167107ad1ff.gif' style='vertical-align: middle; border: none; padding-bottom:2px;' class='tex' alt="\theta " /></span><script type='math/tex'>\theta </script> 就是&ldquo;最优&rdquo;的参数向量。<br />
	求得了这个最优的 <span class='MathJax_Preview'><img src='https://www.codelast.com/wp-content/plugins/latex/cache/tex_50d91f80cbb8feda1d10e167107ad1ff.gif' style='vertical-align: middle; border: none; padding-bottom:2px;' class='tex' alt="\theta " /></span><script type='math/tex'>\theta </script> 之后，把它代入式（1），则对任一个未知的 <span class='MathJax_Preview'><img src='https://www.codelast.com/wp-content/plugins/latex/cache/tex_02129bb861061d1a052c592e2dc6b383.gif' style='vertical-align: middle; border: none; padding-bottom:2px;' class='tex' alt="X" /></span><script type='math/tex'>X</script> ，我们都可以计算出 <span class='MathJax_Preview'><img src='https://www.codelast.com/wp-content/plugins/latex/cache/tex_41c2f2136110516f7d332adc5041b0fe.gif' style='vertical-align: middle; border: none; padding-bottom:2px;' class='tex' alt="f(X)" /></span><script type='math/tex'>f(X)</script> 值，然后再根据一个阈值把它调整到 0 或 1，就得到了这个 <span class='MathJax_Preview'><img src='https://www.codelast.com/wp-content/plugins/latex/cache/tex_02129bb861061d1a052c592e2dc6b383.gif' style='vertical-align: middle; border: none; padding-bottom:2px;' class='tex' alt="X" /></span><script type='math/tex'>X</script> 所属的分类，这样，我们就完成了一次&ldquo;预测&rdquo;的过程。<br />
	<span style="color: rgb(255, 255, 255);">文章来源：</span><a href="http://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">http://www.codelast.com/</span></a></div>
<ul>
<li>
		求解方法</li>
</ul>
<div>
	所以现在问题来了，这个&ldquo;最优&rdquo;的参数向量 <span class='MathJax_Preview'><img src='https://www.codelast.com/wp-content/plugins/latex/cache/tex_50d91f80cbb8feda1d10e167107ad1ff.gif' style='vertical-align: middle; border: none; padding-bottom:2px;' class='tex' alt="\theta " /></span><script type='math/tex'>\theta </script> 怎么求解？<br />
	在大的方向上，你可以选择不使用搜索方向的算法（例如<a href="http://www.codelast.com/?p=7488" rel="noopener noreferrer" target="_blank"><span style="background-color:#ffa07a;">信赖域算法</span></a>），也可以选择众多使用搜索方向的算法（例如<a href="http://www.codelast.com/?p=8006" rel="noopener noreferrer" target="_blank"><span style="background-color:#ffa07a;">梯度下降法</span></a>）。<br />
	在是否计算<span style="color:#0000ff;">目标函数</span>的导数这个方面，你可以使用不用求目标函数导数的算法（例如<a href="http://www.codelast.com/?p=388" rel="noopener noreferrer" target="_blank"><span style="background-color:#ffa07a;">Powell共轭方向集方法</span></a>），也可以使用要求目标函数导数的算法（例如<span style="background-color: rgb(255, 160, 122);"><a href="http://www.codelast.com/?p=8006" rel="noopener noreferrer" target="_blank">梯度下降法</a></span>）。由于某些目标函数形式特别复杂，计算其导数特别麻烦，所以在这种时候，不用计算导数的算法可能大有帮助。</div>
<div>
	求解的过程就是一个<a href="http://www.codelast.com/?p=7364" rel="noopener noreferrer" target="_blank"><span style="background-color:#ffa07a;">最优化</span></a>的过程，本文无法用一两句话描述清楚，请大家移步链接进行阅读。</p>
<p>	事实上，在现在各种机器学习library百花齐放的今天，我们基本上不需要自己编写这些算法的具体实现，只需要调用它们即可。例如，通过Spark的Machine Learning Library (MLlib)，我们可以直接使用Stochastic gradient descent (SGD)，Limited-memory <a href="http://www.codelast.com/?p=2780" rel="noopener noreferrer" target="_blank"><span style="background-color:#ffa07a;">BFGS</span></a> (L-BFGS)等实现。但是对这背后的原理有所了解，对工作学习是有帮助的。<br />
	<span style="color: rgb(255, 255, 255);">文章来源：</span><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><span style="color: rgb(255, 255, 255);">https://www.codelast.com/</span></a><br />
	<span style="color: rgb(255, 0, 0);">➤➤</span>&nbsp;版权声明&nbsp;<span style="color: rgb(255, 0, 0);">➤➤</span>&nbsp;<br />
	转载需注明出处：<u><a href="https://www.codelast.com/" rel="noopener noreferrer" target="_blank"><em><span style="color: rgb(0, 0, 255);"><strong style="font-size: 16px;"><span style="font-family: arial, helvetica, sans-serif;">codelast.com</span></strong></span></em></a></u>&nbsp;<br />
	感谢关注我的微信公众号（微信扫一扫）：</p>
<p style="border: 0px; font-size: 13px; margin: 0px 0px 9px; outline: 0px; padding: 0px; color: rgb(77, 77, 77);">
		<img decoding="async" alt="wechat qrcode of codelast" src="https://www.codelast.com/codelast_wechat_qr_code.jpg" style="width: 200px; height: 200px;" /></p>
</div>
]]></content:encoded>
					
					<wfw:commentRss>https://www.codelast.com/%e5%8e%9f%e5%88%9b-%e7%94%a8%e4%ba%ba%e8%af%9d%e8%a7%a3%e9%87%8a%e6%9c%ba%e5%99%a8%e5%ad%a6%e4%b9%a0%e4%b8%ad%e7%9a%84logistic-regression%ef%bc%88%e9%80%bb%e8%be%91%e5%9b%9e%e5%bd%92%ef%bc%89/feed/</wfw:commentRss>
			<slash:comments>19</slash:comments>
		
		
			</item>
	</channel>
</rss>
