-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_Adult.py
84 lines (68 loc) · 3.19 KB
/
main_Adult.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
import os
import argparse
import torch
import torch.nn as nn
from tqdm import tqdm
import re
from datetime import datetime
from os.path import join as opj
from lib.Adult.Adult_dataset import Adult_loaders
from lib.utils import logger
from lib.fair_loss import MMD2_rq_u
from lib.Adult.net import DenseBlock
from lib.Adult.process_train import process_train
def parse_option():
parser = argparse.ArgumentParser(description='training encoders....', fromfile_prefix_chars='@')
parser.add_argument('--dataset', type=str,
# default='data/Adult/Adult_fair.npz')
default='/home/e/e0474114/2020/FairEmbedding_final/Adult-E1/data/Adult_fair.npz')
parser.add_argument('--out_dir', type=str,
# default='checkpoints/Adult')
default='/home/e/e0474114/2020/FairEmbedding_final/2021-fair-representation/checkpoints/Adult')
parser.add_argument('--device', type=str, default='cuda:2')
parser.add_argument('--batch_size', type=int, default=256,
help='batch size')
parser.add_argument('--epochs', type=int, default=110,
help='number of epochs')
parser.add_argument('--scheduler_steps', type=str, default='70,90',
help='scheduler step size')
parser.add_argument('--lr', type=float, default=1,
help='learning rate')
parser.add_argument('--gamma', type=float, default=0.1,
help='Learning rate step gamma')
parser.add_argument('--dim', type=int, default=16,
help='representation dimension')
parser.add_argument('--lmd', type=float, default=200)
parser.add_argument('--fair_loss_alpha', type=float, default=2)
parser.add_argument('--fair_loss_l', type=float, default=2)
parser.add_argument('--visdom_name', type=str, default='DenseNet')
parser.add_argument('--visdom_port', type=int, default=8099)
args= parser.parse_args()
return args
def main():
args = parse_option()
args.out_dir = os.path.join(args.out_dir)
args.scheduler_steps = [int(i) for i in args.scheduler_steps.split(',')]
loaders = Adult_loaders(args.dataset, args.batch_size, True)
# save dir
if not os.path.exists(args.out_dir):
os.mkdir(args.out_dir)
Logger = logger(args.visdom_name, os.path.join(args.out_dir, 'training_stats.pkl'), args.visdom_port)
fair_loss_func = MMD2_rq_u
Enc = DenseBlock(dim_in=166, dim_hidden=166*8, dim_out=args.dim, sigmoid=False).to(args.device)
Dec = DenseBlock(dim_in=args.dim+1, dim_hidden=166*8, dim_out=166, sigmoid=True).to(args.device)
process = process_train(Enc = Enc,
Dec = Dec,
pretext_loss = nn.MSELoss(),
fair_loss = fair_loss_func,
loaders = loaders,
logger = Logger,
args=args)
for epoch in range(1,args.epochs+1):
process.train()
process.eval()
process.scheduler_step()
Logger.reset_each_run(1)
torch.save(process.Enc.state_dict(), opj(args.out_dir,'encoder.pt'))
if __name__ == '__main__':
main()