You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
384 lines
52 KiB
384 lines
52 KiB
1 year ago
|
|
||
|
|
||
|
|
||
|
<!DOCTYPE html>
|
||
|
<html>
|
||
|
<head>
|
||
|
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
|
||
|
|
||
|
|
||
|
<meta http-equiv="X-UA-Compatible" content="IE=emulateIE7" />
|
||
|
<title>Coverage for LSTMPredictStock\core\model.py: 65%</title>
|
||
|
<link rel="stylesheet" href="style.css" type="text/css">
|
||
|
|
||
|
<script type="text/javascript" src="jquery.min.js"></script>
|
||
|
<script type="text/javascript" src="jquery.hotkeys.js"></script>
|
||
|
<script type="text/javascript" src="jquery.isonscreen.js"></script>
|
||
|
<script type="text/javascript" src="coverage_html.js"></script>
|
||
|
<script type="text/javascript">
|
||
|
jQuery(document).ready(coverage.pyfile_ready);
|
||
|
</script>
|
||
|
</head>
|
||
|
<body class="pyfile">
|
||
|
|
||
|
<div id="header">
|
||
|
<div class="content">
|
||
|
<h1>Coverage for <b>LSTMPredictStock\core\model.py</b> :
|
||
|
<span class="pc_cov">65%</span>
|
||
|
</h1>
|
||
|
|
||
|
<img id="keyboard_icon" src="keybd_closed.png" alt="Show keyboard shortcuts" />
|
||
|
|
||
|
<h2 class="stats">
|
||
|
86 statements
|
||
|
<span class="run hide_run shortkey_r button_toggle_run">56 run</span>
|
||
|
<span class="mis shortkey_m button_toggle_mis">30 missing</span>
|
||
|
<span class="exc shortkey_x button_toggle_exc">0 excluded</span>
|
||
|
|
||
|
|
||
|
</h2>
|
||
|
</div>
|
||
|
</div>
|
||
|
|
||
|
<div class="help_panel">
|
||
|
<img id="panel_icon" src="keybd_open.png" alt="Hide keyboard shortcuts" />
|
||
|
<p class="legend">Hot-keys on this page</p>
|
||
|
<div>
|
||
|
<p class="keyhelp">
|
||
|
<span class="key">r</span>
|
||
|
<span class="key">m</span>
|
||
|
<span class="key">x</span>
|
||
|
<span class="key">p</span> toggle line displays
|
||
|
</p>
|
||
|
<p class="keyhelp">
|
||
|
<span class="key">j</span>
|
||
|
<span class="key">k</span> next/prev highlighted chunk
|
||
|
</p>
|
||
|
<p class="keyhelp">
|
||
|
<span class="key">0</span> (zero) top of page
|
||
|
</p>
|
||
|
<p class="keyhelp">
|
||
|
<span class="key">1</span> (one) first highlighted chunk
|
||
|
</p>
|
||
|
</div>
|
||
|
</div>
|
||
|
|
||
|
<div id="source">
|
||
|
<table>
|
||
|
<tr>
|
||
|
<td class="linenos">
|
||
|
<p id="n1" class="stm run hide_run"><a href="#n1">1</a></p>
|
||
|
<p id="n2" class="stm run hide_run"><a href="#n2">2</a></p>
|
||
|
<p id="n3" class="stm run hide_run"><a href="#n3">3</a></p>
|
||
|
<p id="n4" class="stm run hide_run"><a href="#n4">4</a></p>
|
||
|
<p id="n5" class="stm run hide_run"><a href="#n5">5</a></p>
|
||
|
<p id="n6" class="stm run hide_run"><a href="#n6">6</a></p>
|
||
|
<p id="n7" class="stm run hide_run"><a href="#n7">7</a></p>
|
||
|
<p id="n8" class="stm run hide_run"><a href="#n8">8</a></p>
|
||
|
<p id="n9" class="stm run hide_run"><a href="#n9">9</a></p>
|
||
|
<p id="n10" class="pln"><a href="#n10">10</a></p>
|
||
|
<p id="n11" class="stm run hide_run"><a href="#n11">11</a></p>
|
||
|
<p id="n12" class="pln"><a href="#n12">12</a></p>
|
||
|
<p id="n13" class="pln"><a href="#n13">13</a></p>
|
||
|
<p id="n14" class="stm run hide_run"><a href="#n14">14</a></p>
|
||
|
<p id="n15" class="stm run hide_run"><a href="#n15">15</a></p>
|
||
|
<p id="n16" class="pln"><a href="#n16">16</a></p>
|
||
|
<p id="n17" class="stm run hide_run"><a href="#n17">17</a></p>
|
||
|
<p id="n18" class="pln"><a href="#n18">18</a></p>
|
||
|
<p id="n19" class="pln"><a href="#n19">19</a></p>
|
||
|
<p id="n20" class="pln"><a href="#n20">20</a></p>
|
||
|
<p id="n21" class="pln"><a href="#n21">21</a></p>
|
||
|
<p id="n22" class="stm run hide_run"><a href="#n22">22</a></p>
|
||
|
<p id="n23" class="stm run hide_run"><a href="#n23">23</a></p>
|
||
|
<p id="n24" class="pln"><a href="#n24">24</a></p>
|
||
|
<p id="n25" class="stm run hide_run"><a href="#n25">25</a></p>
|
||
|
<p id="n26" class="pln"><a href="#n26">26</a></p>
|
||
|
<p id="n27" class="pln"><a href="#n27">27</a></p>
|
||
|
<p id="n28" class="pln"><a href="#n28">28</a></p>
|
||
|
<p id="n29" class="pln"><a href="#n29">29</a></p>
|
||
|
<p id="n30" class="stm run hide_run"><a href="#n30">30</a></p>
|
||
|
<p id="n31" class="stm run hide_run"><a href="#n31">31</a></p>
|
||
|
<p id="n32" class="pln"><a href="#n32">32</a></p>
|
||
|
<p id="n33" class="stm run hide_run"><a href="#n33">33</a></p>
|
||
|
<p id="n34" class="stm run hide_run"><a href="#n34">34</a></p>
|
||
|
<p id="n35" class="stm run hide_run"><a href="#n35">35</a></p>
|
||
|
<p id="n36" class="stm run hide_run"><a href="#n36">36</a></p>
|
||
|
<p id="n37" class="stm run hide_run"><a href="#n37">37</a></p>
|
||
|
<p id="n38" class="stm run hide_run"><a href="#n38">38</a></p>
|
||
|
<p id="n39" class="stm run hide_run"><a href="#n39">39</a></p>
|
||
|
<p id="n40" class="pln"><a href="#n40">40</a></p>
|
||
|
<p id="n41" class="stm run hide_run"><a href="#n41">41</a></p>
|
||
|
<p id="n42" class="stm run hide_run"><a href="#n42">42</a></p>
|
||
|
<p id="n43" class="stm run hide_run"><a href="#n43">43</a></p>
|
||
|
<p id="n44" class="stm run hide_run"><a href="#n44">44</a></p>
|
||
|
<p id="n45" class="stm run hide_run"><a href="#n45">45</a></p>
|
||
|
<p id="n46" class="stm run hide_run"><a href="#n46">46</a></p>
|
||
|
<p id="n47" class="pln"><a href="#n47">47</a></p>
|
||
|
<p id="n48" class="stm run hide_run"><a href="#n48">48</a></p>
|
||
|
<p id="n49" class="pln"><a href="#n49">49</a></p>
|
||
|
<p id="n50" class="stm run hide_run"><a href="#n50">50</a></p>
|
||
|
<p id="n51" class="stm run hide_run"><a href="#n51">51</a></p>
|
||
|
<p id="n52" class="pln"><a href="#n52">52</a></p>
|
||
|
<p id="n53" class="stm run hide_run"><a href="#n53">53</a></p>
|
||
|
<p id="n54" class="stm mis"><a href="#n54">54</a></p>
|
||
|
<p id="n55" class="stm mis"><a href="#n55">55</a></p>
|
||
|
<p id="n56" class="stm mis"><a href="#n56">56</a></p>
|
||
|
<p id="n57" class="stm mis"><a href="#n57">57</a></p>
|
||
|
<p id="n58" class="pln"><a href="#n58">58</a></p>
|
||
|
<p id="n59" class="stm mis"><a href="#n59">59</a></p>
|
||
|
<p id="n60" class="stm mis"><a href="#n60">60</a></p>
|
||
|
<p id="n61" class="pln"><a href="#n61">61</a></p>
|
||
|
<p id="n62" class="pln"><a href="#n62">62</a></p>
|
||
|
<p id="n63" class="pln"><a href="#n63">63</a></p>
|
||
|
<p id="n64" class="stm mis"><a href="#n64">64</a></p>
|
||
|
<p id="n65" class="pln"><a href="#n65">65</a></p>
|
||
|
<p id="n66" class="pln"><a href="#n66">66</a></p>
|
||
|
<p id="n67" class="pln"><a href="#n67">67</a></p>
|
||
|
<p id="n68" class="pln"><a href="#n68">68</a></p>
|
||
|
<p id="n69" class="pln"><a href="#n69">69</a></p>
|
||
|
<p id="n70" class="pln"><a href="#n70">70</a></p>
|
||
|
<p id="n71" class="stm mis"><a href="#n71">71</a></p>
|
||
|
<p id="n72" class="pln"><a href="#n72">72</a></p>
|
||
|
<p id="n73" class="stm mis"><a href="#n73">73</a></p>
|
||
|
<p id="n74" class="stm mis"><a href="#n74">74</a></p>
|
||
|
<p id="n75" class="pln"><a href="#n75">75</a></p>
|
||
|
<p id="n76" class="stm run hide_run"><a href="#n76">76</a></p>
|
||
|
<p id="n77" class="pln"><a href="#n77">77</a></p>
|
||
|
<p id="n78" class="pln"><a href="#n78">78</a></p>
|
||
|
<p id="n79" class="pln"><a href="#n79">79</a></p>
|
||
|
<p id="n80" class="stm run hide_run"><a href="#n80">80</a></p>
|
||
|
<p id="n81" class="stm run hide_run"><a href="#n81">81</a></p>
|
||
|
<p id="n82" class="stm run hide_run"><a href="#n82">82</a></p>
|
||
|
<p id="n83" class="stm run hide_run"><a href="#n83">83</a></p>
|
||
|
<p id="n84" class="pln"><a href="#n84">84</a></p>
|
||
|
<p id="n85" class="pln"><a href="#n85">85</a></p>
|
||
|
<p id="n86" class="stm run hide_run"><a href="#n86">86</a></p>
|
||
|
<p id="n87" class="stm run hide_run"><a href="#n87">87</a></p>
|
||
|
<p id="n88" class="pln"><a href="#n88">88</a></p>
|
||
|
<p id="n89" class="pln"><a href="#n89">89</a></p>
|
||
|
<p id="n90" class="stm run hide_run"><a href="#n90">90</a></p>
|
||
|
<p id="n91" class="pln"><a href="#n91">91</a></p>
|
||
|
<p id="n92" class="pln"><a href="#n92">92</a></p>
|
||
|
<p id="n93" class="pln"><a href="#n93">93</a></p>
|
||
|
<p id="n94" class="pln"><a href="#n94">94</a></p>
|
||
|
<p id="n95" class="pln"><a href="#n95">95</a></p>
|
||
|
<p id="n96" class="pln"><a href="#n96">96</a></p>
|
||
|
<p id="n97" class="pln"><a href="#n97">97</a></p>
|
||
|
<p id="n98" class="stm run hide_run"><a href="#n98">98</a></p>
|
||
|
<p id="n99" class="stm run hide_run"><a href="#n99">99</a></p>
|
||
|
<p id="n100" class="pln"><a href="#n100">100</a></p>
|
||
|
<p id="n101" class="pln"><a href="#n101">101</a></p>
|
||
|
<p id="n102" class="stm run hide_run"><a href="#n102">102</a></p>
|
||
|
<p id="n103" class="pln"><a href="#n103">103</a></p>
|
||
|
<p id="n104" class="stm mis"><a href="#n104">104</a></p>
|
||
|
<p id="n105" class="stm mis"><a href="#n105">105</a></p>
|
||
|
<p id="n106" class="stm mis"><a href="#n106">106</a></p>
|
||
|
<p id="n107" class="pln"><a href="#n107">107</a></p>
|
||
|
<p id="n108" class="pln"><a href="#n108">108</a></p>
|
||
|
<p id="n109" class="pln"><a href="#n109">109</a></p>
|
||
|
<p id="n110" class="pln"><a href="#n110">110</a></p>
|
||
|
<p id="n111" class="pln"><a href="#n111">111</a></p>
|
||
|
<p id="n112" class="stm run hide_run"><a href="#n112">112</a></p>
|
||
|
<p id="n113" class="pln"><a href="#n113">113</a></p>
|
||
|
<p id="n114" class="stm mis"><a href="#n114">114</a></p>
|
||
|
<p id="n115" class="stm mis"><a href="#n115">115</a></p>
|
||
|
<p id="n116" class="stm mis"><a href="#n116">116</a></p>
|
||
|
<p id="n117" class="stm mis"><a href="#n117">117</a></p>
|
||
|
<p id="n118" class="stm mis"><a href="#n118">118</a></p>
|
||
|
<p id="n119" class="stm mis"><a href="#n119">119</a></p>
|
||
|
<p id="n120" class="stm mis"><a href="#n120">120</a></p>
|
||
|
<p id="n121" class="stm mis"><a href="#n121">121</a></p>
|
||
|
<p id="n122" class="stm mis"><a href="#n122">122</a></p>
|
||
|
<p id="n123" class="stm mis"><a href="#n123">123</a></p>
|
||
|
<p id="n124" class="pln"><a href="#n124">124</a></p>
|
||
|
<p id="n125" class="pln"><a href="#n125">125</a></p>
|
||
|
<p id="n126" class="pln"><a href="#n126">126</a></p>
|
||
|
<p id="n127" class="stm run hide_run"><a href="#n127">127</a></p>
|
||
|
<p id="n128" class="pln"><a href="#n128">128</a></p>
|
||
|
<p id="n129" class="stm mis"><a href="#n129">129</a></p>
|
||
|
<p id="n130" class="stm mis"><a href="#n130">130</a></p>
|
||
|
<p id="n131" class="stm mis"><a href="#n131">131</a></p>
|
||
|
<p id="n132" class="stm mis"><a href="#n132">132</a></p>
|
||
|
<p id="n133" class="stm mis"><a href="#n133">133</a></p>
|
||
|
<p id="n134" class="stm mis"><a href="#n134">134</a></p>
|
||
|
<p id="n135" class="stm mis"><a href="#n135">135</a></p>
|
||
|
<p id="n136" class="pln"><a href="#n136">136</a></p>
|
||
|
<p id="n137" class="pln"><a href="#n137">137</a></p>
|
||
|
<p id="n138" class="pln"><a href="#n138">138</a></p>
|
||
|
<p id="n139" class="stm run hide_run"><a href="#n139">139</a></p>
|
||
|
<p id="n140" class="pln"><a href="#n140">140</a></p>
|
||
|
<p id="n141" class="stm run hide_run"><a href="#n141">141</a></p>
|
||
|
<p id="n142" class="stm run hide_run"><a href="#n142">142</a></p>
|
||
|
<p id="n143" class="stm run hide_run"><a href="#n143">143</a></p>
|
||
|
<p id="n144" class="stm run hide_run"><a href="#n144">144</a></p>
|
||
|
<p id="n145" class="stm run hide_run"><a href="#n145">145</a></p>
|
||
|
<p id="n146" class="stm run hide_run"><a href="#n146">146</a></p>
|
||
|
<p id="n147" class="stm run hide_run"><a href="#n147">147</a></p>
|
||
|
|
||
|
</td>
|
||
|
<td class="text">
|
||
|
<p id="t1" class="stm run hide_run"><span class="key">import</span> <span class="nam">os</span><span class="strut"> </span></p>
|
||
|
<p id="t2" class="stm run hide_run"><span class="key">import</span> <span class="nam">math</span><span class="strut"> </span></p>
|
||
|
<p id="t3" class="stm run hide_run"><span class="key">import</span> <span class="nam">numpy</span> <span class="key">as</span> <span class="nam">np</span><span class="strut"> </span></p>
|
||
|
<p id="t4" class="stm run hide_run"><span class="key">import</span> <span class="nam">datetime</span> <span class="key">as</span> <span class="nam">dt</span><span class="strut"> </span></p>
|
||
|
<p id="t5" class="stm run hide_run"><span class="key">from</span> <span class="nam">numpy</span> <span class="key">import</span> <span class="nam">newaxis</span><span class="strut"> </span></p>
|
||
|
<p id="t6" class="stm run hide_run"><span class="key">from</span> <span class="nam">LSTMPredictStock</span><span class="op">.</span><span class="nam">core</span><span class="op">.</span><span class="nam">utils</span> <span class="key">import</span> <span class="nam">Timer</span><span class="strut"> </span></p>
|
||
|
<p id="t7" class="stm run hide_run"><span class="key">from</span> <span class="nam">keras</span><span class="op">.</span><span class="nam">layers</span> <span class="key">import</span> <span class="nam">Dense</span><span class="op">,</span> <span class="nam">Activation</span><span class="op">,</span> <span class="nam">Dropout</span><span class="op">,</span> <span class="nam">LSTM</span><span class="strut"> </span></p>
|
||
|
<p id="t8" class="stm run hide_run"><span class="key">from</span> <span class="nam">keras</span><span class="op">.</span><span class="nam">models</span> <span class="key">import</span> <span class="nam">Sequential</span><span class="op">,</span> <span class="nam">load_model</span><span class="strut"> </span></p>
|
||
|
<p id="t9" class="stm run hide_run"><span class="key">from</span> <span class="nam">keras</span><span class="op">.</span><span class="nam">callbacks</span> <span class="key">import</span> <span class="nam">EarlyStopping</span><span class="op">,</span> <span class="nam">ModelCheckpoint</span><span class="strut"> </span></p>
|
||
|
<p id="t10" class="pln"><span class="strut"> </span></p>
|
||
|
<p id="t11" class="stm run hide_run"><span class="key">class</span> <span class="nam">Model</span><span class="op">(</span><span class="op">)</span><span class="op">:</span><span class="strut"> </span></p>
|
||
|
<p id="t12" class="pln"> <span class="str">"""A class for an building and inferencing an lstm model"""</span><span class="strut"> </span></p>
|
||
|
<p id="t13" class="pln"><span class="strut"> </span></p>
|
||
|
<p id="t14" class="stm run hide_run"> <span class="key">def</span> <span class="nam">__init__</span><span class="op">(</span><span class="nam">self</span><span class="op">)</span><span class="op">:</span><span class="strut"> </span></p>
|
||
|
<p id="t15" class="stm run hide_run"> <span class="nam">self</span><span class="op">.</span><span class="nam">model</span> <span class="op">=</span> <span class="nam">Sequential</span><span class="op">(</span><span class="op">)</span><span class="strut"> </span></p>
|
||
|
<p id="t16" class="pln"><span class="strut"> </span></p>
|
||
|
<p id="t17" class="stm run hide_run"> <span class="key">def</span> <span class="nam">load_model</span><span class="op">(</span><span class="nam">self</span><span class="op">,</span> <span class="nam">filepath</span><span class="op">)</span><span class="op">:</span><span class="strut"> </span></p>
|
||
|
<p id="t18" class="pln"> <span class="str">'''</span><span class="strut"> </span></p>
|
||
|
<p id="t19" class="pln"><span class="str"> 从本地保存的模型参数来加载模型</span><span class="strut"> </span></p>
|
||
|
<p id="t20" class="pln"><span class="str"> filepath: .h5 格式文件</span><span class="strut"> </span></p>
|
||
|
<p id="t21" class="pln"><span class="str"> '''</span><span class="strut"> </span></p>
|
||
|
<p id="t22" class="stm run hide_run"> <span class="nam">print</span><span class="op">(</span><span class="str">'[Model] Loading model from file %s'</span> <span class="op">%</span> <span class="nam">filepath</span><span class="op">)</span><span class="strut"> </span></p>
|
||
|
<p id="t23" class="stm run hide_run"> <span class="nam">self</span><span class="op">.</span><span class="nam">model</span> <span class="op">=</span> <span class="nam">load_model</span><span class="op">(</span><span class="nam">filepath</span><span class="op">)</span><span class="strut"> </span></p>
|
||
|
<p id="t24" class="pln"><span class="strut"> </span></p>
|
||
|
<p id="t25" class="stm run hide_run"> <span class="key">def</span> <span class="nam">build_model</span><span class="op">(</span><span class="nam">self</span><span class="op">,</span> <span class="nam">configs</span><span class="op">)</span><span class="op">:</span><span class="strut"> </span></p>
|
||
|
<p id="t26" class="pln"> <span class="str">"""</span><span class="strut"> </span></p>
|
||
|
<p id="t27" class="pln"><span class="str"> 新建一个模型</span><span class="strut"> </span></p>
|
||
|
<p id="t28" class="pln"><span class="str"> configs:配置文件</span><span class="strut"> </span></p>
|
||
|
<p id="t29" class="pln"><span class="str"> """</span><span class="strut"> </span></p>
|
||
|
<p id="t30" class="stm run hide_run"> <span class="nam">timer</span> <span class="op">=</span> <span class="nam">Timer</span><span class="op">(</span><span class="op">)</span><span class="strut"> </span></p>
|
||
|
<p id="t31" class="stm run hide_run"> <span class="nam">timer</span><span class="op">.</span><span class="nam">start</span><span class="op">(</span><span class="op">)</span><span class="strut"> </span></p>
|
||
|
<p id="t32" class="pln"><span class="strut"> </span></p>
|
||
|
<p id="t33" class="stm run hide_run"> <span class="key">for</span> <span class="nam">layer</span> <span class="key">in</span> <span class="nam">configs</span><span class="op">[</span><span class="str">'model'</span><span class="op">]</span><span class="op">[</span><span class="str">'layers'</span><span class="op">]</span><span class="op">:</span><span class="strut"> </span></p>
|
||
|
<p id="t34" class="stm run hide_run"> <span class="nam">neurons</span> <span class="op">=</span> <span class="nam">layer</span><span class="op">[</span><span class="str">'neurons'</span><span class="op">]</span> <span class="key">if</span> <span class="str">'neurons'</span> <span class="key">in</span> <span class="nam">layer</span> <span class="key">else</span> <span class="key">None</span><span class="strut"> </span></p>
|
||
|
<p id="t35" class="stm run hide_run"> <span class="nam">dropout_rate</span> <span class="op">=</span> <span class="nam">layer</span><span class="op">[</span><span class="str">'rate'</span><span class="op">]</span> <span class="key">if</span> <span class="str">'rate'</span> <span class="key">in</span> <span class="nam">layer</span> <span class="key">else</span> <span class="key">None</span><span class="strut"> </span></p>
|
||
|
<p id="t36" class="stm run hide_run"> <span class="nam">activation</span> <span class="op">=</span> <span class="nam">layer</span><span class="op">[</span><span class="str">'activation'</span><span class="op">]</span> <span class="key">if</span> <span class="str">'activation'</span> <span class="key">in</span> <span class="nam">layer</span> <span class="key">else</span> <span class="key">None</span><span class="strut"> </span></p>
|
||
|
<p id="t37" class="stm run hide_run"> <span class="nam">return_seq</span> <span class="op">=</span> <span class="nam">layer</span><span class="op">[</span><span class="str">'return_seq'</span><span class="op">]</span> <span class="key">if</span> <span class="str">'return_seq'</span> <span class="key">in</span> <span class="nam">layer</span> <span class="key">else</span> <span class="key">None</span><span class="strut"> </span></p>
|
||
|
<p id="t38" class="stm run hide_run"> <span class="nam">input_timesteps</span> <span class="op">=</span> <span class="nam">layer</span><span class="op">[</span><span class="str">'input_timesteps'</span><span class="op">]</span> <span class="key">if</span> <span class="str">'input_timesteps'</span> <span class="key">in</span> <span class="nam">layer</span> <span class="key">else</span> <span class="key">None</span><span class="strut"> </span></p>
|
||
|
<p id="t39" class="stm run hide_run"> <span class="nam">input_dim</span> <span class="op">=</span> <span class="nam">layer</span><span class="op">[</span><span class="str">'input_dim'</span><span class="op">]</span> <span class="key">if</span> <span class="str">'input_dim'</span> <span class="key">in</span> <span class="nam">layer</span> <span class="key">else</span> <span class="key">None</span><span class="strut"> </span></p>
|
||
|
<p id="t40" class="pln"><span class="strut"> </span></p>
|
||
|
<p id="t41" class="stm run hide_run"> <span class="key">if</span> <span class="nam">layer</span><span class="op">[</span><span class="str">'type'</span><span class="op">]</span> <span class="op">==</span> <span class="str">'dense'</span><span class="op">:</span><span class="strut"> </span></p>
|
||
|
<p id="t42" class="stm run hide_run"> <span class="nam">self</span><span class="op">.</span><span class="nam">model</span><span class="op">.</span><span class="nam">add</span><span class="op">(</span><span class="nam">Dense</span><span class="op">(</span><span class="nam">neurons</span><span class="op">,</span> <span class="nam">activation</span><span class="op">=</span><span class="nam">activation</span><span class="op">)</span><span class="op">)</span><span class="strut"> </span></p>
|
||
|
<p id="t43" class="stm run hide_run"> <span class="key">if</span> <span class="nam">layer</span><span class="op">[</span><span class="str">'type'</span><span class="op">]</span> <span class="op">==</span> <span class="str">'lstm'</span><span class="op">:</span><span class="strut"> </span></p>
|
||
|
<p id="t44" class="stm run hide_run"> <span class="nam">self</span><span class="op">.</span><span class="nam">model</span><span class="op">.</span><span class="nam">add</span><span class="op">(</span><span class="nam">LSTM</span><span class="op">(</span><span class="nam">neurons</span><span class="op">,</span> <span class="nam">input_shape</span><span class="op">=</span><span class="op">(</span><span class="nam">input_timesteps</span><span class="op">,</span> <span class="nam">input_dim</span><span class="op">)</span><span class="op">,</span> <span class="nam">return_sequences</span><span class="op">=</span><span class="nam">return_seq</span><span class="op">)</span><span class="op">)</span><span class="strut"> </span></p>
|
||
|
<p id="t45" class="stm run hide_run"> <span class="key">if</span> <span class="nam">layer</span><span class="op">[</span><span class="str">'type'</span><span class="op">]</span> <span class="op">==</span> <span class="str">'dropout'</span><span class="op">:</span><span class="strut"> </span></p>
|
||
|
<p id="t46" class="stm run hide_run"> <span class="nam">self</span><span class="op">.</span><span class="nam">model</span><span class="op">.</span><span class="nam">add</span><span class="op">(</span><span class="nam">Dropout</span><span class="op">(</span><span class="nam">dropout_rate</span><span class="op">)</span><span class="op">)</span><span class="strut"> </span></p>
|
||
|
<p id="t47" class="pln"><span class="strut"> </span></p>
|
||
|
<p id="t48" class="stm run hide_run"> <span class="nam">self</span><span class="op">.</span><span class="nam">model</span><span class="op">.</span><span class="nam">compile</span><span class="op">(</span><span class="nam">loss</span><span class="op">=</span><span class="nam">configs</span><span class="op">[</span><span class="str">'model'</span><span class="op">]</span><span class="op">[</span><span class="str">'loss'</span><span class="op">]</span><span class="op">,</span> <span class="nam">optimizer</span><span class="op">=</span><span class="nam">configs</span><span class="op">[</span><span class="str">'model'</span><span class="op">]</span><span class="op">[</span><span class="str">'optimizer'</span><span class="op">]</span><span class="op">)</span><span class="strut"> </span></p>
|
||
|
<p id="t49" class="pln"><span class="strut"> </span></p>
|
||
|
<p id="t50" class="stm run hide_run"> <span class="nam">print</span><span class="op">(</span><span class="str">'[Model] Model Compiled'</span><span class="op">)</span><span class="strut"> </span></p>
|
||
|
<p id="t51" class="stm run hide_run"> <span class="nam">timer</span><span class="op">.</span><span class="nam">stop</span><span class="op">(</span><span class="op">)</span> <span class="com">#输出构建一个模型耗时</span><span class="strut"> </span></p>
|
||
|
<p id="t52" class="pln"><span class="strut"> </span></p>
|
||
|
<p id="t53" class="stm run hide_run"> <span class="key">def</span> <span class="nam">train</span><span class="op">(</span><span class="nam">self</span><span class="op">,</span> <span class="nam">x</span><span class="op">,</span> <span class="nam">y</span><span class="op">,</span> <span class="nam">epochs</span><span class="op">,</span> <span class="nam">batch_size</span><span class="op">,</span> <span class="nam">save_dir</span><span class="op">)</span><span class="op">:</span><span class="strut"> </span></p>
|
||
|
<p id="t54" class="stm mis"> <span class="nam">timer</span> <span class="op">=</span> <span class="nam">Timer</span><span class="op">(</span><span class="op">)</span><span class="strut"> </span></p>
|
||
|
<p id="t55" class="stm mis"> <span class="nam">timer</span><span class="op">.</span><span class="nam">start</span><span class="op">(</span><span class="op">)</span><span class="strut"> </span></p>
|
||
|
<p id="t56" class="stm mis"> <span class="nam">print</span><span class="op">(</span><span class="str">'[Model] Training Started'</span><span class="op">)</span><span class="strut"> </span></p>
|
||
|
<p id="t57" class="stm mis"> <span class="nam">print</span><span class="op">(</span><span class="str">'[Model] %s epochs, %s batch size'</span> <span class="op">%</span> <span class="op">(</span><span class="nam">epochs</span><span class="op">,</span> <span class="nam">batch_size</span><span class="op">)</span><span class="op">)</span><span class="strut"> </span></p>
|
||
|
<p id="t58" class="pln"><span class="strut"> </span></p>
|
||
|
<p id="t59" class="stm mis"> <span class="nam">save_fname</span> <span class="op">=</span> <span class="nam">os</span><span class="op">.</span><span class="nam">path</span><span class="op">.</span><span class="nam">join</span><span class="op">(</span><span class="nam">save_dir</span><span class="op">,</span> <span class="str">'%s-e%s.h5'</span> <span class="op">%</span> <span class="op">(</span><span class="nam">dt</span><span class="op">.</span><span class="nam">datetime</span><span class="op">.</span><span class="nam">now</span><span class="op">(</span><span class="op">)</span><span class="op">.</span><span class="nam">strftime</span><span class="op">(</span><span class="str">'%d%m%Y-%H%M%S'</span><span class="op">)</span><span class="op">,</span> <span class="nam">str</span><span class="op">(</span><span class="nam">epochs</span><span class="op">)</span><span class="op">)</span><span class="op">)</span><span class="strut"> </span></p>
|
||
|
<p id="t60" class="stm mis"> <span class="nam">callbacks</span> <span class="op">=</span> <span class="op">[</span><span class="strut"> </span></p>
|
||
|
<p id="t61" class="pln"> <span class="nam">EarlyStopping</span><span class="op">(</span><span class="nam">monitor</span><span class="op">=</span><span class="str">'val_loss'</span><span class="op">,</span> <span class="nam">patience</span><span class="op">=</span><span class="num">2</span><span class="op">)</span><span class="op">,</span><span class="strut"> </span></p>
|
||
|
<p id="t62" class="pln"> <span class="nam">ModelCheckpoint</span><span class="op">(</span><span class="nam">filepath</span><span class="op">=</span><span class="nam">save_fname</span><span class="op">,</span> <span class="nam">monitor</span><span class="op">=</span><span class="str">'val_loss'</span><span class="op">,</span> <span class="nam">save_best_only</span><span class="op">=</span><span class="key">True</span><span class="op">)</span><span class="strut"> </span></p>
|
||
|
<p id="t63" class="pln"> <span class="op">]</span><span class="strut"> </span></p>
|
||
|
<p id="t64" class="stm mis"> <span class="nam">self</span><span class="op">.</span><span class="nam">model</span><span class="op">.</span><span class="nam">fit</span><span class="op">(</span><span class="strut"> </span></p>
|
||
|
<p id="t65" class="pln"> <span class="nam">x</span><span class="op">,</span><span class="strut"> </span></p>
|
||
|
<p id="t66" class="pln"> <span class="nam">y</span><span class="op">,</span><span class="strut"> </span></p>
|
||
|
<p id="t67" class="pln"> <span class="nam">epochs</span><span class="op">=</span><span class="nam">epochs</span><span class="op">,</span><span class="strut"> </span></p>
|
||
|
<p id="t68" class="pln"> <span class="nam">batch_size</span><span class="op">=</span><span class="nam">batch_size</span><span class="op">,</span><span class="strut"> </span></p>
|
||
|
<p id="t69" class="pln"> <span class="nam">callbacks</span><span class="op">=</span><span class="nam">callbacks</span><span class="strut"> </span></p>
|
||
|
<p id="t70" class="pln"> <span class="op">)</span><span class="strut"> </span></p>
|
||
|
<p id="t71" class="stm mis"> <span class="nam">self</span><span class="op">.</span><span class="nam">model</span><span class="op">.</span><span class="nam">save</span><span class="op">(</span><span class="nam">save_fname</span><span class="op">)</span> <span class="com">#保存训练好的模型</span><span class="strut"> </span></p>
|
||
|
<p id="t72" class="pln"><span class="strut"> </span></p>
|
||
|
<p id="t73" class="stm mis"> <span class="nam">print</span><span class="op">(</span><span class="str">'[Model] Training Completed. Model saved as %s'</span> <span class="op">%</span> <span class="nam">save_fname</span><span class="op">)</span><span class="strut"> </span></p>
|
||
|
<p id="t74" class="stm mis"> <span class="nam">timer</span><span class="op">.</span><span class="nam">stop</span><span class="op">(</span><span class="op">)</span> <span class="com">#输出训练耗时</span><span class="strut"> </span></p>
|
||
|
<p id="t75" class="pln"><span class="strut"> </span></p>
|
||
|
<p id="t76" class="stm run hide_run"> <span class="key">def</span> <span class="nam">train_generator</span><span class="op">(</span><span class="nam">self</span><span class="op">,</span> <span class="nam">data_gen</span><span class="op">,</span> <span class="nam">epochs</span><span class="op">,</span> <span class="nam">batch_size</span><span class="op">,</span> <span class="nam">steps_per_epoch</span><span class="op">,</span> <span class="nam">save_dir</span><span class="op">,</span><span class="nam">save_name</span><span class="op">)</span><span class="op">:</span><span class="strut"> </span></p>
|
||
|
<p id="t77" class="pln"> <span class="str">'''</span><span class="strut"> </span></p>
|
||
|
<p id="t78" class="pln"><span class="str"> 由data_gen数据产生器来,逐步产生训练数据,而不是一次性将数据读入到内存</span><span class="strut"> </span></p>
|
||
|
<p id="t79" class="pln"><span class="str"> '''</span><span class="strut"> </span></p>
|
||
|
<p id="t80" class="stm run hide_run"> <span class="nam">timer</span> <span class="op">=</span> <span class="nam">Timer</span><span class="op">(</span><span class="op">)</span><span class="strut"> </span></p>
|
||
|
<p id="t81" class="stm run hide_run"> <span class="nam">timer</span><span class="op">.</span><span class="nam">start</span><span class="op">(</span><span class="op">)</span><span class="strut"> </span></p>
|
||
|
<p id="t82" class="stm run hide_run"> <span class="nam">print</span><span class="op">(</span><span class="str">'[Model] Training Started'</span><span class="op">)</span><span class="strut"> </span></p>
|
||
|
<p id="t83" class="stm run hide_run"> <span class="nam">print</span><span class="op">(</span><span class="str">'[Model] %s epochs, %s batch size, %s batches per epoch'</span> <span class="op">%</span> <span class="op">(</span><span class="nam">epochs</span><span class="op">,</span> <span class="nam">batch_size</span><span class="op">,</span> <span class="nam">steps_per_epoch</span><span class="op">)</span><span class="op">)</span><span class="strut"> </span></p>
|
||
|
<p id="t84" class="pln"><span class="strut"> </span></p>
|
||
|
<p id="t85" class="pln"> <span class="com"># save_fname = os.path.join(save_dir, '%s-e%s.h5' % (dt.datetime.now().strftime('%d%m%Y-%H%M%S'), str(epochs)))</span><span class="strut"> </span></p>
|
||
|
<p id="t86" class="stm run hide_run"> <span class="nam">save_fname</span> <span class="op">=</span> <span class="nam">os</span><span class="op">.</span><span class="nam">path</span><span class="op">.</span><span class="nam">join</span><span class="op">(</span><span class="nam">save_dir</span><span class="op">,</span> <span class="nam">save_name</span><span class="op">+</span><span class="str">'.h5'</span><span class="op">)</span><span class="strut"> </span></p>
|
||
|
<p id="t87" class="stm run hide_run"> <span class="nam">callbacks</span> <span class="op">=</span> <span class="op">[</span><span class="strut"> </span></p>
|
||
|
<p id="t88" class="pln"> <span class="nam">ModelCheckpoint</span><span class="op">(</span><span class="nam">filepath</span><span class="op">=</span><span class="nam">save_fname</span><span class="op">,</span> <span class="nam">monitor</span><span class="op">=</span><span class="str">'loss'</span><span class="op">,</span> <span class="nam">save_best_only</span><span class="op">=</span><span class="key">True</span><span class="op">)</span><span class="strut"> </span></p>
|
||
|
<p id="t89" class="pln"> <span class="op">]</span><span class="strut"> </span></p>
|
||
|
<p id="t90" class="stm run hide_run"> <span class="nam">self</span><span class="op">.</span><span class="nam">model</span><span class="op">.</span><span class="nam">fit_generator</span><span class="op">(</span><span class="strut"> </span></p>
|
||
|
<p id="t91" class="pln"> <span class="nam">data_gen</span><span class="op">,</span><span class="strut"> </span></p>
|
||
|
<p id="t92" class="pln"> <span class="nam">steps_per_epoch</span><span class="op">=</span><span class="nam">steps_per_epoch</span><span class="op">,</span><span class="strut"> </span></p>
|
||
|
<p id="t93" class="pln"> <span class="nam">epochs</span><span class="op">=</span><span class="nam">epochs</span><span class="op">,</span><span class="strut"> </span></p>
|
||
|
<p id="t94" class="pln"> <span class="nam">callbacks</span><span class="op">=</span><span class="nam">callbacks</span><span class="op">,</span><span class="strut"> </span></p>
|
||
|
<p id="t95" class="pln"> <span class="nam">workers</span><span class="op">=</span><span class="num">1</span><span class="strut"> </span></p>
|
||
|
<p id="t96" class="pln"> <span class="op">)</span><span class="strut"> </span></p>
|
||
|
<p id="t97" class="pln"><span class="strut"> </span></p>
|
||
|
<p id="t98" class="stm run hide_run"> <span class="nam">print</span><span class="op">(</span><span class="str">'[Model] Training Completed. Model saved as %s'</span> <span class="op">%</span> <span class="nam">save_fname</span><span class="op">)</span><span class="strut"> </span></p>
|
||
|
<p id="t99" class="stm run hide_run"> <span class="nam">timer</span><span class="op">.</span><span class="nam">stop</span><span class="op">(</span><span class="op">)</span><span class="strut"> </span></p>
|
||
|
<p id="t100" class="pln"><span class="strut"> </span></p>
|
||
|
<p id="t101" class="pln"> <span class="com"># data必须是三维数据,即shape:(a,b,c)</span><span class="strut"> </span></p>
|
||
|
<p id="t102" class="stm run hide_run"> <span class="key">def</span> <span class="nam">predict_point_by_point</span><span class="op">(</span><span class="nam">self</span><span class="op">,</span> <span class="nam">data</span><span class="op">)</span><span class="op">:</span><span class="strut"> </span></p>
|
||
|
<p id="t103" class="pln"> <span class="com">#Predict each timestep given the last sequence of true data, in effect only predicting 1 step ahead each time</span><span class="strut"> </span></p>
|
||
|
<p id="t104" class="stm mis"> <span class="nam">predicted</span> <span class="op">=</span> <span class="nam">self</span><span class="op">.</span><span class="nam">model</span><span class="op">.</span><span class="nam">predict</span><span class="op">(</span><span class="nam">data</span><span class="op">)</span> <span class="com"># data有多少行就输出多少个预测值,返回的预测值是一个2维数组:(a,1) a:为data的行数</span><span class="strut"> </span></p>
|
||
|
<p id="t105" class="stm mis"> <span class="nam">predicted</span> <span class="op">=</span> <span class="nam">np</span><span class="op">.</span><span class="nam">reshape</span><span class="op">(</span><span class="nam">predicted</span><span class="op">,</span> <span class="op">(</span><span class="nam">predicted</span><span class="op">.</span><span class="nam">size</span><span class="op">,</span><span class="op">)</span><span class="op">)</span> <span class="com"># 这里将二维数组,变成一维数组</span><span class="strut"> </span></p>
|
||
|
<p id="t106" class="stm mis"> <span class="key">return</span> <span class="nam">predicted</span> <span class="com"># 返回一维数组,元素个数与data的a相同</span><span class="strut"> </span></p>
|
||
|
<p id="t107" class="pln"><span class="strut"> </span></p>
|
||
|
<p id="t108" class="pln"> <span class="com"># 对data进行多段预测,每段预测基于一个窗口大小(window_size)的数据,然后输出prediction_len长的预测值(一维数组)</span><span class="strut"> </span></p>
|
||
|
<p id="t109" class="pln"> <span class="com"># 再从上一个窗口移动prediction_len的长度,得到下一个窗口的数据,并基于该数据再预测prediction_len长的预测值</span><span class="strut"> </span></p>
|
||
|
<p id="t110" class="pln"> <span class="com"># 所以prediction_len决定了窗口的移位步数,每次的窗口大小是一样的,所以最后预测的段数 = 窗口个数/预测长度</span><span class="strut"> </span></p>
|
||
|
<p id="t111" class="pln"> <span class="com"># 相当于多次调用predict_1_win_sequence方法</span><span class="strut"> </span></p>
|
||
|
<p id="t112" class="stm run hide_run"> <span class="key">def</span> <span class="nam">predict_sequences_multiple</span><span class="op">(</span><span class="nam">self</span><span class="op">,</span> <span class="nam">data</span><span class="op">,</span> <span class="nam">window_size</span><span class="op">,</span> <span class="nam">prediction_len</span><span class="op">)</span><span class="op">:</span><span class="strut"> </span></p>
|
||
|
<p id="t113" class="pln"> <span class="com">#Predict sequence of 50 steps before shifting prediction run forward by 50 steps</span><span class="strut"> </span></p>
|
||
|
<p id="t114" class="stm mis"> <span class="nam">prediction_seqs</span> <span class="op">=</span> <span class="op">[</span><span class="op">]</span><span class="strut"> </span></p>
|
||
|
<p id="t115" class="stm mis"> <span class="key">for</span> <span class="nam">i</span> <span class="key">in</span> <span class="nam">range</span><span class="op">(</span><span class="nam">int</span><span class="op">(</span><span class="nam">len</span><span class="op">(</span><span class="nam">data</span><span class="op">)</span><span class="op">/</span><span class="nam">prediction_len</span><span class="op">)</span><span class="op">)</span><span class="op">:</span><span class="strut"> </span></p>
|
||
|
<p id="t116" class="stm mis"> <span class="nam">curr_frame</span> <span class="op">=</span> <span class="nam">data</span><span class="op">[</span><span class="nam">i</span><span class="op">*</span><span class="nam">prediction_len</span><span class="op">]</span><span class="strut"> </span></p>
|
||
|
<p id="t117" class="stm mis"> <span class="nam">predicted</span> <span class="op">=</span> <span class="op">[</span><span class="op">]</span><span class="strut"> </span></p>
|
||
|
<p id="t118" class="stm mis"> <span class="key">for</span> <span class="nam">j</span> <span class="key">in</span> <span class="nam">range</span><span class="op">(</span><span class="nam">prediction_len</span><span class="op">)</span><span class="op">:</span><span class="strut"> </span></p>
|
||
|
<p id="t119" class="stm mis"> <span class="nam">predicted</span><span class="op">.</span><span class="nam">append</span><span class="op">(</span><span class="nam">self</span><span class="op">.</span><span class="nam">model</span><span class="op">.</span><span class="nam">predict</span><span class="op">(</span><span class="nam">curr_frame</span><span class="op">[</span><span class="nam">newaxis</span><span class="op">,</span><span class="op">:</span><span class="op">,</span><span class="op">:</span><span class="op">]</span><span class="op">)</span><span class="op">[</span><span class="num">0</span><span class="op">,</span><span class="num">0</span><span class="op">]</span><span class="op">)</span> <span class="com"># newaxis:增加新轴,使得curr_frame变成(1,x,x)三维数据</span><span class="strut"> </span></p>
|
||
|
<p id="t120" class="stm mis"> <span class="nam">curr_frame</span> <span class="op">=</span> <span class="nam">curr_frame</span><span class="op">[</span><span class="num">1</span><span class="op">:</span><span class="op">]</span><span class="strut"> </span></p>
|
||
|
<p id="t121" class="stm mis"> <span class="nam">curr_frame</span> <span class="op">=</span> <span class="nam">np</span><span class="op">.</span><span class="nam">insert</span><span class="op">(</span><span class="nam">curr_frame</span><span class="op">,</span> <span class="op">[</span><span class="nam">window_size</span><span class="op">-</span><span class="num">2</span><span class="op">]</span><span class="op">,</span> <span class="nam">predicted</span><span class="op">[</span><span class="op">-</span><span class="num">1</span><span class="op">]</span><span class="op">,</span> <span class="nam">axis</span><span class="op">=</span><span class="num">0</span><span class="op">)</span><span class="strut"> </span></p>
|
||
|
<p id="t122" class="stm mis"> <span class="nam">prediction_seqs</span><span class="op">.</span><span class="nam">append</span><span class="op">(</span><span class="nam">predicted</span><span class="op">)</span><span class="strut"> </span></p>
|
||
|
<p id="t123" class="stm mis"> <span class="key">return</span> <span class="nam">prediction_seqs</span><span class="strut"> </span></p>
|
||
|
<p id="t124" class="pln"><span class="strut"> </span></p>
|
||
|
<p id="t125" class="pln"> <span class="com"># 输入一个窗口的数据,指定预测的长度,data:依旧是三维数组(1,win_len,fea_len)</span><span class="strut"> </span></p>
|
||
|
<p id="t126" class="pln"> <span class="com"># 返回预测数组</span><span class="strut"> </span></p>
|
||
|
<p id="t127" class="stm run hide_run"> <span class="key">def</span> <span class="nam">predict_sequence_full</span><span class="op">(</span><span class="nam">self</span><span class="op">,</span> <span class="nam">data</span><span class="op">,</span> <span class="nam">window_size</span><span class="op">)</span><span class="op">:</span> <span class="com"># window_size:为输入数据的长度</span><span class="strut"> </span></p>
|
||
|
<p id="t128" class="pln"> <span class="com">#Shift the window by 1 new prediction each time, re-run predictions on new window</span><span class="strut"> </span></p>
|
||
|
<p id="t129" class="stm mis"> <span class="nam">curr_frame</span> <span class="op">=</span> <span class="nam">data</span><span class="op">[</span><span class="num">0</span><span class="op">]</span> <span class="com"># 基于data[0]一个窗口的数据,来预测len(data)个输出</span><span class="strut"> </span></p>
|
||
|
<p id="t130" class="stm mis"> <span class="nam">predicted</span> <span class="op">=</span> <span class="op">[</span><span class="op">]</span><span class="strut"> </span></p>
|
||
|
<p id="t131" class="stm mis"> <span class="key">for</span> <span class="nam">i</span> <span class="key">in</span> <span class="nam">range</span><span class="op">(</span><span class="nam">len</span><span class="op">(</span><span class="nam">data</span><span class="op">)</span><span class="op">)</span><span class="op">:</span><span class="strut"> </span></p>
|
||
|
<p id="t132" class="stm mis"> <span class="nam">predicted</span><span class="op">.</span><span class="nam">append</span><span class="op">(</span><span class="nam">self</span><span class="op">.</span><span class="nam">model</span><span class="op">.</span><span class="nam">predict</span><span class="op">(</span><span class="nam">curr_frame</span><span class="op">[</span><span class="nam">newaxis</span><span class="op">,</span><span class="op">:</span><span class="op">,</span><span class="op">:</span><span class="op">]</span><span class="op">)</span><span class="op">[</span><span class="num">0</span><span class="op">,</span><span class="num">0</span><span class="op">]</span><span class="op">)</span> <span class="com"># append了一个预测值(标量)</span><span class="strut"> </span></p>
|
||
|
<p id="t133" class="stm mis"> <span class="nam">curr_frame</span> <span class="op">=</span> <span class="nam">curr_frame</span><span class="op">[</span><span class="num">1</span><span class="op">:</span><span class="op">]</span><span class="strut"> </span></p>
|
||
|
<p id="t134" class="stm mis"> <span class="nam">curr_frame</span> <span class="op">=</span> <span class="nam">np</span><span class="op">.</span><span class="nam">insert</span><span class="op">(</span><span class="nam">curr_frame</span><span class="op">,</span> <span class="op">[</span><span class="nam">window_size</span><span class="op">-</span><span class="num">2</span><span class="op">]</span><span class="op">,</span> <span class="nam">predicted</span><span class="op">[</span><span class="op">-</span><span class="num">1</span><span class="op">]</span><span class="op">,</span> <span class="nam">axis</span><span class="op">=</span><span class="num">0</span><span class="op">)</span> <span class="com"># 插入位置[window_size-2]:curr_frame的末尾,predicted[-1]:插入值</span><span class="strut"> </span></p>
|
||
|
<p id="t135" class="stm mis"> <span class="key">return</span> <span class="nam">predicted</span><span class="strut"> </span></p>
|
||
|
<p id="t136" class="pln"><span class="strut"> </span></p>
|
||
|
<p id="t137" class="pln"> <span class="com"># 输入一个窗口的数据,指定预测的长度,data:依旧是三维数组(1,win_len,fea_len)</span><span class="strut"> </span></p>
|
||
|
<p id="t138" class="pln"> <span class="com"># 返回预测数组</span><span class="strut"> </span></p>
|
||
|
<p id="t139" class="stm run hide_run"> <span class="key">def</span> <span class="nam">predict_1_win_sequence</span><span class="op">(</span><span class="nam">self</span><span class="op">,</span> <span class="nam">data</span><span class="op">,</span> <span class="nam">window_size</span><span class="op">,</span><span class="nam">predict_length</span><span class="op">)</span><span class="op">:</span> <span class="com"># window_size:data的窗口大小</span><span class="strut"> </span></p>
|
||
|
<p id="t140" class="pln"> <span class="com">#Shift the window by 1 new prediction each time, re-run predictions on new window</span><span class="strut"> </span></p>
|
||
|
<p id="t141" class="stm run hide_run"> <span class="nam">curr_frame</span> <span class="op">=</span> <span class="nam">data</span><span class="op">[</span><span class="num">0</span><span class="op">]</span><span class="strut"> </span></p>
|
||
|
<p id="t142" class="stm run hide_run"> <span class="nam">predicted</span> <span class="op">=</span> <span class="op">[</span><span class="op">]</span><span class="strut"> </span></p>
|
||
|
<p id="t143" class="stm run hide_run"> <span class="key">for</span> <span class="nam">i</span> <span class="key">in</span> <span class="nam">range</span><span class="op">(</span><span class="nam">predict_length</span><span class="op">)</span><span class="op">:</span> <span class="com"># range(len(data))</span><span class="strut"> </span></p>
|
||
|
<p id="t144" class="stm run hide_run"> <span class="nam">predicted</span><span class="op">.</span><span class="nam">append</span><span class="op">(</span><span class="nam">self</span><span class="op">.</span><span class="nam">model</span><span class="op">.</span><span class="nam">predict</span><span class="op">(</span><span class="nam">curr_frame</span><span class="op">[</span><span class="nam">newaxis</span><span class="op">,</span><span class="op">:</span><span class="op">,</span><span class="op">:</span><span class="op">]</span><span class="op">)</span><span class="op">[</span><span class="num">0</span><span class="op">,</span><span class="num">0</span><span class="op">]</span><span class="op">)</span> <span class="com"># append了一个预测值(标量)</span><span class="strut"> </span></p>
|
||
|
<p id="t145" class="stm run hide_run"> <span class="nam">curr_frame</span> <span class="op">=</span> <span class="nam">curr_frame</span><span class="op">[</span><span class="num">1</span><span class="op">:</span><span class="op">]</span><span class="strut"> </span></p>
|
||
|
<p id="t146" class="stm run hide_run"> <span class="nam">curr_frame</span> <span class="op">=</span> <span class="nam">np</span><span class="op">.</span><span class="nam">insert</span><span class="op">(</span><span class="nam">curr_frame</span><span class="op">,</span> <span class="op">[</span><span class="nam">window_size</span><span class="op">-</span><span class="num">2</span><span class="op">]</span><span class="op">,</span> <span class="nam">predicted</span><span class="op">[</span><span class="op">-</span><span class="num">1</span><span class="op">]</span><span class="op">,</span> <span class="nam">axis</span><span class="op">=</span><span class="num">0</span><span class="op">)</span> <span class="com"># 插入位置[window_size-2]:curr_frame的末尾,predicted[-1]:插入值</span><span class="strut"> </span></p>
|
||
|
<p id="t147" class="stm run hide_run"> <span class="key">return</span> <span class="nam">predicted</span><span class="strut"> </span></p>
|
||
|
|
||
|
</td>
|
||
|
</tr>
|
||
|
</table>
|
||
|
</div>
|
||
|
|
||
|
<div id="footer">
|
||
|
<div class="content">
|
||
|
<p>
|
||
|
<a class="nav" href="index.html">« index</a> <a class="nav" href="https://coverage.readthedocs.io">coverage.py v4.5.2</a>,
|
||
|
created at 2018-12-22 16:41
|
||
|
</p>
|
||
|
</div>
|
||
|
</div>
|
||
|
|
||
|
</body>
|
||
|
</html>
|