<?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>TFRecord &#8211; 编码无悔 /  Intent &amp; Focused</title>
	<atom:link href="https://www.codelast.com/tag/tfrecord/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.codelast.com</link>
	<description>最优化之路</description>
	<lastBuildDate>Mon, 24 Apr 2023 18:09:06 +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>[原创] 用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>[原创] 二维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>
	</channel>
</rss>
