data61 / MP-SPDZ

Versatile framework for multi-party computation
Other
941 stars 279 forks source link

How can I retrieve the output of the Dense layer (i.e., the result of the linear computation 𝑦 = 𝑚 𝑥 + 𝑐 y=mx+c) in the logistic regression model after loading it, and decrypt this output? #1528

Open danijimmy19 opened 4 days ago

danijimmy19 commented 4 days ago

I trained the logistic regression model using the MP-SPDZ library's ml.GDLogistic() function. After loading the trained model using the following code:

# load the model and the parameters
print_ln('load the trained model ...')
f = open('Player-Data/Binary-Output-P0-0')
lr_model = np.fromfile(f, "double", count=4) # where 30, 4, 784 is the shape of feature vector for 1 datapoint
log.opt.trainable_variables[0].assign(sfix.input_tensor_via(0, lr_model))
print_ln("weights:")
print_ln('%s', (log.opt.layers[0].W).reveal())

lr_bias = np.fromfile(f, "double", count=4)
log.opt.trainable_variables[1].assign(sfix.input_tensor_via(0, lr_bias))
print_ln("bias: ")
print_ln('%s', (log.opt.layers[0].b).reveal())

After loading the model, I compute the accuracy of the model. However, I want to access the output of the Dense layer, which is the linear combination of inputs and weights (i.e., y=mx+c). How can I get this linear output from the model and decrypt it?

Meaning, How can I compute the time for the logistic regression function where only the y=mx+c is computed and discard the final prediction layer?

Dense -> Sigmoid -> Output

How can I compute the time for only Dense layer here?

Time(Dense) -> Sigmoid -> Output

This below snippet will compute the time for Dense -> Sigmoid -> Output

start_timer(timer_id=2)
n_correct, loss = log.opt.reveal_correctness(x_test, y_test, 128)
print_ln('Secure accuracy (testing): %s (%s/%s)', cfix(n_correct) / len(x_test), n_correct, len(x_test))
stop_timer(timer_id=2)
mkskeller commented 2 days ago

The inputs and outputs to every layer are in the X and Y members, so you should able to find the intermediate results at log.opt.layers[0].Y.

For the timing, you can set log.opt.time_layers = True. This will use the timer 100+i for the i-th layer, i.e., timer 100 will measure the time for the linear layer.