-
Notifications
You must be signed in to change notification settings - Fork 2
/
example_01_singleSGD.py
43 lines (33 loc) · 1.04 KB
/
example_01_singleSGD.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import numpy as np
from src.model import FederatedSGDClassifier
from src.utils import get_logger
from sklearn.datasets import load_iris
LOGGER = get_logger('Example Single SGD')
def step_example():
X, y = load_iris(return_X_y=True)
sgd = FederatedSGDClassifier(n_classes=3, n_features=4)
# Initialize SGD
sgd.set_weights(sgd.generate_weights())
# Train SGD
sgd.run_training_epoch(X, y)
# Evaluate SGD
metrics = sgd.evaluate(X, y)
LOGGER.info(metrics)
def loop_example(loops=100):
X, y = load_iris(return_X_y=True)
sgd = FederatedSGDClassifier(n_classes=3, n_features=4)
# Initialize SGD
sgd.set_weights(sgd.generate_weights())
for _ in range(loops):
# Train SGD
sgd.run_training_epoch(X, y)
# Evaluate SGD
metrics = sgd.evaluate(X, y)
LOGGER.info(metrics)
if __name__ =='__main__':
LOGGER.info('Running one step example')
step_example()
LOGGER.info('Running loop (100 steps) example')
loop_example()