-
Notifications
You must be signed in to change notification settings - Fork 147
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update tensorflow writing algorithm and research
- Loading branch information
1 parent
7c148e4
commit e6c7ada
Showing
13 changed files
with
134 additions
and
251 deletions.
There are no files selected for viewing
2 changes: 1 addition & 1 deletion
2
...ng Algorithms/31 Machine Learning/03 Popular Libraries/08 Tensorflow/01 Introduction.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
<p>This page explains how to build, train, deploy and store <code>Tensorflow</code> v1 models. To view the tutorial on Tensorflow 2, see <a href="/docs/v2/writing-algorithms/machine-learning/popular-libraries/keras">Keras</a>.</p> | ||
<p>This page explains how to build, train, deploy and store <code>Tensorflow</code> models.</p> |
14 changes: 3 additions & 11 deletions
14
...lgorithms/31 Machine Learning/03 Popular Libraries/08 Tensorflow/02 Import Libraries.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,6 @@ | ||
<p>Import the <code>tensorflow</code> libraries.</p> | ||
<p>Import the <code>tensorflow</code> and <code>sklearn</code> libraries.</p> | ||
|
||
<div class="section-example-container"> | ||
<pre class="python">from AlgorithmImports import * | ||
import tensorflow.compat.v1 as tf | ||
from google.protobuf import json_format | ||
import json5 | ||
|
||
tf.disable_v2_behavior()</pre> | ||
</div> | ||
|
||
<p>You need the <code>google.protobuf</code> and <code>json5</code> libraries to store and load models.</p> | ||
|
||
<p>Disable <code>tensorflow</code> v2 behaviors in order to deploy a v1 model.</p> | ||
import tensorflow as tf</pre> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 3 additions & 3 deletions
6
... Algorithms/31 Machine Learning/03 Popular Libraries/08 Tensorflow/06 Predict Labels.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 13 additions & 10 deletions
23
...ing Algorithms/31 Machine Learning/03 Popular Libraries/08 Tensorflow/07 Save Models.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,25 @@ | ||
<p>Follow these steps to save <code>Tensorflow</code> models into the <a href="/docs/v2/writing-algorithms/object-store">Object Store</a>:</p> | ||
|
||
<ol> | ||
<li>Export the <code>TensorFlow</code> graph as a JSON object.</li> | ||
<li>Set the key name of the model to be stored in the Object Store.</li> | ||
<div class="section-example-container"> | ||
<pre class="python">graph_definition = tf.compat.v1.train.export_meta_graph() | ||
json_graph = json_format.MessageToJson(graph_definition)</pre> | ||
<pre class="python">model_key = "model.keras"</pre> | ||
</div> | ||
<p>Note that the model has to have the suffix <code>.keras</code>.</p> | ||
|
||
<li>Export the <code>TensorFlow</code> weights as a JSON object.</li> | ||
<li>Call the <code>GetFilePath</code> method with the key.</li> | ||
<div class="section-example-container"> | ||
<pre class="python">weights = self.model.run(tf.compat.v1.trainable_variables()) | ||
weights = [w.tolist() for w in weights] | ||
json_weights = json5.dumps(weights)</pre> | ||
<pre class="python">file_name = self.ObjectStore.GetFilePath(model_key)</pre> | ||
</div> | ||
<p>This method returns the file path where the model will be stored.</p> | ||
|
||
<li>Save the graph and weights to the Object Store.</li> | ||
<li>Call the <code>save</code> method with the model and file path.</li> | ||
<div class="section-example-container"> | ||
<pre class="python">self.ObjectStore.Save('graph', json_graph) | ||
self.ObjectStore.Save('weights', json_weights)</pre> | ||
<pre class="python">model.save(file_name)</pre> | ||
</div> | ||
|
||
<li>Save the model to the file path.</li> | ||
<div class="section-example-container"> | ||
<pre class="python">self.ObjectStore.Save(model_key)</pre> | ||
</div> | ||
</ol> |
35 changes: 5 additions & 30 deletions
35
...ing Algorithms/31 Machine Learning/03 Popular Libraries/08 Tensorflow/08 Load Models.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,10 @@ | ||
<p>You can load and trade with pre-trained <code>tensorflow</code> models that you saved in the Object Store. To load a <code>tensorflow</code> model from the Object Store, in the <code>Initialize</code> method, get the file path to the saved model and then recall the graph and weights of the model.</p> | ||
<div class="section-example-container"> | ||
<pre class="python">def Initialize(self) -> None: | ||
if self.ObjectStore.ContainsKey('graph') and self.ObjectStore.ContainsKey('weights'): | ||
json_graph = self.ObjectStore.Read('graph') | ||
json_weights = self.ObjectStore.Read('weights') | ||
|
||
# Restore the tensorflow graph from JSON objects | ||
tf.reset_default_graph() | ||
graph_definition = json_format.Parse(json_graph, tf.MetaGraphDef()) | ||
self.model = tf.Session() | ||
tf.train.import_meta_graph(graph_definition) | ||
|
||
# Select the input, output tensors and optimizer | ||
self.X = tf.get_default_graph().get_tensor_by_name('X:0') | ||
self.Y = tf.get_default_graph().get_tensor_by_name('Y:0') | ||
self.output = tf.get_default_graph().get_tensor_by_name('outer:0') | ||
self.optimizer = tf.get_default_graph().get_collection('Variable/Adam') | ||
|
||
# Restore the model weights from the JSON object. | ||
weights = [np.asarray(x) for x in json5.loads(json_weights)] | ||
assign_ops = [] | ||
feed_dict = {} | ||
vs = tf.trainable_variables() | ||
zipped_values = zip(vs, weights) | ||
for var, value in zipped_values: | ||
value = np.asarray(value) | ||
assign_placeholder = tf.placeholder(var.dtype, shape=value.shape) | ||
assign_op = var.assign(assign_placeholder) | ||
assign_ops.append(assign_op) | ||
feed_dict[assign_placeholder] = value | ||
self.model.run(assign_ops, feed_dict=feed_dict)</pre> | ||
model_key = 'model.keras' | ||
if self.ObjectStore.ContainsKey(model_key): | ||
file_name = self.ObjectStore.GetFilePath(model_key) | ||
self.model = tf.keras.models.load_model(file_name)</pre> | ||
</div> | ||
|
||
<p>The <code>ContainsKey</code> method returns a boolean that represents if the <code>graph</code> and <code>weights</code> is in the Object Store. If the Object Store doesn't contain the keys, save the model using them before you proceed.</p> | ||
<p>The <code>ContainsKey</code> method returns a boolean that represents if the <code>model.keras</code> is in the Object Store. If the Object Store doesn't contain the keys, save the model using them before you proceed.</p> |
2 changes: 1 addition & 1 deletion
2
...ms/31 Machine Learning/03 Popular Libraries/08 Tensorflow/09 Clone Example Algorithm.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
<div class="qc-embed-frame" style="display: inline-block; position: relative; width: 100%; min-height: 100px; min-width: 300px;"> | ||
<div class="qc-embed-dummy" style="padding-top: 56.25%;"></div> | ||
<div class="qc-embed-element" style="position: absolute; top: 0; bottom: 0; left: 0; right: 0;"> | ||
<iframe class="qc-embed-backtest" height="100%" width="100%" style="border: 1px solid #ccc; padding: 0; margin: 0;" src="https://www.quantconnect.com/terminal/processCache?request=embedded_backtest_81f30fb424e8dffe1ed14741d6362dd3.html"></iframe> | ||
<iframe class="qc-embed-backtest" height="100%" width="100%" style="border: 1px solid #ccc; padding: 0; margin: 0;" src="https://www.quantconnect.com/terminal/processCache?request=embedded_backtest_7a48d7fec6a0ba0724aeb843fc612d0e.html"></iframe> | ||
</div> | ||
</div> |
8 changes: 3 additions & 5 deletions
8
04 Research Environment/08 Machine Learning/03 TensorFlow/02 Import Libraries.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,8 @@ | ||
<p>Import the <code>tensorflow</code>, <code>sklearn</code>, <code>json5</code> and <code>google.protobuf</code> libraries.</p> | ||
<p>Import the <code>tensorflow</code>, and <code>sklearn</code> libraries.</p> | ||
|
||
<div class="section-example-container"> | ||
<pre class="python">import tensorflow as tf | ||
from sklearn.model_selection import train_test_split | ||
import json5 | ||
from google.protobuf import json_format</pre> | ||
from sklearn.model_selection import train_test_split</pre> | ||
</div> | ||
|
||
<p>You need the <code>sklearn</code> library to prepare the data and the <code>json5</code> and <code>google.protobuf</code> libraries to save models.</p> | ||
<p>You need the <code>sklearn</code> library to prepare the data.</p> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.