Skip to content

Commit 81c8e44

Browse files
Reorganized prediction section
1 parent b8ba2c8 commit 81c8e44

File tree

1 file changed

+56
-71
lines changed

1 file changed

+56
-71
lines changed

articles/machine-learning/how-to-homomorphic-encryption-seal.md

Lines changed: 56 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -231,104 +231,89 @@ Use the test dataset with the model to get predictions.
231231

232232
To make encrypted predictions:
233233

234-
1. Create our Homomorphic Encryption based client
235-
1. Upload Homomorphic Encryption generated public keys
236-
1. Encrypt the data
237-
1. Send the data as JSON to the web service hosted in ACI
238-
1. Use the SDK's `run` API to invoke the service. You can also make raw calls using any HTTP tool such as curl.
234+
1. Create a new `EILinearRegressionClient`, a homomorphic encryption based client, and public keys.
239235

240-
### Create Homomorphic Encryption based client
241-
242-
Create a new `EILinearRegressionClient` and create the public keys.
243-
244-
```python
245-
from encrypted.inference.eiclient import EILinearRegressionClient
246-
247-
# Create a new Encrypted inference client and a new secret key.
248-
edp = EILinearRegressionClient(verbose=True)
249-
250-
public_keys_blob, public_keys_data = edp.get_public_keys()
251-
252-
```
253-
254-
#### Upload HE generated public keys
236+
```python
237+
from encrypted.inference.eiclient import EILinearRegressionClient
255238

256-
Upload the public keys to the workspace default blob store. This will allow you to share the keys with the inference server.
239+
# Create a new Encrypted inference client and a new secret key.
240+
edp = EILinearRegressionClient(verbose=True)
257241

258-
```python
259-
import azureml.core
260-
from azureml.core import Workspace, Datastore
261-
import os
242+
public_keys_blob, public_keys_data = edp.get_public_keys()
243+
```
262244

263-
ws = Workspace.from_config()
245+
1. Upload homomorphic encryption generated public keys to the workspace default blob store. This will allow you to share the keys with the inference server.
264246

265-
datastore = ws.get_default_datastore()
266-
container_name=datastore.container_name
247+
```python
248+
import azureml.core
249+
from azureml.core import Workspace, Datastore
250+
import os
267251

268-
# Create a local file and write the keys to it
269-
public_keys = open(public_keys_blob, "wb")
270-
public_keys.write(public_keys_data)
271-
public_keys.close()
252+
ws = Workspace.from_config()
272253

273-
# Upload the file to blob store
274-
datastore.upload_files([public_keys_blob])
254+
datastore = ws.get_default_datastore()
255+
container_name=datastore.container_name
275256

276-
# Delete the local file
277-
os.remove(public_keys_blob)
278-
```
257+
# Create a local file and write the keys to it
258+
public_keys = open(public_keys_blob, "wb")
259+
public_keys.write(public_keys_data)
260+
public_keys.close()
279261

280-
### Encrypt the test data
262+
# Upload the file to blob store
263+
datastore.upload_files([public_keys_blob])
281264

282-
```python
283-
#choose any one sample from the test data
284-
sample_index = 1
265+
# Delete the local file
266+
os.remove(public_keys_blob)
267+
```
285268

286-
#encrypt the data
287-
raw_data = edp.encrypt(X_test[sample_index])
269+
1. Encrypt the test data
288270

289-
```
271+
```python
272+
#choose any one sample from the test data
273+
sample_index = 1
290274

291-
### Send the test data to the encrypted ACI web service
275+
#encrypt the data
276+
raw_data = edp.encrypt(X_test[sample_index])
292277

293-
Provide the test dataset to the model to get predictions. We will need to send the connection string to the blob storage where the public keys were uploaded.
278+
```
294279

295-
```python
296-
import json
297-
from azureml.core import Webservice
280+
1. Use the SDK's `run` API to invoke the service and provide the test dataset to the model to get predictions. We will need to send the connection string to the blob storage where the public keys were uploaded.
298281

299-
service = Webservice(ws, 'sklearn-encrypted-mnist-svc')
282+
```python
283+
import json
284+
from azureml.core import Webservice
300285

301-
#pass the connection string for blob storage to give the server access to the uploaded public keys
302-
conn_str_template = 'DefaultEndpointsProtocol={};AccountName={};AccountKey={};EndpointSuffix=core.windows.net'
303-
conn_str = conn_str_template.format(datastore.protocol, datastore.account_name, datastore.account_key)
286+
service = Webservice(ws, 'sklearn-encrypted-mnist-svc')
304287

305-
#build the json
306-
data = json.dumps({"data": raw_data, "key_id" : public_keys_blob, "conn_str" : conn_str, "container" : container_name })
307-
data = bytes(data, encoding='ASCII')
288+
#pass the connection string for blob storage to give the server access to the uploaded public keys
289+
conn_str_template = 'DefaultEndpointsProtocol={};AccountName={};AccountKey={};EndpointSuffix=core.windows.net'
290+
conn_str = conn_str_template.format(datastore.protocol, datastore.account_name, datastore.account_key)
308291

309-
print ('Making an encrypted inference web service call ')
310-
eresult = service.run(input_data=data)
292+
#build the json
293+
data = json.dumps({"data": raw_data, "key_id" : public_keys_blob, "conn_str" : conn_str, "container" : container_name })
294+
data = bytes(data, encoding='ASCII')
311295

312-
print ('Received encrypted inference results')
313-
```
296+
print ('Making an encrypted inference web service call ')
297+
eresult = service.run(input_data=data)
314298

315-
### Decrypt the prediction
299+
print ('Received encrypted inference results')
300+
```
316301

317-
Use the client to decrypt the results.
302+
1. Use the client to decrypt the results.
318303

319-
```python
320-
import numpy as np
304+
```python
305+
import numpy as np
321306

322-
results = edp.decrypt(eresult)
307+
results = edp.decrypt(eresult)
323308

324-
print ('Decrypted the results ', results)
309+
print ('Decrypted the results ', results)
325310

326-
#Apply argmax to identify the prediction result
327-
prediction = np.argmax(results)
311+
#Apply argmax to identify the prediction result
312+
prediction = np.argmax(results)
328313

329-
print ( ' Prediction : ', prediction)
330-
print ( ' Actual Label : ', y_test[sample_index])
331-
```
314+
print ( ' Prediction : ', prediction)
315+
print ( ' Actual Label : ', y_test[sample_index])
316+
```
332317

333318
## Clean up resources
334319

0 commit comments

Comments
 (0)