-
Notifications
You must be signed in to change notification settings - Fork 2
/
train_utils.py
210 lines (180 loc) · 6.38 KB
/
train_utils.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
from dataclasses import dataclass
from enum import Enum
import os
import random
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import MinMaxScaler
import torch
from data import DATA, Sampling
from bns import SSLType, PruneMode
from data_prune import get_prune_data
class Dataset(str, Enum):
MNIST = "mnist"
CIFAR10 = "cifar10"
BREASTCANCER = "breast_cancer"
COD_RNA = "cod-rna"
@dataclass(slots=True)
class TensorSet:
X_train: torch.Tensor
X_test: torch.Tensor
y_train: torch.Tensor
y_test: torch.Tensor
@dataclass(slots=True)
class EasyPrunedSpace:
input_features: torch.Tensor
input_labels: torch.Tensor
@dataclass(slots=True)
class HardPrunedSpace:
input_features: torch.Tensor
input_labels: torch.Tensor
@dataclass(slots=True)
class PrunedSpace:
easy: EasyPrunedSpace
hard: HardPrunedSpace
def prepare_data(
dataset: str,
test_size: float = 0.2,
) -> TensorSet:
sample_size = 400
match dataset:
case Dataset.MNIST:
input_features, labels = DATA().mnist
input_features, labels = input_features[:4000], labels[:4000]
case Dataset.CIFAR10:
input_features, labels = DATA( # type: ignore
# sample=Sampling.RANDOM.value, sample_size=sample_size
).cifar_10
case _:
raise NotImplementedError(
"dataset: choose either mnist or cifar10",
)
data_size = len(labels)
test_split = int(np.floor(test_size * data_size))
train_split = int(data_size - test_split)
train_set, train_labels = input_features[:train_split], labels[:train_split]
test_set, test_labels = input_features[-test_split:], labels[-test_split:]
train_set = torch.div(train_set, 255)
test_set = torch.div(test_set, 255)
match dataset:
case Dataset.CIFAR10:
shuffle_list = list(np.arange(sample_size * 10))
random.shuffle(shuffle_list)
data_size = len(shuffle_list)
test_split = int(np.floor(test_size * data_size))
train_split = int(data_size - test_split)
train_s, test_s = shuffle_list[:train_split], shuffle_list[-test_split:]
train_set, train_labels = input_features[train_s], labels[train_s]
test_set, test_labels = input_features[test_s], labels[test_s]
train_set = torch.div(train_set, 255)
test_set = torch.div(test_set, 255)
return TensorSet(
X_train=train_set.float(),
X_test=test_set.float(),
y_train=train_labels,
y_test=test_labels,
)
def get_tensor_data(
dataset: str,
test_size: float = 0.3,
random_state: int = 4,
) -> TensorSet:
match dataset:
case Dataset.BREASTCANCER:
input_features, labels = DATA().breast_cancer
X_train, X_test, y_train, y_test = train_test_split(
np.array(input_features),
np.array(labels),
test_size=test_size,
random_state=random_state,
)
train_scaler, test_scaler = MinMaxScaler(), MinMaxScaler()
X_train = train_scaler.fit_transform(X_train)
X_test = test_scaler.fit_transform(X_test)
case Dataset.COD_RNA:
X_train, y_train, X_test, y_test = DATA().cod_rna
pass
case _:
raise NotImplementedError(
"get_tensor_data: choose breast_cancer",
)
x_input = torch.from_numpy(X_train).to(torch.float32)
y_label = torch.from_numpy(y_train).to(torch.float32)
x_input_test = torch.from_numpy(X_test).to(torch.float32)
y_label_test = torch.from_numpy(y_test).to(torch.long)
return TensorSet(
X_train=x_input,
X_test=x_input_test,
y_train=y_label,
y_test=y_label_test,
)
def get_data(
dataset: str,
test_size: float = 0.3,
random_state: int = 4,
) -> TensorSet:
tabular_set = [Dataset.BREASTCANCER.value, Dataset.COD_RNA.value]
condition1 = dataset in tabular_set
match condition1:
case True:
return get_tensor_data(
dataset=dataset,
test_size=test_size,
random_state=random_state,
)
case False:
return prepare_data(
dataset=dataset,
test_size=test_size,
)
def get_prunning(
dataset: str,
input_features: torch.Tensor,
input_labels: torch.Tensor,
ssl_type: str = SSLType.HCM,
prune_fraction: float = 0.2,
prune_mode: str = PruneMode.EASY,
feature_extraction: bool = True,
) -> EasyPrunedSpace | HardPrunedSpace | PrunedSpace:
with torch.no_grad():
# input_features = input_features.detach().cpu().numpy()
prune_indices = get_prune_data(
data_name=dataset,
dataset=input_features,
labels=input_labels,
ssl_type=ssl_type,
prune_fraction=prune_fraction,
feature_extraction=feature_extraction,
)
match prune_mode:
case PruneMode.EASY:
return EasyPrunedSpace(
input_features=torch.Tensor(input_features[prune_indices[0]]),
input_labels=torch.Tensor(input_labels[prune_indices[0]]),
)
case PruneMode.HARD:
return HardPrunedSpace(
input_features=torch.Tensor(input_features[prune_indices[1]]),
input_labels=torch.Tensor(input_labels[prune_indices[1]]),
)
case PruneMode.BOTH:
return PrunedSpace(
easy=EasyPrunedSpace(
input_features=torch.Tensor(input_features[prune_indices[0]]),
input_labels=torch.Tensor(input_labels[prune_indices[0]]),
),
hard=HardPrunedSpace(
input_features=torch.Tensor(input_features[prune_indices[1]]),
input_labels=torch.Tensor(input_labels[prune_indices[1]]),
),
)
case _:
raise NotImplementedError("PruneMode: select either hard,easy or both")
def seed_everything(seed: int):
random.seed(seed)
os.environ["PYTHONHASHSEED"] = str(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False