test_net.py 1.6 KB
Newer Older
J
jingqinghe 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import env_set
import numpy as np
import paddle.fluid as fluid
J
update  
jingqinghe 已提交
18
import paddle_fl.mpc as pfl_mpc
J
jingqinghe 已提交
19 20 21

role, server, port = env_set.TestOptions().values()

J
update  
jingqinghe 已提交
22
pfl_mpc.init("aby3", int(role), "localhost", server, int(port))
J
jingqinghe 已提交
23 24 25 26

batch_size = 3

# x is in cypher text type
J
update  
jingqinghe 已提交
27
x = pfl_mpc.data(name='x', shape=[batch_size, 8], dtype='int64')
J
jingqinghe 已提交
28
# y is in cypher text type
J
update  
jingqinghe 已提交
29
y = pfl_mpc.data(name='y', shape=[batch_size, 1], dtype='int64')
J
jingqinghe 已提交
30

J
update  
jingqinghe 已提交
31 32 33 34
y_pre = pfl_mpc.layers.fc(input=x, size=1, act=None)
y_relu = pfl_mpc.layers.relu(input=y_pre)
cost = pfl_mpc.layers.square_error_cost(input=y_relu, label=y)
avg_loss = pfl_mpc.layers.mean(cost)
J
jingqinghe 已提交
35

J
update  
jingqinghe 已提交
36
optimizer = pfl_mpc.optimizer.SGD(learning_rate=0.001)
J
jingqinghe 已提交
37 38 39 40 41 42 43 44 45 46 47
optimizer.minimize(avg_loss)

exe = fluid.Executor(place=fluid.CPUPlace())
exe.run(fluid.default_startup_program())

iters = 1
for _ in range(iters):
    d_1 = np.ones(shape=(2, batch_size, 8), dtype='int64')
    d_2 = np.zeros(shape=(2, batch_size, 1), dtype='int64')
    loss = exe.run(feed={'x': d_1, 'y': d_2}, fetch_list=[avg_loss])
    print(loss)