Merge pull request #2 from ShuyaFeng/shuya
Browse files- README.md +120 -62
- app/routes.py +77 -10
- app/static/css/styles.css +21 -0
- app/static/js/main.js +215 -23
- app/templates/index.html +16 -0
- app/training/mock_trainer.py +205 -50
- app/training/real_trainer.py +294 -0
- app/training/simplified_real_trainer.py +411 -0
- requirements.txt +4 -1
- run.py +12 -1
- test_training.py +142 -0
README.md
CHANGED
@@ -1,77 +1,135 @@
|
|
1 |
-
# DP-SGD
|
2 |
-
|
3 |
-
An interactive web application for exploring
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
|
5 |
## Features
|
6 |
|
7 |
-
- Interactive
|
8 |
-
-
|
9 |
-
-
|
10 |
-
- Training
|
11 |
-
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
- Python 3.8 or higher
|
16 |
-
- Modern web browser (Chrome, Firefox, Safari, or Edge)
|
17 |
|
18 |
## Quick Start
|
19 |
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
cd dpsgd-explorer
|
24 |
-
```
|
25 |
-
|
26 |
-
2. Run the start script:
|
27 |
-
```bash
|
28 |
-
./start_server.sh
|
29 |
-
```
|
30 |
|
31 |
-
|
32 |
-
```
|
33 |
-
http://127.0.0.1:5000
|
34 |
-
```
|
35 |
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
## Manual Setup (if the script doesn't work)
|
43 |
-
|
44 |
-
1. Create a virtual environment:
|
45 |
-
```bash
|
46 |
-
python3 -m venv .venv
|
47 |
-
source .venv/bin/activate # On Windows: .venv\Scripts\activate
|
48 |
-
```
|
49 |
|
50 |
2. Install dependencies:
|
51 |
-
|
52 |
-
|
53 |
-
```
|
54 |
-
|
55 |
-
3. Start the server:
|
56 |
-
```bash
|
57 |
-
PYTHONPATH=. python3 run.py
|
58 |
-
```
|
59 |
-
|
60 |
-
## Project Structure
|
61 |
-
|
62 |
```
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
β βββ training/ # Training simulation
|
68 |
-
β βββ routes.py # Flask routes
|
69 |
-
β βββ __init__.py # App initialization
|
70 |
-
βββ requirements.txt # Python dependencies
|
71 |
-
βββ run.py # Application entry point
|
72 |
-
βββ start_server.sh # Start script
|
73 |
```
|
74 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
75 |
## License
|
76 |
|
77 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# DP-SGD Interactive Playground
|
2 |
+
|
3 |
+
An interactive web application for exploring Differentially Private Stochastic Gradient Descent (DP-SGD) training. This tool helps users understand the privacy-utility trade-offs in privacy-preserving machine learning through realistic simulations and visualizations.
|
4 |
+
|
5 |
+
## π Recent Improvements (v2.0)
|
6 |
+
|
7 |
+
### Enhanced Chart Visualization
|
8 |
+
- **Clearer dual-axis charts**: Improved color coding and styling to distinguish accuracy (green, solid line) from loss (red, dashed line)
|
9 |
+
- **Better scaling**: Separate colored axes with appropriate ranges (0-100% for accuracy, 0-3 for loss)
|
10 |
+
- **Enhanced tooltips**: More informative hover information with better formatting
|
11 |
+
- **Visual differentiation**: Added point styles, line weights, and backgrounds for clarity
|
12 |
+
|
13 |
+
### Realistic DP-SGD Training Data
|
14 |
+
- **Research-based accuracy ranges**:
|
15 |
+
- Ξ΅=1: 60-72% accuracy (high privacy)
|
16 |
+
- Ξ΅=2-3: 75-85% accuracy (balanced)
|
17 |
+
- Ξ΅=8: 85-90% accuracy (lower privacy)
|
18 |
+
- **Consistent training progress**: Final metrics now match training chart progression
|
19 |
+
- **Realistic learning curves**: Exponential improvement with noise-dependent variation
|
20 |
+
- **Proper privacy degradation**: Higher noise multipliers significantly impact performance
|
21 |
+
|
22 |
+
### Improved Parameter Recommendations
|
23 |
+
- **Noise multiplier guidance**: Optimal range Ο = 0.8-1.5 for good trade-offs
|
24 |
+
- **Batch size recommendations**: β₯128 for DP-SGD stability
|
25 |
+
- **Learning rate advice**: β€0.02 for noisy training environments
|
26 |
+
- **Epochs guidance**: 8-20 epochs for good convergence vs privacy cost
|
27 |
+
|
28 |
+
### Dynamic Privacy-Utility Display
|
29 |
+
- **Real-time privacy budget**: Shows calculated Ξ΅ values based on actual parameters
|
30 |
+
- **Context-aware assessments**: Different recommendations based on achieved accuracy
|
31 |
+
- **Educational messaging**: Helps users understand what constitutes good/poor trade-offs
|
32 |
|
33 |
## Features
|
34 |
|
35 |
+
- **Interactive Parameter Tuning**: Adjust clipping norm, noise multiplier, batch size, learning rate, and epochs
|
36 |
+
- **Real-time Training**: Choose between mock simulation or actual MNIST training
|
37 |
+
- **Multiple Visualizations**:
|
38 |
+
- Training progress (accuracy/loss over epochs/iterations)
|
39 |
+
- Gradient clipping visualization
|
40 |
+
- Privacy budget tracking
|
41 |
+
- **Smart Recommendations**: Get suggestions for improving your privacy-utility trade-off
|
42 |
+
- **Educational Content**: Learn about DP-SGD concepts through interactive exploration
|
|
|
|
|
43 |
|
44 |
## Quick Start
|
45 |
|
46 |
+
### Prerequisites
|
47 |
+
- Python 3.8+
|
48 |
+
- pip or conda
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
49 |
|
50 |
+
### Installation
|
|
|
|
|
|
|
51 |
|
52 |
+
1. Clone the repository:
|
53 |
+
```bash
|
54 |
+
git clone <repository-url>
|
55 |
+
cd DPSGD
|
56 |
+
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
57 |
|
58 |
2. Install dependencies:
|
59 |
+
```bash
|
60 |
+
pip install -r requirements.txt
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
61 |
```
|
62 |
+
|
63 |
+
3. Run the application:
|
64 |
+
```bash
|
65 |
+
python3 run.py
|
|
|
|
|
|
|
|
|
|
|
|
|
66 |
```
|
67 |
|
68 |
+
4. Open your browser and navigate to `http://127.0.0.1:5000`
|
69 |
+
|
70 |
+
### Using the Application
|
71 |
+
|
72 |
+
1. **Set Parameters**: Use the sliders to adjust DP-SGD parameters
|
73 |
+
2. **Choose Training Mode**: Select between mock simulation (fast) or real MNIST training
|
74 |
+
3. **Run Training**: Click "Run Training" to see results
|
75 |
+
4. **Analyze Results**:
|
76 |
+
- View training progress in the interactive charts
|
77 |
+
- Check final metrics (accuracy, loss, privacy budget)
|
78 |
+
- Read personalized recommendations
|
79 |
+
5. **Experiment**: Try the "Use Optimal Parameters" button for research-backed settings
|
80 |
+
|
81 |
+
## Understanding the Results
|
82 |
+
|
83 |
+
### Chart Interpretation
|
84 |
+
- **Green solid line**: Model accuracy (left y-axis, 0-100%)
|
85 |
+
- **Red dashed line**: Training loss (right y-axis, 0-3)
|
86 |
+
- **Privacy Budget (Ξ΅)**: Lower values = stronger privacy protection
|
87 |
+
- **Consistent metrics**: Training progress matches final results
|
88 |
+
|
89 |
+
### Recommended Parameter Ranges
|
90 |
+
- **Clipping Norm (C)**: 1.0-2.0 (balance between privacy and utility)
|
91 |
+
- **Noise Multiplier (Ο)**: 0.8-1.5 (avoid Ο > 2.0 for usable models)
|
92 |
+
- **Batch Size**: 128+ (larger batches help with DP-SGD stability)
|
93 |
+
- **Learning Rate**: 0.01-0.02 (conservative rates work better with noise)
|
94 |
+
- **Epochs**: 8-20 (balance convergence vs privacy cost)
|
95 |
+
|
96 |
+
### Privacy-Utility Trade-offs
|
97 |
+
- **Ξ΅ < 1**: Very strong privacy, expect 60-70% accuracy
|
98 |
+
- **Ξ΅ = 2-4**: Good privacy-utility balance, expect 75-85% accuracy
|
99 |
+
- **Ξ΅ > 8**: Weaker privacy, expect 85-90% accuracy
|
100 |
+
|
101 |
+
## Technical Details
|
102 |
+
|
103 |
+
### Architecture
|
104 |
+
- **Backend**: Flask with TensorFlow/Keras for real training
|
105 |
+
- **Frontend**: Vanilla JavaScript with Chart.js for visualizations
|
106 |
+
- **Training**: Supports both mock simulation and real DP-SGD with MNIST
|
107 |
+
|
108 |
+
### Algorithms
|
109 |
+
- **Real Training**: Implements simplified DP-SGD with gradient clipping and Gaussian noise
|
110 |
+
- **Mock Training**: Research-based simulation reflecting actual DP-SGD behavior patterns
|
111 |
+
- **Privacy Calculation**: RDP-based privacy budget estimation
|
112 |
+
|
113 |
+
### Research Basis
|
114 |
+
The simulation parameters and accuracy ranges are based on recent DP-SGD research:
|
115 |
+
- "TAN without a burn: Scaling Laws of DP-SGD" (2023)
|
116 |
+
- "Unlocking High-Accuracy Differentially Private Image Classification through Scale" (2022)
|
117 |
+
- "Differentially Private Generation of Small Images" (2020)
|
118 |
+
|
119 |
+
## Contributing
|
120 |
+
|
121 |
+
We welcome contributions! Areas for improvement:
|
122 |
+
- Additional datasets beyond MNIST
|
123 |
+
- More sophisticated privacy accounting methods
|
124 |
+
- Enhanced visualizations
|
125 |
+
- Better mobile responsiveness
|
126 |
+
|
127 |
## License
|
128 |
|
129 |
+
This project is licensed under the MIT License - see the LICENSE file for details.
|
130 |
+
|
131 |
+
## Acknowledgments
|
132 |
+
|
133 |
+
- TensorFlow Privacy team for DP-SGD implementation
|
134 |
+
- Research community for privacy-preserving ML advances
|
135 |
+
- Chart.js for excellent visualization capabilities
|
app/routes.py
CHANGED
@@ -2,11 +2,39 @@ from flask import Blueprint, render_template, jsonify, request, current_app
|
|
2 |
from app.training.mock_trainer import MockTrainer
|
3 |
from app.training.privacy_calculator import PrivacyCalculator
|
4 |
from flask_cors import cross_origin
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
|
6 |
main = Blueprint('main', __name__)
|
7 |
mock_trainer = MockTrainer()
|
8 |
privacy_calculator = PrivacyCalculator()
|
9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
@main.route('/')
|
11 |
def index():
|
12 |
return render_template('index.html')
|
@@ -34,20 +62,44 @@ def train():
|
|
34 |
'epochs': int(data.get('epochs', 5))
|
35 |
}
|
36 |
|
37 |
-
#
|
38 |
-
|
39 |
|
40 |
-
#
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
|
46 |
return jsonify(results)
|
47 |
except (TypeError, ValueError) as e:
|
48 |
return jsonify({'error': f'Invalid parameter values: {str(e)}'}), 400
|
49 |
except Exception as e:
|
50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
51 |
|
52 |
@main.route('/api/privacy-budget', methods=['POST', 'OPTIONS'])
|
53 |
@cross_origin()
|
@@ -67,9 +119,24 @@ def calculate_privacy_budget():
|
|
67 |
'epochs': int(data.get('epochs', 5))
|
68 |
}
|
69 |
|
70 |
-
|
|
|
|
|
|
|
|
|
|
|
71 |
return jsonify({'epsilon': epsilon})
|
72 |
except (TypeError, ValueError) as e:
|
73 |
return jsonify({'error': f'Invalid parameter values: {str(e)}'}), 400
|
74 |
except Exception as e:
|
75 |
-
return jsonify({'error': f'Server error: {str(e)}'}), 500
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
from app.training.mock_trainer import MockTrainer
|
3 |
from app.training.privacy_calculator import PrivacyCalculator
|
4 |
from flask_cors import cross_origin
|
5 |
+
import os
|
6 |
+
|
7 |
+
# Try to import RealTrainer, fallback to MockTrainer if dependencies aren't available
|
8 |
+
try:
|
9 |
+
from app.training.simplified_real_trainer import SimplifiedRealTrainer as RealTrainer
|
10 |
+
REAL_TRAINER_AVAILABLE = True
|
11 |
+
print("Simplified real trainer available - will use MNIST dataset")
|
12 |
+
except ImportError as e:
|
13 |
+
print(f"Real trainer not available ({e}) - trying simplified version")
|
14 |
+
try:
|
15 |
+
from app.training.real_trainer import RealTrainer
|
16 |
+
REAL_TRAINER_AVAILABLE = True
|
17 |
+
print("Full real trainer available - will use MNIST dataset")
|
18 |
+
except ImportError as e2:
|
19 |
+
print(f"No real trainer available ({e2}) - using mock trainer")
|
20 |
+
REAL_TRAINER_AVAILABLE = False
|
21 |
|
22 |
main = Blueprint('main', __name__)
|
23 |
mock_trainer = MockTrainer()
|
24 |
privacy_calculator = PrivacyCalculator()
|
25 |
|
26 |
+
# Initialize real trainer if available
|
27 |
+
if REAL_TRAINER_AVAILABLE:
|
28 |
+
try:
|
29 |
+
real_trainer = RealTrainer()
|
30 |
+
print("Real trainer initialized successfully")
|
31 |
+
except Exception as e:
|
32 |
+
print(f"Failed to initialize real trainer: {e}")
|
33 |
+
REAL_TRAINER_AVAILABLE = False
|
34 |
+
real_trainer = None
|
35 |
+
else:
|
36 |
+
real_trainer = None
|
37 |
+
|
38 |
@main.route('/')
|
39 |
def index():
|
40 |
return render_template('index.html')
|
|
|
62 |
'epochs': int(data.get('epochs', 5))
|
63 |
}
|
64 |
|
65 |
+
# Check if user wants to force mock training
|
66 |
+
use_mock = data.get('use_mock', False)
|
67 |
|
68 |
+
# Use real trainer if available and not forced to use mock
|
69 |
+
if REAL_TRAINER_AVAILABLE and real_trainer and not use_mock:
|
70 |
+
print("Using real trainer with MNIST dataset")
|
71 |
+
results = real_trainer.train(params)
|
72 |
+
results['trainer_type'] = 'real'
|
73 |
+
results['dataset'] = 'MNIST'
|
74 |
+
else:
|
75 |
+
print("Using mock trainer with synthetic data")
|
76 |
+
results = mock_trainer.train(params)
|
77 |
+
results['trainer_type'] = 'mock'
|
78 |
+
results['dataset'] = 'synthetic'
|
79 |
+
|
80 |
+
# Add gradient information for visualization (if not already included)
|
81 |
+
if 'gradient_info' not in results:
|
82 |
+
trainer = real_trainer if (REAL_TRAINER_AVAILABLE and real_trainer and not use_mock) else mock_trainer
|
83 |
+
results['gradient_info'] = {
|
84 |
+
'before_clipping': trainer.generate_gradient_norms(params['clipping_norm']),
|
85 |
+
'after_clipping': trainer.generate_clipped_gradients(params['clipping_norm'])
|
86 |
+
}
|
87 |
|
88 |
return jsonify(results)
|
89 |
except (TypeError, ValueError) as e:
|
90 |
return jsonify({'error': f'Invalid parameter values: {str(e)}'}), 400
|
91 |
except Exception as e:
|
92 |
+
print(f"Training error: {str(e)}")
|
93 |
+
# Fallback to mock trainer on any error
|
94 |
+
try:
|
95 |
+
print("Falling back to mock trainer due to error")
|
96 |
+
results = mock_trainer.train(params)
|
97 |
+
results['trainer_type'] = 'mock'
|
98 |
+
results['dataset'] = 'synthetic'
|
99 |
+
results['fallback_reason'] = str(e)
|
100 |
+
return jsonify(results)
|
101 |
+
except Exception as fallback_error:
|
102 |
+
return jsonify({'error': f'Server error: {str(fallback_error)}'}), 500
|
103 |
|
104 |
@main.route('/api/privacy-budget', methods=['POST', 'OPTIONS'])
|
105 |
@cross_origin()
|
|
|
119 |
'epochs': int(data.get('epochs', 5))
|
120 |
}
|
121 |
|
122 |
+
# Use real trainer's privacy calculation if available, otherwise use privacy calculator
|
123 |
+
if REAL_TRAINER_AVAILABLE and real_trainer:
|
124 |
+
epsilon = real_trainer._calculate_privacy_budget(params)
|
125 |
+
else:
|
126 |
+
epsilon = privacy_calculator.calculate_epsilon(params)
|
127 |
+
|
128 |
return jsonify({'epsilon': epsilon})
|
129 |
except (TypeError, ValueError) as e:
|
130 |
return jsonify({'error': f'Invalid parameter values: {str(e)}'}), 400
|
131 |
except Exception as e:
|
132 |
+
return jsonify({'error': f'Server error: {str(e)}'}), 500
|
133 |
+
|
134 |
+
@main.route('/api/trainer-status', methods=['GET'])
|
135 |
+
@cross_origin()
|
136 |
+
def trainer_status():
|
137 |
+
"""Endpoint to check which trainer is being used."""
|
138 |
+
return jsonify({
|
139 |
+
'real_trainer_available': REAL_TRAINER_AVAILABLE,
|
140 |
+
'current_trainer': 'real' if REAL_TRAINER_AVAILABLE else 'mock',
|
141 |
+
'dataset': 'MNIST' if REAL_TRAINER_AVAILABLE else 'synthetic'
|
142 |
+
})
|
app/static/css/styles.css
CHANGED
@@ -471,6 +471,27 @@ body {
|
|
471 |
animation: slideIn 0.3s ease-out;
|
472 |
}
|
473 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
474 |
@keyframes slideIn {
|
475 |
from {
|
476 |
transform: translateY(-20px);
|
|
|
471 |
animation: slideIn 0.3s ease-out;
|
472 |
}
|
473 |
|
474 |
+
/* View Toggle Buttons */
|
475 |
+
.view-toggle {
|
476 |
+
padding: 4px 12px;
|
477 |
+
border: none;
|
478 |
+
background: transparent;
|
479 |
+
cursor: pointer;
|
480 |
+
border-radius: 2px;
|
481 |
+
font-size: 0.8rem;
|
482 |
+
transition: background-color 0.2s ease;
|
483 |
+
color: var(--text-secondary);
|
484 |
+
}
|
485 |
+
|
486 |
+
.view-toggle:hover {
|
487 |
+
background-color: rgba(63, 81, 181, 0.1);
|
488 |
+
}
|
489 |
+
|
490 |
+
.view-toggle.active {
|
491 |
+
background-color: var(--primary-color);
|
492 |
+
color: white;
|
493 |
+
}
|
494 |
+
|
495 |
@keyframes slideIn {
|
496 |
from {
|
497 |
transform: translateY(-20px);
|
app/static/js/main.js
CHANGED
@@ -4,6 +4,9 @@ class DPSGDExplorer {
|
|
4 |
this.privacyChart = null;
|
5 |
this.gradientChart = null;
|
6 |
this.isTraining = false;
|
|
|
|
|
|
|
7 |
this.initializeUI();
|
8 |
}
|
9 |
|
@@ -16,6 +19,10 @@ class DPSGDExplorer {
|
|
16 |
|
17 |
// Add event listeners
|
18 |
document.getElementById('train-button')?.addEventListener('click', () => this.toggleTraining());
|
|
|
|
|
|
|
|
|
19 |
}
|
20 |
|
21 |
initializeSliders() {
|
@@ -122,14 +129,25 @@ class DPSGDExplorer {
|
|
122 |
{
|
123 |
label: 'Accuracy',
|
124 |
borderColor: '#4caf50',
|
|
|
125 |
data: [],
|
126 |
-
yAxisID: 'y'
|
|
|
|
|
|
|
|
|
127 |
},
|
128 |
{
|
129 |
label: 'Loss',
|
130 |
borderColor: '#f44336',
|
|
|
131 |
data: [],
|
132 |
-
yAxisID: 'y1'
|
|
|
|
|
|
|
|
|
|
|
133 |
}
|
134 |
]
|
135 |
},
|
@@ -140,6 +158,29 @@ class DPSGDExplorer {
|
|
140 |
mode: 'index',
|
141 |
intersect: false,
|
142 |
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
143 |
scales: {
|
144 |
y: {
|
145 |
type: 'linear',
|
@@ -147,10 +188,27 @@ class DPSGDExplorer {
|
|
147 |
position: 'left',
|
148 |
title: {
|
149 |
display: true,
|
150 |
-
text: 'Accuracy (%)'
|
|
|
|
|
|
|
|
|
|
|
151 |
},
|
152 |
min: 0,
|
153 |
-
max: 100
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
154 |
},
|
155 |
y1: {
|
156 |
type: 'linear',
|
@@ -158,13 +216,43 @@ class DPSGDExplorer {
|
|
158 |
position: 'right',
|
159 |
title: {
|
160 |
display: true,
|
161 |
-
text: 'Loss'
|
|
|
|
|
|
|
|
|
|
|
162 |
},
|
163 |
min: 0,
|
164 |
-
max:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
165 |
grid: {
|
166 |
-
drawOnChartArea: false,
|
|
|
167 |
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
168 |
}
|
169 |
}
|
170 |
}
|
@@ -343,7 +431,7 @@ class DPSGDExplorer {
|
|
343 |
console.log('Received training data:', data); // Debug log
|
344 |
|
345 |
// Update charts and results
|
346 |
-
this.updateCharts(data
|
347 |
this.updateResults(data);
|
348 |
} catch (error) {
|
349 |
console.error('Training error:', error);
|
@@ -393,32 +481,89 @@ class DPSGDExplorer {
|
|
393 |
}
|
394 |
}
|
395 |
|
396 |
-
|
397 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
398 |
|
399 |
-
console.log('Updating charts with data:',
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
400 |
|
401 |
// Update training metrics chart
|
402 |
-
const labels =
|
403 |
-
|
404 |
-
|
|
|
|
|
|
|
|
|
|
|
405 |
|
406 |
this.trainingChart.data.labels = labels;
|
407 |
this.trainingChart.data.datasets[0].data = accuracies;
|
408 |
this.trainingChart.data.datasets[1].data = losses;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
409 |
this.trainingChart.update();
|
410 |
|
411 |
// Update current epoch display
|
412 |
const currentEpoch = document.getElementById('current-epoch');
|
413 |
const totalEpochs = document.getElementById('total-epochs');
|
414 |
-
if (currentEpoch && totalEpochs) {
|
415 |
-
currentEpoch.textContent =
|
416 |
totalEpochs.textContent = this.getParameters().epochs;
|
417 |
}
|
418 |
|
419 |
-
// Update privacy budget chart
|
420 |
-
if (this.privacyChart) {
|
421 |
-
const privacyBudgets =
|
422 |
this.calculateEpochPrivacy(i + 1)
|
423 |
);
|
424 |
this.privacyChart.data.labels = labels;
|
@@ -430,10 +575,10 @@ class DPSGDExplorer {
|
|
430 |
if (this.gradientChart) {
|
431 |
const clippingNorm = this.getParameters().clipping_norm;
|
432 |
|
433 |
-
// Generate gradient data if not provided in
|
434 |
let gradientData;
|
435 |
-
if (
|
436 |
-
gradientData =
|
437 |
} else {
|
438 |
// Generate synthetic gradient data
|
439 |
const beforeClipping = [];
|
@@ -502,6 +647,36 @@ class DPSGDExplorer {
|
|
502 |
document.getElementById('training-time-value').textContent =
|
503 |
data.final_metrics.training_time.toFixed(1) + 's';
|
504 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
505 |
// Update recommendations
|
506 |
const recommendationList = document.querySelector('.recommendation-list');
|
507 |
recommendationList.innerHTML = '';
|
@@ -645,4 +820,21 @@ class DPSGDExplorer {
|
|
645 |
// Initialize the application when the DOM is loaded
|
646 |
document.addEventListener('DOMContentLoaded', () => {
|
647 |
window.dpsgdExplorer = new DPSGDExplorer();
|
648 |
-
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
this.privacyChart = null;
|
5 |
this.gradientChart = null;
|
6 |
this.isTraining = false;
|
7 |
+
this.currentView = 'epochs'; // 'epochs' or 'iterations'
|
8 |
+
this.epochsData = [];
|
9 |
+
this.iterationsData = [];
|
10 |
this.initializeUI();
|
11 |
}
|
12 |
|
|
|
19 |
|
20 |
// Add event listeners
|
21 |
document.getElementById('train-button')?.addEventListener('click', () => this.toggleTraining());
|
22 |
+
|
23 |
+
// Add view toggle listeners
|
24 |
+
document.getElementById('view-epochs')?.addEventListener('click', () => this.switchView('epochs'));
|
25 |
+
document.getElementById('view-iterations')?.addEventListener('click', () => this.switchView('iterations'));
|
26 |
}
|
27 |
|
28 |
initializeSliders() {
|
|
|
129 |
{
|
130 |
label: 'Accuracy',
|
131 |
borderColor: '#4caf50',
|
132 |
+
backgroundColor: 'rgba(76, 175, 80, 0.1)',
|
133 |
data: [],
|
134 |
+
yAxisID: 'y',
|
135 |
+
borderWidth: 3,
|
136 |
+
pointRadius: 4,
|
137 |
+
pointHoverRadius: 6,
|
138 |
+
tension: 0.1
|
139 |
},
|
140 |
{
|
141 |
label: 'Loss',
|
142 |
borderColor: '#f44336',
|
143 |
+
backgroundColor: 'rgba(244, 67, 54, 0.1)',
|
144 |
data: [],
|
145 |
+
yAxisID: 'y1',
|
146 |
+
borderWidth: 3,
|
147 |
+
pointRadius: 4,
|
148 |
+
pointHoverRadius: 6,
|
149 |
+
tension: 0.1,
|
150 |
+
borderDash: [5, 5] // Dashed line to differentiate from accuracy
|
151 |
}
|
152 |
]
|
153 |
},
|
|
|
158 |
mode: 'index',
|
159 |
intersect: false,
|
160 |
},
|
161 |
+
plugins: {
|
162 |
+
legend: {
|
163 |
+
display: true,
|
164 |
+
position: 'top',
|
165 |
+
labels: {
|
166 |
+
usePointStyle: true,
|
167 |
+
padding: 20,
|
168 |
+
font: {
|
169 |
+
size: 12,
|
170 |
+
weight: 'bold'
|
171 |
+
}
|
172 |
+
}
|
173 |
+
},
|
174 |
+
tooltip: {
|
175 |
+
mode: 'index',
|
176 |
+
intersect: false,
|
177 |
+
backgroundColor: 'rgba(0, 0, 0, 0.8)',
|
178 |
+
titleColor: '#fff',
|
179 |
+
bodyColor: '#fff',
|
180 |
+
borderColor: '#ddd',
|
181 |
+
borderWidth: 1
|
182 |
+
}
|
183 |
+
},
|
184 |
scales: {
|
185 |
y: {
|
186 |
type: 'linear',
|
|
|
188 |
position: 'left',
|
189 |
title: {
|
190 |
display: true,
|
191 |
+
text: 'Accuracy (%)',
|
192 |
+
color: '#4caf50',
|
193 |
+
font: {
|
194 |
+
size: 14,
|
195 |
+
weight: 'bold'
|
196 |
+
}
|
197 |
},
|
198 |
min: 0,
|
199 |
+
max: 100,
|
200 |
+
ticks: {
|
201 |
+
color: '#4caf50',
|
202 |
+
font: {
|
203 |
+
weight: 'bold'
|
204 |
+
},
|
205 |
+
callback: function(value) {
|
206 |
+
return value + '%';
|
207 |
+
}
|
208 |
+
},
|
209 |
+
grid: {
|
210 |
+
color: 'rgba(76, 175, 80, 0.2)'
|
211 |
+
}
|
212 |
},
|
213 |
y1: {
|
214 |
type: 'linear',
|
|
|
216 |
position: 'right',
|
217 |
title: {
|
218 |
display: true,
|
219 |
+
text: 'Loss',
|
220 |
+
color: '#f44336',
|
221 |
+
font: {
|
222 |
+
size: 14,
|
223 |
+
weight: 'bold'
|
224 |
+
}
|
225 |
},
|
226 |
min: 0,
|
227 |
+
max: 3, // More reasonable max for loss
|
228 |
+
ticks: {
|
229 |
+
color: '#f44336',
|
230 |
+
font: {
|
231 |
+
weight: 'bold'
|
232 |
+
},
|
233 |
+
callback: function(value) {
|
234 |
+
return value.toFixed(1);
|
235 |
+
}
|
236 |
+
},
|
237 |
grid: {
|
238 |
+
drawOnChartArea: false, // Don't overlay grid lines
|
239 |
+
color: 'rgba(244, 67, 54, 0.2)'
|
240 |
},
|
241 |
+
},
|
242 |
+
x: {
|
243 |
+
title: {
|
244 |
+
display: true,
|
245 |
+
text: 'Training Progress',
|
246 |
+
font: {
|
247 |
+
size: 12,
|
248 |
+
weight: 'bold'
|
249 |
+
}
|
250 |
+
},
|
251 |
+
ticks: {
|
252 |
+
font: {
|
253 |
+
size: 11
|
254 |
+
}
|
255 |
+
}
|
256 |
}
|
257 |
}
|
258 |
}
|
|
|
431 |
console.log('Received training data:', data); // Debug log
|
432 |
|
433 |
// Update charts and results
|
434 |
+
this.updateCharts(data);
|
435 |
this.updateResults(data);
|
436 |
} catch (error) {
|
437 |
console.error('Training error:', error);
|
|
|
481 |
}
|
482 |
}
|
483 |
|
484 |
+
switchView(view) {
|
485 |
+
this.currentView = view;
|
486 |
+
|
487 |
+
// Update button states
|
488 |
+
document.querySelectorAll('.view-toggle').forEach(btn => {
|
489 |
+
btn.classList.remove('active');
|
490 |
+
});
|
491 |
+
document.getElementById(`view-${view}`).classList.add('active');
|
492 |
+
|
493 |
+
// Update chart with current data
|
494 |
+
if (view === 'epochs' && this.epochsData.length > 0) {
|
495 |
+
this.updateChartsWithData(this.epochsData, 'epochs');
|
496 |
+
} else if (view === 'iterations' && this.iterationsData.length > 0) {
|
497 |
+
this.updateChartsWithData(this.iterationsData, 'iterations');
|
498 |
+
}
|
499 |
+
}
|
500 |
+
|
501 |
+
updateCharts(data) {
|
502 |
+
if (!this.trainingChart || !data) return;
|
503 |
|
504 |
+
console.log('Updating charts with data:', data); // Debug log
|
505 |
+
|
506 |
+
// Store data for view switching
|
507 |
+
if (data.epochs_data) {
|
508 |
+
this.epochsData = data.epochs_data;
|
509 |
+
}
|
510 |
+
if (data.iterations_data) {
|
511 |
+
this.iterationsData = data.iterations_data;
|
512 |
+
}
|
513 |
+
|
514 |
+
// Use current view to determine which data to display
|
515 |
+
if (this.currentView === 'epochs' && this.epochsData.length > 0) {
|
516 |
+
this.updateChartsWithData(this.epochsData, 'epochs');
|
517 |
+
} else if (this.currentView === 'iterations' && this.iterationsData.length > 0) {
|
518 |
+
this.updateChartsWithData(this.iterationsData, 'iterations');
|
519 |
+
} else if (this.epochsData.length > 0) {
|
520 |
+
// Fallback to epochs if iterations not available
|
521 |
+
this.updateChartsWithData(this.epochsData, 'epochs');
|
522 |
+
}
|
523 |
+
}
|
524 |
+
|
525 |
+
updateChartsWithData(chartData, dataType) {
|
526 |
+
if (!this.trainingChart || !chartData) return;
|
527 |
|
528 |
// Update training metrics chart
|
529 |
+
const labels = chartData.map(d =>
|
530 |
+
dataType === 'epochs' ? `Epoch ${d.epoch}` : `Iter ${d.iteration}`
|
531 |
+
);
|
532 |
+
const accuracies = chartData.map(d => d.accuracy);
|
533 |
+
const losses = chartData.map(d => d.loss);
|
534 |
+
|
535 |
+
console.log(`${dataType} - Accuracies:`, accuracies);
|
536 |
+
console.log(`${dataType} - Losses:`, losses);
|
537 |
|
538 |
this.trainingChart.data.labels = labels;
|
539 |
this.trainingChart.data.datasets[0].data = accuracies;
|
540 |
this.trainingChart.data.datasets[1].data = losses;
|
541 |
+
|
542 |
+
// Auto-adjust loss scale based on actual data
|
543 |
+
const maxLoss = Math.max(...losses);
|
544 |
+
const minLoss = Math.min(...losses);
|
545 |
+
this.trainingChart.options.scales.y1.max = Math.max(maxLoss * 1.1, 3);
|
546 |
+
this.trainingChart.options.scales.y1.min = Math.max(0, minLoss * 0.9);
|
547 |
+
|
548 |
+
// Update chart info
|
549 |
+
const chartInfo = document.getElementById('chart-info');
|
550 |
+
if (chartInfo) {
|
551 |
+
chartInfo.textContent = `Showing ${chartData.length} data points (${dataType})`;
|
552 |
+
}
|
553 |
+
|
554 |
this.trainingChart.update();
|
555 |
|
556 |
// Update current epoch display
|
557 |
const currentEpoch = document.getElementById('current-epoch');
|
558 |
const totalEpochs = document.getElementById('total-epochs');
|
559 |
+
if (currentEpoch && totalEpochs && dataType === 'epochs') {
|
560 |
+
currentEpoch.textContent = chartData.length;
|
561 |
totalEpochs.textContent = this.getParameters().epochs;
|
562 |
}
|
563 |
|
564 |
+
// Update privacy budget chart (only for epochs view)
|
565 |
+
if (this.privacyChart && dataType === 'epochs') {
|
566 |
+
const privacyBudgets = chartData.map((_, i) =>
|
567 |
this.calculateEpochPrivacy(i + 1)
|
568 |
);
|
569 |
this.privacyChart.data.labels = labels;
|
|
|
575 |
if (this.gradientChart) {
|
576 |
const clippingNorm = this.getParameters().clipping_norm;
|
577 |
|
578 |
+
// Generate gradient data if not provided in chartData
|
579 |
let gradientData;
|
580 |
+
if (chartData[chartData.length - 1]?.gradient_info) {
|
581 |
+
gradientData = chartData[chartData.length - 1].gradient_info;
|
582 |
} else {
|
583 |
// Generate synthetic gradient data
|
584 |
const beforeClipping = [];
|
|
|
647 |
document.getElementById('training-time-value').textContent =
|
648 |
data.final_metrics.training_time.toFixed(1) + 's';
|
649 |
|
650 |
+
// Update privacy budget display (make it dynamic)
|
651 |
+
const privacyBudgetElement = document.getElementById('privacy-budget-value');
|
652 |
+
if (privacyBudgetElement) {
|
653 |
+
privacyBudgetElement.textContent = `Ξ΅=${data.privacy_budget.toFixed(1)}`;
|
654 |
+
}
|
655 |
+
|
656 |
+
// Update privacy-utility trade-off explanation dynamically
|
657 |
+
const tradeoffElement = document.getElementById('tradeoff-explanation');
|
658 |
+
if (tradeoffElement) {
|
659 |
+
const accuracy = data.final_metrics.accuracy.toFixed(1);
|
660 |
+
const epsilon = data.privacy_budget.toFixed(1);
|
661 |
+
|
662 |
+
// Generate realistic trade-off assessment
|
663 |
+
let tradeoffAssessment;
|
664 |
+
if (data.final_metrics.accuracy >= 85) {
|
665 |
+
tradeoffAssessment = "This is an excellent trade-off for most applications.";
|
666 |
+
} else if (data.final_metrics.accuracy >= 75) {
|
667 |
+
tradeoffAssessment = "This is a good trade-off for most applications.";
|
668 |
+
} else if (data.final_metrics.accuracy >= 65) {
|
669 |
+
tradeoffAssessment = "This trade-off may be acceptable for privacy-critical applications.";
|
670 |
+
} else if (data.final_metrics.accuracy >= 50) {
|
671 |
+
tradeoffAssessment = "Low utility - consider reducing noise or increasing clipping norm.";
|
672 |
+
} else {
|
673 |
+
tradeoffAssessment = "Very poor utility - privacy parameters need significant adjustment.";
|
674 |
+
}
|
675 |
+
|
676 |
+
tradeoffElement.textContent =
|
677 |
+
`This model achieved ${accuracy}% accuracy with a privacy budget of Ξ΅=${epsilon}. ${tradeoffAssessment}`;
|
678 |
+
}
|
679 |
+
|
680 |
// Update recommendations
|
681 |
const recommendationList = document.querySelector('.recommendation-list');
|
682 |
recommendationList.innerHTML = '';
|
|
|
820 |
// Initialize the application when the DOM is loaded
|
821 |
document.addEventListener('DOMContentLoaded', () => {
|
822 |
window.dpsgdExplorer = new DPSGDExplorer();
|
823 |
+
});
|
824 |
+
|
825 |
+
function setOptimalParameters() {
|
826 |
+
// Set optimal parameters based on actual MNIST DP-SGD training results
|
827 |
+
// These values achieve ~95% accuracy with reasonable privacy budget (Ξ΅β15)
|
828 |
+
document.getElementById('clipping-norm').value = '2.0'; // Balanced clipping norm
|
829 |
+
document.getElementById('noise-multiplier').value = '1.0'; // Moderate noise for good privacy
|
830 |
+
document.getElementById('batch-size').value = '256'; // Large batches for DP-SGD stability
|
831 |
+
document.getElementById('learning-rate').value = '0.05'; // Balanced learning rate
|
832 |
+
document.getElementById('epochs').value = '15'; // Sufficient epochs for convergence
|
833 |
+
|
834 |
+
// Update displays
|
835 |
+
updateClippingNormDisplay();
|
836 |
+
updateNoiseMultiplierDisplay();
|
837 |
+
updateBatchSizeDisplay();
|
838 |
+
updateLearningRateDisplay();
|
839 |
+
updateEpochsDisplay();
|
840 |
+
}
|
app/templates/index.html
CHANGED
@@ -173,6 +173,9 @@
|
|
173 |
<button id="train-button" class="control-button">
|
174 |
Run Training
|
175 |
</button>
|
|
|
|
|
|
|
176 |
</div>
|
177 |
</div>
|
178 |
|
@@ -190,6 +193,19 @@
|
|
190 |
</div>
|
191 |
|
192 |
<div id="training-tab" class="tab-content active">
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
193 |
<div class="chart-container" style="position: relative; height: 300px; width: 100%;">
|
194 |
<canvas id="training-chart"></canvas>
|
195 |
</div>
|
|
|
173 |
<button id="train-button" class="control-button">
|
174 |
Run Training
|
175 |
</button>
|
176 |
+
<button onclick="setOptimalParameters()" class="control-button" style="margin-top: 0.5rem; background-color: var(--secondary-color);">
|
177 |
+
π― Use Optimal Parameters
|
178 |
+
</button>
|
179 |
</div>
|
180 |
</div>
|
181 |
|
|
|
193 |
</div>
|
194 |
|
195 |
<div id="training-tab" class="tab-content active">
|
196 |
+
<div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 1rem;">
|
197 |
+
<div style="display: flex; align-items: center; gap: 1rem;">
|
198 |
+
<span style="font-size: 0.9rem; color: var(--text-secondary);">View:</span>
|
199 |
+
<div style="display: flex; background-color: var(--background-off); border-radius: 4px; padding: 2px;">
|
200 |
+
<button id="view-epochs" class="view-toggle active" data-view="epochs">Epochs</button>
|
201 |
+
<button id="view-iterations" class="view-toggle" data-view="iterations">Iterations</button>
|
202 |
+
</div>
|
203 |
+
</div>
|
204 |
+
<div id="chart-info" style="font-size: 0.8rem; color: var(--text-secondary);">
|
205 |
+
Showing 5 data points
|
206 |
+
</div>
|
207 |
+
</div>
|
208 |
+
|
209 |
<div class="chart-container" style="position: relative; height: 300px; width: 100%;">
|
210 |
<canvas id="training-chart"></canvas>
|
211 |
</div>
|
app/training/mock_trainer.py
CHANGED
@@ -4,12 +4,13 @@ from typing import Dict, List, Any
|
|
4 |
|
5 |
class MockTrainer:
|
6 |
def __init__(self):
|
7 |
-
|
8 |
-
self.
|
|
|
9 |
|
10 |
def train(self, params: Dict[str, Any]) -> Dict[str, Any]:
|
11 |
"""
|
12 |
-
Simulate DP-SGD training with given parameters.
|
13 |
|
14 |
Args:
|
15 |
params: Dictionary containing training parameters:
|
@@ -29,13 +30,16 @@ class MockTrainer:
|
|
29 |
learning_rate = params['learning_rate']
|
30 |
epochs = params['epochs']
|
31 |
|
32 |
-
# Calculate privacy impact on performance
|
33 |
-
privacy_factor = self.
|
34 |
|
35 |
# Generate epoch-wise data
|
36 |
epochs_data = self._generate_epoch_data(epochs, privacy_factor)
|
37 |
|
38 |
-
#
|
|
|
|
|
|
|
39 |
final_metrics = self._calculate_final_metrics(epochs_data, privacy_factor)
|
40 |
|
41 |
# Generate recommendations
|
@@ -47,113 +51,264 @@ class MockTrainer:
|
|
47 |
'after_clipping': self.generate_clipped_gradients(clipping_norm)
|
48 |
}
|
49 |
|
|
|
|
|
|
|
50 |
return {
|
51 |
'epochs_data': epochs_data,
|
|
|
52 |
'final_metrics': final_metrics,
|
53 |
'recommendations': recommendations,
|
54 |
-
'gradient_info': gradient_info
|
|
|
55 |
}
|
56 |
|
57 |
-
def
|
58 |
-
"""Calculate
|
59 |
-
|
60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
61 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
62 |
def _generate_epoch_data(self, epochs: int, privacy_factor: float) -> List[Dict[str, float]]:
|
63 |
"""Generate realistic training metrics for each epoch."""
|
64 |
epochs_data = []
|
65 |
|
66 |
-
#
|
67 |
base_accuracy = self.base_accuracy * privacy_factor
|
68 |
base_loss = self.base_loss / privacy_factor
|
69 |
|
70 |
for epoch in range(1, epochs + 1):
|
71 |
-
#
|
72 |
progress = epoch / epochs
|
73 |
-
|
|
|
|
|
|
|
|
|
74 |
|
75 |
-
|
76 |
-
|
|
|
77 |
|
78 |
epochs_data.append({
|
79 |
'epoch': epoch,
|
80 |
-
'accuracy': max(
|
81 |
-
'loss': max(0, loss)
|
|
|
|
|
82 |
})
|
83 |
|
84 |
return epochs_data
|
85 |
|
86 |
def _calculate_final_metrics(self, epochs_data: List[Dict[str, float]], privacy_factor: float) -> Dict[str, float]:
|
87 |
-
"""Calculate final training metrics."""
|
|
|
|
|
|
|
|
|
88 |
final_epoch = epochs_data[-1]
|
89 |
|
90 |
-
#
|
91 |
-
base_time = 0.
|
92 |
-
|
|
|
93 |
|
94 |
return {
|
95 |
-
'accuracy': final_epoch['accuracy'],
|
96 |
'loss': final_epoch['loss'],
|
97 |
-
'training_time': base_time *
|
98 |
}
|
99 |
|
100 |
def _generate_recommendations(self, params: Dict[str, Any], metrics: Dict[str, float]) -> List[Dict[str, str]]:
|
101 |
-
"""Generate recommendations based on
|
102 |
recommendations = []
|
103 |
|
104 |
-
#
|
105 |
-
if params['
|
|
|
|
|
|
|
|
|
|
|
106 |
recommendations.append({
|
107 |
'icon': 'β οΈ',
|
108 |
-
'text': '
|
109 |
})
|
110 |
-
elif params['
|
111 |
recommendations.append({
|
112 |
-
'icon': '
|
113 |
-
'text': 'Consider reducing
|
114 |
})
|
115 |
|
116 |
-
#
|
117 |
-
if params['
|
118 |
recommendations.append({
|
119 |
-
'icon': '
|
120 |
-
'text': '
|
121 |
})
|
122 |
-
elif params['
|
123 |
recommendations.append({
|
124 |
-
'icon': '
|
125 |
-
'text': '
|
126 |
})
|
127 |
|
128 |
-
#
|
129 |
if params['batch_size'] < 64:
|
130 |
recommendations.append({
|
131 |
'icon': 'β‘',
|
132 |
-
'text': 'Small batch
|
133 |
})
|
134 |
-
elif params['batch_size'] >
|
135 |
recommendations.append({
|
136 |
-
'icon': '
|
137 |
-
'text': '
|
138 |
})
|
139 |
|
140 |
-
#
|
141 |
if params['learning_rate'] > 0.05:
|
142 |
recommendations.append({
|
143 |
'icon': 'β οΈ',
|
144 |
-
'text': 'High learning rate
|
145 |
})
|
146 |
-
elif params['learning_rate'] < 0.
|
147 |
recommendations.append({
|
148 |
'icon': 'β³',
|
149 |
-
'text': 'Very low learning rate
|
150 |
})
|
151 |
|
152 |
-
#
|
153 |
-
if
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
154 |
recommendations.append({
|
155 |
'icon': 'π',
|
156 |
-
'text': '
|
|
|
|
|
|
|
|
|
|
|
157 |
})
|
158 |
|
159 |
return recommendations
|
|
|
4 |
|
5 |
class MockTrainer:
|
6 |
def __init__(self):
|
7 |
+
# More realistic base accuracy for DP-SGD on MNIST (should achieve 85-98% like research shows)
|
8 |
+
self.base_accuracy = 0.98 # Non-private MNIST accuracy
|
9 |
+
self.base_loss = 0.08 # Corresponding base loss
|
10 |
|
11 |
def train(self, params: Dict[str, Any]) -> Dict[str, Any]:
|
12 |
"""
|
13 |
+
Simulate DP-SGD training with given parameters using realistic privacy trade-offs.
|
14 |
|
15 |
Args:
|
16 |
params: Dictionary containing training parameters:
|
|
|
30 |
learning_rate = params['learning_rate']
|
31 |
epochs = params['epochs']
|
32 |
|
33 |
+
# Calculate realistic privacy impact on performance
|
34 |
+
privacy_factor = self._calculate_realistic_privacy_factor(clipping_norm, noise_multiplier, batch_size, epochs)
|
35 |
|
36 |
# Generate epoch-wise data
|
37 |
epochs_data = self._generate_epoch_data(epochs, privacy_factor)
|
38 |
|
39 |
+
# Generate iteration-wise data (mock version for consistency)
|
40 |
+
iterations_data = self._generate_iteration_data(epochs, privacy_factor, batch_size)
|
41 |
+
|
42 |
+
# Calculate final metrics (must be consistent with epoch data)
|
43 |
final_metrics = self._calculate_final_metrics(epochs_data, privacy_factor)
|
44 |
|
45 |
# Generate recommendations
|
|
|
51 |
'after_clipping': self.generate_clipped_gradients(clipping_norm)
|
52 |
}
|
53 |
|
54 |
+
# Calculate realistic privacy budget
|
55 |
+
privacy_budget = self._calculate_mock_privacy_budget(params)
|
56 |
+
|
57 |
return {
|
58 |
'epochs_data': epochs_data,
|
59 |
+
'iterations_data': iterations_data,
|
60 |
'final_metrics': final_metrics,
|
61 |
'recommendations': recommendations,
|
62 |
+
'gradient_info': gradient_info,
|
63 |
+
'privacy_budget': privacy_budget
|
64 |
}
|
65 |
|
66 |
+
def _calculate_mock_privacy_budget(self, params: Dict[str, Any]) -> float:
|
67 |
+
"""Calculate a realistic mock privacy budget based on DP-SGD theory."""
|
68 |
+
noise_multiplier = params['noise_multiplier']
|
69 |
+
epochs = params['epochs']
|
70 |
+
batch_size = params['batch_size']
|
71 |
+
|
72 |
+
# More realistic calculation based on DP-SGD research
|
73 |
+
q = batch_size / 60000 # Sampling rate for MNIST
|
74 |
+
steps = epochs * (60000 // batch_size)
|
75 |
+
|
76 |
+
# Simplified but more accurate RDP calculation
|
77 |
+
# Based on research: Ξ΅ β q*sqrt(steps*log(1/Ξ΄)) / Ο for large Ο
|
78 |
+
import math
|
79 |
+
delta = 1e-5
|
80 |
+
epsilon = (q * math.sqrt(steps * math.log(1/delta))) / noise_multiplier
|
81 |
+
|
82 |
+
# Add some realistic variation
|
83 |
+
epsilon *= (1 + np.random.normal(0, 0.1))
|
84 |
+
|
85 |
+
return max(0.1, min(50.0, epsilon))
|
86 |
+
|
87 |
+
def _calculate_realistic_privacy_factor(self, clipping_norm: float, noise_multiplier: float, batch_size: int, epochs: int) -> float:
|
88 |
+
"""Calculate realistic privacy impact based on DP-SGD research."""
|
89 |
+
# Research shows DP-SGD can achieve 85-98% accuracy with proper parameters
|
90 |
+
# The privacy impact should be much less severe than previously modeled
|
91 |
+
|
92 |
+
# Base degradation from noise (much less severe)
|
93 |
+
if noise_multiplier <= 0.5:
|
94 |
+
noise_degradation = 0.02 # Very little impact with low noise
|
95 |
+
elif noise_multiplier <= 1.0:
|
96 |
+
noise_degradation = 0.05 # Small impact with medium noise
|
97 |
+
elif noise_multiplier <= 1.5:
|
98 |
+
noise_degradation = 0.12 # Moderate impact
|
99 |
+
else:
|
100 |
+
noise_degradation = min(0.25, 0.1 + 0.05 * noise_multiplier) # Higher impact with very high noise
|
101 |
+
|
102 |
+
# Clipping degradation (much less severe)
|
103 |
+
if clipping_norm >= 2.0:
|
104 |
+
clipping_degradation = 0.01 # Minimal impact with good clipping
|
105 |
+
elif clipping_norm >= 1.0:
|
106 |
+
clipping_degradation = 0.03 # Small impact
|
107 |
+
else:
|
108 |
+
clipping_degradation = min(0.15, 0.2 / clipping_norm) # More impact with very low clipping
|
109 |
+
|
110 |
+
# Batch size effect (larger batches help significantly)
|
111 |
+
if batch_size >= 256:
|
112 |
+
batch_factor = -0.02 # Bonus for large batches
|
113 |
+
elif batch_size >= 128:
|
114 |
+
batch_factor = 0.01 # Small penalty
|
115 |
+
else:
|
116 |
+
batch_factor = min(0.08, 0.001 * (128 - batch_size))
|
117 |
+
|
118 |
+
# Epochs effect (more training helps overcome noise)
|
119 |
+
if epochs >= 10:
|
120 |
+
epoch_factor = -0.03 # Bonus for sufficient training
|
121 |
+
elif epochs >= 5:
|
122 |
+
epoch_factor = 0.01 # Small penalty
|
123 |
+
else:
|
124 |
+
epoch_factor = 0.05 # Penalty for insufficient training
|
125 |
+
|
126 |
+
total_degradation = noise_degradation + clipping_degradation + batch_factor + epoch_factor
|
127 |
+
privacy_factor = 1.0 - max(0, total_degradation) # Much less degradation overall
|
128 |
+
|
129 |
+
return max(0.7, privacy_factor) # Ensure minimum 70% of original performance (can achieve 85%+ with good params)
|
130 |
|
131 |
+
def _generate_iteration_data(self, epochs: int, privacy_factor: float, batch_size: int) -> List[Dict[str, float]]:
|
132 |
+
"""Generate realistic iteration-wise training metrics."""
|
133 |
+
iterations_data = []
|
134 |
+
|
135 |
+
# Simulate ~60,000 training samples, so iterations_per_epoch = 60000 / batch_size
|
136 |
+
dataset_size = 60000
|
137 |
+
iterations_per_epoch = dataset_size // batch_size
|
138 |
+
|
139 |
+
# Realistic base learning curve parameters
|
140 |
+
base_accuracy = self.base_accuracy * privacy_factor
|
141 |
+
base_loss = self.base_loss / privacy_factor
|
142 |
+
|
143 |
+
current_iteration = 0
|
144 |
+
for epoch in range(1, epochs + 1):
|
145 |
+
for iteration_in_epoch in range(0, iterations_per_epoch, 10): # Sample every 10th
|
146 |
+
current_iteration += 10
|
147 |
+
|
148 |
+
# Overall progress through all training
|
149 |
+
total_iterations = epochs * iterations_per_epoch
|
150 |
+
overall_progress = current_iteration / total_iterations
|
151 |
+
|
152 |
+
# More realistic learning curve: slower start, plateau effect
|
153 |
+
learning_progress = 1 - np.exp(-3 * overall_progress) # Exponential approach to target
|
154 |
+
|
155 |
+
# Add realistic variation (DP-SGD has more noise)
|
156 |
+
noise_std = 0.08 if privacy_factor < 0.7 else 0.04 # More noise for high privacy
|
157 |
+
noise = np.random.normal(0, noise_std)
|
158 |
+
|
159 |
+
# Calculate realistic accuracy progression
|
160 |
+
target_accuracy = base_accuracy * (0.4 + 0.6 * learning_progress)
|
161 |
+
accuracy = target_accuracy + noise
|
162 |
+
|
163 |
+
# Calculate corresponding loss
|
164 |
+
target_loss = base_loss * (1.5 - 0.5 * learning_progress)
|
165 |
+
loss = target_loss - noise * 0.3 # Loss inversely correlated with accuracy
|
166 |
+
|
167 |
+
# Add some iteration-level oscillations (typical of SGD)
|
168 |
+
oscillation = 0.015 * np.sin(current_iteration * 0.05)
|
169 |
+
accuracy += oscillation
|
170 |
+
loss -= oscillation * 0.5
|
171 |
+
|
172 |
+
iterations_data.append({
|
173 |
+
'iteration': current_iteration,
|
174 |
+
'epoch': epoch,
|
175 |
+
'accuracy': max(5, min(95, accuracy * 100)), # Realistic bounds
|
176 |
+
'loss': max(0.05, loss),
|
177 |
+
'train_accuracy': max(5, min(95, (accuracy + np.random.normal(0, 0.02)) * 100)),
|
178 |
+
'train_loss': max(0.05, loss + np.random.normal(0, 0.1))
|
179 |
+
})
|
180 |
+
|
181 |
+
return iterations_data
|
182 |
+
|
183 |
def _generate_epoch_data(self, epochs: int, privacy_factor: float) -> List[Dict[str, float]]:
|
184 |
"""Generate realistic training metrics for each epoch."""
|
185 |
epochs_data = []
|
186 |
|
187 |
+
# Realistic base learning curve parameters
|
188 |
base_accuracy = self.base_accuracy * privacy_factor
|
189 |
base_loss = self.base_loss / privacy_factor
|
190 |
|
191 |
for epoch in range(1, epochs + 1):
|
192 |
+
# Realistic learning curve: fast early improvement, then plateau
|
193 |
progress = epoch / epochs
|
194 |
+
learning_factor = 1 - np.exp(-2.5 * progress) # Exponential learning curve
|
195 |
+
|
196 |
+
# Add realistic epoch-to-epoch variation
|
197 |
+
noise_std = 0.03 if privacy_factor < 0.7 else 0.015
|
198 |
+
noise = np.random.normal(0, noise_std)
|
199 |
|
200 |
+
# Calculate realistic metrics
|
201 |
+
accuracy = base_accuracy * (0.4 + 0.6 * learning_factor) + noise
|
202 |
+
loss = base_loss * (1.4 - 0.4 * learning_factor) - noise * 0.3
|
203 |
|
204 |
epochs_data.append({
|
205 |
'epoch': epoch,
|
206 |
+
'accuracy': max(5, min(95, accuracy * 100)), # Convert to percentage with bounds
|
207 |
+
'loss': max(0.05, loss),
|
208 |
+
'train_accuracy': max(5, min(95, (accuracy + np.random.normal(0, 0.01)) * 100)),
|
209 |
+
'train_loss': max(0.05, loss + np.random.normal(0, 0.05))
|
210 |
})
|
211 |
|
212 |
return epochs_data
|
213 |
|
214 |
def _calculate_final_metrics(self, epochs_data: List[Dict[str, float]], privacy_factor: float) -> Dict[str, float]:
|
215 |
+
"""Calculate final training metrics that are CONSISTENT with epoch data."""
|
216 |
+
if not epochs_data:
|
217 |
+
return {'accuracy': 50.0, 'loss': 1.0, 'training_time': 1.0}
|
218 |
+
|
219 |
+
# Use the LAST epoch's results as final metrics (consistency!)
|
220 |
final_epoch = epochs_data[-1]
|
221 |
|
222 |
+
# Training time should be realistic for DP-SGD (slower than normal)
|
223 |
+
base_time = len(epochs_data) * 0.8 # Base time per epoch
|
224 |
+
privacy_slowdown = (2.0 - privacy_factor) # DP-SGD is slower
|
225 |
+
time_variation = 1.0 + np.random.normal(0, 0.1)
|
226 |
|
227 |
return {
|
228 |
+
'accuracy': final_epoch['accuracy'], # Consistent with training progress!
|
229 |
'loss': final_epoch['loss'],
|
230 |
+
'training_time': base_time * privacy_slowdown * time_variation
|
231 |
}
|
232 |
|
233 |
def _generate_recommendations(self, params: Dict[str, Any], metrics: Dict[str, float]) -> List[Dict[str, str]]:
|
234 |
+
"""Generate realistic recommendations based on DP-SGD best practices."""
|
235 |
recommendations = []
|
236 |
|
237 |
+
# Noise multiplier recommendations (critical for DP-SGD)
|
238 |
+
if params['noise_multiplier'] < 0.5:
|
239 |
+
recommendations.append({
|
240 |
+
'icon': 'π',
|
241 |
+
'text': 'Very low noise provides minimal privacy. Consider Ο β₯ 0.8 for meaningful privacy.'
|
242 |
+
})
|
243 |
+
elif params['noise_multiplier'] > 2.0:
|
244 |
recommendations.append({
|
245 |
'icon': 'β οΈ',
|
246 |
+
'text': 'High noise (Ο > 2.0) significantly degrades accuracy. Try reducing to 0.8-1.5.'
|
247 |
})
|
248 |
+
elif params['noise_multiplier'] > 1.5:
|
249 |
recommendations.append({
|
250 |
+
'icon': 'π‘',
|
251 |
+
'text': 'Consider reducing noise multiplier to 0.8-1.2 for better utility-privacy trade-off.'
|
252 |
})
|
253 |
|
254 |
+
# Clipping norm recommendations
|
255 |
+
if params['clipping_norm'] < 0.5:
|
256 |
recommendations.append({
|
257 |
+
'icon': 'β οΈ',
|
258 |
+
'text': 'Very low clipping norm can prevent learning. Try C = 1.0-2.0.'
|
259 |
})
|
260 |
+
elif params['clipping_norm'] > 3.0:
|
261 |
recommendations.append({
|
262 |
+
'icon': 'π',
|
263 |
+
'text': 'Large clipping norm reduces privacy protection. Consider C β€ 2.0.'
|
264 |
})
|
265 |
|
266 |
+
# Batch size recommendations (important for DP-SGD)
|
267 |
if params['batch_size'] < 64:
|
268 |
recommendations.append({
|
269 |
'icon': 'β‘',
|
270 |
+
'text': 'Small batch sizes amplify noise effects. Try batch size β₯ 128 for better stability.'
|
271 |
})
|
272 |
+
elif params['batch_size'] > 512:
|
273 |
recommendations.append({
|
274 |
+
'icon': 'πΎ',
|
275 |
+
'text': 'Very large batch sizes may require more memory and longer training time.'
|
276 |
})
|
277 |
|
278 |
+
# Learning rate recommendations
|
279 |
if params['learning_rate'] > 0.05:
|
280 |
recommendations.append({
|
281 |
'icon': 'β οΈ',
|
282 |
+
'text': 'High learning rate with noise can destabilize training. Try β€ 0.02.'
|
283 |
})
|
284 |
+
elif params['learning_rate'] < 0.005:
|
285 |
recommendations.append({
|
286 |
'icon': 'β³',
|
287 |
+
'text': 'Very low learning rate may require more epochs for convergence.'
|
288 |
})
|
289 |
|
290 |
+
# Epochs recommendations
|
291 |
+
if params['epochs'] < 5:
|
292 |
+
recommendations.append({
|
293 |
+
'icon': 'π',
|
294 |
+
'text': 'Few epochs may not be enough to overcome noise. Try 8-15 epochs.'
|
295 |
+
})
|
296 |
+
elif params['epochs'] > 20:
|
297 |
+
recommendations.append({
|
298 |
+
'icon': 'π',
|
299 |
+
'text': 'Many epochs increase privacy cost. Consider early stopping around 10-15 epochs.'
|
300 |
+
})
|
301 |
+
|
302 |
+
# Accuracy-based recommendations
|
303 |
+
if metrics['accuracy'] < 60:
|
304 |
recommendations.append({
|
305 |
'icon': 'π',
|
306 |
+
'text': 'Low accuracy suggests too much noise. Reduce Ο or increase C for better utility.'
|
307 |
+
})
|
308 |
+
elif metrics['accuracy'] > 85:
|
309 |
+
recommendations.append({
|
310 |
+
'icon': 'π―',
|
311 |
+
'text': 'Good accuracy! This is a well-balanced privacy-utility trade-off.'
|
312 |
})
|
313 |
|
314 |
return recommendations
|
app/training/real_trainer.py
ADDED
@@ -0,0 +1,294 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
import tensorflow as tf
|
3 |
+
from tensorflow import keras
|
4 |
+
from tensorflow_privacy.privacy.optimizers import dp_optimizer_keras
|
5 |
+
from tensorflow_privacy.privacy.analysis import compute_dp_sgd_privacy
|
6 |
+
import time
|
7 |
+
from typing import Dict, List, Any, Union
|
8 |
+
try:
|
9 |
+
from typing import List, Dict
|
10 |
+
except ImportError:
|
11 |
+
pass
|
12 |
+
import logging
|
13 |
+
|
14 |
+
# Set up logging
|
15 |
+
logging.getLogger('tensorflow').setLevel(logging.ERROR)
|
16 |
+
|
17 |
+
class RealTrainer:
|
18 |
+
def __init__(self):
|
19 |
+
# Set random seeds for reproducibility
|
20 |
+
tf.random.set_seed(42)
|
21 |
+
np.random.seed(42)
|
22 |
+
|
23 |
+
# Load and preprocess MNIST dataset
|
24 |
+
self.x_train, self.y_train, self.x_test, self.y_test = self._load_mnist()
|
25 |
+
self.model = None
|
26 |
+
|
27 |
+
def _load_mnist(self):
|
28 |
+
"""Load and preprocess MNIST dataset."""
|
29 |
+
print("Loading MNIST dataset...")
|
30 |
+
|
31 |
+
# Load MNIST data
|
32 |
+
(x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data()
|
33 |
+
|
34 |
+
# Normalize pixel values to [0, 1]
|
35 |
+
x_train = x_train.astype('float32') / 255.0
|
36 |
+
x_test = x_test.astype('float32') / 255.0
|
37 |
+
|
38 |
+
# Reshape to flatten images
|
39 |
+
x_train = x_train.reshape(-1, 28 * 28)
|
40 |
+
x_test = x_test.reshape(-1, 28 * 28)
|
41 |
+
|
42 |
+
# Convert labels to categorical
|
43 |
+
y_train = keras.utils.to_categorical(y_train, 10)
|
44 |
+
y_test = keras.utils.to_categorical(y_test, 10)
|
45 |
+
|
46 |
+
print(f"Training data shape: {x_train.shape}")
|
47 |
+
print(f"Test data shape: {x_test.shape}")
|
48 |
+
|
49 |
+
return x_train, y_train, x_test, y_test
|
50 |
+
|
51 |
+
def _create_model(self):
|
52 |
+
"""Create a simple MLP model for MNIST classification."""
|
53 |
+
model = keras.Sequential([
|
54 |
+
keras.layers.Dense(128, activation='relu', input_shape=(784,)),
|
55 |
+
keras.layers.Dropout(0.2),
|
56 |
+
keras.layers.Dense(64, activation='relu'),
|
57 |
+
keras.layers.Dropout(0.2),
|
58 |
+
keras.layers.Dense(10, activation='softmax')
|
59 |
+
])
|
60 |
+
return model
|
61 |
+
|
62 |
+
def train(self, params):
|
63 |
+
"""
|
64 |
+
Train a model on MNIST using DP-SGD.
|
65 |
+
|
66 |
+
Args:
|
67 |
+
params: Dictionary containing training parameters:
|
68 |
+
- clipping_norm: float
|
69 |
+
- noise_multiplier: float
|
70 |
+
- batch_size: int
|
71 |
+
- learning_rate: float
|
72 |
+
- epochs: int
|
73 |
+
|
74 |
+
Returns:
|
75 |
+
Dictionary containing training results and metrics
|
76 |
+
"""
|
77 |
+
try:
|
78 |
+
print(f"Starting training with parameters: {params}")
|
79 |
+
|
80 |
+
# Extract parameters
|
81 |
+
clipping_norm = params['clipping_norm']
|
82 |
+
noise_multiplier = params['noise_multiplier']
|
83 |
+
batch_size = params['batch_size']
|
84 |
+
learning_rate = params['learning_rate']
|
85 |
+
epochs = params['epochs']
|
86 |
+
|
87 |
+
# Create model
|
88 |
+
self.model = self._create_model()
|
89 |
+
|
90 |
+
# Create DP optimizer
|
91 |
+
optimizer = dp_optimizer_keras.DPKerasAdamOptimizer(
|
92 |
+
l2_norm_clip=clipping_norm,
|
93 |
+
noise_multiplier=noise_multiplier,
|
94 |
+
num_microbatches=batch_size,
|
95 |
+
learning_rate=learning_rate
|
96 |
+
)
|
97 |
+
|
98 |
+
# Compile model
|
99 |
+
self.model.compile(
|
100 |
+
optimizer=optimizer,
|
101 |
+
loss='categorical_crossentropy',
|
102 |
+
metrics=['accuracy']
|
103 |
+
)
|
104 |
+
|
105 |
+
# Prepare training data
|
106 |
+
train_dataset = tf.data.Dataset.from_tensor_slices((self.x_train, self.y_train))
|
107 |
+
train_dataset = train_dataset.batch(batch_size).shuffle(1000)
|
108 |
+
|
109 |
+
# Prepare test data
|
110 |
+
test_dataset = tf.data.Dataset.from_tensor_slices((self.x_test, self.y_test))
|
111 |
+
test_dataset = test_dataset.batch(batch_size)
|
112 |
+
|
113 |
+
# Track training metrics
|
114 |
+
epochs_data = []
|
115 |
+
start_time = time.time()
|
116 |
+
|
117 |
+
# Training loop
|
118 |
+
for epoch in range(epochs):
|
119 |
+
print(f"Epoch {epoch + 1}/{epochs}")
|
120 |
+
|
121 |
+
# Train for one epoch
|
122 |
+
history = self.model.fit(
|
123 |
+
train_dataset,
|
124 |
+
epochs=1,
|
125 |
+
verbose='0',
|
126 |
+
validation_data=test_dataset
|
127 |
+
)
|
128 |
+
|
129 |
+
# Record metrics
|
130 |
+
train_accuracy = history.history['accuracy'][0] * 100
|
131 |
+
train_loss = history.history['loss'][0]
|
132 |
+
val_accuracy = history.history['val_accuracy'][0] * 100
|
133 |
+
val_loss = history.history['val_loss'][0]
|
134 |
+
|
135 |
+
epochs_data.append({
|
136 |
+
'epoch': epoch + 1,
|
137 |
+
'accuracy': val_accuracy, # Use validation accuracy for display
|
138 |
+
'loss': val_loss,
|
139 |
+
'train_accuracy': train_accuracy,
|
140 |
+
'train_loss': train_loss
|
141 |
+
})
|
142 |
+
|
143 |
+
print(f" Train accuracy: {train_accuracy:.2f}%, Loss: {train_loss:.4f}")
|
144 |
+
print(f" Val accuracy: {val_accuracy:.2f}%, Loss: {val_loss:.4f}")
|
145 |
+
|
146 |
+
training_time = time.time() - start_time
|
147 |
+
|
148 |
+
# Calculate final metrics
|
149 |
+
final_metrics = {
|
150 |
+
'accuracy': epochs_data[-1]['accuracy'],
|
151 |
+
'loss': epochs_data[-1]['loss'],
|
152 |
+
'training_time': training_time
|
153 |
+
}
|
154 |
+
|
155 |
+
# Calculate privacy budget
|
156 |
+
privacy_budget = self._calculate_privacy_budget(params)
|
157 |
+
|
158 |
+
# Generate recommendations
|
159 |
+
recommendations = self._generate_recommendations(params, final_metrics)
|
160 |
+
|
161 |
+
# Generate gradient information (mock for visualization)
|
162 |
+
gradient_info = {
|
163 |
+
'before_clipping': self.generate_gradient_norms(clipping_norm),
|
164 |
+
'after_clipping': self.generate_clipped_gradients(clipping_norm)
|
165 |
+
}
|
166 |
+
|
167 |
+
print(f"Training completed in {training_time:.2f} seconds")
|
168 |
+
print(f"Final accuracy: {final_metrics['accuracy']:.2f}%")
|
169 |
+
print(f"Privacy budget (Ξ΅): {privacy_budget:.2f}")
|
170 |
+
|
171 |
+
return {
|
172 |
+
'epochs_data': epochs_data,
|
173 |
+
'final_metrics': final_metrics,
|
174 |
+
'recommendations': recommendations,
|
175 |
+
'gradient_info': gradient_info,
|
176 |
+
'privacy_budget': privacy_budget
|
177 |
+
}
|
178 |
+
|
179 |
+
except Exception as e:
|
180 |
+
print(f"Training error: {str(e)}")
|
181 |
+
# Fall back to mock training if real training fails
|
182 |
+
return self._fallback_training(params)
|
183 |
+
|
184 |
+
def _calculate_privacy_budget(self, params):
|
185 |
+
"""Calculate the actual privacy budget using TensorFlow Privacy."""
|
186 |
+
try:
|
187 |
+
dataset_size = len(self.x_train)
|
188 |
+
batch_size = params['batch_size']
|
189 |
+
epochs = params['epochs']
|
190 |
+
noise_multiplier = params['noise_multiplier']
|
191 |
+
|
192 |
+
# Calculate the privacy budget
|
193 |
+
eps, delta = compute_dp_sgd_privacy.compute_dp_sgd_privacy(
|
194 |
+
n=dataset_size,
|
195 |
+
batch_size=batch_size,
|
196 |
+
noise_multiplier=noise_multiplier,
|
197 |
+
epochs=epochs,
|
198 |
+
delta=1e-5
|
199 |
+
)
|
200 |
+
|
201 |
+
return eps
|
202 |
+
except Exception as e:
|
203 |
+
print(f"Privacy calculation error: {str(e)}")
|
204 |
+
# Return a reasonable estimate
|
205 |
+
return max(0.1, 10.0 / params['noise_multiplier'])
|
206 |
+
|
207 |
+
def _fallback_training(self, params):
|
208 |
+
"""Fallback to mock training if real training fails."""
|
209 |
+
print("Falling back to mock training...")
|
210 |
+
from .mock_trainer import MockTrainer
|
211 |
+
mock_trainer = MockTrainer()
|
212 |
+
return mock_trainer.train(params)
|
213 |
+
|
214 |
+
def _generate_recommendations(self, params, metrics):
|
215 |
+
"""Generate recommendations based on real training results."""
|
216 |
+
recommendations = []
|
217 |
+
|
218 |
+
# Check clipping norm
|
219 |
+
if params['clipping_norm'] < 0.5:
|
220 |
+
recommendations.append({
|
221 |
+
'icon': 'β οΈ',
|
222 |
+
'text': 'Very low clipping norm detected. This might severely limit gradient updates.'
|
223 |
+
})
|
224 |
+
elif params['clipping_norm'] > 5.0:
|
225 |
+
recommendations.append({
|
226 |
+
'icon': 'π',
|
227 |
+
'text': 'High clipping norm reduces privacy protection. Consider lowering it.'
|
228 |
+
})
|
229 |
+
|
230 |
+
# Check noise multiplier based on actual performance
|
231 |
+
if params['noise_multiplier'] < 0.8:
|
232 |
+
recommendations.append({
|
233 |
+
'icon': 'π',
|
234 |
+
'text': 'Low noise multiplier provides weaker privacy guarantees.'
|
235 |
+
})
|
236 |
+
elif params['noise_multiplier'] > 3.0:
|
237 |
+
recommendations.append({
|
238 |
+
'icon': 'β οΈ',
|
239 |
+
'text': 'Very high noise is significantly impacting model accuracy.'
|
240 |
+
})
|
241 |
+
|
242 |
+
# Check actual accuracy results
|
243 |
+
if metrics['accuracy'] < 70:
|
244 |
+
recommendations.append({
|
245 |
+
'icon': 'π',
|
246 |
+
'text': 'Low accuracy achieved. Consider reducing noise or increasing epochs.'
|
247 |
+
})
|
248 |
+
elif metrics['accuracy'] > 95:
|
249 |
+
recommendations.append({
|
250 |
+
'icon': 'β
',
|
251 |
+
'text': 'Excellent accuracy! Privacy-utility tradeoff is well balanced.'
|
252 |
+
})
|
253 |
+
|
254 |
+
# Check batch size for DP-SGD
|
255 |
+
if params['batch_size'] < 32:
|
256 |
+
recommendations.append({
|
257 |
+
'icon': 'β‘',
|
258 |
+
'text': 'Small batch size with DP-SGD can lead to poor convergence.'
|
259 |
+
})
|
260 |
+
|
261 |
+
# Check learning rate
|
262 |
+
if params['learning_rate'] > 0.1:
|
263 |
+
recommendations.append({
|
264 |
+
'icon': 'β οΈ',
|
265 |
+
'text': 'High learning rate may cause instability with DP-SGD noise.'
|
266 |
+
})
|
267 |
+
|
268 |
+
return recommendations
|
269 |
+
|
270 |
+
def generate_gradient_norms(self, clipping_norm):
|
271 |
+
"""Generate realistic gradient norms for visualization."""
|
272 |
+
num_points = 100
|
273 |
+
gradients = []
|
274 |
+
|
275 |
+
# Generate log-normal distributed gradient norms
|
276 |
+
for _ in range(num_points):
|
277 |
+
# Most gradients are smaller than clipping norm, some exceed it
|
278 |
+
if np.random.random() < 0.7:
|
279 |
+
norm = np.random.gamma(2, clipping_norm / 3)
|
280 |
+
else:
|
281 |
+
norm = np.random.gamma(3, clipping_norm / 2)
|
282 |
+
|
283 |
+
# Create density for visualization
|
284 |
+
density = np.exp(-((norm - clipping_norm/2) ** 2) / (2 * (clipping_norm/3) ** 2))
|
285 |
+
density = 0.1 + 0.9 * density + 0.1 * np.random.random()
|
286 |
+
|
287 |
+
gradients.append({'x': float(norm), 'y': float(density)})
|
288 |
+
|
289 |
+
return sorted(gradients, key=lambda x: x['x'])
|
290 |
+
|
291 |
+
def generate_clipped_gradients(self, clipping_norm):
|
292 |
+
"""Generate clipped versions of the gradient norms."""
|
293 |
+
original_gradients = self.generate_gradient_norms(clipping_norm)
|
294 |
+
return [{'x': min(g['x'], clipping_norm), 'y': g['y']} for g in original_gradients]
|
app/training/simplified_real_trainer.py
ADDED
@@ -0,0 +1,411 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
import tensorflow as tf
|
3 |
+
from tensorflow import keras
|
4 |
+
import time
|
5 |
+
import logging
|
6 |
+
|
7 |
+
# Set up logging
|
8 |
+
logging.getLogger('tensorflow').setLevel(logging.ERROR)
|
9 |
+
|
10 |
+
class SimplifiedRealTrainer:
|
11 |
+
def __init__(self):
|
12 |
+
# Set random seeds for reproducibility
|
13 |
+
tf.random.set_seed(42)
|
14 |
+
np.random.seed(42)
|
15 |
+
|
16 |
+
# Load and preprocess MNIST dataset
|
17 |
+
self.x_train, self.y_train, self.x_test, self.y_test = self._load_mnist()
|
18 |
+
self.model = None
|
19 |
+
|
20 |
+
def _load_mnist(self):
|
21 |
+
"""Load and preprocess MNIST dataset."""
|
22 |
+
print("Loading MNIST dataset...")
|
23 |
+
|
24 |
+
# Load MNIST data
|
25 |
+
(x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data()
|
26 |
+
|
27 |
+
# Normalize pixel values to [0, 1]
|
28 |
+
x_train = x_train.astype('float32') / 255.0
|
29 |
+
x_test = x_test.astype('float32') / 255.0
|
30 |
+
|
31 |
+
# Reshape to flatten images
|
32 |
+
x_train = x_train.reshape(-1, 28 * 28)
|
33 |
+
x_test = x_test.reshape(-1, 28 * 28)
|
34 |
+
|
35 |
+
# Convert labels to categorical
|
36 |
+
y_train = keras.utils.to_categorical(y_train, 10)
|
37 |
+
y_test = keras.utils.to_categorical(y_test, 10)
|
38 |
+
|
39 |
+
print(f"Training data shape: {x_train.shape}")
|
40 |
+
print(f"Test data shape: {x_test.shape}")
|
41 |
+
|
42 |
+
return x_train, y_train, x_test, y_test
|
43 |
+
|
44 |
+
def _create_model(self):
|
45 |
+
"""Create a simple MLP model for MNIST classification optimized for DP-SGD."""
|
46 |
+
# Use a simpler, more robust architecture for DP-SGD
|
47 |
+
model = keras.Sequential([
|
48 |
+
keras.layers.Dense(256, activation='tanh', input_shape=(784,)), # tanh works better with DP-SGD
|
49 |
+
keras.layers.Dense(128, activation='tanh'),
|
50 |
+
keras.layers.Dense(10, activation='softmax')
|
51 |
+
])
|
52 |
+
return model
|
53 |
+
|
54 |
+
def _clip_gradients(self, gradients, clipping_norm):
|
55 |
+
"""Clip gradients to a maximum L2 norm globally across all parameters."""
|
56 |
+
# Calculate global L2 norm across all gradients
|
57 |
+
global_norm = tf.linalg.global_norm(gradients)
|
58 |
+
|
59 |
+
# Clip if necessary
|
60 |
+
if global_norm > clipping_norm:
|
61 |
+
# Scale all gradients uniformly
|
62 |
+
scaling_factor = clipping_norm / global_norm
|
63 |
+
clipped_gradients = [grad * scaling_factor if grad is not None else grad
|
64 |
+
for grad in gradients]
|
65 |
+
else:
|
66 |
+
clipped_gradients = gradients
|
67 |
+
|
68 |
+
return clipped_gradients
|
69 |
+
|
70 |
+
def _add_gaussian_noise(self, gradients, noise_multiplier, clipping_norm, batch_size):
|
71 |
+
"""Add Gaussian noise to gradients for differential privacy."""
|
72 |
+
noisy_gradients = []
|
73 |
+
for grad in gradients:
|
74 |
+
if grad is not None:
|
75 |
+
# Proper noise scaling for DP-SGD: noise_stddev = clipping_norm * noise_multiplier / batch_size
|
76 |
+
# This ensures the noise is calibrated correctly for the batch size
|
77 |
+
noise_stddev = clipping_norm * noise_multiplier / batch_size
|
78 |
+
noise = tf.random.normal(tf.shape(grad), mean=0.0, stddev=noise_stddev)
|
79 |
+
noisy_grad = grad + noise
|
80 |
+
noisy_gradients.append(noisy_grad)
|
81 |
+
else:
|
82 |
+
noisy_gradients.append(grad)
|
83 |
+
return noisy_gradients
|
84 |
+
|
85 |
+
def train(self, params):
|
86 |
+
"""
|
87 |
+
Train a model on MNIST using a simplified DP-SGD implementation.
|
88 |
+
|
89 |
+
Args:
|
90 |
+
params: Dictionary containing training parameters
|
91 |
+
|
92 |
+
Returns:
|
93 |
+
Dictionary containing training results and metrics
|
94 |
+
"""
|
95 |
+
try:
|
96 |
+
print(f"Starting training with parameters: {params}")
|
97 |
+
|
98 |
+
# Extract parameters with balanced defaults for real MNIST DP-SGD training
|
99 |
+
clipping_norm = params.get('clipping_norm', 2.0) # Balanced clipping norm
|
100 |
+
noise_multiplier = params.get('noise_multiplier', 1.0) # Moderate noise for privacy
|
101 |
+
batch_size = params.get('batch_size', 256) # Large batches help with DP-SGD
|
102 |
+
learning_rate = params.get('learning_rate', 0.05) # Balanced learning rate
|
103 |
+
epochs = params.get('epochs', 15)
|
104 |
+
|
105 |
+
# Adjust parameters based on research findings for good accuracy
|
106 |
+
if noise_multiplier > 1.5:
|
107 |
+
print(f"Warning: Noise multiplier {noise_multiplier} is very high, reducing to 1.5 for better learning")
|
108 |
+
noise_multiplier = min(noise_multiplier, 1.5)
|
109 |
+
|
110 |
+
if clipping_norm < 1.0:
|
111 |
+
print(f"Warning: Clipping norm {clipping_norm} is too low, increasing to 1.0 for better learning")
|
112 |
+
clipping_norm = max(clipping_norm, 1.0)
|
113 |
+
|
114 |
+
if batch_size < 128:
|
115 |
+
print(f"Warning: Batch size {batch_size} is too small for DP-SGD, using 128")
|
116 |
+
batch_size = max(batch_size, 128)
|
117 |
+
|
118 |
+
# Adjust learning rate based on noise level
|
119 |
+
if noise_multiplier <= 0.5:
|
120 |
+
learning_rate = max(learning_rate, 0.15) # Can use higher LR with low noise
|
121 |
+
elif noise_multiplier <= 1.0:
|
122 |
+
learning_rate = max(learning_rate, 0.1) # Medium LR with medium noise
|
123 |
+
else:
|
124 |
+
learning_rate = max(learning_rate, 0.05) # Lower LR with high noise
|
125 |
+
|
126 |
+
print(f"Adjusted parameters - LR: {learning_rate}, Noise: {noise_multiplier}, Clipping: {clipping_norm}, Batch: {batch_size}")
|
127 |
+
|
128 |
+
# Create model
|
129 |
+
self.model = self._create_model()
|
130 |
+
|
131 |
+
# Create optimizer with adjusted learning rate
|
132 |
+
optimizer = keras.optimizers.SGD(learning_rate=learning_rate, momentum=0.9) # SGD often works better than Adam for DP-SGD
|
133 |
+
|
134 |
+
# Compile model
|
135 |
+
self.model.compile(
|
136 |
+
optimizer=optimizer,
|
137 |
+
loss='categorical_crossentropy',
|
138 |
+
metrics=['accuracy']
|
139 |
+
)
|
140 |
+
|
141 |
+
# Track training metrics
|
142 |
+
epochs_data = []
|
143 |
+
iterations_data = []
|
144 |
+
start_time = time.time()
|
145 |
+
|
146 |
+
# Convert to TensorFlow datasets
|
147 |
+
train_dataset = tf.data.Dataset.from_tensor_slices((self.x_train, self.y_train))
|
148 |
+
train_dataset = train_dataset.batch(batch_size).shuffle(1000)
|
149 |
+
|
150 |
+
test_dataset = tf.data.Dataset.from_tensor_slices((self.x_test, self.y_test))
|
151 |
+
test_dataset = test_dataset.batch(1000) # Larger batch for evaluation
|
152 |
+
|
153 |
+
# Calculate total iterations for progress tracking
|
154 |
+
total_iterations = epochs * (len(self.x_train) // batch_size)
|
155 |
+
current_iteration = 0
|
156 |
+
|
157 |
+
print(f"Starting training: {epochs} epochs, ~{len(self.x_train) // batch_size} iterations per epoch")
|
158 |
+
print(f"Total iterations: {total_iterations}")
|
159 |
+
|
160 |
+
# Training loop with manual DP-SGD
|
161 |
+
for epoch in range(epochs):
|
162 |
+
print(f"Epoch {epoch + 1}/{epochs}")
|
163 |
+
|
164 |
+
epoch_loss = 0
|
165 |
+
epoch_accuracy = 0
|
166 |
+
num_batches = 0
|
167 |
+
|
168 |
+
for batch_x, batch_y in train_dataset:
|
169 |
+
current_iteration += 1
|
170 |
+
|
171 |
+
with tf.GradientTape() as tape:
|
172 |
+
predictions = self.model(batch_x, training=True)
|
173 |
+
loss = keras.losses.categorical_crossentropy(batch_y, predictions)
|
174 |
+
loss = tf.reduce_mean(loss)
|
175 |
+
|
176 |
+
# Compute gradients
|
177 |
+
gradients = tape.gradient(loss, self.model.trainable_variables)
|
178 |
+
|
179 |
+
# Clip gradients
|
180 |
+
gradients = self._clip_gradients(gradients, clipping_norm)
|
181 |
+
|
182 |
+
# Add noise for differential privacy
|
183 |
+
gradients = self._add_gaussian_noise(gradients, noise_multiplier, clipping_norm, batch_size)
|
184 |
+
|
185 |
+
# Apply gradients
|
186 |
+
optimizer.apply_gradients(zip(gradients, self.model.trainable_variables))
|
187 |
+
|
188 |
+
# Track metrics
|
189 |
+
accuracy = keras.metrics.categorical_accuracy(batch_y, predictions)
|
190 |
+
batch_loss = loss.numpy()
|
191 |
+
batch_accuracy = tf.reduce_mean(accuracy).numpy() * 100
|
192 |
+
|
193 |
+
epoch_loss += batch_loss
|
194 |
+
epoch_accuracy += batch_accuracy / 100 # Keep as fraction for averaging
|
195 |
+
num_batches += 1
|
196 |
+
|
197 |
+
# Record iteration-level metrics (sample every 10th iteration to reduce data size)
|
198 |
+
if current_iteration % 10 == 0 or current_iteration == total_iterations:
|
199 |
+
# Quick test accuracy evaluation (subset for speed)
|
200 |
+
test_subset = test_dataset.take(1) # Use just one batch for speed
|
201 |
+
test_loss_batch, test_accuracy_batch = self.model.evaluate(test_subset, verbose='0')
|
202 |
+
|
203 |
+
iterations_data.append({
|
204 |
+
'iteration': current_iteration,
|
205 |
+
'epoch': epoch + 1,
|
206 |
+
'accuracy': float(test_accuracy_batch * 100),
|
207 |
+
'loss': float(test_loss_batch),
|
208 |
+
'train_accuracy': float(batch_accuracy),
|
209 |
+
'train_loss': float(batch_loss)
|
210 |
+
})
|
211 |
+
|
212 |
+
# Progress indicator
|
213 |
+
if current_iteration % 100 == 0:
|
214 |
+
progress = (current_iteration / total_iterations) * 100
|
215 |
+
print(f" Progress: {progress:.1f}% (iteration {current_iteration}/{total_iterations})")
|
216 |
+
|
217 |
+
# Calculate average metrics for epoch
|
218 |
+
epoch_loss = epoch_loss / num_batches
|
219 |
+
epoch_accuracy = (epoch_accuracy / num_batches) * 100
|
220 |
+
|
221 |
+
# Evaluate on full test set
|
222 |
+
test_loss, test_accuracy = self.model.evaluate(test_dataset, verbose='0')
|
223 |
+
test_accuracy *= 100
|
224 |
+
|
225 |
+
epochs_data.append({
|
226 |
+
'epoch': epoch + 1,
|
227 |
+
'accuracy': float(test_accuracy),
|
228 |
+
'loss': float(test_loss),
|
229 |
+
'train_accuracy': float(epoch_accuracy),
|
230 |
+
'train_loss': float(epoch_loss)
|
231 |
+
})
|
232 |
+
|
233 |
+
print(f" Epoch complete - Train accuracy: {epoch_accuracy:.2f}%, Loss: {epoch_loss:.4f}")
|
234 |
+
print(f" Test accuracy: {test_accuracy:.2f}%, Loss: {test_loss:.4f}")
|
235 |
+
|
236 |
+
training_time = time.time() - start_time
|
237 |
+
|
238 |
+
# Calculate final metrics
|
239 |
+
final_metrics = {
|
240 |
+
'accuracy': float(epochs_data[-1]['accuracy']),
|
241 |
+
'loss': float(epochs_data[-1]['loss']),
|
242 |
+
'training_time': float(training_time)
|
243 |
+
}
|
244 |
+
|
245 |
+
# Calculate privacy budget (simplified estimate)
|
246 |
+
privacy_budget = float(self._calculate_privacy_budget(params))
|
247 |
+
|
248 |
+
# Generate recommendations
|
249 |
+
recommendations = self._generate_recommendations(params, final_metrics)
|
250 |
+
|
251 |
+
# Generate gradient information (mock for visualization)
|
252 |
+
gradient_info = {
|
253 |
+
'before_clipping': self.generate_gradient_norms(clipping_norm),
|
254 |
+
'after_clipping': self.generate_clipped_gradients(clipping_norm)
|
255 |
+
}
|
256 |
+
|
257 |
+
print(f"Training completed in {training_time:.2f} seconds")
|
258 |
+
print(f"Final test accuracy: {final_metrics['accuracy']:.2f}%")
|
259 |
+
print(f"Estimated privacy budget (Ξ΅): {privacy_budget:.2f}")
|
260 |
+
|
261 |
+
return {
|
262 |
+
'epochs_data': epochs_data,
|
263 |
+
'iterations_data': iterations_data,
|
264 |
+
'final_metrics': final_metrics,
|
265 |
+
'recommendations': recommendations,
|
266 |
+
'gradient_info': gradient_info,
|
267 |
+
'privacy_budget': privacy_budget
|
268 |
+
}
|
269 |
+
|
270 |
+
except Exception as e:
|
271 |
+
print(f"Training error: {str(e)}")
|
272 |
+
# Fall back to mock training if real training fails
|
273 |
+
return self._fallback_training(params)
|
274 |
+
|
275 |
+
def _calculate_privacy_budget(self, params):
|
276 |
+
"""Calculate a simplified privacy budget estimate."""
|
277 |
+
try:
|
278 |
+
# Simplified privacy calculation based on composition theorem
|
279 |
+
# This is a rough approximation for educational purposes
|
280 |
+
noise_multiplier = params['noise_multiplier']
|
281 |
+
epochs = params['epochs']
|
282 |
+
batch_size = params['batch_size']
|
283 |
+
|
284 |
+
# Sampling probability
|
285 |
+
q = batch_size / len(self.x_train)
|
286 |
+
|
287 |
+
# Simple composition (this is not tight, but gives reasonable estimates)
|
288 |
+
steps = epochs * (len(self.x_train) // batch_size)
|
289 |
+
|
290 |
+
# Approximate epsilon using basic composition
|
291 |
+
# eps β q * steps / (noise_multiplier^2)
|
292 |
+
epsilon = (q * steps) / (noise_multiplier ** 2)
|
293 |
+
|
294 |
+
# Add some realistic scaling
|
295 |
+
epsilon = max(0.1, min(100.0, epsilon))
|
296 |
+
|
297 |
+
return epsilon
|
298 |
+
except Exception as e:
|
299 |
+
print(f"Privacy calculation error: {str(e)}")
|
300 |
+
return max(0.1, 10.0 / params['noise_multiplier'])
|
301 |
+
|
302 |
+
def _fallback_training(self, params):
|
303 |
+
"""Fallback to mock training if real training fails."""
|
304 |
+
print("Falling back to mock training...")
|
305 |
+
from .mock_trainer import MockTrainer
|
306 |
+
mock_trainer = MockTrainer()
|
307 |
+
return mock_trainer.train(params)
|
308 |
+
|
309 |
+
def _generate_recommendations(self, params, metrics):
|
310 |
+
"""Generate recommendations based on real training results."""
|
311 |
+
recommendations = []
|
312 |
+
|
313 |
+
# Check clipping norm
|
314 |
+
if params['clipping_norm'] < 0.5:
|
315 |
+
recommendations.append({
|
316 |
+
'icon': 'β οΈ',
|
317 |
+
'text': 'Very low clipping norm detected. This severely limits gradient updates and learning.'
|
318 |
+
})
|
319 |
+
elif params['clipping_norm'] > 5.0:
|
320 |
+
recommendations.append({
|
321 |
+
'icon': 'π',
|
322 |
+
'text': 'High clipping norm reduces privacy protection. Consider lowering to 1-2.'
|
323 |
+
})
|
324 |
+
|
325 |
+
# Check noise multiplier based on actual performance
|
326 |
+
if params['noise_multiplier'] < 0.5:
|
327 |
+
recommendations.append({
|
328 |
+
'icon': 'π',
|
329 |
+
'text': 'Low noise multiplier provides weaker privacy guarantees.'
|
330 |
+
})
|
331 |
+
elif params['noise_multiplier'] > 2.0:
|
332 |
+
recommendations.append({
|
333 |
+
'icon': 'β οΈ',
|
334 |
+
'text': 'High noise is preventing convergence. Try reducing to 0.8-1.5 range.'
|
335 |
+
})
|
336 |
+
|
337 |
+
# Check actual accuracy results with more specific guidance
|
338 |
+
if metrics['accuracy'] < 30:
|
339 |
+
recommendations.append({
|
340 |
+
'icon': 'π¨',
|
341 |
+
'text': 'Very poor accuracy. Reduce noise_multiplier to 0.8-1.2 and learning_rate to 0.01-0.02.'
|
342 |
+
})
|
343 |
+
elif metrics['accuracy'] < 60:
|
344 |
+
recommendations.append({
|
345 |
+
'icon': 'π',
|
346 |
+
'text': 'Low accuracy. Try: noise_multiplier=1.0, clipping_norm=1.0, learning_rate=0.02.'
|
347 |
+
})
|
348 |
+
elif metrics['accuracy'] > 85:
|
349 |
+
recommendations.append({
|
350 |
+
'icon': 'β
',
|
351 |
+
'text': 'Good accuracy! Privacy-utility tradeoff is well balanced.'
|
352 |
+
})
|
353 |
+
|
354 |
+
# Check batch size for DP-SGD
|
355 |
+
if params['batch_size'] < 32:
|
356 |
+
recommendations.append({
|
357 |
+
'icon': 'β‘',
|
358 |
+
'text': 'Small batch size with DP-SGD can lead to poor convergence. Try 64-128.'
|
359 |
+
})
|
360 |
+
elif params['batch_size'] > 512:
|
361 |
+
recommendations.append({
|
362 |
+
'icon': 'π',
|
363 |
+
'text': 'Large batch size may weaken privacy guarantees in DP-SGD.'
|
364 |
+
})
|
365 |
+
|
366 |
+
# Check learning rate with DP-SGD context
|
367 |
+
if params['learning_rate'] > 0.05:
|
368 |
+
recommendations.append({
|
369 |
+
'icon': 'β οΈ',
|
370 |
+
'text': 'High learning rate causes instability with DP noise. Try 0.01-0.02.'
|
371 |
+
})
|
372 |
+
elif params['learning_rate'] < 0.005:
|
373 |
+
recommendations.append({
|
374 |
+
'icon': 'π',
|
375 |
+
'text': 'Very low learning rate may slow convergence. Try 0.01-0.02.'
|
376 |
+
})
|
377 |
+
|
378 |
+
# Add specific recommendation for common failing case
|
379 |
+
if metrics['accuracy'] < 50 and params['noise_multiplier'] > 1.5:
|
380 |
+
recommendations.append({
|
381 |
+
'icon': 'π‘',
|
382 |
+
'text': 'Quick fix: Try noise_multiplier=1.0, clipping_norm=1.0, learning_rate=0.015, batch_size=128.'
|
383 |
+
})
|
384 |
+
|
385 |
+
return recommendations
|
386 |
+
|
387 |
+
def generate_gradient_norms(self, clipping_norm):
|
388 |
+
"""Generate realistic gradient norms for visualization."""
|
389 |
+
num_points = 100
|
390 |
+
gradients = []
|
391 |
+
|
392 |
+
# Generate log-normal distributed gradient norms
|
393 |
+
for _ in range(num_points):
|
394 |
+
# Most gradients are smaller than clipping norm, some exceed it
|
395 |
+
if np.random.random() < 0.7:
|
396 |
+
norm = np.random.gamma(2, clipping_norm / 3)
|
397 |
+
else:
|
398 |
+
norm = np.random.gamma(3, clipping_norm / 2)
|
399 |
+
|
400 |
+
# Create density for visualization
|
401 |
+
density = np.exp(-((norm - clipping_norm/2) ** 2) / (2 * (clipping_norm/3) ** 2))
|
402 |
+
density = 0.1 + 0.9 * density + 0.1 * np.random.random()
|
403 |
+
|
404 |
+
gradients.append({'x': float(norm), 'y': float(density)})
|
405 |
+
|
406 |
+
return sorted(gradients, key=lambda x: x['x'])
|
407 |
+
|
408 |
+
def generate_clipped_gradients(self, clipping_norm):
|
409 |
+
"""Generate clipped versions of the gradient norms."""
|
410 |
+
original_gradients = self.generate_gradient_norms(clipping_norm)
|
411 |
+
return [{'x': min(g['x'], clipping_norm), 'y': g['y']} for g in original_gradients]
|
requirements.txt
CHANGED
@@ -2,4 +2,7 @@ flask==3.0.0
|
|
2 |
flask-cors==4.0.0
|
3 |
python-dotenv==1.0.0
|
4 |
gunicorn==21.2.0
|
5 |
-
numpy==1.24.3
|
|
|
|
|
|
|
|
2 |
flask-cors==4.0.0
|
3 |
python-dotenv==1.0.0
|
4 |
gunicorn==21.2.0
|
5 |
+
numpy==1.24.3
|
6 |
+
tensorflow==2.13.1
|
7 |
+
tensorflow-privacy==0.8.11
|
8 |
+
scikit-learn==1.3.0
|
run.py
CHANGED
@@ -1,12 +1,23 @@
|
|
1 |
from app import create_app
|
2 |
import os
|
|
|
|
|
3 |
|
4 |
app = create_app()
|
5 |
|
6 |
if __name__ == '__main__':
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
# Enable debug mode for development
|
8 |
app.config['DEBUG'] = True
|
9 |
# Disable CORS in development
|
10 |
app.config['CORS_HEADERS'] = 'Content-Type'
|
|
|
|
|
|
|
11 |
# Run the application
|
12 |
-
app.run(host=
|
|
|
1 |
from app import create_app
|
2 |
import os
|
3 |
+
import sys
|
4 |
+
import argparse
|
5 |
|
6 |
app = create_app()
|
7 |
|
8 |
if __name__ == '__main__':
|
9 |
+
# Parse command line arguments
|
10 |
+
parser = argparse.ArgumentParser(description='Run DP-SGD Explorer')
|
11 |
+
parser.add_argument('--port', type=int, default=5000, help='Port to run the server on (default: 5000)')
|
12 |
+
parser.add_argument('--host', type=str, default='127.0.0.1', help='Host to run the server on (default: 127.0.0.1)')
|
13 |
+
args = parser.parse_args()
|
14 |
+
|
15 |
# Enable debug mode for development
|
16 |
app.config['DEBUG'] = True
|
17 |
# Disable CORS in development
|
18 |
app.config['CORS_HEADERS'] = 'Content-Type'
|
19 |
+
|
20 |
+
print(f"Starting server on http://{args.host}:{args.port}")
|
21 |
+
|
22 |
# Run the application
|
23 |
+
app.run(host=args.host, port=args.port, debug=True)
|
test_training.py
ADDED
@@ -0,0 +1,142 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#!/usr/bin/env python3
|
2 |
+
"""
|
3 |
+
Test script to verify MNIST training with DP-SGD works correctly.
|
4 |
+
Run this script to test the real trainer implementation.
|
5 |
+
"""
|
6 |
+
|
7 |
+
import sys
|
8 |
+
import os
|
9 |
+
sys.path.append('.')
|
10 |
+
|
11 |
+
def test_real_trainer():
|
12 |
+
"""Test the real trainer with MNIST dataset."""
|
13 |
+
print("Testing Real Trainer with MNIST Dataset")
|
14 |
+
print("=" * 50)
|
15 |
+
|
16 |
+
try:
|
17 |
+
try:
|
18 |
+
from app.training.simplified_real_trainer import SimplifiedRealTrainer as RealTrainer
|
19 |
+
print("β
Successfully imported SimplifiedRealTrainer")
|
20 |
+
except ImportError:
|
21 |
+
from app.training.real_trainer import RealTrainer
|
22 |
+
print("β
Successfully imported RealTrainer")
|
23 |
+
|
24 |
+
# Initialize trainer
|
25 |
+
trainer = RealTrainer()
|
26 |
+
print("β
Successfully initialized RealTrainer")
|
27 |
+
print(f"β
Training data shape: {trainer.x_train.shape}")
|
28 |
+
print(f"β
Test data shape: {trainer.x_test.shape}")
|
29 |
+
|
30 |
+
# Test with small parameters for quick execution
|
31 |
+
test_params = {
|
32 |
+
'clipping_norm': 1.0,
|
33 |
+
'noise_multiplier': 1.1,
|
34 |
+
'batch_size': 128,
|
35 |
+
'learning_rate': 0.01,
|
36 |
+
'epochs': 2 # Small number for testing
|
37 |
+
}
|
38 |
+
|
39 |
+
print(f"\nTraining with parameters: {test_params}")
|
40 |
+
results = trainer.train(test_params)
|
41 |
+
|
42 |
+
print(f"\nβ
Training completed successfully!")
|
43 |
+
print(f"Final accuracy: {results['final_metrics']['accuracy']:.2f}%")
|
44 |
+
print(f"Final loss: {results['final_metrics']['loss']:.4f}")
|
45 |
+
print(f"Training time: {results['final_metrics']['training_time']:.2f} seconds")
|
46 |
+
|
47 |
+
if 'privacy_budget' in results:
|
48 |
+
print(f"Privacy budget (Ξ΅): {results['privacy_budget']:.2f}")
|
49 |
+
|
50 |
+
print(f"Number of epochs recorded: {len(results['epochs_data'])}")
|
51 |
+
print(f"Number of recommendations: {len(results['recommendations'])}")
|
52 |
+
|
53 |
+
return True
|
54 |
+
|
55 |
+
except ImportError as e:
|
56 |
+
print(f"β Import Error: {e}")
|
57 |
+
print("Make sure TensorFlow and TensorFlow Privacy are installed:")
|
58 |
+
print("pip install tensorflow==2.15.0 tensorflow-privacy==0.9.0")
|
59 |
+
return False
|
60 |
+
|
61 |
+
except Exception as e:
|
62 |
+
print(f"β Training Error: {e}")
|
63 |
+
return False
|
64 |
+
|
65 |
+
def test_mock_trainer():
|
66 |
+
"""Test the mock trainer as fallback."""
|
67 |
+
print("\nTesting Mock Trainer (Fallback)")
|
68 |
+
print("=" * 50)
|
69 |
+
|
70 |
+
try:
|
71 |
+
from app.training.mock_trainer import MockTrainer
|
72 |
+
|
73 |
+
trainer = MockTrainer()
|
74 |
+
test_params = {
|
75 |
+
'clipping_norm': 1.0,
|
76 |
+
'noise_multiplier': 1.1,
|
77 |
+
'batch_size': 128,
|
78 |
+
'learning_rate': 0.01,
|
79 |
+
'epochs': 2
|
80 |
+
}
|
81 |
+
|
82 |
+
results = trainer.train(test_params)
|
83 |
+
|
84 |
+
print(f"β
Mock training completed!")
|
85 |
+
print(f"Final accuracy: {results['final_metrics']['accuracy']:.2f}%")
|
86 |
+
print(f"Final loss: {results['final_metrics']['loss']:.4f}")
|
87 |
+
print(f"Training time: {results['final_metrics']['training_time']:.2f} seconds")
|
88 |
+
|
89 |
+
return True
|
90 |
+
|
91 |
+
except Exception as e:
|
92 |
+
print(f"β Mock trainer error: {e}")
|
93 |
+
return False
|
94 |
+
|
95 |
+
def test_web_app():
|
96 |
+
"""Test that the web app routes work."""
|
97 |
+
print("\nTesting Web App Routes")
|
98 |
+
print("=" * 50)
|
99 |
+
|
100 |
+
try:
|
101 |
+
from app.routes import main
|
102 |
+
print("β
Successfully imported routes")
|
103 |
+
|
104 |
+
# Test trainer status
|
105 |
+
from app.routes import REAL_TRAINER_AVAILABLE, real_trainer
|
106 |
+
print(f"Real trainer available: {REAL_TRAINER_AVAILABLE}")
|
107 |
+
if REAL_TRAINER_AVAILABLE and real_trainer:
|
108 |
+
print("β
Real trainer is ready for use")
|
109 |
+
else:
|
110 |
+
print("β οΈ Will use mock trainer")
|
111 |
+
|
112 |
+
return True
|
113 |
+
|
114 |
+
except Exception as e:
|
115 |
+
print(f"β Web app test error: {e}")
|
116 |
+
return False
|
117 |
+
|
118 |
+
if __name__ == "__main__":
|
119 |
+
print("DPSGD Training System Test")
|
120 |
+
print("=" * 60)
|
121 |
+
|
122 |
+
# Test components
|
123 |
+
mock_success = test_mock_trainer()
|
124 |
+
real_success = test_real_trainer()
|
125 |
+
web_success = test_web_app()
|
126 |
+
|
127 |
+
print("\n" + "=" * 60)
|
128 |
+
print("TEST SUMMARY")
|
129 |
+
print("=" * 60)
|
130 |
+
print(f"Mock Trainer: {'β
PASS' if mock_success else 'β FAIL'}")
|
131 |
+
print(f"Real Trainer: {'β
PASS' if real_success else 'β FAIL'}")
|
132 |
+
print(f"Web App: {'β
PASS' if web_success else 'β FAIL'}")
|
133 |
+
|
134 |
+
if real_success:
|
135 |
+
print("\nπ All tests passed! The system will use real MNIST data.")
|
136 |
+
elif mock_success:
|
137 |
+
print("\nβ οΈ Real trainer failed, but mock trainer works. System will use synthetic data.")
|
138 |
+
else:
|
139 |
+
print("\nβ Critical errors found. Please check your setup.")
|
140 |
+
|
141 |
+
print("\nTo install missing dependencies, run:")
|
142 |
+
print("pip install -r requirements.txt")
|