-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTrain_SuperPNN_semi.py
147 lines (116 loc) · 4.09 KB
/
Train_SuperPNN_semi.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
#!/usr/bin/env python
#SBATCH --job-name=SpNNSemi
#SBATCH --error=%x.%j.err
#SBATCH --output=%x.%j.out
#SBATCH --mail-type=END,FAIL
#SBATCH [email protected]
#SBATCH --export=ALL
#SBATCH --time=48:00:00
#SBATCH --partition=sdil
#SBATCH --gres=gpu:1
# Import library
import importlib
import torch
import pickle
import os
import sys
sys.path.append('/pfs/data5/home/kit/tm/px3192/Split_Manufacturing_One_Mask/')
import matplotlib.pyplot as plt
import numpy as np
import pNN_Split as pnn
import random
import config
import evaluation as E
import training
from torch.utils.data import TensorDataset
from torch.utils.data import DataLoader
seed = int(sys.argv[1])
# create file
topology_name = ''
for t in config.semi_topology:
topology_name += str(t)
whole_file_path = f'./result/super pNN semi {topology_name}'
if not os.path.exists(whole_file_path):
os.mkdir(whole_file_path)
if not os.path.exists(whole_file_path + '/model'):
os.mkdir(whole_file_path + '/model')
# Device
device = config.device
# device = torch.device('cuda:0')
# device = 'cpu'
# Prepare data
## Datasets
datasets = os.listdir('./Datasets/datasets/')
datasets = [d for d in datasets if d.endswith('.p')]
datasets.sort()
## Load data
names = []
num_in = []
num_out = []
X_trains = []
y_trains = []
X_valids = []
y_valids = []
X_tests = []
y_tests = []
for dataset in datasets:
datapath = os.path.join('./Datasets/datasets/' + dataset)
with open(datapath, 'rb') as f:
data = pickle.load(f)
X_train = data['X_train']
y_train = data['y_train']
X_valid = data['X_valid']
y_valid = data['y_valid']
data_name = data['name']
N_class = data['n_class']
N_feature = data['n_feature']
N_train = X_train.shape[0]
N_valid = X_valid.shape[0]
print('Loading', data_name, N_feature, N_class, N_train, N_valid)
names.append(data_name)
num_in.append(N_feature)
num_out.append(N_class)
X_trains.append(X_train.to(device))
y_trains.append(y_train.to(device))
X_valids.append(X_valid.to(device))
y_valids.append(y_valid.to(device))
print('Finish data loading.')
# load normalization factors
acc_reference = np.loadtxt(f'./result/seperate pNN semi {topology_name}/acc_factor.txt').flatten()
train_reference = np.loadtxt(f'./result/seperate pNN semi {topology_name}/train_factor.txt').flatten()
valid_reference = np.loadtxt(f'./result/seperate pNN semi {topology_name}/valid_factor.txt').flatten()
if not config.normalization:
train_reference = np.ones(len(datasets))
valid_reference = np.ones(len(datasets))
acc_factor = 1 / acc_reference
train_factor = 1 / (train_reference+0.01)
valid_factor = 1 / (valid_reference+0.01)
acc_factor = (acc_factor / acc_factor.shape[0]).tolist()
train_factor = (train_factor / np.sum(train_factor)).tolist()
valid_factor = (valid_factor / np.sum(valid_factor)).tolist()
# hyper parameter for penalty
alphas = np.zeros([102])
alphas[1:-1] = np.logspace(np.log(1e-5), np.log(1e5), 100, base=np.e)
alphas[-1] = 1e6
alphas = np.round(alphas, 5)
for alpha in alphas:
# run over seeds
# for seed in range(10):
if os.path.isfile(f'{whole_file_path}/model/spnn_{alpha}_{seed}'):
print(f'Super pNN {alpha} {seed} exists, skip this training.')
else:
print(f'Training with penalty:{alpha} seed:{seed}.')
config.SetSeed(seed)
# SuperPNN
## Define
SuperPNN = pnn.SuperSemiPNN(num_in, config.semi_topology)
SuperPNN.to(device)
optimizer = torch.optim.Adam(SuperPNN.parameters(), lr=config.slr)
## Training
SuperPNN, _, _, _, _ = training.train_spnn(SuperPNN,
X_trains, y_trains,
X_valids, y_valids,
optimizer, pnn.LOSSFUNCTION,
train_factor, valid_factor, acc_factor,
alpha, UUID=f'semi_{alpha}_{seed}')
torch.save(SuperPNN, f'{whole_file_path}/model/spnn_{alpha}_{seed}')