Paillier Encrypted Linear Classification Example

DISCLAIMER: This is a proof-of-concept implementation. It does not represent a remotely product ready implementation or follow proper conventions for security, convenience, or scalability. It is part of a broader proof-of-concept demonstrating the vision of the OpenMined project, its major moving parts, and how they might work together.


In [1]:
from syft.he.Paillier import KeyPair
from syft.nn.linear import LinearClassifier
import numpy as np

In [2]:
pubkey,prikey = KeyPair().generate(n_length=1024)

In [3]:
model = LinearClassifier(n_inputs=4,n_labels=2).encrypt(pubkey)

In [4]:
input = np.array([[0,0,1,1],[0,0,1,0],[1,0,1,1],[0,0,1,0]])
target = np.array([[0,1],[0,0],[1,1],[0,0]])

In [5]:
for iter in range(4):
    for i in range(len(input)):
        print("Grads:" + str((model.learn(input=input[i],target=target[i],alpha=0.5))))


Grads:[[e e]
 [e e]
 [e e]
 [e e]]
Grads:[[e e]
 [e e]
 [e e]
 [e e]]
Grads:[[e e]
 [e e]
 [e e]
 [e e]]
Grads:[[e e]
 [e e]
 [e e]
 [e e]]
Grads:[[e e]
 [e e]
 [e e]
 [e e]]
Grads:[[e e]
 [e e]
 [e e]
 [e e]]
Grads:[[e e]
 [e e]
 [e e]
 [e e]]
Grads:[[e e]
 [e e]
 [e e]
 [e e]]
Grads:[[e e]
 [e e]
 [e e]
 [e e]]
Grads:[[e e]
 [e e]
 [e e]
 [e e]]
Grads:[[e e]
 [e e]
 [e e]
 [e e]]
Grads:[[e e]
 [e e]
 [e e]
 [e e]]
Grads:[[e e]
 [e e]
 [e e]
 [e e]]
Grads:[[e e]
 [e e]
 [e e]
 [e e]]
Grads:[[e e]
 [e e]
 [e e]
 [e e]]
Grads:[[e e]
 [e e]
 [e e]
 [e e]]

In [6]:
model = model.decrypt(prikey)

In [7]:
for i in range(len(input)):
    print(model.forward(input[i]))


[0.17156982421875 0.8747100830078125]
[0.00225830078125 0.0154266357421875]
[1.05718994140625 0.9582366943359375]
[0.00225830078125 0.0154266357421875]

In [ ]:


In [ ]: