From 75635fdb0f3e74a5c48d5d9c5ce62a2c49076cde Mon Sep 17 00:00:00 2001 From: qiaolongfei Date: Wed, 13 Sep 2017 18:12:12 -0700 Subject: [PATCH] save topology for chapter 1 --- 01.fit_a_line/README.cn.md | 10 ++++++++++ 01.fit_a_line/README.md | 11 +++++++++++ 01.fit_a_line/index.cn.html | 10 ++++++++++ 01.fit_a_line/index.html | 11 +++++++++++ 01.fit_a_line/train.py | 9 +++++---- 5 files changed, 47 insertions(+), 4 deletions(-) diff --git a/01.fit_a_line/README.cn.md b/01.fit_a_line/README.cn.md index 30e78a4..1bb070d 100644 --- a/01.fit_a_line/README.cn.md +++ b/01.fit_a_line/README.cn.md @@ -128,6 +128,16 @@ y_predict = paddle.layer.fc(input=x, y = paddle.layer.data(name='y', type=paddle.data_type.dense_vector(1)) cost = paddle.layer.mse_cost(input=y_predict, label=y) ``` + +### 保存网络拓扑 + +```python +# Save the inference topology to protobuf. +inference_topology = paddle.topology.Topology(layers=y_predict) +with open("inference_topology.pkl", 'wb') as f: + inference_topology.serialize_for_inference(f) +``` + ### 创建参数 ```python diff --git a/01.fit_a_line/README.md b/01.fit_a_line/README.md index 814cd1c..fc531f5 100644 --- a/01.fit_a_line/README.md +++ b/01.fit_a_line/README.md @@ -134,6 +134,17 @@ y_predict = paddle.layer.fc(input=x, y = paddle.layer.data(name='y', type=paddle.data_type.dense_vector(1)) cost = paddle.layer.mse_cost(input=y_predict, label=y) ``` + +### Save Topology + +```python +# Save the inference topology to protobuf. +inference_topology = paddle.topology.Topology(layers=y_predict) +with open("inference_topology.pkl", 'wb') as f: + inference_topology.serialize_for_inference(f) +``` + + ### Create Parameters ```python diff --git a/01.fit_a_line/index.cn.html b/01.fit_a_line/index.cn.html index 933e5a4..7702f81 100644 --- a/01.fit_a_line/index.cn.html +++ b/01.fit_a_line/index.cn.html @@ -170,6 +170,16 @@ y_predict = paddle.layer.fc(input=x, y = paddle.layer.data(name='y', type=paddle.data_type.dense_vector(1)) cost = paddle.layer.mse_cost(input=y_predict, label=y) ``` + +### 保存网络拓扑 + +```python +# Save the inference topology to protobuf. +inference_topology = paddle.topology.Topology(layers=y_predict) +with open("inference_topology.pkl", 'wb') as f: + inference_topology.serialize_for_inference(f) +``` + ### 创建参数 ```python diff --git a/01.fit_a_line/index.html b/01.fit_a_line/index.html index f143f7f..df9d348 100644 --- a/01.fit_a_line/index.html +++ b/01.fit_a_line/index.html @@ -176,6 +176,17 @@ y_predict = paddle.layer.fc(input=x, y = paddle.layer.data(name='y', type=paddle.data_type.dense_vector(1)) cost = paddle.layer.mse_cost(input=y_predict, label=y) ``` + +### Save Topology + +```python +# Save the inference topology to protobuf. +inference_topology = paddle.topology.Topology(layers=y_predict) +with open("inference_topology.pkl", 'wb') as f: + inference_topology.serialize_for_inference(f) +``` + + ### Create Parameters ```python diff --git a/01.fit_a_line/train.py b/01.fit_a_line/train.py index 255180d..15f94f9 100644 --- a/01.fit_a_line/train.py +++ b/01.fit_a_line/train.py @@ -12,6 +12,11 @@ def main(): y = paddle.layer.data(name='y', type=paddle.data_type.dense_vector(1)) cost = paddle.layer.square_error_cost(input=y_predict, label=y) + # Save the inference topology to protobuf. + inference_topology = paddle.topology.Topology(layers=y_predict) + with open("inference_topology.pkl", 'wb') as f: + inference_topology.serialize_for_inference(f) + # create parameters parameters = paddle.parameters.create(cost) @@ -21,10 +26,6 @@ def main(): trainer = paddle.trainer.SGD( cost=cost, parameters=parameters, update_equation=optimizer) - # save model proto as file - with open("model.proto", "w") as f: - f.write(str(trainer.__topology_in_proto__)) - feeding = {'x': 0, 'y': 1} # event_handler to print training and testing info -- GitLab