-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbinary-classification_torchModel.py
76 lines (59 loc) · 2.94 KB
/
binary-classification_torchModel.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
"""
Author : Abdullah Al Masud\n
email : [email protected]\n
LICENSE : MIT License
"""
# torchModel() binary classification example
import pandas as pd
from sklearn.datasets import load_breast_cancer
import torch
import os
import sys
project_dir = os.getcwd()
sys.path.append(project_dir)
from msdlib import msd
from msdlib import mlutils
savepath = 'examples/binary-classification_torchModel'
# Loading the data and separating data and label
source_data = load_breast_cancer()
feature_names = source_data['feature_names'].copy()
data = pd.DataFrame(source_data['data'], columns=feature_names)
label = pd.Series(source_data['target'], name=source_data['target_names'][1])
# print(source_data['DESCR'])
print('data:', data.head())
print('label', label)
print('classes:', label.unique())
# Standardizing numerical data
data = msd.standardize(data)
# Splitting data set into train, validation and test
splitter = msd.SplitDataset(data, label, test_ratio=.1)
outdata = splitter.random_split(val_ratio=.1)
print('outdata keys:', outdata.keys())
print('outdata["train"].keys() :', outdata['train'].keys())
print("outdata['validation'].keys() :", outdata['validation'].keys())
print("outdata['test'].keys() :", outdata['test'].keys())
print('train > data, label and index shapes',
outdata['train']['data'].shape, outdata['train']['label'].shape, outdata['train']['index'].shape)
print('validation > data, label and index shapes',
outdata['validation']['data'].shape, outdata['validation']['label'].shape, outdata['validation']['index'].shape)
print('test > data, label and index shapes',
outdata['test']['data'].shape, outdata['test']['label'].shape, outdata['test']['index'].shape)
# defining layers inside a list
layers = mlutils.define_layers(data.shape[1], 1, [100, 100, 100, 100, 100, 100], dropout_rate=.2, model_type='binary-classifier',
actual_units=True, activation=torch.nn.ReLU(), final_activation=torch.nn.Sigmoid())
# building model
tmodel = mlutils.torchModel(layers=layers, model_type='binary-classifier', tensorboard_path='runs',
savepath=savepath, batch_size=32, epoch=80, learning_rate=.0001, lr_reduce=.995)
# Training Pytorch model
tmodel.fit(outdata['train']['data'], outdata['train']['label'])
# Evaluating the model's performance
result, all_results = tmodel.evaluate(data_sets=[outdata['train']['data'], outdata['test']['data']],
label_sets=[
outdata['train']['label'], outdata['test']['label']],
set_names=['Train', 'Test'], savepath=savepath)
print('classification score:\n', result)
# scores for classification
print('test scores:\n', all_results['Test'][0])
# confusion matrix for classification
print('test data confusion matrix:\n', all_results['Test'][1].T)
assert all_results['Test'][0]['average'].loc['f1_score'] >= .95, 'test set f1-score is less than .95'