Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# Scikit-learn based Linear Regression Tutorial

### 1. About dataset

Generate 1-dimensional noisy data for linear regression of sinusoid.

Define the below pamameter in shard_config in the envoy_config.yaml file as the random seed for the dataset generation for a specific Envoy
- rank

### 2. About model

Linear Regression Lasso Model based on Scikit-learn.


### 3. How to run this tutorial (without TLC and locally as a simulation):

1. Run director:

```sh
cd director folder
./start_director.sh
```

2. Run envoy:

Step 1: Activate virtual environment and install packages
```
cd envoy folder
pip install -r requirements.txt
```
Step 2: start the envoy
```sh
./start_envoy.sh env_instance_1 envoy_config.yaml
```

Optional: start second envoy:

- Copy `envoy_folder` to another place and follow the same process as above:

```sh
./start_envoy.sh env_instance_2 envoy_config_2.yaml
```

3. Run `scikit_learn_linear_regression.ipynb` jupyter notebook:

```sh
cd workspace
jupyter lab scikit_learn_linear_regression.ipynb
```

4. Visualization

```
tensorboard --logdir logs/
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
settings:
listen_host: localhost
listen_port: 50050
sample_shape: ['1'] # Modify this param if experimenting with `n_features` of shard_descriptor.
target_shape: ['1']
envoy_health_check_period: 5 # in seconds
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/bash
set -e

fx director start --disable-tls -c director_config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
params:
cuda_devices: []

optional_plugin_components: {}

shard_descriptor:
template: linreg_shard_descriptor.LinRegSD
params:
rank: 1
n_samples: 80
noise: 0.15

Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# Copyright (C) 2020-2021 Intel Corporation
# SPDX-License-Identifier: Apache-2.0
"""Noisy-Sin Shard Descriptor."""

from typing import List

import numpy as np

from openfl.interface.interactive_api.shard_descriptor import ShardDescriptor


class LinRegSD(ShardDescriptor):
"""Shard descriptor class."""

def __init__(self, rank: int, n_samples: int = 10, noise: float = 0.15) -> None:
"""
Initialize LinReg Shard Descriptor.

This Shard Descriptor generate random data. Sample features are
floats between pi/3 and 5*pi/3, and targets are calculated
calculated as sin(feature) + normal_noise.
"""
np.random.seed(rank) # Setting seed for reproducibility
self.n_samples = max(n_samples, 5)
self.interval = 240
self.x_start = 60
x = np.random.rand(n_samples, 1) * self.interval + self.x_start
x *= np.pi / 180
y = np.sin(x) + np.random.normal(0, noise, size=(n_samples, 1))
self.data = np.concatenate((x, y), axis=1)

def get_dataset(self, dataset_type: str) -> np.ndarray:
"""
Return a shard dataset by type.

A simple list with elements (x, y) implemets the Shard Dataset interface.
"""
if dataset_type == 'train':
return self.data[:self.n_samples // 2]
elif dataset_type == 'val':
return self.data[self.n_samples // 2:]
else:
pass

@property
def sample_shape(self) -> List[str]:
"""Return the sample shape info."""
(*x, _) = self.data[0]
return [str(i) for i in np.array(x, ndmin=1).shape]

@property
def target_shape(self) -> List[str]:
"""Return the target shape info."""
(*_, y) = self.data[0]
return [str(i) for i in np.array(y, ndmin=1).shape]

@property
def dataset_description(self) -> str:
"""Return the dataset description."""
return 'Allowed dataset types are `train` and `val`'
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
openfl>=1.2.1
numpy>=1.13.3
scikit-learn>=0.24.1
matplotlib>=2.0.0
mistune>=2.0.3 # not directly required, pinned by Snyk to avoid a vulnerability
setuptools>=65.5.1 # not directly required, pinned by Snyk to avoid a vulnerability
wheel>=0.38.0 # not directly required, pinned by Snyk to avoid a vulnerability
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/bash
set -e
ENVOY_NAME=$1
ENVOY_CONF=$2

fx envoy start -n "$ENVOY_NAME" --disable-tls --envoy-config-path "$ENVOY_CONF" -dh localhost -dp 50050
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Copyright (C) 2020-2023 Intel Corporation
# SPDX-License-Identifier: Apache-2.0
"""Custom model numpy adapter."""

from openfl.plugins.frameworks_adapters.framework_adapter_interface import (
FrameworkAdapterPluginInterface,
)


class CustomFrameworkAdapter(FrameworkAdapterPluginInterface):
"""Framework adapter plugin class."""

@staticmethod
def get_tensor_dict(model, optimizer=None):
"""Extract tensors from a model."""
return {'w': model.weights}

@staticmethod
def set_tensor_dict(model, tensor_dict, optimizer=None, device='cpu'):
"""Load tensors to a model."""
model.weights = tensor_dict['w']
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
absl-py==1.4.0
aiofiles==22.1.0
aiosqlite==0.17.0
anyio==3.6.2
argon2-cffi==21.3.0
argon2-cffi-bindings==21.2.0
arrow==1.2.3
attrs==23.1.0
Babel==2.12.1
backcall==0.2.0
beautifulsoup4==4.12.2
bleach==6.0.0
cached-property==1.5.2
cachetools==5.3.0
certifi==2022.12.7
cffi==1.15.1
charset-normalizer==3.1.0
click==8.0.1
cloudpickle==2.2.1
colorama==0.4.6
commonmark==0.9.1
cryptography==40.0.2
cycler==0.11.0
debugpy==1.6.7
decorator==5.1.1
defusedxml==0.7.1
docker==6.0.1
dynaconf==3.1.7
entrypoints==0.4
exceptiongroup==1.1.1
fastjsonschema==2.16.3
flatbuffers==2.0.7
flatten-json==0.1.13
fonttools==4.38.0
fqdn==1.5.1
google-auth==2.17.3
google-auth-oauthlib==0.4.6
grpcio==1.34.1
grpcio-tools==1.34.1
idna==3.4
importlib-metadata==6.6.0
importlib-resources==5.12.0
iniconfig==2.0.0
ipykernel==6.16.2
ipython==7.34.0
ipython-genutils==0.2.0
ipywidgets==8.0.6
isoduration==20.11.0
jax==0.3.13
jaxlib==0.3.10
jedi==0.18.2
Jinja2==3.1.2
joblib==1.2.0
json5==0.9.11
jsonpointer==2.3
jsonschema==4.17.3
jupyter==1.0.0
jupyter-console==6.6.3
jupyter-events==0.6.3
jupyter-server==1.24.0
jupyter-ydoc==0.2.4
jupyter_client==7.4.9
jupyter_core==4.12.0
jupyter_server_fileid==0.9.0
jupyter_server_ydoc==0.8.0
jupyterlab==3.6.3
jupyterlab-pygments==0.2.2
jupyterlab-widgets==3.0.7
jupyterlab_server==2.22.1
kiwisolver==1.4.4
Markdown==3.4.3
markdown-it-py==2.2.0
MarkupSafe==2.1.2
matplotlib==3.5.3
matplotlib-inline==0.1.6
mdurl==0.1.2
mistune==2.0.5
nbclassic==0.5.5
nbclient==0.7.3
nbconvert==7.3.1
nbformat==5.8.0
nest-asyncio==1.5.6
notebook==6.5.4
notebook_shim==0.2.2
numpy==1.21.6
oauthlib==3.2.2
openfl==1.2.1
opt-einsum==3.3.0
packaging==23.1
pandas==1.3.5
pandocfilters==1.5.0
parso==0.8.3
pexpect==4.8.0
pickleshare==0.7.5
Pillow==9.5.0
pip==23.1.1
pkgutil_resolve_name==1.3.10
pluggy==1.0.0
prometheus-client==0.16.0
prompt-toolkit==3.0.38
protobuf==3.19.6
psutil==5.9.5
ptyprocess==0.7.0
pyasn1==0.5.0
pyasn1-modules==0.3.0
pycparser==2.21
Pygments==2.15.1
pyparsing==3.0.9
pyrsistent==0.19.3
pytest==7.3.1
python-dateutil==2.8.2
python-json-logger==2.0.7
pytz==2023.3
PyYAML==6.0
pyzmq==25.0.2
qtconsole==5.4.2
QtPy==2.3.1
requests==2.28.2
requests-oauthlib==1.3.1
rfc3339-validator==0.1.4
rfc3986-validator==0.1.1
rich==9.1.0
rsa==4.9
scikit-learn==1.0.2
scipy==1.7.3
Send2Trash==1.8.0
setuptools==67.7.1
six==1.16.0
sniffio==1.3.0
soupsieve==2.4.1
tensorboard==2.11.2
tensorboard-data-server==0.6.1
tensorboard-plugin-wit==1.8.1
tensorboardX==2.6
terminado==0.17.1
threadpoolctl==3.1.0
tinycss2==1.2.1
tomli==2.0.1
tornado==6.2
tqdm==4.65.0
traitlets==5.9.0
typing-extensions==3.10.0.2
uri-template==1.2.0
urllib3==1.26.15
wcwidth==0.2.6
webcolors==1.13
webencodings==0.5.1
websocket-client==1.5.1
Werkzeug==2.2.3
wheel==0.40.0
widgetsnbextension==4.0.7
y-py==0.5.9
ypy-websocket==0.8.2
zipp==3.15.0
Loading