-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathvisualisation.py
More file actions
57 lines (47 loc) · 1.7 KB
/
Copy pathvisualisation.py
File metadata and controls
57 lines (47 loc) · 1.7 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# -*- coding: utf-8 -*-
"""
@author: Yassine BEZZA <[email protected]>
@brief:
"""
import numpy as np
from pydd.solver import GenericSolver
from pydd.models import MLP
from pydd.connectors import ArrayConnector
from sklearn import datasets, metrics, model_selection, preprocessing
import subprocess
import sys
# Redirect stdout to a file
orig_stdout = sys.stdout
log_file = 'logs.txt'
f = open(log_file, 'w')
sys.stdout = f
# Parameters
seed = 1337
np.random.seed(seed) # for reproducibility
n_classes = 10
params = {"port": 8080, "nclasses": n_classes, "gpu": True}
split_params = {"test_size": 0.2, "random_state": seed}
# create dataset
X, y = datasets.load_digits(n_class=n_classes, return_X_y=True)
X = preprocessing.StandardScaler().fit_transform(X)
xtr, xte, ytr, yte = model_selection.train_test_split(X, y, **split_params)
# Define models and class weights
clf = MLP(**params)
solver = GenericSolver(iterations=5000, solver_type="SGD", base_lr=0.01, gamma=0.1, stepsize=30, momentum=0.9)
# one class weight value for each class
class_weights = [1., 1., 1., 1., 1., 1., 1., 1., 1., 1]
train_data, test_data = ArrayConnector(xtr, ytr), ArrayConnector(xte, yte)
# Start visdom on another process
#start_visdom = ["python'", "-m", "visdom.server"]
#subprocess.Popen(["python", "-m", "visdom.server"])
# Listen visdom on logs file
#listen_logs = ["pydd", "--log_dir", log_file]
#subprocess.Popen(listen_logs)
# Start listening to the port
logs = clf.fit(train_data, validation_data=[test_data], solver=solver, class_weights=class_weights, batch_size=128)
yte_pred = clf.predict(test_data)
report = metrics.classification_report(yte, yte_pred)
print(report)
# Close the output file
sys.stdout = orig_stdout
f.close()