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(3):
    for i in range(len(input)):
        print("Grads:" + str((model.learn(input=input[i],target=target[i],alpha=0.5))))


Grads:PaillierTensor: [[e e]
 [e e]
 [e e]
 [e e]]
Grads:PaillierTensor: [[e e]
 [e e]
 [e e]
 [e e]]
Grads:PaillierTensor: [[e e]
 [e e]
 [e e]
 [e e]]
Grads:PaillierTensor: [[e e]
 [e e]
 [e e]
 [e e]]
Grads:PaillierTensor: [[e e]
 [e e]
 [e e]
 [e e]]
Grads:PaillierTensor: [[e e]
 [e e]
 [e e]
 [e e]]
Grads:PaillierTensor: [[e e]
 [e e]
 [e e]
 [e e]]
Grads:PaillierTensor: [[e e]
 [e e]
 [e e]
 [e e]]
Grads:PaillierTensor: [[e e]
 [e e]
 [e e]
 [e e]]
Grads:PaillierTensor: [[e e]
 [e e]
 [e e]
 [e e]]
Grads:PaillierTensor: [[e e]
 [e e]
 [e e]
 [e e]]
Grads:PaillierTensor: [[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]))


BaseTensor: [ 0.26074219  0.26074219]
BaseTensor: [ 0.02050781  0.02050781]
BaseTensor: [ 1.08691406  1.08691406]
BaseTensor: [ 0.02050781  0.02050781]

In [ ]:


In [ ]: