<?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>TensorFlow &#8211; 编码无悔 /  Intent &amp; Focused</title>
	<atom:link href="https://www.codelast.com/tag/tensorflow/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.codelast.com</link>
	<description>最优化之路</description>
	<lastBuildDate>Wed, 24 May 2023 09:34:13 +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>[原创] 怎样确认当前正在运行的TensorFlow model-serving服务加载的是哪个.pb模型</title>
		<link>https://www.codelast.com/%e6%80%8e%e6%a0%b7%e7%a1%ae%e8%ae%a4%e5%bd%93%e5%89%8d%e6%ad%a3%e5%9c%a8%e8%bf%90%e8%a1%8c%e7%9a%84tensorflow-model-serving%e6%9c%8d%e5%8a%a1%e5%8a%a0%e8%bd%bd%e7%9a%84%e6%98%af%e5%93%aa%e4%b8%aa-pb/</link>
					<comments>https://www.codelast.com/%e6%80%8e%e6%a0%b7%e7%a1%ae%e8%ae%a4%e5%bd%93%e5%89%8d%e6%ad%a3%e5%9c%a8%e8%bf%90%e8%a1%8c%e7%9a%84tensorflow-model-serving%e6%9c%8d%e5%8a%a1%e5%8a%a0%e8%bd%bd%e7%9a%84%e6%98%af%e5%93%aa%e4%b8%aa-pb/#respond</comments>
		
		<dc:creator><![CDATA[learnhard]]></dc:creator>
		<pubDate>Wed, 24 May 2023 09:33:49 +0000</pubDate>
				<category><![CDATA[AI]]></category>
		<category><![CDATA[原创]]></category>
		<category><![CDATA[model-serving]]></category>
		<category><![CDATA[pb模型]]></category>
		<category><![CDATA[TensorFlow]]></category>
		<guid isPermaLink="false">https://www.codelast.com/?p=13912</guid>

					<description><![CDATA[<p>跑起来一个TensorFlow model-serving服务后，有时候记不清它加载的是哪个.pb模型了，可以采用下面的办法来确认。<br />
<span id="more-13912"></span><br />
访问URL：<br />
http://&#60;your_model_serving_host&#62;:18501/v1/models/&#60;your_model_name&#62;<br />
其中：<br />
&#60;your_model_serving_host&#62; 是你的model-serving服务器的域名或IP。<br />
&#60;your_model_name&#62; 是你的模型名称。<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 />
会看到页面输出类似于下面的内容：</p>
<blockquote>
<div>
		{</div>
<div>
		&#160;&#34;model_version_status&#34;: [</div>
<div>
		&#160; {</div>
<div>
		&#160; &#160;&#34;version&#34;: &#34;1684833957&#34;,</div>
<div>
		&#160; &#160;&#34;state&#34;: &#34;AVAILABLE&#34;,</div>
<div>
		&#160; &#160;&#34;status&#34;: {</div>
<div>
		&#160; &#160; &#34;error_code&#34;: &#34;OK&#34;,</div>
<div>
		&#160; &#160; &#34;error_message&#34;: &#34;&#34;</div>
<div>
		&#160; &#160;}</div>
<div>
		&#160; }</div>
<div>
		&#160;]</div>
<div>
		}</div>
</blockquote>
<p>其中，version就是我们要找的东西。<br />
到你保存.pb模型的父目录下（可能是HDFS或本地磁盘），无脑搜version对应的关键字&#8230; <a href="https://www.codelast.com/%e6%80%8e%e6%a0%b7%e7%a1%ae%e8%ae%a4%e5%bd%93%e5%89%8d%e6%ad%a3%e5%9c%a8%e8%bf%90%e8%a1%8c%e7%9a%84tensorflow-model-serving%e6%9c%8d%e5%8a%a1%e5%8a%a0%e8%bd%bd%e7%9a%84%e6%98%af%e5%93%aa%e4%b8%aa-pb/" class="read-more">Read More </a></p>]]></description>
										<content:encoded><![CDATA[<p>跑起来一个TensorFlow model-serving服务后，有时候记不清它加载的是哪个.pb模型了，可以采用下面的办法来确认。<br />
<span id="more-13912"></span><br />
访问URL：<br />
http://&lt;your_model_serving_host&gt;:18501/v1/models/&lt;your_model_name&gt;<br />
其中：<br />
&lt;your_model_serving_host&gt; 是你的model-serving服务器的域名或IP。<br />
&lt;your_model_name&gt; 是你的模型名称。<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 />
会看到页面输出类似于下面的内容：</p>
<blockquote>
<div>
		{</div>
<div>
		&nbsp;&quot;model_version_status&quot;: [</div>
<div>
		&nbsp; {</div>
<div>
		&nbsp; &nbsp;&quot;version&quot;: &quot;1684833957&quot;,</div>
<div>
		&nbsp; &nbsp;&quot;state&quot;: &quot;AVAILABLE&quot;,</div>
<div>
		&nbsp; &nbsp;&quot;status&quot;: {</div>
<div>
		&nbsp; &nbsp; &quot;error_code&quot;: &quot;OK&quot;,</div>
<div>
		&nbsp; &nbsp; &quot;error_message&quot;: &quot;&quot;</div>
<div>
		&nbsp; &nbsp;}</div>
<div>
		&nbsp; }</div>
<div>
		&nbsp;]</div>
<div>
		}</div>
</blockquote>
<p>其中，version就是我们要找的东西。<br />
到你保存.pb模型的父目录下（可能是HDFS或本地磁盘），无脑搜version对应的关键字 1684833957，找到哪个目录，就是我们要找的.pb模型所在的目录。<br />
通常这个目录下会有一个&nbsp;saved_model.pb 文件，以及一个&nbsp;variables 子目录。<br />
为什么可以这样做？因为version里的时间戳就是导出 .pb 模型的时间戳，这个时间戳精确到秒，一般情况下，两个模型几乎不太可能在同一秒生成，所以这个时间戳是唯一的，因此只要能找到这个目录名，那么目录里的 .pb 模型几乎肯定是我们要找的模型。</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 />
	感谢关注我的微信公众号（微信扫一扫）：<br />
	<img decoding="async" alt="wechat qrcode of codelast" src="https://www.codelast.com/codelast_wechat_qr_code.jpg" style="color: rgb(77, 77, 77); font-size: 13px; width: 200px; height: 200px;" /><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="" src="https://www.codelast.com/wechat_shipinhao_qr_code.jpg" style="text-align: center; width: 200px; height: 199px;" /></p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.codelast.com/%e6%80%8e%e6%a0%b7%e7%a1%ae%e8%ae%a4%e5%bd%93%e5%89%8d%e6%ad%a3%e5%9c%a8%e8%bf%90%e8%a1%8c%e7%9a%84tensorflow-model-serving%e6%9c%8d%e5%8a%a1%e5%8a%a0%e8%bd%bd%e7%9a%84%e6%98%af%e5%93%aa%e4%b8%aa-pb/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>[原创] 用JAVA读取本地的TFRecord文件</title>
		<link>https://www.codelast.com/%e5%8e%9f%e5%88%9b-%e7%94%a8java%e8%af%bb%e5%8f%96%e6%9c%ac%e5%9c%b0%e7%9a%84tfrecord%e6%96%87%e4%bb%b6/</link>
					<comments>https://www.codelast.com/%e5%8e%9f%e5%88%9b-%e7%94%a8java%e8%af%bb%e5%8f%96%e6%9c%ac%e5%9c%b0%e7%9a%84tfrecord%e6%96%87%e4%bb%b6/#respond</comments>
		
		<dc:creator><![CDATA[learnhard]]></dc:creator>
		<pubDate>Mon, 24 Apr 2023 18:09:06 +0000</pubDate>
				<category><![CDATA[原创]]></category>
		<category><![CDATA[综合]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[TensorFlow]]></category>
		<category><![CDATA[TFRecord]]></category>
		<category><![CDATA[本地]]></category>
		<guid isPermaLink="false">https://www.codelast.com/?p=13895</guid>

					<description><![CDATA[<div>
	TFRecord是一种用于TensorFlow的二进制数据格式，它可以更高效地存储和读取大规模数据集。TFRecord文件包含了一系列记录（record），每个记录可以是一个张量（tensor）或者一个序列（sequence）。</div>
<div>
	与文本文件不同，TFRecord文件被编码成二进制格式，这使得它们更易于在网络上传输和存储。同时，TFRecord也允许我们将大型数据集分割成多个部分，并且可以有效地并行读取和处理这些部分。</div>
<div>
	在TensorFlow中，我们通常使用TFRecord文件来存储和加载模型的训练数据、验证数据、测试数据等。创建TFRecord文件需要经过一定的序列化操作，但这些操作很容易实现，因为TensorFlow提供了相应的API支持。</div>
<p><span id="more-13895"></span><br />
在大数据处理流程中，TFRecord文件通常是由map-reduce&#160;job生成的，数据量通常很大。有时为了验证文件内容正确，我们需要取少量数据来检查，例如，我们可以拿map-reduce job生成的N个TFRecord文件中的一个，在本地解析出来，打印出其中的内容看是否正确。<br />
下面就是一个用JAVA程序读取TFRecord文件并打印出其中一个Example的例子：</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: &#34;Helvetica Neue&#34;, Helvetica, &#34;Hiragino Sans GB&#34;, &#34;Microsoft YaHei&#34;, Arial, sans-serif;">
<pre style="font-size: inherit; color: inherit; line-height: inherit; margin-top: 0px; margin-bottom: 0px; padding: 0px;">
<code class="javascript language-javascript 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;">&#160;&#160;&#160;&#160;<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;">String</span>&#160;localTfRecordFile&#160;=&#160;<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&#34;/path/to/your/tfrecord/file&#34;</span>;
&#160;&#160;&#160;&#160;InputStream&#160;inputStream&#160;=&#160;Files.newInputStream(Paths.get(localTfRecordFile));
&#160;&#160;&#160;&#160;DataInput&#160;dataInput&#160;=&#160;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">new</span>&#160;DataInputStream(inputStream);
&#160;&#160;&#160;&#160;TFRecordReader&#160;reader&#160;=&#160;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">new</span>&#160;TFRecordReader(dataInput,&#160;<span class="hljs-literal" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(174, 135, 250); word-wrap: inherit !important; word-break: inherit !important;">true</span>);

&#160;&#160;&#160;&#160;byte[]&#160;recordBytes&#160;=&#160;reader.read();
&#160;&#160;&#160;&#160;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">while</span>&#160;(recordBytes&#160;!=&#160;<span class="hljs-literal" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(174, 135, 250); word-wrap: inherit !important; word-break: inherit !important;">null</span>)&#160;{
&#160;&#160;&#160;&#160;&#160;&#160;Example&#160;example&#160;=&#160;Example.parseFrom(recordBytes);
&#160;&#160;&#160;&#160;&#160;&#160;System.out.println(example.toString());
&#160;&#160;&#160;&#160;&#160;&#160;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">break</span>;&#160;&#160;<span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(128, 128, 128); word-wrap: inherit !important; word-break: inherit !important;">//&#160;只打印一个Example</span>
&#160;&#160;&#160;&#160;}
&#160;&#160;&#160;&#160;inputStream.close();
</code></pre>
</section>
<p>唯一需要注意的就是一个引入：import java.nio.file.Paths;<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 />
再详细说明一下：</p>
<div>
	TFRecord文件和Example是TensorFlow中用于数据序列化和存储的两个概念，它们之间有着紧密的关系。</div>
<div>
	TFRecord是一种二进制格式的文件，在TensorFlow中被用来高效地存储大量的数据。它通常是由多个Example组成的序列化数据。而Example则是TensorFlow中序列化数据的标准格式，可以包含多个Features，每个Feature又包含一个Tensor（可以是张量、字符串等）。在将数据写入TFRecord文件时，需要将其封装为Example格式；在读取TFRecord文件时，也需要将其中的每个Example解析出来。</div>
<div>
	简而言之，TFRecord文件就像是一个容器，而Example则是这个容器里面每个元素的具体格式。在使用TFRecord时，我们通常会先定义好我们要存储哪些数据以及这些数据应该怎么被划分为不同的Features，并封装成一个或多个Example，在把这些Example写入到TFRecord文件中。
<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></p></div>&#8230; <a href="https://www.codelast.com/%e5%8e%9f%e5%88%9b-%e7%94%a8java%e8%af%bb%e5%8f%96%e6%9c%ac%e5%9c%b0%e7%9a%84tfrecord%e6%96%87%e4%bb%b6/" class="read-more">Read More </a>]]></description>
										<content:encoded><![CDATA[<div>
	TFRecord是一种用于TensorFlow的二进制数据格式，它可以更高效地存储和读取大规模数据集。TFRecord文件包含了一系列记录（record），每个记录可以是一个张量（tensor）或者一个序列（sequence）。</div>
<div>
	与文本文件不同，TFRecord文件被编码成二进制格式，这使得它们更易于在网络上传输和存储。同时，TFRecord也允许我们将大型数据集分割成多个部分，并且可以有效地并行读取和处理这些部分。</div>
<div>
	在TensorFlow中，我们通常使用TFRecord文件来存储和加载模型的训练数据、验证数据、测试数据等。创建TFRecord文件需要经过一定的序列化操作，但这些操作很容易实现，因为TensorFlow提供了相应的API支持。</div>
<p><span id="more-13895"></span><br />
在大数据处理流程中，TFRecord文件通常是由map-reduce&nbsp;job生成的，数据量通常很大。有时为了验证文件内容正确，我们需要取少量数据来检查，例如，我们可以拿map-reduce job生成的N个TFRecord文件中的一个，在本地解析出来，打印出其中的内容看是否正确。<br />
下面就是一个用JAVA程序读取TFRecord文件并打印出其中一个Example的例子：</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="javascript language-javascript 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;">&nbsp;&nbsp;&nbsp;&nbsp;<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;">String</span>&nbsp;localTfRecordFile&nbsp;=&nbsp;<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;/path/to/your/tfrecord/file&quot;</span>;
&nbsp;&nbsp;&nbsp;&nbsp;InputStream&nbsp;inputStream&nbsp;=&nbsp;Files.newInputStream(Paths.get(localTfRecordFile));
&nbsp;&nbsp;&nbsp;&nbsp;DataInput&nbsp;dataInput&nbsp;=&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">new</span>&nbsp;DataInputStream(inputStream);
&nbsp;&nbsp;&nbsp;&nbsp;TFRecordReader&nbsp;reader&nbsp;=&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">new</span>&nbsp;TFRecordReader(dataInput,&nbsp;<span class="hljs-literal" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(174, 135, 250); word-wrap: inherit !important; word-break: inherit !important;">true</span>);

&nbsp;&nbsp;&nbsp;&nbsp;byte[]&nbsp;recordBytes&nbsp;=&nbsp;reader.read();
&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">while</span>&nbsp;(recordBytes&nbsp;!=&nbsp;<span class="hljs-literal" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(174, 135, 250); word-wrap: inherit !important; word-break: inherit !important;">null</span>)&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Example&nbsp;example&nbsp;=&nbsp;Example.parseFrom(recordBytes);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(example.toString());
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">break</span>;&nbsp;&nbsp;<span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(128, 128, 128); word-wrap: inherit !important; word-break: inherit !important;">//&nbsp;只打印一个Example</span>
&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;inputStream.close();
</code></pre>
</section>
<p>唯一需要注意的就是一个引入：import java.nio.file.Paths;<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 />
再详细说明一下：</p>
<div>
	TFRecord文件和Example是TensorFlow中用于数据序列化和存储的两个概念，它们之间有着紧密的关系。</div>
<div>
	TFRecord是一种二进制格式的文件，在TensorFlow中被用来高效地存储大量的数据。它通常是由多个Example组成的序列化数据。而Example则是TensorFlow中序列化数据的标准格式，可以包含多个Features，每个Feature又包含一个Tensor（可以是张量、字符串等）。在将数据写入TFRecord文件时，需要将其封装为Example格式；在读取TFRecord文件时，也需要将其中的每个Example解析出来。</div>
<div>
	简而言之，TFRecord文件就像是一个容器，而Example则是这个容器里面每个元素的具体格式。在使用TFRecord时，我们通常会先定义好我们要存储哪些数据以及这些数据应该怎么被划分为不同的Features，并封装成一个或多个Example，在把这些Example写入到TFRecord文件中。</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 />
		感谢关注我的微信公众号（微信扫一扫）：<br />
		<img decoding="async" alt="wechat qrcode of codelast" src="https://www.codelast.com/codelast_wechat_qr_code.jpg" style="color: rgb(77, 77, 77); font-size: 13px; width: 200px; height: 200px;" /><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="" src="https://www.codelast.com/wechat_shipinhao_qr_code.jpg" style="text-align: center; width: 200px; height: 199px;" /></p>
</div>
]]></content:encoded>
					
					<wfw:commentRss>https://www.codelast.com/%e5%8e%9f%e5%88%9b-%e7%94%a8java%e8%af%bb%e5%8f%96%e6%9c%ac%e5%9c%b0%e7%9a%84tfrecord%e6%96%87%e4%bb%b6/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>[原创] 伯克利人工智能研究所的开源[强化学习框架] rlpyt，让人眼前一亮</title>
		<link>https://www.codelast.com/%e5%8e%9f%e5%88%9b-%e4%bc%af%e5%85%8b%e5%88%a9%e4%ba%ba%e5%b7%a5%e6%99%ba%e8%83%bd%e7%a0%94%e7%a9%b6%e6%89%80%e7%9a%84%e5%bc%80%e6%ba%90%e5%bc%ba%e5%8c%96%e5%ad%a6%e4%b9%a0%e6%a1%86%e6%9e%b6-rlpyt/</link>
					<comments>https://www.codelast.com/%e5%8e%9f%e5%88%9b-%e4%bc%af%e5%85%8b%e5%88%a9%e4%ba%ba%e5%b7%a5%e6%99%ba%e8%83%bd%e7%a0%94%e7%a9%b6%e6%89%80%e7%9a%84%e5%bc%80%e6%ba%90%e5%bc%ba%e5%8c%96%e5%ad%a6%e4%b9%a0%e6%a1%86%e6%9e%b6-rlpyt/#respond</comments>
		
		<dc:creator><![CDATA[learnhard]]></dc:creator>
		<pubDate>Sun, 27 Oct 2019 03:38:19 +0000</pubDate>
				<category><![CDATA[Algorithm]]></category>
		<category><![CDATA[原创]]></category>
		<category><![CDATA[Reinforcement Learning]]></category>
		<category><![CDATA[rlpyt]]></category>
		<category><![CDATA[TensorFlow]]></category>
		<category><![CDATA[强化学习]]></category>
		<guid isPermaLink="false">https://www.codelast.com/?p=10643</guid>

					<description><![CDATA[<p>
<span style="color:#0000ff;">BAIR</span>（Berkeley Artificial Intelligence Research，伯克利人工智能研究所），开源了一个强化学习(<span style="color:#ff0000;">RL</span>)框架 <a href="https://github.com/astooke/rlpyt" rel="noopener noreferrer" target="_blank"><span style="background-color:#ffa07a;">rlpyt</span></a>，并于2019.09.24在其主页上对它进行了很长篇幅的<a href="https://bair.berkeley.edu/blog/2019/09/24/rlpyt/" rel="noopener noreferrer" target="_blank"><span style="background-color:#ffa07a;">介绍</span></a>（论文在<a href="https://arxiv.org/abs/1909.01500" rel="noopener noreferrer" target="_blank"><span style="background-color:#ffa07a;">这里</span></a>）。<br />
市面上开源强化学习框架已经很多了，这个框架是否值得你上车？我认为，未来怎样不好说，但至少现在(2019.10)看来是值得入手的，因为它确实有其他框架不具备/不完善的功能&#8212;&#8212;最主要的就是对<span style="color:#0000ff;">并行</span>（parallelism）的良好支持。<br />
在强化学习领域，agent与environment互动来收集training data的过程是最耗时的，如果能并行地用多个agent与多个environment互动来收集数据，那么速度可以极大提升。类似于<a href="https://github.com/google/dopamine" rel="noopener noreferrer" target="_blank"><span style="background-color:#ffa07a;">Google&#160;Dopamine</span></a>这样的RL框架，根本没有把&#160;parallelism 作为设计理念的一部分，所以如果你入了Dopamine的坑，等你对模型训练速度有要求的时候再想着换框架，成本就高多了。<br />
<span id="more-10643"></span><br />
<span style="background-color: rgb(0, 255, 0);">▶</span>&#160;rlpyt 这个框架的主要特色是什么？<br />
这里没打算把BAIR对 rlypt&#160;的介绍它全部重新写一遍，只提一些我认为重要的特点：</p>
<ul>
<li>
		适用于小型、中型的强化学习实验。什么叫中型？举个&#8220;大型&#8221;的例子：像OpenAI的Dota那种使用上百个GPU的强化学习实验叫&#8220;大型&#8221;。对一般的RL应用来说，这个适用范围绰绰有余了。</li>
<li>
		可以用串行方式运行，也可以完全并行的方式运行采样器（采样就是指agent与environment交互获取training data的过程），并且支持用CPU或GPU训练，也支持用单GPU或多GPU来优化（优化指的是强化学习算法里更新网络参数的操作）。</li>
<li>
		实现了各种主流RL算法，例如A2C，PPO，DQN，Double DQN，Dueling DQN，Rainbow，DDPG等（这里不一一列出），可以说是比较全面了。</li>
</ul>
<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="background-color: rgb(0, 255, 0);">▶</span>&#160;你也需要知道的一些事实</p>
<ul>
<li>
		<span style="color:#b22222;">rlpyt 是基于PyTorch开发的</span>，它利用了PyTorch的很多特性。如果你想用TensorFlow，那只能出门右转去下一家了。</li>
</ul>
<ul>
<li>
		rlpyt 中包含的并行性仅限于单节点情况&#8212;&#8212;尽管它的组件可以作为分布式框架的构建块。这里的意思大概是说，只能在一台物理机上使用rlpyt，并行是发生在这台机器内的，不能在多台物理机上并行。</li>
</ul>
<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>&#8230; <a href="https://www.codelast.com/%e5%8e%9f%e5%88%9b-%e4%bc%af%e5%85%8b%e5%88%a9%e4%ba%ba%e5%b7%a5%e6%99%ba%e8%83%bd%e7%a0%94%e7%a9%b6%e6%89%80%e7%9a%84%e5%bc%80%e6%ba%90%e5%bc%ba%e5%8c%96%e5%ad%a6%e4%b9%a0%e6%a1%86%e6%9e%b6-rlpyt/" class="read-more">Read More </a></p>]]></description>
										<content:encoded><![CDATA[<p>
<span style="color:#0000ff;">BAIR</span>（Berkeley Artificial Intelligence Research，伯克利人工智能研究所），开源了一个强化学习(<span style="color:#ff0000;">RL</span>)框架 <a href="https://github.com/astooke/rlpyt" rel="noopener noreferrer" target="_blank"><span style="background-color:#ffa07a;">rlpyt</span></a>，并于2019.09.24在其主页上对它进行了很长篇幅的<a href="https://bair.berkeley.edu/blog/2019/09/24/rlpyt/" rel="noopener noreferrer" target="_blank"><span style="background-color:#ffa07a;">介绍</span></a>（论文在<a href="https://arxiv.org/abs/1909.01500" rel="noopener noreferrer" target="_blank"><span style="background-color:#ffa07a;">这里</span></a>）。<br />
市面上开源强化学习框架已经很多了，这个框架是否值得你上车？我认为，未来怎样不好说，但至少现在(2019.10)看来是值得入手的，因为它确实有其他框架不具备/不完善的功能&mdash;&mdash;最主要的就是对<span style="color:#0000ff;">并行</span>（parallelism）的良好支持。<br />
在强化学习领域，agent与environment互动来收集training data的过程是最耗时的，如果能并行地用多个agent与多个environment互动来收集数据，那么速度可以极大提升。类似于<a href="https://github.com/google/dopamine" rel="noopener noreferrer" target="_blank"><span style="background-color:#ffa07a;">Google&nbsp;Dopamine</span></a>这样的RL框架，根本没有把&nbsp;parallelism 作为设计理念的一部分，所以如果你入了Dopamine的坑，等你对模型训练速度有要求的时候再想着换框架，成本就高多了。<br />
<span id="more-10643"></span><br />
<span style="background-color: rgb(0, 255, 0);"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/25b6.png" alt="▶" class="wp-smiley" style="height: 1em; max-height: 1em;" /></span>&nbsp;rlpyt 这个框架的主要特色是什么？<br />
这里没打算把BAIR对 rlypt&nbsp;的介绍它全部重新写一遍，只提一些我认为重要的特点：</p>
<ul>
<li>
		适用于小型、中型的强化学习实验。什么叫中型？举个&ldquo;大型&rdquo;的例子：像OpenAI的Dota那种使用上百个GPU的强化学习实验叫&ldquo;大型&rdquo;。对一般的RL应用来说，这个适用范围绰绰有余了。</li>
<li>
		可以用串行方式运行，也可以完全并行的方式运行采样器（采样就是指agent与environment交互获取training data的过程），并且支持用CPU或GPU训练，也支持用单GPU或多GPU来优化（优化指的是强化学习算法里更新网络参数的操作）。</li>
<li>
		实现了各种主流RL算法，例如A2C，PPO，DQN，Double DQN，Dueling DQN，Rainbow，DDPG等（这里不一一列出），可以说是比较全面了。</li>
</ul>
<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="background-color: rgb(0, 255, 0);"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/25b6.png" alt="▶" class="wp-smiley" style="height: 1em; max-height: 1em;" /></span>&nbsp;你也需要知道的一些事实</p>
<ul>
<li>
		<span style="color:#b22222;">rlpyt 是基于PyTorch开发的</span>，它利用了PyTorch的很多特性。如果你想用TensorFlow，那只能出门右转去下一家了。</li>
</ul>
<ul>
<li>
		rlpyt 中包含的并行性仅限于单节点情况&mdash;&mdash;尽管它的组件可以作为分布式框架的构建块。这里的意思大概是说，只能在一台物理机上使用rlpyt，并行是发生在这台机器内的，不能在多台物理机上并行。</li>
</ul>
<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="background-color: rgb(0, 255, 0);"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/25b6.png" alt="▶" class="wp-smiley" style="height: 1em; max-height: 1em;" /></span>&nbsp;下面来说一下 rlpyt&nbsp;的并行功能<br />
下面是官方的图：<br />
<img decoding="async" alt="" src="https://www.codelast.com/wp-content/uploads/ckfinder/images/rlpyt_samplers.png" style="width: 700px; height: 202px;" /><br />
虚线表示一个Python进程。<br />
<span style="color:#b22222;">左图</span>表示以<span style="color:#0000ff;">Serial</span>（串行）方式来训练模型，这种方式就跟其他大多数RL框架提供的功能一样，平淡无奇。<br />
<span style="color:#b22222;">中图</span>表示以<span style="color:#0000ff;">Parallel-CPU</span>（CPU并行）的方式来训练模型，这有就点意思了：你可以在多个CPU上运行多个worker，每个worker自带agent的一个副本，在worker内选择action，与environment交互。尽管worker是运行在CPU上的，但你仍然可以把优化（optimization）设置成跑在GPU上，当你这样做的时候，模型参数会被拷贝到共享内存（shared memory）上，供CPU使用来选择action&mdash;&mdash;因为action selection是在CPU上做的，因此CPU需要知道网络的参数，如果这些参数在GPU上，CPU要能获取到才能使用。<br />
<span style="color:#b22222;">右图</span>表示以<span style="color:#0000ff;">Parallel-GPU</span>（GPU并行）的方式来训练模型，这就更有意思了：仍然在多个CPU上运行多个worker来与environment交互，但在交互的过程中，并不是在每个worker内部做的action selection，把所有worker里的observation（可以理解为state，或feature）汇总到一个主进程里去做action selection。假设你把优化（optimization）设置成跑在GPU上，那么这些action selection也会使用GPU来完成。这样做的好处是什么？大家知道对Deep RL来说，action selection其实就是神经网络正向传播的过程，因此如果能利用GPU的话，也是能快不少的。<br />
还有一种并行模型，rlpyt&nbsp;称之为&nbsp;<span style="color:#0000ff;">Alternating-GPU</span>（交替GPU） 模式，这种模式和右图的GPU并行模式差不多，但与之不同的是，它有两<span style="color:#ff0000;">组</span>worker，其中一组用于在environment中步进（同样不做action selection），另一组就等着做action selection的工作。我从直观介绍上来看，认为它就是加强了<span style="color: rgb(0, 0, 255);">Parallel-GPU</span>模式下的action selection那一块的能力，使得负责action selection的主进程在某些情况下不至于&ldquo;忙不过来&rdquo;，不知道这样理解对不对。</p>
<p>这么丰富的并行（parallelism）方式支持，可以说是不知道比市面上的其他RL框架要强了多少倍了，管它们是什么大公司出品的，rlpyt&nbsp;照样能把它们的脸踩在地上使劲摩擦。<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></p>
<p><span style="background-color: rgb(0, 255, 0);"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/25b6.png" alt="▶" class="wp-smiley" style="height: 1em; max-height: 1em;" /></span>&nbsp;下面再来说一说 rlpyt&nbsp;的优化（optimization）功能<br />
依然引用官方的原图：<br />
<img decoding="async" alt="" src="https://www.codelast.com/wp-content/uploads/ckfinder/images/rlpyt_optimization.png" style="width: 600px; height: 287px;" /><br />
上面是 rlpyt&nbsp;支持的一种优化模式，称之为同步多进程优化模式（按我的理解这仅限于GPU并行模式），一个虚线框代表一个Python进程，它在每一个Python进程里都运行一个完整的[采样器-算法]栈的副本（图里的[Sampler-Algo]），并在PyTorch的DistribuedDataParallel类的反向传播期间隐式强制执行同步。前面说了 rlpyt&nbsp;为什么要基于 PyTorch&nbsp;开发，是因为它依赖于 PyTorch&nbsp;的很多特性，这里就是一个。<br />
这里的All-Reduce是什么呢？</p>
<blockquote>
<div>
		在同步数据并行分布式深度学习中，主要的计算步骤是：</div>
<div>
		(1) 在每个GPU上使用一个minibatch来计算loss function的梯度</div>
<div>
		(2) 通过GPU间的通信计算梯度的均值</div>
<div>
		(3) 更新模型</div>
<div>
		AllReduce就是用来计算上面第2步中的多GPU之间梯度的均值的方法。</div>
<div>
		&nbsp;</div>
</blockquote>
<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 />
除了上面的同步模式，rlpyt&nbsp;还支持一种异步采样/优化模式，如下图所示：<br />
<img decoding="async" alt="" src="https://www.codelast.com/wp-content/uploads/ckfinder/images/rlpyt_optimization_async.png" style="width: 400px; height: 335px;" /></p>
<div>
	这种模式在不同的Python进程里跑采样器（Sampler）和优化（Optimization，即上图的Algo）。这种模式的初衷是让二者可以连续运转，有更佳的硬件使用率。二者通过构建在共享内存（shared memory）之上的Replay Buffer关联起来，采样器不间断地向另一个buffer写入数据，有另一个Memory Copier进程不断地把里面的数据取出来写到上图的Replay Buffer里，Algo再取出来用。这里的Sampler和Algo同时又可以是并行的，比如说有N个Sampler，又有N个Algo。<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></p>
<p>	<span style="background-color: rgb(0, 255, 0);"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/25b6.png" alt="▶" class="wp-smiley" style="height: 1em; max-height: 1em;" /></span>&nbsp;高亮案例<br />
	为了说明 rlpyt&nbsp;有多好，BAIR对Atari游戏进行了<a href="https://openreview.net/pdf?id=r1lyTjAqYX" rel="noopener noreferrer" target="_blank"><span style="background-color:#ffa07a;">R2D2</span></a>（Recurrent Replay Distributed DQN）的重现。<br />
	R2D2是发表在ICLR 2019上的一篇论文，当时取得了Atari游戏上的&ldquo;当前最优&rdquo;效果，并且<span style="color:#0000ff;">它之前只能在分布式计算下实现</span>&mdash;&mdash;所以 rlpyt&nbsp;要拿它来下手，表示：我也可以做到啊！<br />
	<span style="color:#b22222;">R2D2有多牛？据它的论文称，它是第一个使用单一网络架构和固定的超参数集就实现了如下目标的架构：大幅超越Atari-57游戏集上的当前最先进水平，以及不输于DMLab-30游戏集的当前最先进水平。</span><br />
	R2D2的论文使用了256个CPU，一个GPU，每秒能走66000步；而 rlpyt&nbsp;在一台工作站上，仅用了 24个不怎么先进的CPU，以及3个Titan-Xp GPU，就达到了每秒走16000步，这已经可以达到在单机上进行实验的条件了。<br />
	这有力地表明了：rlpyt&nbsp;对并行（parallelism）的支持就是很厉害啊。<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="background-color: rgb(0, 255, 0);"><img src="https://s.w.org/images/core/emoji/17.0.2/72x72/25b6.png" alt="▶" class="wp-smiley" style="height: 1em; max-height: 1em;" /></span>&nbsp;基于TensorFlow的RL框架，就没有和 rlpyt&nbsp;水平差不多的吗？<br />
	所以有人会说，现在基于TensorFlow的RL框架，在并行（parallelism）这个功能点上，一个能打的都没有吗？受限于我的知识，我不敢说当前（2019.10）的TF-based RL框架都很挫，但就我调研过的那些来说，好像确实是一个真●能打的都没有。</div>
<ul>
<li>
		光看Google自己出品的那些RL框架，什么Dopamine，<a href="https://github.com/tensorflow/agents" rel="noopener noreferrer" target="_blank"><span style="background-color:#ffa07a;">TF-Agents</span></a>，目前都没有对并行的支持，而且都还不是Google的正式产品；还有一个2015年就发了论文的<a href="https://arxiv.org/abs/1507.04296" rel="noopener noreferrer" target="_blank"><span style="background-color:#ffa07a;">Gorilla</span></a>，它应该不是一个框架，而是一个架构，但它号称是支持大规模并发DRL的架构，不过这玩意并不开源啊。感觉就是Google内部，现在还没有扶持一个&ldquo;正牌&rdquo;的产品上位，各个RL团队们在为了KPI各自为战，而Google现在还没有想在开源世界里把哪个团队的RL框架扶植到TensorFlow那种地位上，等哪天Google真的下定决心想把这事给做好了，我们恐怕才有机会在Google那里看到一个像样的RL框架。</li>
<li>
		如果把DeepMind的开源产品也算做Google的话，那么还有类似于<a href="https://github.com/deepmind/trfl" rel="noopener noreferrer" target="_blank"><span style="background-color:#ffa07a;">TRFL</span></a>这样的RL&nbsp;lib，但是截至目前它都已经半年多没更新了好吗，看样子DeepMind自己都要放弃它了。</li>
<li>
		还有其他一些RL框架，是在<a href="https://ray.readthedocs.io/en/latest/index.html" rel="noopener noreferrer" target="_blank"><span style="background-color:#ffa07a;">Ray</span></a>这个分布式框架的基础上搭建的，比如<a href="https://ray.readthedocs.io/en/latest/rllib.html" rel="noopener noreferrer" target="_blank"><span style="background-color:#ffa07a;">RLlib</span></a>，可能会把小的实验复杂化。</li>
<li>
		一个最有力的竞争者恐怕就是<a href="https://github.com/tensorforce/tensorforce" rel="noopener noreferrer" target="_blank"><span style="background-color:#ffa07a;">Tensorforce</span></a>了，当前该框架仍然处于活跃的开发中，并且它是支持并行的！但并行功能没有 rlpyty&nbsp;支持的那么丰富。从一些文章的介绍中可以看出来，如果你是TensorFlow的死忠，这个框架可能是你当前的最优选择了。不过也需要提醒一下，Tensorforce是剑桥大学的几个博士生在开发维护，Tensorforce原来的主页已经挂了，现在只剩GitHub源码页，不知道会不会有后续开发保障。</li>
</ul>
<div>
	<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 />
	综上所述，如果你在找一款强化学习开发框架，并且你对使用 PyTorch 或 TensorFlow&nbsp;没有特别的偏好，同时你还对强化学习实验的并行（parallelism）过程十分在意，那么 rlpyt&nbsp;可能是你的当前最好选择；如果你非得上TensorFlow的船，那么 TensorForce 或者基于Ray的框架就可能是你的当前最好选择。</p>
<p>	2020-09-10 更新：<br />
	在 rlpyt 发布约一年后，来自清华的&ldquo;<a href="https://github.com/thu-ml/tianshou" target="_blank" rel="noopener noreferrer"><span style="background-color:#ffa07a;">天授</span></a>&rdquo;强化学习框架发布了，其主要作者是一个本科生，没错，来自清华本科生的暴击，人和人的差距就是这么大。<br />
	天授也是基于PyTorch的RL框架，通过天授发布的评测结果，可以发现其优秀程度比起 rlpyt 有过之而无不及。因此，如果你是一个正在看这篇文章，并且正在选择RL框架的人，那么，我强烈建议你可以从&ldquo;天授&rdquo;入手。<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-%e4%bc%af%e5%85%8b%e5%88%a9%e4%ba%ba%e5%b7%a5%e6%99%ba%e8%83%bd%e7%a0%94%e7%a9%b6%e6%89%80%e7%9a%84%e5%bc%80%e6%ba%90%e5%bc%ba%e5%8c%96%e5%ad%a6%e4%b9%a0%e6%a1%86%e6%9e%b6-rlpyt/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>[原创] 如何取出 tf.layers.dense 定义的全连接层的weight和bias参数值</title>
		<link>https://www.codelast.com/%e5%8e%9f%e5%88%9b-%e5%a6%82%e4%bd%95%e5%8f%96%e5%87%ba-tf-layers-dense-%e5%ae%9a%e4%b9%89%e7%9a%84%e5%85%a8%e8%bf%9e%e6%8e%a5%e5%b1%82%e7%9a%84weight%e5%92%8cbias%e5%8f%82%e6%95%b0%e5%80%bc/</link>
					<comments>https://www.codelast.com/%e5%8e%9f%e5%88%9b-%e5%a6%82%e4%bd%95%e5%8f%96%e5%87%ba-tf-layers-dense-%e5%ae%9a%e4%b9%89%e7%9a%84%e5%85%a8%e8%bf%9e%e6%8e%a5%e5%b1%82%e7%9a%84weight%e5%92%8cbias%e5%8f%82%e6%95%b0%e5%80%bc/#respond</comments>
		
		<dc:creator><![CDATA[learnhard]]></dc:creator>
		<pubDate>Fri, 27 Sep 2019 09:24:54 +0000</pubDate>
				<category><![CDATA[Algorithm]]></category>
		<category><![CDATA[原创]]></category>
		<category><![CDATA[bias]]></category>
		<category><![CDATA[TensorFlow]]></category>
		<category><![CDATA[tf.layers.dense]]></category>
		<category><![CDATA[weight]]></category>
		<guid isPermaLink="false">https://www.codelast.com/?p=10578</guid>

					<description><![CDATA[<p>
TensorFlow版本：1.14.0<br />
Python版本：3.6.8</p>
<p>在TensorFlow中，<span style="background-color: rgb(43, 43, 43); color: rgb(169, 183, 198); font-family: &#34;Droid Sans Mono&#34;; font-size: 13.5pt;">tf.layers.dense</span>&#160;定义了一个全连接层，其实现的是（来自官方文档）：</p>
<blockquote>
<p>
		This layer implements the operation: outputs = activation(inputs * kernel + bias) Where activation is the activation function passed as the activation argument (if not None), kernel is a weights matrix created by the layer, and bias is a bias vector created by the layer (only if use_bias is True).</p></blockquote>&#8230; <a href="https://www.codelast.com/%e5%8e%9f%e5%88%9b-%e5%a6%82%e4%bd%95%e5%8f%96%e5%87%ba-tf-layers-dense-%e5%ae%9a%e4%b9%89%e7%9a%84%e5%85%a8%e8%bf%9e%e6%8e%a5%e5%b1%82%e7%9a%84weight%e5%92%8cbias%e5%8f%82%e6%95%b0%e5%80%bc/" class="read-more">Read More </a>]]></description>
										<content:encoded><![CDATA[<p>
TensorFlow版本：1.14.0<br />
Python版本：3.6.8</p>
<p>在TensorFlow中，<span style="background-color: rgb(43, 43, 43); color: rgb(169, 183, 198); font-family: &quot;Droid Sans Mono&quot;; font-size: 13.5pt;">tf.layers.dense</span>&nbsp;定义了一个全连接层，其实现的是（来自官方文档）：</p>
<blockquote>
<p>
		This layer implements the operation: outputs = activation(inputs * kernel + bias) Where activation is the activation function passed as the activation argument (if not None), kernel is a weights matrix created by the layer, and bias is a bias vector created by the layer (only if use_bias is True).</p>
</blockquote>
<p>意思就是它实现了 y = activation(x * kernel + bias) 的操作，其中，activation是激活函数。在这里，<span style="color:#0000ff;">kernel</span> 就是指我们通常所说的 weight，它被TF称为&nbsp;<span style="color:#0000ff;">kernel</span>&nbsp;而不是 weight。因此，如果你想从这个模型里取出weight参数的话，就要注意它的名字了，否则会读不到这个参数。<br />
下面我们就来看具体的例子。<br />
<span id="more-10578"></span><br />
下面这段代码的逻辑非常简单，它干了下面这些事情：<br />
<span style="color:#800000;">* 创建了一个非常简单的2层全连接网络<br />
* 训练网络<br />
* 训练完成后把它保存成checkpoint格式的模型文件<br />
* 从保存的checkpoint读取模型里第一层的 weight 和 bias 参数值，打印出来</span><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></p>
<pre style="background-color:#2b2b2b;color:#a9b7c6;font-family:'Droid Sans Mono';font-size:13.5pt;">
<span style="color:#cc7832;">import </span>tensorflow <span style="color:#cc7832;">as </span>tf
<span style="color:#cc7832;">import </span>numpy <span style="color:#cc7832;">as </span>np

LAYER_1_NAME = <span style="color:#6a8759;">&#39;layer1&#39;  </span><span style="color:#808080;"># </span><span style="color:#808080;font-family:'AR PL UKai CN';">第一层的名字
</span>LAYER_2_NAME = <span style="color:#6a8759;">&#39;layer2&#39;  </span><span style="color:#808080;"># </span><span style="color:#808080;font-family:'AR PL UKai CN';">第二层的名字
</span>
<span style="color:#808080;"># </span><span style="color:#808080;font-family:'AR PL UKai CN';">创建一个非常简单的神经网络，它有两层
</span>x = tf.placeholder(<span style="color:#aa4926;">shape</span>=[<span style="color:#cc7832;">None, </span><span style="color:#6897bb;">2</span>]<span style="color:#cc7832;">, </span><span style="color:#aa4926;">dtype</span>=tf.float32)
layer1 = tf.layers.dense(x<span style="color:#cc7832;">, </span><span style="color:#6897bb;">5</span><span style="color:#cc7832;">, </span><span style="color:#aa4926;">activation</span>=tf.nn.sigmoid<span style="color:#cc7832;">, </span><span style="color:#aa4926;">name</span>=LAYER_1_NAME)
layer2 = tf.layers.dense(layer1<span style="color:#cc7832;">, </span><span style="color:#6897bb;">2</span><span style="color:#cc7832;">, </span><span style="color:#aa4926;">activation</span>=tf.nn.sigmoid<span style="color:#cc7832;">, </span><span style="color:#aa4926;">name</span>=LAYER_2_NAME)

loss = tf.reduce_mean((layer2 - x) ** <span style="color:#6897bb;">2</span>)
optimizer = tf.train.AdamOptimizer(<span style="color:#6897bb;">0.01</span>).minimize(loss)

<span style="color:#cc7832;">with </span>tf.Session() <span style="color:#cc7832;">as </span>sess:
    sess.run(tf.global_variables_initializer())
    x_values = np.random.normal(<span style="color:#6897bb;">0</span><span style="color:#cc7832;">, </span><span style="color:#6897bb;">1</span><span style="color:#cc7832;">, </span>(<span style="color:#6897bb;">5000</span><span style="color:#cc7832;">, </span><span style="color:#6897bb;">2</span>))  <span style="color:#808080;"># </span><span style="color:#808080;font-family:'AR PL UKai CN';">生成用于输入的随机数
</span><span style="color:#808080;font-family:'AR PL UKai CN';">    </span><span style="color:#cc7832;">for </span>step <span style="color:#cc7832;">in </span><span style="color:#8888c6;">range</span>(<span style="color:#6897bb;">1000</span>):
        _<span style="color:#cc7832;">, </span>loss_value = sess.run([optimizer<span style="color:#cc7832;">, </span>loss]<span style="color:#cc7832;">, </span><span style="color:#aa4926;">feed_dict</span>={x: x_values})
        <span style="color:#cc7832;">if </span>step % <span style="color:#6897bb;">100 </span>== <span style="color:#6897bb;">0</span>:
            <span style="color:#8888c6;">print</span>(<span style="color:#6a8759;">&quot;step: %d, loss: %f&quot; </span>% (step<span style="color:#cc7832;">, </span>loss_value))

    <span style="color:#808080;"># </span><span style="color:#808080;font-family:'AR PL UKai CN';">把模型保存成</span><span style="color:#808080;">checkpoint
</span><span style="color:#808080;">    </span>saver = tf.compat.v1.train.Saver()
    save_path = saver.save(sess<span style="color:#cc7832;">, </span><span style="color:#6a8759;">&#39;./checkpoint/model.ckpt&#39;</span>)
    <span style="color:#8888c6;">print</span>(<span style="color:#6a8759;">&quot;model saved in path: %s&quot; </span>% save_path<span style="color:#cc7832;">, </span><span style="color:#aa4926;">flush</span>=<span style="color:#cc7832;">True</span>)

    <span style="color:#808080;"># </span><span style="color:#808080;font-family:'AR PL UKai CN';">读取刚保存的</span><span style="color:#808080;">checkpoint
</span><span style="color:#808080;">    </span>reader = tf.train.NewCheckpointReader(save_path)
    weights = reader.get_tensor(LAYER_1_NAME + <span style="color:#6a8759;">&#39;/kernel&#39;</span>)  <span style="color:#808080;"># weight</span><span style="color:#808080;font-family:'AR PL UKai CN';">的名字，是由对应层的名字，加上默认的&quot;kernel&quot;组成的</span><span style="color:#808080;">
</span><span style="color:#808080;">    </span>bias = reader.get_tensor(LAYER_1_NAME + <span style="color:#6a8759;">&#39;/bias&#39;</span>)  <span style="color:#808080;"># bias</span><span style="color:#808080;font-family:'AR PL UKai CN';">的名字
</span><span style="color:#808080;font-family:'AR PL UKai CN';">    </span><span style="color:#8888c6;">print</span>(weights)
    <span style="color:#8888c6;">print</span>(bias)

    <span style="color:#808080;"># </span><span style="color:#808080;font-family:'AR PL UKai CN';">如果想打印模型中的所有参数名和参数值的话，把下面几行取消注释
</span><span style="color:#808080;font-family:'AR PL UKai CN';">    </span><span style="color:#808080;"># var_to_shape_map = reader.get_variable_to_shape_map()
</span><span style="color:#808080;">    # for key in var_to_shape_map:
</span><span style="color:#808080;">    #     print(&quot;tensor name: &quot;, key)
</span><span style="color:#808080;">    #     print(reader.get_tensor(key))</span></pre>
<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 />
上面程序的输出：</p>
<blockquote>
<div>
		step: 0, loss: 1.166642</div>
<div>
		step: 100, loss: 0.764896</div>
<div>
		step: 200, loss: 0.659553</div>
<div>
		step: 300, loss: 0.622590</div>
<div>
		step: 400, loss: 0.604577</div>
<div>
		step: 500, loss: 0.595308</div>
<div>
		step: 600, loss: 0.590193</div>
<div>
		step: 700, loss: 0.587147</div>
<div>
		step: 800, loss: 0.585213</div>
<div>
		step: 900, loss: 0.583916</div>
<div>
		model saved in path: ./checkpoint/model.ckpt</div>
<div>
		[[-3.0937738&nbsp; &nbsp;3.6378026&nbsp; &nbsp;0.91563785 -1.2663897&nbsp; -0.09645918]</div>
<div>
		&nbsp;[ 0.727915&nbsp; &nbsp; 0.22842272 -2.656725&nbsp; &nbsp;-2.0831287&nbsp; -3.4844823 ]]</div>
<div>
		[ 0.906906&nbsp; &nbsp;-1.6009021&nbsp; &nbsp;0.46897942&nbsp; 0.75946033&nbsp; 1.4348172 ]</div>
</blockquote>
<p>
注意，在取某一层的 weight 参数值的时候，我们用了以下方法：</p>
<pre style="background-color:#2b2b2b;color:#a9b7c6;font-family:'Droid Sans Mono';font-size:13.5pt;">
weights = reader.get_tensor(LAYER_1_NAME + <span style="color:#6a8759;">&#39;/kernel&#39;</span>)</pre>
<p>这里，weight 的名字，是类似于 &quot;<span style="color:#ff0000;">layer1/kernel</span>&quot; 这样的字符串，其中，&quot;<span style="color:#b22222;">layer1</span>&quot;就是在用&nbsp;tf.layers.dense() 创建网络的一个layer的时候，设置的 &quot;name=xxx&quot; 里的名字。前面已经说过了，TF 里把 weight 叫做 <span style="color:#0000ff;">kernel</span>，所以这里的 weight 名是 &quot;<span style="color: rgb(255, 0, 0);">layer1/kernel</span>&quot; 而不是 &quot;<span style="color: rgb(255, 0, 0);">layer1/weight</span>&quot;！<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>
]]></content:encoded>
					
					<wfw:commentRss>https://www.codelast.com/%e5%8e%9f%e5%88%9b-%e5%a6%82%e4%bd%95%e5%8f%96%e5%87%ba-tf-layers-dense-%e5%ae%9a%e4%b9%89%e7%9a%84%e5%85%a8%e8%bf%9e%e6%8e%a5%e5%b1%82%e7%9a%84weight%e5%92%8cbias%e5%8f%82%e6%95%b0%e5%80%bc/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>[原创] 如何打印出TensorFlow保存的checkpoint里的参数名</title>
		<link>https://www.codelast.com/%e5%8e%9f%e5%88%9b-%e5%a6%82%e4%bd%95%e6%89%93%e5%8d%b0%e5%87%batensorflow%e4%bf%9d%e5%ad%98%e7%9a%84checkpoint%e9%87%8c%e7%9a%84%e5%8f%82%e6%95%b0%e5%90%8d/</link>
					<comments>https://www.codelast.com/%e5%8e%9f%e5%88%9b-%e5%a6%82%e4%bd%95%e6%89%93%e5%8d%b0%e5%87%batensorflow%e4%bf%9d%e5%ad%98%e7%9a%84checkpoint%e9%87%8c%e7%9a%84%e5%8f%82%e6%95%b0%e5%90%8d/#respond</comments>
		
		<dc:creator><![CDATA[learnhard]]></dc:creator>
		<pubDate>Thu, 26 Sep 2019 02:33:43 +0000</pubDate>
				<category><![CDATA[Algorithm]]></category>
		<category><![CDATA[原创]]></category>
		<category><![CDATA[checkpoint]]></category>
		<category><![CDATA[TensorFlow]]></category>
		<category><![CDATA[参数名]]></category>
		<guid isPermaLink="false">https://www.codelast.com/?p=10565</guid>

					<description><![CDATA[<p>
TensorFlow版本：1.14.0<br />
Python版本：3.6.8</p>
<p>checkpoint文件是TensorFlow保存出来的一种模型文件格式。通常save下来的时候会得到4个文件，例如：</p>
<blockquote>
<p>
		checkpoint<br />
		model.ckpt-1.data-00000-of-00001<br />
		model.ckpt-1.index<br />
		model.ckpt-1.meta</p>
</blockquote>
<p>如何查看这些文件里的模型参数名称呢？<br />
<span id="more-10565"></span><br />
<span style="background-color:#00ff00;">[1]</span> 方法一：用TensorFlow自带的 <span style="color:#0000ff;">inspect_checkpoint</span> 工具<br />
首先 cd 到那4个model文件所在的目录，然后执行：</p>
<blockquote>
<p>
		<span id="docs-internal-guid-9874ec20-7fff-14d0-7d9d-3938223b2dc9"><span style="font-size: 14pt; font-family: Arial; color: rgb(255, 0, 0); font-variant-numeric: normal; font-variant-east-asian: normal; vertical-align: baseline; white-space: pre-wrap;">python -m tensorflow.python.tools.inspect_checkpoint --file_name=</span><span style="font-size: 14pt; font-family: Arial; color: rgb(56, 118, 29); font-variant-numeric: normal; font-variant-east-asian: normal; vertical-align: baseline; white-space: pre-wrap;">model.ckpt-1</span><span style="font-size: 14pt; font-family: Arial; color: rgb(255, 0, 0); font-variant-numeric: normal; font-variant-east-asian: normal; vertical-align: baseline; white-space: pre-wrap;"> &#34;$@&#34;</span></span></p>
</blockquote>
<div>
	其中，model.ckpt-1 是输出的checkpoint文件名中的一部分（后3个文件的最后一个&#8220;点&#8221;号前面的部分）。</div>
<div>
	输出：</div>
<blockquote>
<div>
		pi/actor13/bias (DT_FLOAT) [32]</div>
<div>
		pi/actor13/kernel (DT_FLOAT) [101,32]</div>
<div>
		pi/actor15/bias (DT_FLOAT) [8]</div>
<div>
		pi/actor15/kernel (DT_FLOAT) [32,8]</div>
<div>
		pi/ap/bias (DT_FLOAT) [2]</div>
<div>
		pi/ap/kernel (DT_FLOAT) [8,2]</div>
<div>
		&#160;</div>
<div>
		# Total number of params: 3546</div>
</blockquote>
<div>
	上面类似于&#160;<span style="color:#0000ff;">pi/actor13/kernel</span> 的部分就是一个参数名。<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></div>&#8230; <a href="https://www.codelast.com/%e5%8e%9f%e5%88%9b-%e5%a6%82%e4%bd%95%e6%89%93%e5%8d%b0%e5%87%batensorflow%e4%bf%9d%e5%ad%98%e7%9a%84checkpoint%e9%87%8c%e7%9a%84%e5%8f%82%e6%95%b0%e5%90%8d/" class="read-more">Read More </a>]]></description>
										<content:encoded><![CDATA[<p>
TensorFlow版本：1.14.0<br />
Python版本：3.6.8</p>
<p>checkpoint文件是TensorFlow保存出来的一种模型文件格式。通常save下来的时候会得到4个文件，例如：</p>
<blockquote>
<p>
		checkpoint<br />
		model.ckpt-1.data-00000-of-00001<br />
		model.ckpt-1.index<br />
		model.ckpt-1.meta</p>
</blockquote>
<p>如何查看这些文件里的模型参数名称呢？<br />
<span id="more-10565"></span><br />
<span style="background-color:#00ff00;">[1]</span> 方法一：用TensorFlow自带的 <span style="color:#0000ff;">inspect_checkpoint</span> 工具<br />
首先 cd 到那4个model文件所在的目录，然后执行：</p>
<blockquote>
<p>
		<span id="docs-internal-guid-9874ec20-7fff-14d0-7d9d-3938223b2dc9"><span style="font-size: 14pt; font-family: Arial; color: rgb(255, 0, 0); font-variant-numeric: normal; font-variant-east-asian: normal; vertical-align: baseline; white-space: pre-wrap;">python -m tensorflow.python.tools.inspect_checkpoint --file_name=</span><span style="font-size: 14pt; font-family: Arial; color: rgb(56, 118, 29); font-variant-numeric: normal; font-variant-east-asian: normal; vertical-align: baseline; white-space: pre-wrap;">model.ckpt-1</span><span style="font-size: 14pt; font-family: Arial; color: rgb(255, 0, 0); font-variant-numeric: normal; font-variant-east-asian: normal; vertical-align: baseline; white-space: pre-wrap;"> &quot;$@&quot;</span></span></p>
</blockquote>
<div>
	其中，model.ckpt-1 是输出的checkpoint文件名中的一部分（后3个文件的最后一个&ldquo;点&rdquo;号前面的部分）。</div>
<div>
	输出：</div>
<blockquote>
<div>
		pi/actor13/bias (DT_FLOAT) [32]</div>
<div>
		pi/actor13/kernel (DT_FLOAT) [101,32]</div>
<div>
		pi/actor15/bias (DT_FLOAT) [8]</div>
<div>
		pi/actor15/kernel (DT_FLOAT) [32,8]</div>
<div>
		pi/ap/bias (DT_FLOAT) [2]</div>
<div>
		pi/ap/kernel (DT_FLOAT) [8,2]</div>
<div>
		&nbsp;</div>
<div>
		# Total number of params: 3546</div>
</blockquote>
<div>
	上面类似于&nbsp;<span style="color:#0000ff;">pi/actor13/kernel</span> 的部分就是一个参数名。<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="background-color: rgb(0, 255, 0);">[2]</span>&nbsp;方法二：写Python程序实现</p>
<ol class="dp-py" start="1" style="box-sizing: border-box; margin: 0px 0px 5px; padding: 2px; color: rgb(102, 102, 102); overflow-wrap: break-word; list-style: none; font-family: &quot;Microsoft Yahei&quot;, Helvetica, Arial, sans-serif; font-size: 15px;">
<li class="alt" style="box-sizing: border-box; border-left: 1px dashed rgb(204, 204, 204); padding-left: 10px; line-height: 28.5px; margin: 0px 35px 0px 50px; list-style: decimal;">
			<span style="box-sizing: border-box; color: black;">checkpoint_path&nbsp;=&nbsp;&#39;/path_to_those_four_files/model.ckpt-1&#39;&nbsp;&nbsp;</span></li>
<li style="box-sizing: border-box; border-left: 1px dashed rgb(204, 204, 204); padding-left: 10px; line-height: 28.5px; margin: 0px 35px 0px 50px; list-style: decimal;">
			<span style="box-sizing: border-box; color: black;">reader&nbsp;=&nbsp;tf.train.NewCheckpointReader(checkpoint_path)&nbsp;&nbsp;</span></li>
<li class="alt" style="box-sizing: border-box; border-left: 1px dashed rgb(204, 204, 204); padding-left: 10px; line-height: 28.5px; margin: 0px 35px 0px 50px; list-style: decimal;">
			<span style="box-sizing: border-box; color: black;">var_to_shape_map&nbsp;=&nbsp;reader.get_variable_to_shape_map()&nbsp;&nbsp;</span></li>
<li style="box-sizing: border-box; border-left: 1px dashed rgb(204, 204, 204); padding-left: 10px; line-height: 28.5px; margin: 0px 35px 0px 50px; list-style: decimal;">
			<span style="box-sizing: border-box; color: black;">&nbsp;&nbsp;</span></li>
<li class="alt" style="box-sizing: border-box; border-left: 1px dashed rgb(204, 204, 204); padding-left: 10px; line-height: 28.5px; margin: 0px 35px 0px 50px; list-style: decimal;">
			<span style="box-sizing: border-box; color: black;"><span class="keyword" style="box-sizing: border-box; color: blue; font-weight: bold;">for</span><span style="box-sizing: border-box;">&nbsp;key&nbsp;</span><span class="keyword" style="box-sizing: border-box; color: blue; font-weight: bold;">in</span><span style="box-sizing: border-box;">&nbsp;var_to_shape_map:&nbsp;&nbsp;</span></span></li>
<li style="box-sizing: border-box; border-left: 1px dashed rgb(204, 204, 204); padding-left: 10px; line-height: 28.5px; margin: 0px 35px 0px 50px; list-style: decimal;">
			<span style="box-sizing: border-box; color: black;">&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword" style="box-sizing: border-box; color: blue; font-weight: bold;">print</span><span style="box-sizing: border-box;">(&quot;tensor&nbsp;name:&nbsp;&quot;,&nbsp;key)&nbsp;&nbsp;</span></span></li>
<li class="alt" style="box-sizing: border-box; border-left: 1px dashed rgb(204, 204, 204); padding-left: 10px; line-height: 28.5px; margin: 0px 35px 0px 50px; list-style: decimal;">
			<span style="box-sizing: border-box; color: black;">&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword" style="box-sizing: border-box; color: blue; font-weight: bold;">print</span><span style="box-sizing: border-box;">(reader.get_tensor(key))&nbsp;&nbsp;</span><span class="comment" style="box-sizing: border-box; color: green;">#&nbsp;打印出Tensor的值</span><span style="box-sizing: border-box;">&nbsp;&nbsp;</span></span></li>
</ol>
<p>
	其中，<span style="color:#0000ff;">checkpoint_path</span> 变量中的 <span style="color:#b22222;">path_to_those_four_files</span> 这一部分指的是那4个model文件所在的目录路径，后面的 &ldquo;model.ckpt-1&rdquo;&nbsp;是输出的checkpoint文件名中的一部分，并不是完整的文件名，这一点要注意，很多文章没有说清楚，会让人搞混。<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 />
	部分输出内容类似于：</p>
<blockquote>
<div>
			tensor_name:&nbsp; pi/actor15/bias</div>
<div>
			[ 0.01485237 -0.04058828&nbsp; 0.00179128 -0.00357329 -0.05909787&nbsp; 0.00424578</div>
<div>
			&nbsp;-0.03840631 -0.00575123]</div>
<div>
			tensor_name:&nbsp; pi/ap/bias</div>
<div>
			[ 0.00746095 -0.00746095]</div>
<p>
			...</p>
</blockquote>
<p>	（完）<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-%e5%a6%82%e4%bd%95%e6%89%93%e5%8d%b0%e5%87%batensorflow%e4%bf%9d%e5%ad%98%e7%9a%84checkpoint%e9%87%8c%e7%9a%84%e5%8f%82%e6%95%b0%e5%90%8d/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>[原创] 二维numpy数组保存到TFRecord并读取还原回来</title>
		<link>https://www.codelast.com/%e5%8e%9f%e5%88%9b-%e4%ba%8c%e7%bb%b4numpy%e6%95%b0%e7%bb%84%e4%bf%9d%e5%ad%98%e5%88%b0tfrecord%e5%b9%b6%e8%af%bb%e5%8f%96%e8%bf%98%e5%8e%9f%e5%9b%9e%e6%9d%a5/</link>
					<comments>https://www.codelast.com/%e5%8e%9f%e5%88%9b-%e4%ba%8c%e7%bb%b4numpy%e6%95%b0%e7%bb%84%e4%bf%9d%e5%ad%98%e5%88%b0tfrecord%e5%b9%b6%e8%af%bb%e5%8f%96%e8%bf%98%e5%8e%9f%e5%9b%9e%e6%9d%a5/#respond</comments>
		
		<dc:creator><![CDATA[learnhard]]></dc:creator>
		<pubDate>Wed, 18 Sep 2019 10:44:42 +0000</pubDate>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[原创]]></category>
		<category><![CDATA[未分类]]></category>
		<category><![CDATA[numpy array]]></category>
		<category><![CDATA[TensorFlow]]></category>
		<category><![CDATA[TFRecord]]></category>
		<guid isPermaLink="false">https://www.codelast.com/?p=10534</guid>

					<description><![CDATA[<p>
TensorFlow版本：1.14.0<br />
Python版本：3.6.8</p>
<p>TFRecord 文件格式是一种面向记录的简单二进制格式，很多 TensorFlow 应用采用此格式来训练数据。<br />
TFRecord 内部有一系列的 Example ，Example 是 protocolbuf 协议下的消息体。<br />
<span id="more-10534"></span><br />
把一个一维的numpy数组保存为TFRecord文件很容易，但如果numpy数组是二维的可能就比较容易写错。下面是一个例子。</p>
<ol class="dp-py" start="1" style="box-sizing: border-box; margin: 0px 0px 5px; padding: 2px; color: rgb(102, 102, 102); overflow-wrap: break-word; list-style: none; font-family: &#34;Microsoft Yahei&#34;, Helvetica, Arial, sans-serif; font-size: 15px;">
<li class="alt" style="box-sizing: border-box; border-left: 1px dashed rgb(204, 204, 204); padding-left: 10px; line-height: 28.5px; margin: 0px 35px 0px 50px; list-style: decimal;">
		<span style="box-sizing: border-box; color: black;"><span class="string" style="box-sizing: border-box; color: red;">&#34;&#34;&#34;</span>&#160;</span></li>
<li style="box-sizing: border-box; border-left: 1px dashed rgb(204, 204, 204); padding-left: 10px; line-height: 28.5px; margin: 0px 35px 0px 50px; list-style: decimal;">
		<span style="box-sizing: border-box; color: black;"><span class="string" style="box-sizing: border-box; color: red;">本程序演示了如何保存numpy&#160;array为TFRecords文件，并将其读取出来。</span>&#160;</span></li>
<li class="alt" style="box-sizing: border-box; border-left: 1px dashed rgb(204, 204, 204); padding-left: 10px; line-height: 28.5px; margin: 0px 35px 0px 50px; list-style: decimal;">
		<span style="box-sizing: border-box; color: black;"><span class="string" style="box-sizing: border-box; color: red;">&#34;&#34;&#34;</span><span style="box-sizing: border-box;">&#160;&#160;</span></span></li>
<li style="box-sizing: border-box; border-left: 1px dashed rgb(204, 204, 204); padding-left: 10px; line-height: 28.5px; margin: 0px 35px 0px 50px; list-style: decimal;">
		<span style="box-sizing: border-box; color: black;"><span class="keyword" style="box-sizing: border-box; color: blue; font-weight: bold;">import</span><span style="box-sizing: border-box;">&#160;</span><span class="commonlibs" style="box-sizing: border-box; color: rgb(138, 43, 226); font-style: italic;">random</span><span style="box-sizing: border-box;">&#160;&#160;</span></span></li>
<li class="alt" style="box-sizing: border-box; border-left: 1px dashed rgb(204, 204, 204); padding-left: 10px; line-height: 28.5px; margin: 0px 35px 0px 50px; list-style: decimal;">
		<span style="box-sizing: border-box; color: black;">&#160;&#160;</span></li>
<li style="box-sizing: border-box; border-left: 1px dashed rgb(204, 204, 204); padding-left: 10px; line-height: 28.5px; margin: 0px 35px 0px 50px; list-style: decimal;">
		<span style="box-sizing: border-box; color: black;"><span class="keyword" style="box-sizing: border-box; color: blue; font-weight: bold;">import</span><span style="box-sizing: border-box;">&#160;numpy&#160;as&#160;np&#160;&#160;</span></span></li>
<li class="alt" style="box-sizing: border-box; border-left: 1px dashed rgb(204, 204, 204); padding-left: 10px; line-height: 28.5px; margin: 0px 35px 0px 50px; list-style: decimal;">
		<span style="box-sizing: border-box; color: black;"><span class="keyword" style="box-sizing: border-box; color: blue; font-weight: bold;">import</span><span style="box-sizing: border-box;">&#160;tensorflow&#160;as&#160;tf&#160;&#160;</span></span></li>
<li style="box-sizing: border-box; border-left: 1px dashed rgb(204, 204, 204); padding-left: 10px; line-height: 28.5px; margin: 0px 35px 0px 50px; list-style: decimal;">
		<span style="box-sizing: border-box; color: black;">&#160;&#160;</span></li>
<li class="alt" style="box-sizing: border-box; border-left: 1px dashed rgb(204, 204, 204); padding-left: 10px; line-height: 28.5px; margin: 0px 35px 0px 50px; list-style: decimal;">
		<span style="box-sizing: border-box; color: black;"><span class="builtins" style="box-sizing: border-box; color: rgb(255, 20, 147);">__author__</span><span style="box-sizing: border-box;">&#160;=&#160;&#39;Darran&#160;Zhang&#160;@&#160;codelast.com&#39;&#160;&#160;</span></span></li>
<li style="box-sizing: border-box; border-left: 1px dashed rgb(204, 204, 204); padding-left: 10px; line-height: 28.5px; margin: 0px 35px 0px 50px; list-style: decimal;">
		<span style="box-sizing: border-box; color: black;">&#160;&#160;</span></li>
<li class="alt" style="box-sizing: border-box; border-left: 1px dashed rgb(204, 204, 204); padding-left: 10px; line-height: 28.5px; margin: 0px 35px 0px 50px; list-style: decimal;">
		<span style="box-sizing: border-box; color: black;">&#160;&#160;</span></li>
<li style="box-sizing: border-box; border-left: 1px dashed rgb(204, 204, 204); padding-left: 10px; line-height: 28.5px; margin: 0px 35px 0px 50px; list-style: decimal;">
		<span style="box-sizing: border-box; color: black;"><span class="keyword" style="box-sizing: border-box; color: blue; font-weight: bold;">def</span><span style="box-sizing: border-box;">&#160;save_tfrecords(state_data,&#160;action_data,&#160;reward_data,&#160;dest_file):&#160;&#160;</span></span></li>
<li class="alt" style="box-sizing: border-box; border-left: 1px dashed rgb(204, 204, 204); padding-left: 10px; line-height: 28.5px; margin: 0px 35px 0px 50px; list-style: decimal;">
		<span style="box-sizing: border-box; color: black;">&#160;&#160;&#160;&#160;<span class="string" style="box-sizing: border-box; color: red;">&#34;&#34;&#34;</span>&#160;</span></li>
<li style="box-sizing: border-box; border-left: 1px dashed rgb(204, 204, 204); padding-left: 10px; line-height: 28.5px; margin: 0px 35px 0px 50px; list-style: decimal;">
		<span style="box-sizing: border-box; color: black;"><span class="string" style="box-sizing: border-box; color: red;">&#160;&#160;&#160;&#160;保存numpy&#160;array到TFRecord文件中。</span>&#160;</span></li>
<li class="alt" style="box-sizing: border-box; border-left: 1px dashed rgb(204, 204, 204); padding-left: 10px; line-height: 28.5px; margin: 0px 35px 0px 50px; list-style: decimal;">
		<span style="box-sizing: border-box; color: black;"><span class="string" style="box-sizing: border-box; color: red;">&#160;&#160;&#160;&#160;这里输入了三个不同的numpy&#160;array来做演示，它们含有不同类型的元素。</span>&#160;</span></li>
<li style="box-sizing: border-box; border-left: 1px dashed rgb(204, 204, 204); padding-left: 10px; line-height: 28.5px; margin: 0px 35px 0px 50px; list-style: decimal;">
		<span style="box-sizing: border-box; color: black;"><span class="string" style="box-sizing: border-box; color: red;">&#160;&#160;&#160;&#160;Args:</span>&#160;</span></li>
<li class="alt" style="box-sizing: border-box; border-left: 1px dashed rgb(204, 204, 204); padding-left: 10px; line-height: 28.5px; margin: 0px 35px 0px 50px; list-style: decimal;">
		<span style="box-sizing: border-box; color: black;"><span class="string" style="box-sizing: border-box; color: red;">&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;state_data:&#160;要保存到TFRecord文件的第1个numpy&#160;array，每一个&#160;state_data[i]&#160;是一个&#160;numpy.ndarray（数组里的每个元素又是一个浮点</span></span></li></ol>&#8230; <a href="https://www.codelast.com/%e5%8e%9f%e5%88%9b-%e4%ba%8c%e7%bb%b4numpy%e6%95%b0%e7%bb%84%e4%bf%9d%e5%ad%98%e5%88%b0tfrecord%e5%b9%b6%e8%af%bb%e5%8f%96%e8%bf%98%e5%8e%9f%e5%9b%9e%e6%9d%a5/" class="read-more">Read More </a>]]></description>
										<content:encoded><![CDATA[<p>
TensorFlow版本：1.14.0<br />
Python版本：3.6.8</p>
<p>TFRecord 文件格式是一种面向记录的简单二进制格式，很多 TensorFlow 应用采用此格式来训练数据。<br />
TFRecord 内部有一系列的 Example ，Example 是 protocolbuf 协议下的消息体。<br />
<span id="more-10534"></span><br />
把一个一维的numpy数组保存为TFRecord文件很容易，但如果numpy数组是二维的可能就比较容易写错。下面是一个例子。</p>
<ol class="dp-py" start="1" style="box-sizing: border-box; margin: 0px 0px 5px; padding: 2px; color: rgb(102, 102, 102); overflow-wrap: break-word; list-style: none; font-family: &quot;Microsoft Yahei&quot;, Helvetica, Arial, sans-serif; font-size: 15px;">
<li class="alt" style="box-sizing: border-box; border-left: 1px dashed rgb(204, 204, 204); padding-left: 10px; line-height: 28.5px; margin: 0px 35px 0px 50px; list-style: decimal;">
		<span style="box-sizing: border-box; color: black;"><span class="string" style="box-sizing: border-box; color: red;">&quot;&quot;&quot;</span>&nbsp;</span></li>
<li style="box-sizing: border-box; border-left: 1px dashed rgb(204, 204, 204); padding-left: 10px; line-height: 28.5px; margin: 0px 35px 0px 50px; list-style: decimal;">
		<span style="box-sizing: border-box; color: black;"><span class="string" style="box-sizing: border-box; color: red;">本程序演示了如何保存numpy&nbsp;array为TFRecords文件，并将其读取出来。</span>&nbsp;</span></li>
<li class="alt" style="box-sizing: border-box; border-left: 1px dashed rgb(204, 204, 204); padding-left: 10px; line-height: 28.5px; margin: 0px 35px 0px 50px; list-style: decimal;">
		<span style="box-sizing: border-box; color: black;"><span class="string" style="box-sizing: border-box; color: red;">&quot;&quot;&quot;</span><span style="box-sizing: border-box;">&nbsp;&nbsp;</span></span></li>
<li style="box-sizing: border-box; border-left: 1px dashed rgb(204, 204, 204); padding-left: 10px; line-height: 28.5px; margin: 0px 35px 0px 50px; list-style: decimal;">
		<span style="box-sizing: border-box; color: black;"><span class="keyword" style="box-sizing: border-box; color: blue; font-weight: bold;">import</span><span style="box-sizing: border-box;">&nbsp;</span><span class="commonlibs" style="box-sizing: border-box; color: rgb(138, 43, 226); font-style: italic;">random</span><span style="box-sizing: border-box;">&nbsp;&nbsp;</span></span></li>
<li class="alt" style="box-sizing: border-box; border-left: 1px dashed rgb(204, 204, 204); padding-left: 10px; line-height: 28.5px; margin: 0px 35px 0px 50px; list-style: decimal;">
		<span style="box-sizing: border-box; color: black;">&nbsp;&nbsp;</span></li>
<li style="box-sizing: border-box; border-left: 1px dashed rgb(204, 204, 204); padding-left: 10px; line-height: 28.5px; margin: 0px 35px 0px 50px; list-style: decimal;">
		<span style="box-sizing: border-box; color: black;"><span class="keyword" style="box-sizing: border-box; color: blue; font-weight: bold;">import</span><span style="box-sizing: border-box;">&nbsp;numpy&nbsp;as&nbsp;np&nbsp;&nbsp;</span></span></li>
<li class="alt" style="box-sizing: border-box; border-left: 1px dashed rgb(204, 204, 204); padding-left: 10px; line-height: 28.5px; margin: 0px 35px 0px 50px; list-style: decimal;">
		<span style="box-sizing: border-box; color: black;"><span class="keyword" style="box-sizing: border-box; color: blue; font-weight: bold;">import</span><span style="box-sizing: border-box;">&nbsp;tensorflow&nbsp;as&nbsp;tf&nbsp;&nbsp;</span></span></li>
<li style="box-sizing: border-box; border-left: 1px dashed rgb(204, 204, 204); padding-left: 10px; line-height: 28.5px; margin: 0px 35px 0px 50px; list-style: decimal;">
		<span style="box-sizing: border-box; color: black;">&nbsp;&nbsp;</span></li>
<li class="alt" style="box-sizing: border-box; border-left: 1px dashed rgb(204, 204, 204); padding-left: 10px; line-height: 28.5px; margin: 0px 35px 0px 50px; list-style: decimal;">
		<span style="box-sizing: border-box; color: black;"><span class="builtins" style="box-sizing: border-box; color: rgb(255, 20, 147);">__author__</span><span style="box-sizing: border-box;">&nbsp;=&nbsp;&#39;Darran&nbsp;Zhang&nbsp;@&nbsp;codelast.com&#39;&nbsp;&nbsp;</span></span></li>
<li style="box-sizing: border-box; border-left: 1px dashed rgb(204, 204, 204); padding-left: 10px; line-height: 28.5px; margin: 0px 35px 0px 50px; list-style: decimal;">
		<span style="box-sizing: border-box; color: black;">&nbsp;&nbsp;</span></li>
<li class="alt" style="box-sizing: border-box; border-left: 1px dashed rgb(204, 204, 204); padding-left: 10px; line-height: 28.5px; margin: 0px 35px 0px 50px; list-style: decimal;">
		<span style="box-sizing: border-box; color: black;">&nbsp;&nbsp;</span></li>
<li style="box-sizing: border-box; border-left: 1px dashed rgb(204, 204, 204); padding-left: 10px; line-height: 28.5px; margin: 0px 35px 0px 50px; list-style: decimal;">
		<span style="box-sizing: border-box; color: black;"><span class="keyword" style="box-sizing: border-box; color: blue; font-weight: bold;">def</span><span style="box-sizing: border-box;">&nbsp;save_tfrecords(state_data,&nbsp;action_data,&nbsp;reward_data,&nbsp;dest_file):&nbsp;&nbsp;</span></span></li>
<li class="alt" style="box-sizing: border-box; border-left: 1px dashed rgb(204, 204, 204); padding-left: 10px; line-height: 28.5px; margin: 0px 35px 0px 50px; list-style: decimal;">
		<span style="box-sizing: border-box; color: black;">&nbsp;&nbsp;&nbsp;&nbsp;<span class="string" style="box-sizing: border-box; color: red;">&quot;&quot;&quot;</span>&nbsp;</span></li>
<li style="box-sizing: border-box; border-left: 1px dashed rgb(204, 204, 204); padding-left: 10px; line-height: 28.5px; margin: 0px 35px 0px 50px; list-style: decimal;">
		<span style="box-sizing: border-box; color: black;"><span class="string" style="box-sizing: border-box; color: red;">&nbsp;&nbsp;&nbsp;&nbsp;保存numpy&nbsp;array到TFRecord文件中。</span>&nbsp;</span></li>
<li class="alt" style="box-sizing: border-box; border-left: 1px dashed rgb(204, 204, 204); padding-left: 10px; line-height: 28.5px; margin: 0px 35px 0px 50px; list-style: decimal;">
		<span style="box-sizing: border-box; color: black;"><span class="string" style="box-sizing: border-box; color: red;">&nbsp;&nbsp;&nbsp;&nbsp;这里输入了三个不同的numpy&nbsp;array来做演示，它们含有不同类型的元素。</span>&nbsp;</span></li>
<li style="box-sizing: border-box; border-left: 1px dashed rgb(204, 204, 204); padding-left: 10px; line-height: 28.5px; margin: 0px 35px 0px 50px; list-style: decimal;">
		<span style="box-sizing: border-box; color: black;"><span class="string" style="box-sizing: border-box; color: red;">&nbsp;&nbsp;&nbsp;&nbsp;Args:</span>&nbsp;</span></li>
<li class="alt" style="box-sizing: border-box; border-left: 1px dashed rgb(204, 204, 204); padding-left: 10px; line-height: 28.5px; margin: 0px 35px 0px 50px; list-style: decimal;">
		<span style="box-sizing: border-box; color: black;"><span class="string" style="box-sizing: border-box; color: red;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;state_data:&nbsp;要保存到TFRecord文件的第1个numpy&nbsp;array，每一个&nbsp;state_data[i]&nbsp;是一个&nbsp;numpy.ndarray（数组里的每个元素又是一个浮点</span>&nbsp;</span></li>
<li style="box-sizing: border-box; border-left: 1px dashed rgb(204, 204, 204); padding-left: 10px; line-height: 28.5px; margin: 0px 35px 0px 50px; list-style: decimal;">
		<span style="box-sizing: border-box; color: black;"><span class="string" style="box-sizing: border-box; color: red;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;数），因此不能用&nbsp;Int64List&nbsp;或&nbsp;FloatList&nbsp;来存储，只能用&nbsp;BytesList。</span>&nbsp;</span></li>
<li class="alt" style="box-sizing: border-box; border-left: 1px dashed rgb(204, 204, 204); padding-left: 10px; line-height: 28.5px; margin: 0px 35px 0px 50px; list-style: decimal;">
		<span style="box-sizing: border-box; color: black;"><span class="string" style="box-sizing: border-box; color: red;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;action_data:&nbsp;要保存到TFRecord文件的第2个numpy&nbsp;array，每一个&nbsp;action_data[i]&nbsp;是一个整数，使用&nbsp;Int64List&nbsp;来存储。</span>&nbsp;</span></li>
<li style="box-sizing: border-box; border-left: 1px dashed rgb(204, 204, 204); padding-left: 10px; line-height: 28.5px; margin: 0px 35px 0px 50px; list-style: decimal;">
		<span style="box-sizing: border-box; color: black;"><span class="string" style="box-sizing: border-box; color: red;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;reward_data:&nbsp;要保存到TFRecord文件的第3个numpy&nbsp;array，每一个&nbsp;reward_data[i]&nbsp;是一个整数，使用&nbsp;Int64List&nbsp;来存储。</span>&nbsp;</span></li>
<li class="alt" style="box-sizing: border-box; border-left: 1px dashed rgb(204, 204, 204); padding-left: 10px; line-height: 28.5px; margin: 0px 35px 0px 50px; list-style: decimal;">
		<span style="box-sizing: border-box; color: black;"><span class="string" style="box-sizing: border-box; color: red;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dest_file:&nbsp;输出文件的路径。</span>&nbsp;</span></li>
<li style="box-sizing: border-box; border-left: 1px dashed rgb(204, 204, 204); padding-left: 10px; line-height: 28.5px; margin: 0px 35px 0px 50px; list-style: decimal;">
		<span style="box-sizing: border-box; color: black;"><span class="string" style="box-sizing: border-box; color: red;">&nbsp;&nbsp;&nbsp;&nbsp;Returns:</span>&nbsp;</span></li>
<li class="alt" style="box-sizing: border-box; border-left: 1px dashed rgb(204, 204, 204); padding-left: 10px; line-height: 28.5px; margin: 0px 35px 0px 50px; list-style: decimal;">
		<span style="box-sizing: border-box; color: black;"><span class="string" style="box-sizing: border-box; color: red;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;不返回任何值</span>&nbsp;</span></li>
<li style="box-sizing: border-box; border-left: 1px dashed rgb(204, 204, 204); padding-left: 10px; line-height: 28.5px; margin: 0px 35px 0px 50px; list-style: decimal;">
		<span style="box-sizing: border-box; color: black;"><span class="string" style="box-sizing: border-box; color: red;">&nbsp;&nbsp;&nbsp;&nbsp;&quot;&quot;&quot;</span><span style="box-sizing: border-box;">&nbsp;&nbsp;</span></span></li>
<li class="alt" style="box-sizing: border-box; border-left: 1px dashed rgb(204, 204, 204); padding-left: 10px; line-height: 28.5px; margin: 0px 35px 0px 50px; list-style: decimal;">
		<span style="box-sizing: border-box; color: black;">&nbsp;&nbsp;&nbsp;&nbsp;with&nbsp;tf.io.TFRecordWriter(dest_file)&nbsp;as&nbsp;writer:&nbsp;&nbsp;</span></li>
<li style="box-sizing: border-box; border-left: 1px dashed rgb(204, 204, 204); padding-left: 10px; line-height: 28.5px; margin: 0px 35px 0px 50px; list-style: decimal;">
		<span style="box-sizing: border-box; color: black;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword" style="box-sizing: border-box; color: blue; font-weight: bold;">for</span><span style="box-sizing: border-box;">&nbsp;i&nbsp;</span><span class="keyword" style="box-sizing: border-box; color: blue; font-weight: bold;">in</span><span style="box-sizing: border-box;">&nbsp;</span><span class="builtins" style="box-sizing: border-box; color: rgb(255, 20, 147);">range</span><span style="box-sizing: border-box;">(</span><span class="builtins" style="box-sizing: border-box; color: rgb(255, 20, 147);">len</span><span style="box-sizing: border-box;">(state_data)):&nbsp;&nbsp;</span></span></li>
<li class="alt" style="box-sizing: border-box; border-left: 1px dashed rgb(204, 204, 204); padding-left: 10px; line-height: 28.5px; margin: 0px 35px 0px 50px; list-style: decimal;">
		<span style="box-sizing: border-box; color: black;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;features&nbsp;=&nbsp;tf.train.Features(&nbsp;&nbsp;</span></li>
<li style="box-sizing: border-box; border-left: 1px dashed rgb(204, 204, 204); padding-left: 10px; line-height: 28.5px; margin: 0px 35px 0px 50px; list-style: decimal;">
		<span style="box-sizing: border-box; color: black;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;feature={&nbsp;&nbsp;</span></li>
<li class="alt" style="box-sizing: border-box; border-left: 1px dashed rgb(204, 204, 204); padding-left: 10px; line-height: 28.5px; margin: 0px 35px 0px 50px; list-style: decimal;">
		<span style="box-sizing: border-box; color: black;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&quot;state&quot;:&nbsp;tf.train.Feature(&nbsp;&nbsp;</span></li>
<li style="box-sizing: border-box; border-left: 1px dashed rgb(204, 204, 204); padding-left: 10px; line-height: 28.5px; margin: 0px 35px 0px 50px; list-style: decimal;">
		<span style="box-sizing: border-box; color: black;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;bytes_list=tf.train.BytesList(value=[state_data[i].astype(np.float32).tostring()])),&nbsp;&nbsp;</span></li>
<li class="alt" style="box-sizing: border-box; border-left: 1px dashed rgb(204, 204, 204); padding-left: 10px; line-height: 28.5px; margin: 0px 35px 0px 50px; list-style: decimal;">
		<span style="box-sizing: border-box; color: black;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&quot;action&quot;:&nbsp;tf.train.Feature(&nbsp;&nbsp;</span></li>
<li style="box-sizing: border-box; border-left: 1px dashed rgb(204, 204, 204); padding-left: 10px; line-height: 28.5px; margin: 0px 35px 0px 50px; list-style: decimal;">
		<span style="box-sizing: border-box; color: black;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int64_list=tf.train.Int64List(value=[action_data[i]])),&nbsp;&nbsp;</span></li>
<li class="alt" style="box-sizing: border-box; border-left: 1px dashed rgb(204, 204, 204); padding-left: 10px; line-height: 28.5px; margin: 0px 35px 0px 50px; list-style: decimal;">
		<span style="box-sizing: border-box; color: black;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&quot;reward&quot;:&nbsp;tf.train.Feature(&nbsp;&nbsp;</span></li>
<li style="box-sizing: border-box; border-left: 1px dashed rgb(204, 204, 204); padding-left: 10px; line-height: 28.5px; margin: 0px 35px 0px 50px; list-style: decimal;">
		<span style="box-sizing: border-box; color: black;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;int64_list=tf.train.Int64List(value=[reward_data[i]]))&nbsp;&nbsp;</span></li>
<li class="alt" style="box-sizing: border-box; border-left: 1px dashed rgb(204, 204, 204); padding-left: 10px; line-height: 28.5px; margin: 0px 35px 0px 50px; list-style: decimal;">
		<span style="box-sizing: border-box; color: black;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;</span></li>
<li style="box-sizing: border-box; border-left: 1px dashed rgb(204, 204, 204); padding-left: 10px; line-height: 28.5px; margin: 0px 35px 0px 50px; list-style: decimal;">
		<span style="box-sizing: border-box; color: black;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;)&nbsp;&nbsp;</span></li>
<li class="alt" style="box-sizing: border-box; border-left: 1px dashed rgb(204, 204, 204); padding-left: 10px; line-height: 28.5px; margin: 0px 35px 0px 50px; list-style: decimal;">
		<span style="box-sizing: border-box; color: black;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tf_example&nbsp;=&nbsp;tf.train.Example(features=features)&nbsp;&nbsp;</span></li>
<li style="box-sizing: border-box; border-left: 1px dashed rgb(204, 204, 204); padding-left: 10px; line-height: 28.5px; margin: 0px 35px 0px 50px; list-style: decimal;">
		<span style="box-sizing: border-box; color: black;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;serialized&nbsp;=&nbsp;tf_example.SerializeToString()&nbsp;&nbsp;</span></li>
<li class="alt" style="box-sizing: border-box; border-left: 1px dashed rgb(204, 204, 204); padding-left: 10px; line-height: 28.5px; margin: 0px 35px 0px 50px; list-style: decimal;">
		<span style="box-sizing: border-box; color: black;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;writer.<span class="builtins" style="box-sizing: border-box; color: rgb(255, 20, 147);">write</span><span style="box-sizing: border-box;">(serialized)&nbsp;&nbsp;</span></span></li>
<li style="box-sizing: border-box; border-left: 1px dashed rgb(204, 204, 204); padding-left: 10px; line-height: 28.5px; margin: 0px 35px 0px 50px; list-style: decimal;">
		<span style="box-sizing: border-box; color: black;">&nbsp;&nbsp;</span></li>
<li class="alt" style="box-sizing: border-box; border-left: 1px dashed rgb(204, 204, 204); padding-left: 10px; line-height: 28.5px; margin: 0px 35px 0px 50px; list-style: decimal;">
		<span style="box-sizing: border-box; color: black;">&nbsp;&nbsp;</span></li>
<li style="box-sizing: border-box; border-left: 1px dashed rgb(204, 204, 204); padding-left: 10px; line-height: 28.5px; margin: 0px 35px 0px 50px; list-style: decimal;">
		<span style="box-sizing: border-box; color: black;"><span class="keyword" style="box-sizing: border-box; color: blue; font-weight: bold;">def</span><span style="box-sizing: border-box;">&nbsp;parse_fn(example_proto):&nbsp;&nbsp;</span></span></li>
<li class="alt" style="box-sizing: border-box; border-left: 1px dashed rgb(204, 204, 204); padding-left: 10px; line-height: 28.5px; margin: 0px 35px 0px 50px; list-style: decimal;">
		<span style="box-sizing: border-box; color: black;">&nbsp;&nbsp;&nbsp;&nbsp;features&nbsp;=&nbsp;{&quot;state&quot;:&nbsp;tf.FixedLenFeature((),&nbsp;tf.<span class="commonlibs" style="box-sizing: border-box; color: rgb(138, 43, 226); font-style: italic;">string</span><span style="box-sizing: border-box;">),&nbsp;&nbsp;</span></span></li>
<li style="box-sizing: border-box; border-left: 1px dashed rgb(204, 204, 204); padding-left: 10px; line-height: 28.5px; margin: 0px 35px 0px 50px; list-style: decimal;">
		<span style="box-sizing: border-box; color: black;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&quot;action&quot;:&nbsp;tf.FixedLenFeature((),&nbsp;tf.int64),&nbsp;&nbsp;</span></li>
<li class="alt" style="box-sizing: border-box; border-left: 1px dashed rgb(204, 204, 204); padding-left: 10px; line-height: 28.5px; margin: 0px 35px 0px 50px; list-style: decimal;">
		<span style="box-sizing: border-box; color: black;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&quot;reward&quot;:&nbsp;tf.FixedLenFeature((),&nbsp;tf.int64)}&nbsp;&nbsp;</span></li>
<li style="box-sizing: border-box; border-left: 1px dashed rgb(204, 204, 204); padding-left: 10px; line-height: 28.5px; margin: 0px 35px 0px 50px; list-style: decimal;">
		<span style="box-sizing: border-box; color: black;">&nbsp;&nbsp;&nbsp;&nbsp;parsed_features&nbsp;=&nbsp;tf.parse_single_example(example_proto,&nbsp;features)&nbsp;&nbsp;</span></li>
<li class="alt" style="box-sizing: border-box; border-left: 1px dashed rgb(204, 204, 204); padding-left: 10px; line-height: 28.5px; margin: 0px 35px 0px 50px; list-style: decimal;">
		<span style="box-sizing: border-box; color: black;">&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword" style="box-sizing: border-box; color: blue; font-weight: bold;">return</span><span style="box-sizing: border-box;">&nbsp;tf.decode_raw(parsed_features[&#39;state&#39;],&nbsp;tf.float32),&nbsp;parsed_features[&#39;action&#39;],&nbsp;parsed_features[&#39;reward&#39;]&nbsp;&nbsp;</span></span></li>
<li style="box-sizing: border-box; border-left: 1px dashed rgb(204, 204, 204); padding-left: 10px; line-height: 28.5px; margin: 0px 35px 0px 50px; list-style: decimal;">
		<span style="box-sizing: border-box; color: black;">&nbsp;&nbsp;</span></li>
<li class="alt" style="box-sizing: border-box; border-left: 1px dashed rgb(204, 204, 204); padding-left: 10px; line-height: 28.5px; margin: 0px 35px 0px 50px; list-style: decimal;">
		<span style="box-sizing: border-box; color: black;">&nbsp;&nbsp;</span></li>
<li style="box-sizing: border-box; border-left: 1px dashed rgb(204, 204, 204); padding-left: 10px; line-height: 28.5px; margin: 0px 35px 0px 50px; list-style: decimal;">
		<span style="box-sizing: border-box; color: black;"><span class="keyword" style="box-sizing: border-box; color: blue; font-weight: bold;">if</span><span style="box-sizing: border-box;">&nbsp;</span><span class="builtins" style="box-sizing: border-box; color: rgb(255, 20, 147);">__name__</span><span style="box-sizing: border-box;">&nbsp;==&nbsp;&#39;</span><span class="builtins" style="box-sizing: border-box; color: rgb(255, 20, 147);">__main__</span><span style="box-sizing: border-box;">&#39;:&nbsp;&nbsp;</span></span></li>
<li class="alt" style="box-sizing: border-box; border-left: 1px dashed rgb(204, 204, 204); padding-left: 10px; line-height: 28.5px; margin: 0px 35px 0px 50px; list-style: decimal;">
		<span style="box-sizing: border-box; color: black;">&nbsp;&nbsp;&nbsp;&nbsp;buffer_s,&nbsp;buffer_a,&nbsp;buffer_r&nbsp;=&nbsp;[],&nbsp;[],&nbsp;[]&nbsp;&nbsp;</span></li>
<li style="box-sizing: border-box; border-left: 1px dashed rgb(204, 204, 204); padding-left: 10px; line-height: 28.5px; margin: 0px 35px 0px 50px; list-style: decimal;">
		<span style="box-sizing: border-box; color: black;">&nbsp;&nbsp;</span></li>
<li class="alt" style="box-sizing: border-box; border-left: 1px dashed rgb(204, 204, 204); padding-left: 10px; line-height: 28.5px; margin: 0px 35px 0px 50px; list-style: decimal;">
		<span style="box-sizing: border-box; color: black;">&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment" style="box-sizing: border-box; color: green;">#&nbsp;随机生成一些数据</span><span style="box-sizing: border-box;">&nbsp;&nbsp;</span></span></li>
<li style="box-sizing: border-box; border-left: 1px dashed rgb(204, 204, 204); padding-left: 10px; line-height: 28.5px; margin: 0px 35px 0px 50px; list-style: decimal;">
		<span style="box-sizing: border-box; color: black;">&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword" style="box-sizing: border-box; color: blue; font-weight: bold;">for</span><span style="box-sizing: border-box;">&nbsp;i&nbsp;</span><span class="keyword" style="box-sizing: border-box; color: blue; font-weight: bold;">in</span><span style="box-sizing: border-box;">&nbsp;</span><span class="builtins" style="box-sizing: border-box; color: rgb(255, 20, 147);">range</span><span style="box-sizing: border-box;">(3):&nbsp;&nbsp;</span></span></li>
<li class="alt" style="box-sizing: border-box; border-left: 1px dashed rgb(204, 204, 204); padding-left: 10px; line-height: 28.5px; margin: 0px 35px 0px 50px; list-style: decimal;">
		<span style="box-sizing: border-box; color: black;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;state&nbsp;=&nbsp;[<span class="builtins" style="box-sizing: border-box; color: rgb(255, 20, 147);">round</span><span style="box-sizing: border-box;">(</span><span class="commonlibs" style="box-sizing: border-box; color: rgb(138, 43, 226); font-style: italic;">random</span><span style="box-sizing: border-box;">.</span><span class="commonlibs" style="box-sizing: border-box; color: rgb(138, 43, 226); font-style: italic;">random</span><span style="box-sizing: border-box;">()&nbsp;*&nbsp;100,&nbsp;2)&nbsp;</span><span class="keyword" style="box-sizing: border-box; color: blue; font-weight: bold;">for</span><span style="box-sizing: border-box;">&nbsp;_&nbsp;</span><span class="keyword" style="box-sizing: border-box; color: blue; font-weight: bold;">in</span><span style="box-sizing: border-box;">&nbsp;</span><span class="builtins" style="box-sizing: border-box; color: rgb(255, 20, 147);">range</span><span style="box-sizing: border-box;">(0,&nbsp;10)]&nbsp;&nbsp;</span><span class="comment" style="box-sizing: border-box; color: green;">#&nbsp;一个数组，里面有10个数，每个都是一个浮点数</span><span style="box-sizing: border-box;">&nbsp;&nbsp;</span></span></li>
<li style="box-sizing: border-box; border-left: 1px dashed rgb(204, 204, 204); padding-left: 10px; line-height: 28.5px; margin: 0px 35px 0px 50px; list-style: decimal;">
		<span style="box-sizing: border-box; color: black;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;action&nbsp;=&nbsp;<span class="commonlibs" style="box-sizing: border-box; color: rgb(138, 43, 226); font-style: italic;">random</span><span style="box-sizing: border-box;">.randrange(0,&nbsp;2)&nbsp;&nbsp;</span><span class="comment" style="box-sizing: border-box; color: green;">#&nbsp;一个数，值为&nbsp;0&nbsp;或&nbsp;1</span><span style="box-sizing: border-box;">&nbsp;&nbsp;</span></span></li>
<li class="alt" style="box-sizing: border-box; border-left: 1px dashed rgb(204, 204, 204); padding-left: 10px; line-height: 28.5px; margin: 0px 35px 0px 50px; list-style: decimal;">
		<span style="box-sizing: border-box; color: black;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;reward&nbsp;=&nbsp;<span class="commonlibs" style="box-sizing: border-box; color: rgb(138, 43, 226); font-style: italic;">random</span><span style="box-sizing: border-box;">.randrange(0,&nbsp;100)&nbsp;&nbsp;</span><span class="comment" style="box-sizing: border-box; color: green;">#&nbsp;一个数，值域&nbsp;[0,&nbsp;100)</span><span style="box-sizing: border-box;">&nbsp;&nbsp;</span></span></li>
<li style="box-sizing: border-box; border-left: 1px dashed rgb(204, 204, 204); padding-left: 10px; line-height: 28.5px; margin: 0px 35px 0px 50px; list-style: decimal;">
		<span style="box-sizing: border-box; color: black;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment" style="box-sizing: border-box; color: green;">#&nbsp;把生成的数分别添加到3个list中</span><span style="box-sizing: border-box;">&nbsp;&nbsp;</span></span></li>
<li class="alt" style="box-sizing: border-box; border-left: 1px dashed rgb(204, 204, 204); padding-left: 10px; line-height: 28.5px; margin: 0px 35px 0px 50px; list-style: decimal;">
		<span style="box-sizing: border-box; color: black;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;buffer_s.<span class="builtins" style="box-sizing: border-box; color: rgb(255, 20, 147);">append</span><span style="box-sizing: border-box;">(state)&nbsp;&nbsp;</span></span></li>
<li style="box-sizing: border-box; border-left: 1px dashed rgb(204, 204, 204); padding-left: 10px; line-height: 28.5px; margin: 0px 35px 0px 50px; list-style: decimal;">
		<span style="box-sizing: border-box; color: black;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;buffer_a.<span class="builtins" style="box-sizing: border-box; color: rgb(255, 20, 147);">append</span><span style="box-sizing: border-box;">(action)&nbsp;&nbsp;</span></span></li>
<li class="alt" style="box-sizing: border-box; border-left: 1px dashed rgb(204, 204, 204); padding-left: 10px; line-height: 28.5px; margin: 0px 35px 0px 50px; list-style: decimal;">
		<span style="box-sizing: border-box; color: black;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;buffer_r.<span class="builtins" style="box-sizing: border-box; color: rgb(255, 20, 147);">append</span><span style="box-sizing: border-box;">(reward)&nbsp;&nbsp;</span></span></li>
<li style="box-sizing: border-box; border-left: 1px dashed rgb(204, 204, 204); padding-left: 10px; line-height: 28.5px; margin: 0px 35px 0px 50px; list-style: decimal;">
		<span style="box-sizing: border-box; color: black;">&nbsp;&nbsp;</span></li>
<li class="alt" style="box-sizing: border-box; border-left: 1px dashed rgb(204, 204, 204); padding-left: 10px; line-height: 28.5px; margin: 0px 35px 0px 50px; list-style: decimal;">
		<span style="box-sizing: border-box; color: black;">&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment" style="box-sizing: border-box; color: green;">#&nbsp;查看生成的数据</span><span style="box-sizing: border-box;">&nbsp;&nbsp;</span></span></li>
<li style="box-sizing: border-box; border-left: 1px dashed rgb(204, 204, 204); padding-left: 10px; line-height: 28.5px; margin: 0px 35px 0px 50px; list-style: decimal;">
		<span style="box-sizing: border-box; color: black;">&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword" style="box-sizing: border-box; color: blue; font-weight: bold;">print</span><span style="box-sizing: border-box;">(buffer_s)&nbsp;&nbsp;</span></span></li>
<li class="alt" style="box-sizing: border-box; border-left: 1px dashed rgb(204, 204, 204); padding-left: 10px; line-height: 28.5px; margin: 0px 35px 0px 50px; list-style: decimal;">
		<span style="box-sizing: border-box; color: black;">&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword" style="box-sizing: border-box; color: blue; font-weight: bold;">print</span><span style="box-sizing: border-box;">(buffer_a)&nbsp;&nbsp;</span></span></li>
<li style="box-sizing: border-box; border-left: 1px dashed rgb(204, 204, 204); padding-left: 10px; line-height: 28.5px; margin: 0px 35px 0px 50px; list-style: decimal;">
		<span style="box-sizing: border-box; color: black;">&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword" style="box-sizing: border-box; color: blue; font-weight: bold;">print</span><span style="box-sizing: border-box;">(buffer_r)&nbsp;&nbsp;</span></span></li>
<li class="alt" style="box-sizing: border-box; border-left: 1px dashed rgb(204, 204, 204); padding-left: 10px; line-height: 28.5px; margin: 0px 35px 0px 50px; list-style: decimal;">
		<span style="box-sizing: border-box; color: black;">&nbsp;&nbsp;</span></li>
<li style="box-sizing: border-box; border-left: 1px dashed rgb(204, 204, 204); padding-left: 10px; line-height: 28.5px; margin: 0px 35px 0px 50px; list-style: decimal;">
		<span style="box-sizing: border-box; color: black;">&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment" style="box-sizing: border-box; color: green;">#&nbsp;在水平方向把各个list堆叠起来，堆叠的结果：得到3个矩阵</span><span style="box-sizing: border-box;">&nbsp;&nbsp;</span></span></li>
<li class="alt" style="box-sizing: border-box; border-left: 1px dashed rgb(204, 204, 204); padding-left: 10px; line-height: 28.5px; margin: 0px 35px 0px 50px; list-style: decimal;">
		<span style="box-sizing: border-box; color: black;">&nbsp;&nbsp;&nbsp;&nbsp;s_stacked&nbsp;=&nbsp;np.vstack(buffer_s)&nbsp;&nbsp;</span></li>
<li style="box-sizing: border-box; border-left: 1px dashed rgb(204, 204, 204); padding-left: 10px; line-height: 28.5px; margin: 0px 35px 0px 50px; list-style: decimal;">
		<span style="box-sizing: border-box; color: black;">&nbsp;&nbsp;&nbsp;&nbsp;a_stacked&nbsp;=&nbsp;np.vstack(buffer_a)&nbsp;&nbsp;</span></li>
<li class="alt" style="box-sizing: border-box; border-left: 1px dashed rgb(204, 204, 204); padding-left: 10px; line-height: 28.5px; margin: 0px 35px 0px 50px; list-style: decimal;">
		<span style="box-sizing: border-box; color: black;">&nbsp;&nbsp;&nbsp;&nbsp;r_stacked&nbsp;=&nbsp;np.vstack(buffer_r)&nbsp;&nbsp;</span></li>
<li style="box-sizing: border-box; border-left: 1px dashed rgb(204, 204, 204); padding-left: 10px; line-height: 28.5px; margin: 0px 35px 0px 50px; list-style: decimal;">
		<span style="box-sizing: border-box; color: black;">&nbsp;&nbsp;</span></li>
<li class="alt" style="box-sizing: border-box; border-left: 1px dashed rgb(204, 204, 204); padding-left: 10px; line-height: 28.5px; margin: 0px 35px 0px 50px; list-style: decimal;">
		<span style="box-sizing: border-box; color: black;">&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword" style="box-sizing: border-box; color: blue; font-weight: bold;">print</span><span style="box-sizing: border-box;">(s_stacked.shape)&nbsp;&nbsp;</span><span class="comment" style="box-sizing: border-box; color: green;">#&nbsp;(3,&nbsp;10)</span><span style="box-sizing: border-box;">&nbsp;&nbsp;</span></span></li>
<li style="box-sizing: border-box; border-left: 1px dashed rgb(204, 204, 204); padding-left: 10px; line-height: 28.5px; margin: 0px 35px 0px 50px; list-style: decimal;">
		<span style="box-sizing: border-box; color: black;">&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword" style="box-sizing: border-box; color: blue; font-weight: bold;">print</span><span style="box-sizing: border-box;">(a_stacked.shape)&nbsp;&nbsp;</span><span class="comment" style="box-sizing: border-box; color: green;">#&nbsp;(3,&nbsp;1)</span><span style="box-sizing: border-box;">&nbsp;&nbsp;</span></span></li>
<li class="alt" style="box-sizing: border-box; border-left: 1px dashed rgb(204, 204, 204); padding-left: 10px; line-height: 28.5px; margin: 0px 35px 0px 50px; list-style: decimal;">
		<span style="box-sizing: border-box; color: black;">&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword" style="box-sizing: border-box; color: blue; font-weight: bold;">print</span><span style="box-sizing: border-box;">(r_stacked.shape)&nbsp;&nbsp;</span><span class="comment" style="box-sizing: border-box; color: green;">#&nbsp;(3,&nbsp;1)</span><span style="box-sizing: border-box;">&nbsp;&nbsp;</span></span></li>
<li style="box-sizing: border-box; border-left: 1px dashed rgb(204, 204, 204); padding-left: 10px; line-height: 28.5px; margin: 0px 35px 0px 50px; list-style: decimal;">
		<span style="box-sizing: border-box; color: black;">&nbsp;&nbsp;</span></li>
<li class="alt" style="box-sizing: border-box; border-left: 1px dashed rgb(204, 204, 204); padding-left: 10px; line-height: 28.5px; margin: 0px 35px 0px 50px; list-style: decimal;">
		<span style="box-sizing: border-box; color: black;">&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment" style="box-sizing: border-box; color: green;">#&nbsp;写入TFRecord文件</span><span style="box-sizing: border-box;">&nbsp;&nbsp;</span></span></li>
<li style="box-sizing: border-box; border-left: 1px dashed rgb(204, 204, 204); padding-left: 10px; line-height: 28.5px; margin: 0px 35px 0px 50px; list-style: decimal;">
		<span style="box-sizing: border-box; color: black;">&nbsp;&nbsp;&nbsp;&nbsp;output_file&nbsp;=&nbsp;&#39;./data.tfrecord&#39;&nbsp;&nbsp;<span class="comment" style="box-sizing: border-box; color: green;">#&nbsp;输出文件的路径</span><span style="box-sizing: border-box;">&nbsp;&nbsp;</span></span></li>
<li class="alt" style="box-sizing: border-box; border-left: 1px dashed rgb(204, 204, 204); padding-left: 10px; line-height: 28.5px; margin: 0px 35px 0px 50px; list-style: decimal;">
		<span style="box-sizing: border-box; color: black;">&nbsp;&nbsp;&nbsp;&nbsp;save_tfrecords(s_stacked,&nbsp;a_stacked,&nbsp;r_stacked,&nbsp;output_file)&nbsp;&nbsp;</span></li>
<li style="box-sizing: border-box; border-left: 1px dashed rgb(204, 204, 204); padding-left: 10px; line-height: 28.5px; margin: 0px 35px 0px 50px; list-style: decimal;">
		<span style="box-sizing: border-box; color: black;">&nbsp;&nbsp;</span></li>
<li class="alt" style="box-sizing: border-box; border-left: 1px dashed rgb(204, 204, 204); padding-left: 10px; line-height: 28.5px; margin: 0px 35px 0px 50px; list-style: decimal;">
		<span style="box-sizing: border-box; color: black;">&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment" style="box-sizing: border-box; color: green;">#&nbsp;读取TFRecord文件并打印出其内容</span><span style="box-sizing: border-box;">&nbsp;&nbsp;</span></span></li>
<li style="box-sizing: border-box; border-left: 1px dashed rgb(204, 204, 204); padding-left: 10px; line-height: 28.5px; margin: 0px 35px 0px 50px; list-style: decimal;">
		<span style="box-sizing: border-box; color: black;">&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword" style="box-sizing: border-box; color: blue; font-weight: bold;">for</span><span style="box-sizing: border-box;">&nbsp;example&nbsp;</span><span class="keyword" style="box-sizing: border-box; color: blue; font-weight: bold;">in</span><span style="box-sizing: border-box;">&nbsp;tf.io.tf_record_iterator(output_file):&nbsp;&nbsp;</span></span></li>
<li class="alt" style="box-sizing: border-box; border-left: 1px dashed rgb(204, 204, 204); padding-left: 10px; line-height: 28.5px; margin: 0px 35px 0px 50px; list-style: decimal;">
		<span style="box-sizing: border-box; color: black;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword" style="box-sizing: border-box; color: blue; font-weight: bold;">print</span><span style="box-sizing: border-box;">(tf.train.Example.FromString(example))&nbsp;&nbsp;</span></span></li>
<li style="box-sizing: border-box; border-left: 1px dashed rgb(204, 204, 204); padding-left: 10px; line-height: 28.5px; margin: 0px 35px 0px 50px; list-style: decimal;">
		<span style="box-sizing: border-box; color: black;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment" style="box-sizing: border-box; color: green;">#&nbsp;或者用下面的方法</span><span style="box-sizing: border-box;">&nbsp;&nbsp;</span></span></li>
<li class="alt" style="box-sizing: border-box; border-left: 1px dashed rgb(204, 204, 204); padding-left: 10px; line-height: 28.5px; margin: 0px 35px 0px 50px; list-style: decimal;">
		<span style="box-sizing: border-box; color: black;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment" style="box-sizing: border-box; color: green;">#&nbsp;from&nbsp;google.protobuf.json_format&nbsp;import&nbsp;MessageToJson</span><span style="box-sizing: border-box;">&nbsp;&nbsp;</span></span></li>
<li style="box-sizing: border-box; border-left: 1px dashed rgb(204, 204, 204); padding-left: 10px; line-height: 28.5px; margin: 0px 35px 0px 50px; list-style: decimal;">
		<span style="box-sizing: border-box; color: black;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment" style="box-sizing: border-box; color: green;">#&nbsp;jsonMessage&nbsp;=&nbsp;MessageToJson(tf.train.Example.FromString(example))</span><span style="box-sizing: border-box;">&nbsp;&nbsp;</span></span></li>
<li class="alt" style="box-sizing: border-box; border-left: 1px dashed rgb(204, 204, 204); padding-left: 10px; line-height: 28.5px; margin: 0px 35px 0px 50px; list-style: decimal;">
		<span style="box-sizing: border-box; color: black;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment" style="box-sizing: border-box; color: green;">#&nbsp;print(jsonMessage)</span><span style="box-sizing: border-box;">&nbsp;&nbsp;</span></span></li>
<li style="box-sizing: border-box; border-left: 1px dashed rgb(204, 204, 204); padding-left: 10px; line-height: 28.5px; margin: 0px 35px 0px 50px; list-style: decimal;">
		<span style="box-sizing: border-box; color: black;">&nbsp;&nbsp;</span></li>
<li class="alt" style="box-sizing: border-box; border-left: 1px dashed rgb(204, 204, 204); padding-left: 10px; line-height: 28.5px; margin: 0px 35px 0px 50px; list-style: decimal;">
		<span style="box-sizing: border-box; color: black;">&nbsp;&nbsp;&nbsp;&nbsp;<span class="comment" style="box-sizing: border-box; color: green;">#&nbsp;读取TFRecord文件并还原成numpy&nbsp;array，再打印出来</span><span style="box-sizing: border-box;">&nbsp;&nbsp;</span></span></li>
<li style="box-sizing: border-box; border-left: 1px dashed rgb(204, 204, 204); padding-left: 10px; line-height: 28.5px; margin: 0px 35px 0px 50px; list-style: decimal;">
		<span style="box-sizing: border-box; color: black;">&nbsp;&nbsp;&nbsp;&nbsp;with&nbsp;tf.Session()&nbsp;as&nbsp;sess:&nbsp;&nbsp;</span></li>
<li class="alt" style="box-sizing: border-box; border-left: 1px dashed rgb(204, 204, 204); padding-left: 10px; line-height: 28.5px; margin: 0px 35px 0px 50px; list-style: decimal;">
		<span style="box-sizing: border-box; color: black;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dataset&nbsp;=&nbsp;tf.data.TFRecordDataset(output_file)&nbsp;&nbsp;<span class="comment" style="box-sizing: border-box; color: green;">#&nbsp;加载TFRecord文件</span><span style="box-sizing: border-box;">&nbsp;&nbsp;</span></span></li>
<li style="box-sizing: border-box; border-left: 1px dashed rgb(204, 204, 204); padding-left: 10px; line-height: 28.5px; margin: 0px 35px 0px 50px; list-style: decimal;">
		<span style="box-sizing: border-box; color: black;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dataset&nbsp;=&nbsp;dataset.<span class="builtins" style="box-sizing: border-box; color: rgb(255, 20, 147);">map</span><span style="box-sizing: border-box;">(parse_fn)&nbsp;&nbsp;</span><span class="comment" style="box-sizing: border-box; color: green;">#&nbsp;解析data到Tensor</span><span style="box-sizing: border-box;">&nbsp;&nbsp;</span></span></li>
<li class="alt" style="box-sizing: border-box; border-left: 1px dashed rgb(204, 204, 204); padding-left: 10px; line-height: 28.5px; margin: 0px 35px 0px 50px; list-style: decimal;">
		<span style="box-sizing: border-box; color: black;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dataset&nbsp;=&nbsp;dataset.repeat(1)&nbsp;&nbsp;<span class="comment" style="box-sizing: border-box; color: green;">#&nbsp;重复N&nbsp;epochs</span><span style="box-sizing: border-box;">&nbsp;&nbsp;</span></span></li>
<li style="box-sizing: border-box; border-left: 1px dashed rgb(204, 204, 204); padding-left: 10px; line-height: 28.5px; margin: 0px 35px 0px 50px; list-style: decimal;">
		<span style="box-sizing: border-box; color: black;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dataset&nbsp;=&nbsp;dataset.batch(3)&nbsp;&nbsp;<span class="comment" style="box-sizing: border-box; color: green;">#&nbsp;batch&nbsp;size</span><span style="box-sizing: border-box;">&nbsp;&nbsp;</span></span></li>
<li class="alt" style="box-sizing: border-box; border-left: 1px dashed rgb(204, 204, 204); padding-left: 10px; line-height: 28.5px; margin: 0px 35px 0px 50px; list-style: decimal;">
		<span style="box-sizing: border-box; color: black;">&nbsp;&nbsp;</span></li>
<li style="box-sizing: border-box; border-left: 1px dashed rgb(204, 204, 204); padding-left: 10px; line-height: 28.5px; margin: 0px 35px 0px 50px; list-style: decimal;">
		<span style="box-sizing: border-box; color: black;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;iterator&nbsp;=&nbsp;dataset.make_one_shot_iterator()&nbsp;&nbsp;</span></li>
<li class="alt" style="box-sizing: border-box; border-left: 1px dashed rgb(204, 204, 204); padding-left: 10px; line-height: 28.5px; margin: 0px 35px 0px 50px; list-style: decimal;">
		<span style="box-sizing: border-box; color: black;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;next_data&nbsp;=&nbsp;iterator.get_next()&nbsp;&nbsp;</span></li>
<li style="box-sizing: border-box; border-left: 1px dashed rgb(204, 204, 204); padding-left: 10px; line-height: 28.5px; margin: 0px 35px 0px 50px; list-style: decimal;">
		<span style="box-sizing: border-box; color: black;">&nbsp;&nbsp;</span></li>
<li class="alt" style="box-sizing: border-box; border-left: 1px dashed rgb(204, 204, 204); padding-left: 10px; line-height: 28.5px; margin: 0px 35px 0px 50px; list-style: decimal;">
		<span style="box-sizing: border-box; color: black;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword" style="box-sizing: border-box; color: blue; font-weight: bold;">while</span><span style="box-sizing: border-box;">&nbsp;</span><span class="builtins" style="box-sizing: border-box; color: rgb(255, 20, 147);">True</span><span style="box-sizing: border-box;">:&nbsp;&nbsp;</span></span></li>
<li style="box-sizing: border-box; border-left: 1px dashed rgb(204, 204, 204); padding-left: 10px; line-height: 28.5px; margin: 0px 35px 0px 50px; list-style: decimal;">
		<span style="box-sizing: border-box; color: black;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword" style="box-sizing: border-box; color: blue; font-weight: bold;">try</span><span style="box-sizing: border-box;">:&nbsp;&nbsp;</span></span></li>
<li class="alt" style="box-sizing: border-box; border-left: 1px dashed rgb(204, 204, 204); padding-left: 10px; line-height: 28.5px; margin: 0px 35px 0px 50px; list-style: decimal;">
		<span style="box-sizing: border-box; color: black;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;state,&nbsp;action,&nbsp;reward&nbsp;=&nbsp;sess.run(next_data)&nbsp;&nbsp;</span></li>
<li style="box-sizing: border-box; border-left: 1px dashed rgb(204, 204, 204); padding-left: 10px; line-height: 28.5px; margin: 0px 35px 0px 50px; list-style: decimal;">
		<span style="box-sizing: border-box; color: black;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword" style="box-sizing: border-box; color: blue; font-weight: bold;">print</span><span style="box-sizing: border-box;">(state)&nbsp;&nbsp;</span></span></li>
<li class="alt" style="box-sizing: border-box; border-left: 1px dashed rgb(204, 204, 204); padding-left: 10px; line-height: 28.5px; margin: 0px 35px 0px 50px; list-style: decimal;">
		<span style="box-sizing: border-box; color: black;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword" style="box-sizing: border-box; color: blue; font-weight: bold;">print</span><span style="box-sizing: border-box;">(action)&nbsp;&nbsp;</span></span></li>
<li style="box-sizing: border-box; border-left: 1px dashed rgb(204, 204, 204); padding-left: 10px; line-height: 28.5px; margin: 0px 35px 0px 50px; list-style: decimal;">
		<span style="box-sizing: border-box; color: black;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword" style="box-sizing: border-box; color: blue; font-weight: bold;">print</span><span style="box-sizing: border-box;">(reward)&nbsp;&nbsp;</span></span></li>
<li class="alt" style="box-sizing: border-box; border-left: 1px dashed rgb(204, 204, 204); padding-left: 10px; line-height: 28.5px; margin: 0px 35px 0px 50px; list-style: decimal;">
		<span style="box-sizing: border-box; color: black;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword" style="box-sizing: border-box; color: blue; font-weight: bold;">except</span><span style="box-sizing: border-box;">&nbsp;tf.errors.OutOfRangeError:&nbsp;&nbsp;</span></span></li>
<li style="box-sizing: border-box; border-left: 1px dashed rgb(204, 204, 204); padding-left: 10px; line-height: 28.5px; margin: 0px 35px 0px 50px; list-style: decimal;">
		<span style="box-sizing: border-box; color: black;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="keyword" style="box-sizing: border-box; color: blue; font-weight: bold;">break</span><span style="box-sizing: border-box;">&nbsp;&nbsp;</span></span></li>
</ol>
<p>
注意：对二维数组，需要用&nbsp;tf.train.BytesList 来保存，还原成numpy array的时候，要用&nbsp;tf.decode_raw() 来解析。<br />
由于生成的数据是随机数，因此你看到的输出会和我不一样。<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 />
生成的数据：</p>
<blockquote>
<div>
		[[56.31, 8.72, 78.21, 44.52, 98.18, 95.23, 85.89, 95.76, 63.96, 41.56], [21.78, 66.52, 17.58, 35.36, 29.25, 63.54, 49.12, 82.71, 77.38, 20.04], [65.86, 78.81, 17.64, 3.21, 60.88, 92.98, 80.63, 92.86, 80.7, 4.12]]</div>
<div>
		[1, 0, 1]</div>
<div>
		[55, 97, 89]</div>
</blockquote>
<p>
numpy数组写成TFRecord后再重新读取出来，并重新转成numpy数组后，数据是：</p>
<blockquote>
<div>
		[[56.31&nbsp; 8.72 78.21 44.52 98.18 95.23 85.89 95.76 63.96 41.56]</div>
<div>
		&nbsp;[21.78 66.52 17.58 35.36 29.25 63.54 49.12 82.71 77.38 20.04]</div>
<div>
		&nbsp;[65.86 78.81 17.64&nbsp; 3.21 60.88 92.98 80.63 92.86 80.7&nbsp; &nbsp;4.12]]</div>
<div>
		[1 0 1]</div>
<div>
		[55 97 89]</div>
</blockquote>
<p>可见数据和生成的一样，这说明上面的程序互相转没有问题。</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-%e4%ba%8c%e7%bb%b4numpy%e6%95%b0%e7%bb%84%e4%bf%9d%e5%ad%98%e5%88%b0tfrecord%e5%b9%b6%e8%af%bb%e5%8f%96%e8%bf%98%e5%8e%9f%e5%9b%9e%e6%9d%a5/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>[原创] tf.tile() 用法举例(TensorFlow)</title>
		<link>https://www.codelast.com/%e5%8e%9f%e5%88%9b-tf-tile-%e7%94%a8%e6%b3%95tensorflow/</link>
					<comments>https://www.codelast.com/%e5%8e%9f%e5%88%9b-tf-tile-%e7%94%a8%e6%b3%95tensorflow/#comments</comments>
		
		<dc:creator><![CDATA[learnhard]]></dc:creator>
		<pubDate>Wed, 11 Sep 2019 03:35:57 +0000</pubDate>
				<category><![CDATA[原创]]></category>
		<category><![CDATA[TensorFlow]]></category>
		<guid isPermaLink="false">https://www.codelast.com/?p=10520</guid>

					<description><![CDATA[<p>
<span style="color:#0000ff;">tile</span> 的含义：</p>
<blockquote>
<div>
		n. 瓦 / 瓷砖 / 软木片 / 亚麻油地毡片</div>
<div>
		v. 用瓦盖 / 贴砖于 / 铺以瓦 / 铺以瓷砖</div>
</blockquote>
<p>函数定义：</p>
<pre style="background-color:#2b2b2b;color:#a9b7c6;font-family:'Droid Sans Mono';font-size:13.5pt;">
<span style="color:#cc7832;">def </span><span style="color:#ffc66d;">tile</span>(input<span style="color:#cc7832;">, </span>multiples<span style="color:#cc7832;">, </span>name=<span style="color:#cc7832;">None</span>):</pre>
<p>函数功能：</p>
<blockquote>
<p>
		Constructs a tensor by tiling a given tensor.</p>
</blockquote>
<p>即：通过&#8220;平铺&#8221;一个给定的 tensor 来构造一个新的 tensor。用人话讲就是：把输入的 tensor，在指定的维度上复制N遍（就像铺瓷砖一样），来创建出一个新的 tensor。<br />
3个参数：<br />
<span style="color:#800000;">input：输入的tensor<br />
multiples：在指定的维度上复制原tensor的次数<br />
name：operation的名字</span></p>
<p><span id="more-10520"></span><br />
下面举例：</p>
<pre style="background-color:#2b2b2b;color:#a9b7c6;font-family:'Droid Sans Mono';font-size:13.5pt;">
<span style="color:#cc7832;">import </span>tensorflow <span style="color:#cc7832;">as </span>tf

<span style="color:#cc7832;">with </span>tf.Session()</pre>&#8230; <a href="https://www.codelast.com/%e5%8e%9f%e5%88%9b-tf-tile-%e7%94%a8%e6%b3%95tensorflow/" class="read-more">Read More </a>]]></description>
										<content:encoded><![CDATA[<p>
<span style="color:#0000ff;">tile</span> 的含义：</p>
<blockquote>
<div>
		n. 瓦 / 瓷砖 / 软木片 / 亚麻油地毡片</div>
<div>
		v. 用瓦盖 / 贴砖于 / 铺以瓦 / 铺以瓷砖</div>
</blockquote>
<p>函数定义：</p>
<pre style="background-color:#2b2b2b;color:#a9b7c6;font-family:'Droid Sans Mono';font-size:13.5pt;">
<span style="color:#cc7832;">def </span><span style="color:#ffc66d;">tile</span>(input<span style="color:#cc7832;">, </span>multiples<span style="color:#cc7832;">, </span>name=<span style="color:#cc7832;">None</span>):</pre>
<p>函数功能：</p>
<blockquote>
<p>
		Constructs a tensor by tiling a given tensor.</p>
</blockquote>
<p>即：通过&ldquo;平铺&rdquo;一个给定的 tensor 来构造一个新的 tensor。用人话讲就是：把输入的 tensor，在指定的维度上复制N遍（就像铺瓷砖一样），来创建出一个新的 tensor。<br />
3个参数：<br />
<span style="color:#800000;">input：输入的tensor<br />
multiples：在指定的维度上复制原tensor的次数<br />
name：operation的名字</span></p>
<p><span id="more-10520"></span><br />
下面举例：</p>
<pre style="background-color:#2b2b2b;color:#a9b7c6;font-family:'Droid Sans Mono';font-size:13.5pt;">
<span style="color:#cc7832;">import </span>tensorflow <span style="color:#cc7832;">as </span>tf

<span style="color:#cc7832;">with </span>tf.Session() <span style="color:#cc7832;">as </span>sess:
    a = tf.constant([[<span style="color:#6897bb;">15</span><span style="color:#cc7832;">, </span><span style="color:#6897bb;">16</span>]<span style="color:#cc7832;">, </span>[<span style="color:#6897bb;">17</span><span style="color:#cc7832;">, </span><span style="color:#6897bb;">18</span>]])
    b = tf.tile(a<span style="color:#cc7832;">, </span>[<span style="color:#6897bb;">1</span><span style="color:#cc7832;">, </span><span style="color:#6897bb;">3</span>])
    c = tf.tile(a<span style="color:#cc7832;">, </span>[<span style="color:#6897bb;">3</span><span style="color:#cc7832;">, </span><span style="color:#6897bb;">2</span>])
    <span style="color:#8888c6;">print</span>(<span style="color:#6a8759;">&#39;------------------------------------&#39;</span>)
    <span style="color:#8888c6;">print</span>(sess.run(a))
    <span style="color:#8888c6;">print</span>(<span style="color:#6a8759;">&#39;------------------------------------&#39;</span>)
    <span style="color:#8888c6;">print</span>(sess.run(b))
    <span style="color:#8888c6;">print</span>(<span style="color:#6a8759;">&#39;------------------------------------&#39;</span>)
    <span style="color:#8888c6;">print</span>(sess.run(c))</pre>
<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 />
输出：</p>
<blockquote>
<div>
		[[15 16]</div>
<div>
		&nbsp;[17 18]]</div>
<div>
		------------------------------------</div>
<div>
		[[15 16 15 16 15 16]</div>
<div>
		&nbsp;[17 18 17 18 17 18]]</div>
<div>
		------------------------------------</div>
<div>
		[[15 16 15 16]</div>
<div>
		&nbsp;[17 18 17 18]</div>
<div>
		&nbsp;[15 16 15 16]</div>
<div>
		&nbsp;[17 18 17 18]</div>
<div>
		&nbsp;[15 16 15 16]</div>
<div>
		&nbsp;[17 18 17 18]]</div>
</blockquote>
<div>
	解释一下：<br />
	输入的 a 是一个 2x2 的矩阵，tf.tile(a, [1, 3]) 里的 [1, 3] 表示在第一个维度上把输入的tensor重复1遍，在第二个维度上把输入的tensor重复3遍。在本例中，第一个维度就是行，第二个维度就是列，因此 b 就变成了 2x6 的矩阵。<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 />
	注意：tf.tile() 里的第2个参数，例如 [1, 3]，里面有两个元素，它必须与输入的 tensor 的维度一样（2维），如果输入的 tensor 是3维的，那么 tf.tile() 的第2个参数里也必须有3个元素，例如 [2, 3, 5]，否则会报类似于下面的错：</div>
<blockquote>
<p>
		ValueError: Shape must be rank 3 but is rank 1 for &#39;Tile_1&#39; (op: &#39;Tile&#39;) with input shapes</p>
</blockquote>
<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-tf-tile-%e7%94%a8%e6%b3%95tensorflow/feed/</wfw:commentRss>
			<slash:comments>2</slash:comments>
		
		
			</item>
		<item>
		<title>[原创] 在树莓派上跑起来TensorBoard</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%e8%b7%91%e8%b5%b7%e6%9d%a5tensorboard/</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%e8%b7%91%e8%b5%b7%e6%9d%a5tensorboard/#respond</comments>
		
		<dc:creator><![CDATA[learnhard]]></dc:creator>
		<pubDate>Sun, 19 Mar 2017 08:34:29 +0000</pubDate>
				<category><![CDATA[Raspberry Pi/树莓派]]></category>
		<category><![CDATA[原创]]></category>
		<category><![CDATA[Raspberry Pi]]></category>
		<category><![CDATA[TensorBoard]]></category>
		<category><![CDATA[TensorFlow]]></category>
		<category><![CDATA[树莓派]]></category>
		<guid isPermaLink="false">http://www.codelast.com/?p=9249</guid>

					<description><![CDATA[<p>
本文软硬件环境：<br />
树莓派：3代 Model B V1.2<br />
OS：Arch Linux ARM，32bit</p>
<p><a href="https://www.tensorflow.org/get_started/summaries_and_tensorboard" rel="noopener noreferrer" target="_blank"><span style="background-color:#ffa07a;">TensorBoard</span></a>是Tensorflow的可视化工具。当我们用<a href="http://www.codelast.com/?p=8941" rel="noopener noreferrer" target="_blank"><span style="background-color:#ffa07a;">这篇</span></a>文章里的方法在树莓派上安装好Tensorflow之后，TensorBoard自然就装好了。于是，下面只剩下怎么启动它的问题。<br />
以下是一个例子。<br />
<span id="more-9249"></span><br />
首先，我们从TensorFlow的官方Github里下载这个文件：</p>
<blockquote>
<p>
		https://github.com/tensorflow/tensorflow/blob/master/tensorflow/examples/tutorials/mnist/mnist_with_summaries.py</p>
</blockquote>
<p>它实现了一个模型训练的过程，并且在模型训练的过程中，将TensorBoard所需的数据文件，输出到了&#160;/tmp/tensorflow/mnist/logs/mnist_with_summaries 这个目录下（写死在 mnist_with_summaries.py 文件中，为了简单无需修改）。<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 />
之后我们把这个模型训练的程序跑起来：</p>
<blockquote>
<p>
		python2 mnist_with_summaries.py</p>
</blockquote>
<p>然后程序会自动去下载MNIST数据，并开始训练模型：</p>
<blockquote>
<div>
		Successfully downloaded train-images-idx3-ubyte.gz 9912422 bytes.</div>
<div>
		Extracting /tmp/tensorflow/mnist/input_data/train-images-idx3-ubyte.gz</div>
<div>
		Successfully downloaded train-labels-idx1-ubyte.gz 28881 bytes.</div>
<div>
		Extracting /tmp/tensorflow/mnist/input_data/train-labels-idx1-ubyte.gz</div>
<div>
		Successfully downloaded t10k-images-idx3-ubyte.gz</div></blockquote>&#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%e8%b7%91%e8%b5%b7%e6%9d%a5tensorboard/" class="read-more">Read More </a>]]></description>
										<content:encoded><![CDATA[<p>
本文软硬件环境：<br />
树莓派：3代 Model B V1.2<br />
OS：Arch Linux ARM，32bit</p>
<p><a href="https://www.tensorflow.org/get_started/summaries_and_tensorboard" rel="noopener noreferrer" target="_blank"><span style="background-color:#ffa07a;">TensorBoard</span></a>是Tensorflow的可视化工具。当我们用<a href="http://www.codelast.com/?p=8941" rel="noopener noreferrer" target="_blank"><span style="background-color:#ffa07a;">这篇</span></a>文章里的方法在树莓派上安装好Tensorflow之后，TensorBoard自然就装好了。于是，下面只剩下怎么启动它的问题。<br />
以下是一个例子。<br />
<span id="more-9249"></span><br />
首先，我们从TensorFlow的官方Github里下载这个文件：</p>
<blockquote>
<p>
		https://github.com/tensorflow/tensorflow/blob/master/tensorflow/examples/tutorials/mnist/mnist_with_summaries.py</p>
</blockquote>
<p>它实现了一个模型训练的过程，并且在模型训练的过程中，将TensorBoard所需的数据文件，输出到了&nbsp;/tmp/tensorflow/mnist/logs/mnist_with_summaries 这个目录下（写死在 mnist_with_summaries.py 文件中，为了简单无需修改）。<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 />
之后我们把这个模型训练的程序跑起来：</p>
<blockquote>
<p>
		python2 mnist_with_summaries.py</p>
</blockquote>
<p>然后程序会自动去下载MNIST数据，并开始训练模型：</p>
<blockquote>
<div>
		Successfully downloaded train-images-idx3-ubyte.gz 9912422 bytes.</div>
<div>
		Extracting /tmp/tensorflow/mnist/input_data/train-images-idx3-ubyte.gz</div>
<div>
		Successfully downloaded train-labels-idx1-ubyte.gz 28881 bytes.</div>
<div>
		Extracting /tmp/tensorflow/mnist/input_data/train-labels-idx1-ubyte.gz</div>
<div>
		Successfully downloaded t10k-images-idx3-ubyte.gz 1648877 bytes.</div>
<div>
		Extracting /tmp/tensorflow/mnist/input_data/t10k-images-idx3-ubyte.gz</div>
<div>
		Successfully downloaded t10k-labels-idx1-ubyte.gz 4542 bytes.</div>
<div>
		Extracting /tmp/tensorflow/mnist/input_data/t10k-labels-idx1-ubyte.gz</div>
<div>
		Accuracy at step 0: 0.1114</div>
<div>
		Accuracy at step 10: 0.6652</div>
<div>
		Accuracy at step 20: 0.8119</div>
<div>
		Accuracy at step 30: 0.8533</div>
<div>
		Accuracy at step 40: 0.868</div>
<div>
		Accuracy at step 50: 0.8723</div>
<div>
		Accuracy at step 60: 0.8779</div>
<div>
		Accuracy at step 70: 0.879</div>
<div>
		Accuracy at step 80: 0.8817</div>
<div>
		Accuracy at step 90: 0.8876</div>
<div>
		Adding run metadata for 99</div>
<div>
		Accuracy at step 100: 0.896</div>
<div>
		Accuracy at step 110: 0.9061</div>
<div>
		Accuracy at step 120: 0.9116</div>
<div>
		Accuracy at step 130: 0.9167</div>
<div>
		Accuracy at step 140: 0.9216</div>
<div>
		Accuracy at step 150: 0.915</div>
<div>
		Accuracy at step 160: 0.9246</div>
<div>
		Accuracy at step 170: 0.9267</div>
<div>
		Accuracy at step 180: 0.9215</div>
<div>
		Accuracy at step 190: 0.9207</div>
<div>
		Adding run metadata for 199</div>
<div>
		Accuracy at step 200: 0.9287</div>
<div>
		Accuracy at step 210: 0.933</div>
<div>
		Accuracy at step 220: 0.9307</div>
<div>
		Accuracy at step 230: 0.9308</div>
<div>
		......</div>
</blockquote>
<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 />
	在这个漫长的过程中，我们可以去启动TensorBoard了：</div>
<blockquote>
<div>
		tensorboard --logdir=/tmp/tensorflow/mnist/logs/mnist_with_summaries</div>
</blockquote>
<div>
	其中，--logdir参数指定的目录就是&nbsp;mnist_with_summaries.py 程序里用&nbsp;--log_dir 参数指定的那个路径，TensorBoard会从这个路径下读取数据并可视化展示在web页面中。<br />
	过一会就会看到命令行提示：</p>
<blockquote>
<div>
			Starting TensorBoard 41 on port 6006</div>
<div>
			(You can navigate to http://10.0.0.2:6006)</div>
</blockquote>
<div>
		所以现在打开浏览器，访问这个地址，就可以看到图了：<br />
		<a href="http://www.codelast.com" rel="noopener noreferrer" target="_blank"><img decoding="async" alt="tensorboard exmaple scalars" src="https://www.codelast.com/wp-content/uploads/ckfinder/images/tensorboard_scalars.png" style="width: 600px; height: 657px;" /></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 />
		<a href="http://www.codelast.com" rel="noopener noreferrer" target="_blank"><img decoding="async" alt="tensorboard example graph" src="https://www.codelast.com/wp-content/uploads/ckfinder/images/tensorboard_graph.png" style="width: 600px; height: 466px;" /></a></p>
<p>		&nbsp;</p></div>
</div>
<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%e8%b7%91%e8%b5%b7%e6%9d%a5tensorboard/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>[原创] 加快TensorFlow在树莓派上的执行速度——服务常驻内存</title>
		<link>https://www.codelast.com/%e5%8e%9f%e5%88%9b-%e5%8a%a0%e5%bf%abtensorflow%e5%9c%a8%e6%a0%91%e8%8e%93%e6%b4%be%e4%b8%8a%e7%9a%84%e6%89%a7%e8%a1%8c%e9%80%9f%e5%ba%a6-%e6%9c%8d%e5%8a%a1%e5%b8%b8%e9%a9%bb/</link>
					<comments>https://www.codelast.com/%e5%8e%9f%e5%88%9b-%e5%8a%a0%e5%bf%abtensorflow%e5%9c%a8%e6%a0%91%e8%8e%93%e6%b4%be%e4%b8%8a%e7%9a%84%e6%89%a7%e8%a1%8c%e9%80%9f%e5%ba%a6-%e6%9c%8d%e5%8a%a1%e5%b8%b8%e9%a9%bb/#respond</comments>
		
		<dc:creator><![CDATA[learnhard]]></dc:creator>
		<pubDate>Sun, 11 Dec 2016 06:19:30 +0000</pubDate>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Raspberry Pi/树莓派]]></category>
		<category><![CDATA[原创]]></category>
		<category><![CDATA[deep learning]]></category>
		<category><![CDATA[Raspberry Pi]]></category>
		<category><![CDATA[TensorFlow]]></category>
		<category><![CDATA[树莓派]]></category>
		<category><![CDATA[模型]]></category>
		<category><![CDATA[深度学习]]></category>
		<guid isPermaLink="false">http://www.codelast.com/?p=8995</guid>

					<description><![CDATA[<p>
本文软硬件环境：<br />
树莓派：3代 Model B V1.2，内存1GB<br />
OS：Arch Linux ARM</p>
<p>在<a href="http://www.codelast.com/?p=8984" rel="noopener noreferrer" target="_blank"><span style="background-color: rgb(255, 160, 122);">上一篇文章</span></a>中，我尝试了加快TensorFlow预测速度的一个方法&#8212;&#8212;模型&#8220;预热&#8221;，实验证明它非常有效，但那仍然没有解决一个问题：每次运行程序，都要加载一次模型，然后预热N次，这个过程非常耗时，因此减少这部分时间也是非常关键的。把TensorFlow做成一个常驻内存的服务就可以解决这个问题。<br />
解决这个问题的正确姿势是：TensorFlow已经提供了一个叫作 <a href="https://github.com/tensorflow/serving" rel="noopener noreferrer" target="_blank"><span style="background-color:#ffa07a;">TensorFlow Serving</span></a> 的library来实现这个需求。但麻烦的是，在树莓派上编译TensorFlow Serving会遇到很多问题，所以，在没有人搞出在树莓派上一键安装的Python wheel包之前，还是算了吧...<br />
因此，下面我用一个很挫的办法来实现一个简陋的TensorFlow service。<br />
<span id="more-8995"></span><br />
<span style="background-color:#00ff00;">『1』</span>简单的思路<br />
服务端，用Python实现一个web server，处理HTTP Get请求，通过解析URL中的参数（例如 <span style="color:#b22222;">http://127.0.0.1:8080/?image_path=/root/abc.jpg</span> 中的 <span style="color:#0000ff;">/root/abc.jpg</span>），来获取待处理的图片的路径，处理完之后，把处理结果返回给客户端。<br />
客户端，通过 curl 命令就可以调用服务了，例如：</p>
<blockquote>
<p>
		curl&#160;http://127.0.0.1:8080/?image_path=/root/abc.jpg</p>
</blockquote>
<p>这表示让服务端处理 /root/abc.jpg 这个文件。<br />
当然，为了简单，这里的设定有一个局限：server和client都在同一个树莓派上，如果要跨机器，那么需要client把图片post到server端，server取出来之后再处理，但本文不涉及这种情况。<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;">『2』</span>简单Python web server的实现<br />
不考虑什么并发，多线程之类的情况，我们可以用非常简单的一点代码就实现一个Python web server。</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: &#34;Helvetica Neue&#34;, Helvetica, &#34;Hiragino Sans GB&#34;, &#34;Microsoft YaHei&#34;, Arial, sans-serif;">
<pre style="font-size: inherit; color: inherit; line-height: inherit; margin-top: 0px; margin-bottom: 0px; padding: 0px;">
<code class="python language-python 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-comment" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(128, 128, 128); word-wrap: inherit !important; word-break: inherit !important;">#!/usr/bin/python3.5</span></code></pre>&#8230; <a href="https://www.codelast.com/%e5%8e%9f%e5%88%9b-%e5%8a%a0%e5%bf%abtensorflow%e5%9c%a8%e6%a0%91%e8%8e%93%e6%b4%be%e4%b8%8a%e7%9a%84%e6%89%a7%e8%a1%8c%e9%80%9f%e5%ba%a6-%e6%9c%8d%e5%8a%a1%e5%b8%b8%e9%a9%bb/" class="read-more">Read More </a></section>]]></description>
										<content:encoded><![CDATA[<p>
本文软硬件环境：<br />
树莓派：3代 Model B V1.2，内存1GB<br />
OS：Arch Linux ARM</p>
<p>在<a href="http://www.codelast.com/?p=8984" rel="noopener noreferrer" target="_blank"><span style="background-color: rgb(255, 160, 122);">上一篇文章</span></a>中，我尝试了加快TensorFlow预测速度的一个方法&mdash;&mdash;模型&ldquo;预热&rdquo;，实验证明它非常有效，但那仍然没有解决一个问题：每次运行程序，都要加载一次模型，然后预热N次，这个过程非常耗时，因此减少这部分时间也是非常关键的。把TensorFlow做成一个常驻内存的服务就可以解决这个问题。<br />
解决这个问题的正确姿势是：TensorFlow已经提供了一个叫作 <a href="https://github.com/tensorflow/serving" rel="noopener noreferrer" target="_blank"><span style="background-color:#ffa07a;">TensorFlow Serving</span></a> 的library来实现这个需求。但麻烦的是，在树莓派上编译TensorFlow Serving会遇到很多问题，所以，在没有人搞出在树莓派上一键安装的Python wheel包之前，还是算了吧...<br />
因此，下面我用一个很挫的办法来实现一个简陋的TensorFlow service。<br />
<span id="more-8995"></span><br />
<span style="background-color:#00ff00;">『1』</span>简单的思路<br />
服务端，用Python实现一个web server，处理HTTP Get请求，通过解析URL中的参数（例如 <span style="color:#b22222;">http://127.0.0.1:8080/?image_path=/root/abc.jpg</span> 中的 <span style="color:#0000ff;">/root/abc.jpg</span>），来获取待处理的图片的路径，处理完之后，把处理结果返回给客户端。<br />
客户端，通过 curl 命令就可以调用服务了，例如：</p>
<blockquote>
<p>
		curl&nbsp;http://127.0.0.1:8080/?image_path=/root/abc.jpg</p>
</blockquote>
<p>这表示让服务端处理 /root/abc.jpg 这个文件。<br />
当然，为了简单，这里的设定有一个局限：server和client都在同一个树莓派上，如果要跨机器，那么需要client把图片post到server端，server取出来之后再处理，但本文不涉及这种情况。<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;">『2』</span>简单Python web server的实现<br />
不考虑什么并发，多线程之类的情况，我们可以用非常简单的一点代码就实现一个Python web server。</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="python language-python 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-comment" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(128, 128, 128); word-wrap: inherit !important; word-break: inherit !important;">#!/usr/bin/python3.5
</span>

<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">from</span>&nbsp;http.server&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">import</span>&nbsp;BaseHTTPRequestHandler,&nbsp;HTTPServer
<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">from</span>&nbsp;urllib.parse&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">import</span>&nbsp;parse_qsl

<span class="hljs-class" style="font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px; word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">class</span>&nbsp;<span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(165, 218, 45); word-wrap: inherit !important; word-break: inherit !important;">MyRequestHandler</span><span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(255, 152, 35); word-wrap: inherit !important; word-break: inherit !important;">(BaseHTTPRequestHandler)</span>:</span>
&nbsp;&nbsp;<span class="hljs-function" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; overflow-wrap: inherit !important; word-break: inherit !important;">def</span>&nbsp;<span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(165, 218, 45); word-wrap: inherit !important; word-break: inherit !important;">do_GET</span><span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(255, 152, 35); word-wrap: inherit !important; word-break: inherit !important;">(self)</span>:</span>
&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(128, 128, 128); word-wrap: inherit !important; word-break: inherit !important;">#&nbsp;e.g.&nbsp;&quot;/?image_path=/root/mobike.jpg&quot;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>
&nbsp;&nbsp;&nbsp;&nbsp;path&nbsp;=&nbsp;self.path
&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(128, 128, 128); word-wrap: inherit !important; word-break: inherit !important;">#&nbsp;e.g.&nbsp;&quot;/root/mobike.jpg&quot;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>
&nbsp;&nbsp;&nbsp;&nbsp;image_path&nbsp;=&nbsp;parse_qsl(path[<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(174, 135, 250); word-wrap: inherit !important; word-break: inherit !important;">2</span>:])[<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(174, 135, 250); word-wrap: inherit !important; word-break: inherit !important;">0</span>][<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(174, 135, 250); word-wrap: inherit !important; word-break: inherit !important;">1</span>]&nbsp;+&nbsp;<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&#39;\n&#39;</span>

&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(128, 128, 128); word-wrap: inherit !important; word-break: inherit !important;">#&nbsp;send&nbsp;response&nbsp;status&nbsp;code&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>
&nbsp;&nbsp;&nbsp;&nbsp;self.send_response(<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(174, 135, 250); word-wrap: inherit !important; word-break: inherit !important;">200</span>)

&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(128, 128, 128); word-wrap: inherit !important; word-break: inherit !important;">#&nbsp;send&nbsp;headers&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>
&nbsp;&nbsp;&nbsp;&nbsp;self.send_header(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&#39;Content-type&#39;</span>,<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&#39;text/html&#39;</span>)
&nbsp;&nbsp;&nbsp;&nbsp;self.end_headers()

&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(128, 128, 128); word-wrap: inherit !important; word-break: inherit !important;">#&nbsp;send&nbsp;message&nbsp;back&nbsp;to&nbsp;client,&nbsp;write&nbsp;content&nbsp;as&nbsp;utf-8&nbsp;data&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>
&nbsp;&nbsp;&nbsp;&nbsp;self.wfile.write(bytes(image_path,&nbsp;<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;utf8&quot;</span>))
&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">return</span>

<span class="hljs-function" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; overflow-wrap: inherit !important; word-break: inherit !important;">def</span>&nbsp;<span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(165, 218, 45); word-wrap: inherit !important; word-break: inherit !important;">start_web_server</span><span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(255, 152, 35); word-wrap: inherit !important; word-break: inherit !important;">()</span>:</span>
&nbsp;&nbsp;print(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&#39;Starting&nbsp;web&nbsp;server...&#39;</span>)

&nbsp;&nbsp;server_address&nbsp;=&nbsp;(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&#39;127.0.0.1&#39;</span>,&nbsp;<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(174, 135, 250); word-wrap: inherit !important; word-break: inherit !important;">8080</span>)
&nbsp;&nbsp;httpd&nbsp;=&nbsp;HTTPServer(server_address,&nbsp;MyRequestHandler)
&nbsp;&nbsp;httpd.serve_forever()


start_web_server()
</code></pre>
</section>
<p>其中，在&nbsp;MyRequestHandler 这个类中，我们会处理每一个客户端的请求，这里只是把从URL中解析到的图片文件路径简单地发送回客户端。<br />
此代码可在我的<a href="https://github.com/codelast/raspberry-pi/tree/master/ai/tensorflow-related/bin/simple-python-web-server-example" rel="noopener noreferrer" target="_blank"><span style="background-color:#ffa07a;">GitHub</span></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 />
<span style="background-color:#00ff00;">『3』</span>TensorFlow服务测试<br />
按&ldquo;模型加载1次，预热10次&rdquo;的原则，再结合上面的简单web server代码，我们可以很容易地把TensorFlow做成服务。具体代码比较长，这里就不粘贴上来了，在我的<a href="https://github.com/codelast/raspberry-pi/tree/master/ai/tensorflow-related/bin/tensorflow-service" rel="noopener noreferrer" target="_blank"><span style="background-color:#ffa07a;">GitHub</span></a>可以下载到。</p>
<p>下面测试一下效果。<br />
把service启动起来：</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;">./run-tensorflow-service.sh
</code></pre>
</section>
<p>经历几十秒的漫长等待之后，模型加载&amp;预热就完成了（命令行输出会有提示），此时，我们再<span style="color:#0000ff;">在同一台树莓派上</span>运行client，向server发送一个处理图片的请求：</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;">./client.sh&nbsp;/root/raspberry-pi/ai/tensorflow-related/resource/<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>大概4秒多之后，client端会打印出如下信息：</p>
<blockquote>
<div>
		mountain bike, all-terrain bike, off-roader (score = 0.56671)</div>
<div>
		tricycle, trike, velocipede (score = 0.12035)</div>
<div>
		bicycle-built-for-two, tandem bicycle, tandem (score = 0.08768)</div>
<div>
		lawn mower, mower (score = 0.00651)</div>
<div>
		alp (score = 0.00387)</div>
<div>
		<span style="color:#ff0000;">Prediction used time:4.171393394470215 Seconds</span></div>
</blockquote>
<div>
	换一张大小相仿的图片来测试，消耗的时间也是差不多的，达到了我们预期的效果。至此，它距离&ldquo;实用&rdquo;又更近了一步。</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>
</div>
]]></content:encoded>
					
					<wfw:commentRss>https://www.codelast.com/%e5%8e%9f%e5%88%9b-%e5%8a%a0%e5%bf%abtensorflow%e5%9c%a8%e6%a0%91%e8%8e%93%e6%b4%be%e4%b8%8a%e7%9a%84%e6%89%a7%e8%a1%8c%e9%80%9f%e5%ba%a6-%e6%9c%8d%e5%8a%a1%e5%b8%b8%e9%a9%bb/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>[原创] 加快TensorFlow在树莓派上的执行速度——模型预热</title>
		<link>https://www.codelast.com/%e5%8e%9f%e5%88%9b-%e5%8a%a0%e5%bf%abtensorflow%e5%9c%a8%e6%a0%91%e8%8e%93%e6%b4%be%e4%b8%8a%e7%9a%84%e6%89%a7%e8%a1%8c%e9%80%9f%e5%ba%a6-%e6%a8%a1%e5%9e%8b%e9%a2%84%e7%83%ad/</link>
					<comments>https://www.codelast.com/%e5%8e%9f%e5%88%9b-%e5%8a%a0%e5%bf%abtensorflow%e5%9c%a8%e6%a0%91%e8%8e%93%e6%b4%be%e4%b8%8a%e7%9a%84%e6%89%a7%e8%a1%8c%e9%80%9f%e5%ba%a6-%e6%a8%a1%e5%9e%8b%e9%a2%84%e7%83%ad/#respond</comments>
		
		<dc:creator><![CDATA[learnhard]]></dc:creator>
		<pubDate>Sat, 10 Dec 2016 04:42:08 +0000</pubDate>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Raspberry Pi/树莓派]]></category>
		<category><![CDATA[原创]]></category>
		<category><![CDATA[Raspberry Pi]]></category>
		<category><![CDATA[TensorFlow]]></category>
		<category><![CDATA[warm-up]]></category>
		<category><![CDATA[树莓派]]></category>
		<category><![CDATA[模型]]></category>
		<category><![CDATA[深度学习]]></category>
		<category><![CDATA[预热]]></category>
		<guid isPermaLink="false">http://www.codelast.com/?p=8984</guid>

					<description><![CDATA[<p>
本文软硬件环境：<br />
树莓派：3代 Model B V1.2，内存1GB<br />
OS：Arch Linux ARM</p>
<p>在<a href="http://www.codelast.com/?p=8941" rel="noopener noreferrer" target="_blank"><span style="background-color:#ffa07a;">上一篇文章</span></a>中，我写了在树莓派上用TensorFlow做的一个深度学习（图像识别）实验，但正如文中所说，50秒执行一次预测的实用性为0。因此，有必要采取一些措施来加快TensorFlow的执行速度，其中一个可行的方法就是&#8220;预热&#8221;（warm-up），把TensorFlow移植到树莓派上的作者Sam Abrahams已经比较详细地在GitHub上列出了<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>。依照作者的描述，我也测试了一下，看看那悲催的50秒时间能减少到多少秒。<br />
<span id="more-8984"></span><br />
<span style="background-color:#00ff00;">『1』</span>什么是预热（warm-up）<br />
首先，本文还是对TensorFlow的Python图像分类程序 <span style="color:#ff0000;">classify_image.py</span> 来描述的。<br />
预热就是指在真正执行一次预测之前，先执行若干次 <span style="color:#0000ff;">Session.run()&#160;</span>方法，从而达到加快一次预测的执行速度的目的。<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;">『2』</span>代码修改<br />
代码改起来其实很简单。为了能衡量程序运行时间，需要使用Python的time模块，因此在一开始需要import：</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: &#34;Helvetica Neue&#34;, Helvetica, &#34;Hiragino Sans GB&#34;, &#34;Microsoft YaHei&#34;, Arial, sans-serif;">
<pre style="font-size: inherit; color: inherit; line-height: inherit; margin-top: 0px; margin-bottom: 0px; padding: 0px;">
<code class="python language-python 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-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">import</span>&#160;time
</code></pre>
</section>
<p>然后对 <span style="color:#0000ff;">run_inference_on_image</span> 方法做一些修改，如下：</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: &#34;Helvetica Neue&#34;, Helvetica, &#34;Hiragino Sans GB&#34;, &#34;Microsoft YaHei&#34;, Arial, sans-serif;">
<pre style="font-size: inherit; color: inherit; line-height: inherit; margin-top: 0px; margin-bottom: 0px; padding: 0px;">
<code class="python language-python 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-function" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; overflow-wrap: inherit !important; word-break: inherit !important;">def</span>&#160;<span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(165, 218, 45); word-wrap: inherit !important; word-break: inherit !important;">run_inference_on_image</span><span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(255, 152, 35); word-wrap: inherit !important; word-break: inherit !important;">(image)</span>:</span>
&#160;&#160;<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&#34;&#34;&#34;Runs&#160;inference&#160;on&#160;an&#160;image.

&#160;&#160;Args:
&#160;&#160;&#160;&#160;image:&#160;Image&#160;file&#160;name.</span></code></pre>&#8230; <a href="https://www.codelast.com/%e5%8e%9f%e5%88%9b-%e5%8a%a0%e5%bf%abtensorflow%e5%9c%a8%e6%a0%91%e8%8e%93%e6%b4%be%e4%b8%8a%e7%9a%84%e6%89%a7%e8%a1%8c%e9%80%9f%e5%ba%a6-%e6%a8%a1%e5%9e%8b%e9%a2%84%e7%83%ad/" class="read-more">Read More </a></section>]]></description>
										<content:encoded><![CDATA[<p>
本文软硬件环境：<br />
树莓派：3代 Model B V1.2，内存1GB<br />
OS：Arch Linux ARM</p>
<p>在<a href="http://www.codelast.com/?p=8941" rel="noopener noreferrer" target="_blank"><span style="background-color:#ffa07a;">上一篇文章</span></a>中，我写了在树莓派上用TensorFlow做的一个深度学习（图像识别）实验，但正如文中所说，50秒执行一次预测的实用性为0。因此，有必要采取一些措施来加快TensorFlow的执行速度，其中一个可行的方法就是&ldquo;预热&rdquo;（warm-up），把TensorFlow移植到树莓派上的作者Sam Abrahams已经比较详细地在GitHub上列出了<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>。依照作者的描述，我也测试了一下，看看那悲催的50秒时间能减少到多少秒。<br />
<span id="more-8984"></span><br />
<span style="background-color:#00ff00;">『1』</span>什么是预热（warm-up）<br />
首先，本文还是对TensorFlow的Python图像分类程序 <span style="color:#ff0000;">classify_image.py</span> 来描述的。<br />
预热就是指在真正执行一次预测之前，先执行若干次 <span style="color:#0000ff;">Session.run()&nbsp;</span>方法，从而达到加快一次预测的执行速度的目的。<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;">『2』</span>代码修改<br />
代码改起来其实很简单。为了能衡量程序运行时间，需要使用Python的time模块，因此在一开始需要import：</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="python language-python 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-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">import</span>&nbsp;time
</code></pre>
</section>
<p>然后对 <span style="color:#0000ff;">run_inference_on_image</span> 方法做一些修改，如下：</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="python language-python 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-function" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;"><span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; overflow-wrap: inherit !important; word-break: inherit !important;">def</span>&nbsp;<span class="hljs-title" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(165, 218, 45); word-wrap: inherit !important; word-break: inherit !important;">run_inference_on_image</span><span class="hljs-params" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(255, 152, 35); word-wrap: inherit !important; word-break: inherit !important;">(image)</span>:</span>
&nbsp;&nbsp;<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;&quot;&quot;Runs&nbsp;inference&nbsp;on&nbsp;an&nbsp;image.

&nbsp;&nbsp;Args:
&nbsp;&nbsp;&nbsp;&nbsp;image:&nbsp;Image&nbsp;file&nbsp;name.

&nbsp;&nbsp;Returns:
&nbsp;&nbsp;&nbsp;&nbsp;Nothing
&nbsp;&nbsp;&quot;&quot;&quot;</span>
&nbsp;&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">if</span>&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">not</span>&nbsp;tf.gfile.Exists(image):
&nbsp;&nbsp;&nbsp;&nbsp;tf.logging.fatal(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&#39;File&nbsp;does&nbsp;not&nbsp;exist&nbsp;%s&#39;</span>,&nbsp;image)
&nbsp;&nbsp;image_data&nbsp;=&nbsp;tf.gfile.FastGFile(image,&nbsp;<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&#39;rb&#39;</span>).read()

&nbsp;&nbsp;<span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(128, 128, 128); word-wrap: inherit !important; word-break: inherit !important;">#&nbsp;the&nbsp;image&nbsp;used&nbsp;to&nbsp;warm-up&nbsp;TensorFlow&nbsp;model</span>
&nbsp;&nbsp;warm_up_image_data&nbsp;=&nbsp;tf.gfile.FastGFile(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&#39;/root/tensorflow-related/test-images/ubike.jpg&#39;</span>,&nbsp;<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&#39;rb&#39;</span>).read()

&nbsp;&nbsp;<span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(128, 128, 128); word-wrap: inherit !important; word-break: inherit !important;">#&nbsp;Creates&nbsp;graph&nbsp;from&nbsp;saved&nbsp;GraphDef.</span>
&nbsp;&nbsp;create_graph()

&nbsp;&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">with</span>&nbsp;tf.Session()&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">as</span>&nbsp;sess:
&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(128, 128, 128); word-wrap: inherit !important; word-break: inherit !important;">#&nbsp;Some&nbsp;useful&nbsp;tensors:</span>
&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(128, 128, 128); word-wrap: inherit !important; word-break: inherit !important;">#&nbsp;&#39;softmax:0&#39;:&nbsp;A&nbsp;tensor&nbsp;containing&nbsp;the&nbsp;normalized&nbsp;prediction&nbsp;across</span>
&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(128, 128, 128); word-wrap: inherit !important; word-break: inherit !important;">#&nbsp;&nbsp;&nbsp;1000&nbsp;labels.</span>
&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(128, 128, 128); word-wrap: inherit !important; word-break: inherit !important;">#&nbsp;&#39;pool_3:0&#39;:&nbsp;A&nbsp;tensor&nbsp;containing&nbsp;the&nbsp;next-to-last&nbsp;layer&nbsp;containing&nbsp;2048</span>
&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(128, 128, 128); word-wrap: inherit !important; word-break: inherit !important;">#&nbsp;&nbsp;&nbsp;float&nbsp;description&nbsp;of&nbsp;the&nbsp;image.</span>
&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(128, 128, 128); word-wrap: inherit !important; word-break: inherit !important;">#&nbsp;&#39;DecodeJpeg/contents:0&#39;:&nbsp;A&nbsp;tensor&nbsp;containing&nbsp;a&nbsp;string&nbsp;providing&nbsp;JPEG</span>
&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(128, 128, 128); word-wrap: inherit !important; word-break: inherit !important;">#&nbsp;&nbsp;&nbsp;encoding&nbsp;of&nbsp;the&nbsp;image.</span>
&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(128, 128, 128); word-wrap: inherit !important; word-break: inherit !important;">#&nbsp;Runs&nbsp;the&nbsp;softmax&nbsp;tensor&nbsp;by&nbsp;feeding&nbsp;the&nbsp;image_data&nbsp;as&nbsp;input&nbsp;to&nbsp;the&nbsp;graph.</span>
&nbsp;&nbsp;&nbsp;&nbsp;softmax_tensor&nbsp;=&nbsp;sess.graph.get_tensor_by_name(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&#39;softmax:0&#39;</span>)

&nbsp;&nbsp;&nbsp;&nbsp;print(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;Warm-up&nbsp;start&quot;</span>)
&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">for</span>&nbsp;i&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">in</span>&nbsp;range(<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(174, 135, 250); word-wrap: inherit !important; word-break: inherit !important;">10</span>):
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;Warm-up&nbsp;for&nbsp;time&nbsp;{}&quot;</span>.format(i))
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;predictions&nbsp;=&nbsp;sess.run(softmax_tensor,
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&#39;DecodeJpeg/contents:0&#39;</span>:&nbsp;warm_up_image_data})

&nbsp;&nbsp;&nbsp;&nbsp;print(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;Warm-up&nbsp;finished&quot;</span>)

&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(128, 128, 128); word-wrap: inherit !important; word-break: inherit !important;">#&nbsp;record&nbsp;the&nbsp;start&nbsp;time&nbsp;of&nbsp;the&nbsp;actual&nbsp;prediction</span>
&nbsp;&nbsp;&nbsp;&nbsp;start_time&nbsp;=&nbsp;time.time()

&nbsp;&nbsp;&nbsp;&nbsp;predictions&nbsp;=&nbsp;sess.run(softmax_tensor,
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&#39;DecodeJpeg/contents:0&#39;</span>:&nbsp;image_data})
&nbsp;&nbsp;&nbsp;&nbsp;predictions&nbsp;=&nbsp;np.squeeze(predictions)

&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(128, 128, 128); word-wrap: inherit !important; word-break: inherit !important;">#&nbsp;Creates&nbsp;node&nbsp;ID&nbsp;--&gt;&nbsp;English&nbsp;string&nbsp;lookup.</span>
&nbsp;&nbsp;&nbsp;&nbsp;node_lookup&nbsp;=&nbsp;NodeLookup()

&nbsp;&nbsp;&nbsp;&nbsp;top_k&nbsp;=&nbsp;predictions.argsort()[-FLAGS.num_top_predictions:][::<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(174, 135, 250); word-wrap: inherit !important; word-break: inherit !important;">-1</span>]
&nbsp;&nbsp;&nbsp;&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">for</span>&nbsp;node_id&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">in</span>&nbsp;top_k:
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;human_string&nbsp;=&nbsp;node_lookup.id_to_string(node_id)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;score&nbsp;=&nbsp;predictions[node_id]
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&#39;%s&nbsp;(score&nbsp;=&nbsp;%.5f)&#39;</span>&nbsp;%&nbsp;(human_string,&nbsp;score))

&nbsp;&nbsp;&nbsp;&nbsp;print(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;Prediction&nbsp;used&nbsp;time:{}&nbsp;S&quot;</span>.format(time.time()&nbsp;-&nbsp;start_time))
</code></pre>
</section>
<p>其中，我们自己添加的代码有如下几部分：</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="python language-python 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-comment" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(128, 128, 128); word-wrap: inherit !important; word-break: inherit !important;">#&nbsp;the&nbsp;image&nbsp;used&nbsp;to&nbsp;warm-up&nbsp;TensorFlow&nbsp;model</span>
warm_up_image_data&nbsp;=&nbsp;tf.gfile.FastGFile(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&#39;/root/tensorflow-related/test-images/ubike.jpg&#39;</span>,&nbsp;<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&#39;rb&#39;</span>).read()
</code></pre>
</section>
<p>这里使用了另外一张图片来预热模型（和真正预测时使用的不是同一张图片），为了简单写死了路径。</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="python language-python 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;">print(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;Warm-up&nbsp;start&quot;</span>)
<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">for</span>&nbsp;i&nbsp;<span class="hljs-keyword" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(248, 35, 117); word-wrap: inherit !important; word-break: inherit !important;">in</span>&nbsp;range(<span class="hljs-number" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(174, 135, 250); word-wrap: inherit !important; word-break: inherit !important;">10</span>):
&nbsp;&nbsp;print(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;Warm-up&nbsp;for&nbsp;time&nbsp;{}&quot;</span>.format(i))
&nbsp;&nbsp;predictions&nbsp;=&nbsp;sess.run(softmax_tensor,
&nbsp;&nbsp;&nbsp;{<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&#39;DecodeJpeg/contents:0&#39;</span>:&nbsp;warm_up_image_data})

print(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;Warm-up&nbsp;finished&quot;</span>)
</code></pre>
</section>
<p>这里循环10次来预热模型。</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="python language-python 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-comment" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(128, 128, 128); word-wrap: inherit !important; word-break: inherit !important;">#&nbsp;record&nbsp;the&nbsp;start&nbsp;time&nbsp;of&nbsp;the&nbsp;actual&nbsp;prediction</span>
start_time&nbsp;=&nbsp;time.time()
<span class="hljs-comment" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(128, 128, 128); word-wrap: inherit !important; word-break: inherit !important;">#&nbsp;(中间省略)</span>
print(<span class="hljs-string" style="font-size: inherit; line-height: inherit; margin: 0px; padding: 0px; color: rgb(238, 220, 112); word-wrap: inherit !important; word-break: inherit !important;">&quot;Prediction&nbsp;used&nbsp;time:{}&nbsp;S&quot;</span>.format(time.time()&nbsp;-&nbsp;start_time))
</code></pre>
</section>
<p>这里打印出了真正预测一张图片的执行时间（秒数），这个时间就是我们真正需要关心的，看它能减少到多少秒。<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 />
执行和上一篇文章一样的命令，输出如下：</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>
		<span style="color:#0000ff;">Warm-up start</span></div>
<div>
		<span style="color:#0000ff;">Warm-up for time 0</span></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:#0000ff;">Warm-up for time 1</span></div>
<div>
		<span style="color:#0000ff;">Warm-up for time 2</span></div>
<div>
		<span style="color:#0000ff;">Warm-up for time 3</span></div>
<div>
		<span style="color:#0000ff;">Warm-up for time 4</span></div>
<div>
		<span style="color:#0000ff;">Warm-up for time 5</span></div>
<div>
		<span style="color:#0000ff;">Warm-up for time 6</span></div>
<div>
		<span style="color:#0000ff;">Warm-up for time 7</span></div>
<div>
		<span style="color:#0000ff;">Warm-up for time 8</span></div>
<div>
		<span style="color:#0000ff;">Warm-up for time 9</span></div>
<div>
		<span style="color:#0000ff;">Warm-up finished</span></div>
<div>
		mountain bike, all-terrain bike, off-roader (score = 0.56671)</div>
<div>
		tricycle, trike, velocipede (score = 0.12035)</div>
<div>
		bicycle-built-for-two, tandem bicycle, tandem (score = 0.08768)</div>
<div>
		lawn mower, mower (score = 0.00651)</div>
<div>
		alp (score = 0.00387)</div>
<div>
		Prediction used time:<span style="color:#ff0000;">4.141446590423584 Seconds</span></div>
</blockquote>
<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 />
	可见：在10次预热之后，一次预测消耗的时间是 4.14 秒，虽然4秒多还是没有达到我们心目中的理想速度，但这已经比之前的50秒强太多了。<br />
	此外，从测试结果我们可以体会到的是：预热（Session.run()）的头几次特别慢，后面就快起来了，所以，预热次数太少是不行的。</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>
</div>
]]></content:encoded>
					
					<wfw:commentRss>https://www.codelast.com/%e5%8e%9f%e5%88%9b-%e5%8a%a0%e5%bf%abtensorflow%e5%9c%a8%e6%a0%91%e8%8e%93%e6%b4%be%e4%b8%8a%e7%9a%84%e6%89%a7%e8%a1%8c%e9%80%9f%e5%ba%a6-%e6%a8%a1%e5%9e%8b%e9%a2%84%e7%83%ad/feed/</wfw:commentRss>
			<slash:comments>0</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>
	</channel>
</rss>
