diff --git a/python/examples/fit_a_line/local_train.py b/python/examples/fit_a_line/local_train.py new file mode 100644 index 0000000000000000000000000000000000000000..699ad7b67d5bb2a8f8937418a002e19991058473 --- /dev/null +++ b/python/examples/fit_a_line/local_train.py @@ -0,0 +1,36 @@ +import sys +import paddle +import paddle.fluid as fluid + +train_reader = paddle.batch(paddle.reader.shuffle( + paddle.dataset.uci_housing.train(), buf_size=500), batch_size=16) + +test_reader = paddle.batch(paddle.reader.shuffle( + paddle.dataset.uci_housing.test(), buf_size=500), batch_size=16) + +x = fluid.data(name='x', shape=[None, 13], dtype='float32') +y = fluid.data(name='y', shape=[None, 1], dtype='float32') + +y_predict = fluid.layers.fc(input=x, size=1, act=None) +cost = fluid.layers.square_error_cost(input=y_predict, label=y) +avg_loss = fluid.layers.mean(cost) +sgd_optimizer = fluid.optimizer.SGD(learning_rate=0.01) +sgd_optimizer.minimize(avg_loss) + +place = fluid.CPUPlace() +feeder = fluid.DataFeeder(place=place, feed_list=[x, y]) +exe = fluid.Executor(place) +exe.run(fluid.default_startup_program()) + +import paddle_serving_client.io as serving_io + +for pass_id in range(30): + for data_train in train_reader(): + avg_loss_value, = exe.run( + fluid.default_main_program(), + feed=feeder.feed(data_train), + fetch_list=[avg_loss]) + +serving_io.save_model( + "serving_server_model", "serving_client_conf", + {"x": x}, {"y": y_predict}, fluid.default_main_program()) diff --git a/python/examples/fit_a_line/test_client.py b/python/examples/fit_a_line/test_client.py new file mode 100644 index 0000000000000000000000000000000000000000..e4aa9cd51b953349b88e8ff707ac7ad2bea168f7 --- /dev/null +++ b/python/examples/fit_a_line/test_client.py @@ -0,0 +1,15 @@ +from paddle_serving_client import Client +import paddle +import sys + +client = Client() +client.load_client_config(sys.argv[1]) +client.connect(["127.0.0.1:9292"]) + +test_reader = paddle.batch(paddle.reader.shuffle( + paddle.dataset.uci_housing.test(), buf_size=500), batch_size=1) + +for data in test_reader(): + fetch_map = client.predict(feed={"x": data[0][0]}, fetch=["y"]) + print("{} {}".format(fetch_map["y"][0], data[0][1][0])) + diff --git a/python/examples/fit_a_line/test_server.py b/python/examples/fit_a_line/test_server.py new file mode 100644 index 0000000000000000000000000000000000000000..e77c5fb6a4842bbc455193c67d8494ee6231c90f --- /dev/null +++ b/python/examples/fit_a_line/test_server.py @@ -0,0 +1,19 @@ +import os +import sys +from paddle_serving_server import OpMaker +from paddle_serving_server import OpSeqMaker +from paddle_serving_server import Server + +op_maker = OpMaker() +read_op = op_maker.create('general_reader') +general_infer_op = op_maker.create('general_infer') + +op_seq_maker = OpSeqMaker() +op_seq_maker.add_op(read_op) +op_seq_maker.add_op(general_infer_op) + +server = Server() +server.set_op_sequence(op_seq_maker.get_op_sequence()) +server.load_model_config(sys.argv[1]) +server.prepare_server(workdir="work_dir1", port=9292, device="cpu") +server.run_server() diff --git a/python/paddle_serving_client/io/__init__.py b/python/paddle_serving_client/io/__init__.py index f3c41d7625d0e444d3ab5b584bb1b2d775700612..8faab294ac33f18d257a5b13ca8e68326c342418 100644 --- a/python/paddle_serving_client/io/__init__.py +++ b/python/paddle_serving_client/io/__init__.py @@ -71,6 +71,10 @@ def save_model(server_model_folder, fout.write(str(config)) with open("{}/serving_server_conf.prototxt".format(server_model_folder), "w") as fout: fout.write(str(config)) + with open("{}/serving_client_conf.stream.prototxt".format(client_config_folder), "wb") as fout: + fout.write(config.serialize_to_string()) + with open("{}/serving_server_conf.stream.prototxt".format(server_model_folder), "wb") as fout: + fout.write(config.serialize_to_string())