Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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 this issue Nov 8, 2024 · 1 comment

Comments

@danijimmy19
Copy link

danijimmy19 commented Nov 8, 2024

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
Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants