Health Analysis with LatticaAI Demo Tutorial
Overview of the Model
Our Health Analysis model is trained on the Disease Prediction Kaggle dataset. This dataset is designed to facilitate the application of machine learning to the medical field, aiding physicians by automating disease diagnosis based on symptoms.
The dataset consists of 131 binary columns representing different symptoms that a person may experience, and maps symptoms to 41 different diseases, allowing classification based on input symptoms.
We trained multi-class logistic regression model, implemented the equivalent fully homomorphic model and deployed it to our cloud service for secure inference.
- Input Format: binary vector of length
131. - Output: Probability vector of length
41that represents the possible diseases.
The equivalent pytorch code for the inference is:
symptoms.jsonList of 131 possible symptoms
diseases.jsonList of 41 disease
coef.npyintercept.npyimport torch
import json
import numpy as np
# load the symptoms and diseases mappings
with open('symptoms.json', 'r') as f:
symptoms = json.load(f)
with open('diseases.json', 'r') as f:
diseases = json.load(f)
# load the coef matrix and intercept vector of the trained logistic regression model
W = np.load('coef.npy') # shape (41,131)
b = np.load('intercept.npy') # shape (41,)
# create binary vector of length 131, corresponding to the mapping symptoms.json
pt = torch.zeros(131, dtype=torch.float64)
pt[symptoms['Continuous Sneezing']] = 1
pt[symptoms['Shivering']] = 1
pt[symptoms['Chills']] = 1
pt[symptoms['Watering From Eyes']] = 1
# calculate the logistic regression prediction
res = torch.nn.functional.softmax(pt @ W.T + b, dim=0)
# the predicted disease according to the mapping diseases.json
disease = diseases[res.argmax().item()]
print(f"Predicted disease: {disease}")
Achieving Full Privacy with LatticaAI
First install our client package
- 🧊 Python SDK
- 📦 TypeScript SDK
import os
import torch
from lattica_query import QueryClient, QueryToken
# Set LATTICA_QUERY_TOKEN to a query token scoped to this demo model.
token = QueryToken(os.environ["LATTICA_QUERY_TOKEN"])
pt = torch.randint(0, 2, (131,), dtype=torch.float64)
with QueryClient(token) as client:
key = client.keys.ensure()
res = client.query.encrypted(pt, key=key)
import {
getDemoToken,
initWasm,
LatticaQueryClient,
vecToTensor,
} from '@lattica-ai/query';
initWasm();
const modelId = 'healthPrediction';
const token = await getDemoToken(modelId);
const client = new LatticaQueryClient(token);
// Generate the keys and register the evaluation key.
await client.init();
// Set selected symptom indexes to 1.
const symptoms = new Float64Array(131);
symptoms[0] = 1;
symptoms[4] = 1;
const pt = vecToTensor(Array.from(symptoms));
const encryptedInput = await client.encrypt(pt);
const encryptedResult = await client.apply_query(encryptedInput);
const result = await client.decrypt(encryptedResult);
See our step-by-step guide for a detailed explanation of each step in this flow.
To use the health analysis model, use the healthPrediction model ID.