-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathmain.py
67 lines (59 loc) · 2.29 KB
/
main.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
import argparse
import numpy as np
from maml import MAML
def argsparser():
parser = argparse.ArgumentParser("Tensorflow Implementation of MAML")
parser.add_argument('--seed', type=int, default=1)
# Dataset
parser.add_argument('--dataset', help='environment ID', choices=['sin'],
required=True)
# MAML
parser.add_argument('--K', type=int, default=10)
parser.add_argument('--model_type', type=str, default='fc')
parser.add_argument('--loss_type', type=str, default='MSE')
parser.add_argument('--num_updates', type=int, default=3)
parser.add_argument('--norm', choices=['None', 'batch_norm'], default='batch_norm')
# Train
parser.add_argument('--is_train', action='store_true', default=False)
parser.add_argument('--max_steps', type=int, default=7e4)
parser.add_argument('--alpha', type=float, default=1e-3)
parser.add_argument('--beta', type=float, default=1e-3)
parser.add_argument('--batch_size', type=int, default=25)
# Test
parser.add_argument('--restore_checkpoint', type=str)
parser.add_argument('--restore_dir', type=str)
parser.add_argument('--test_sample', type=int, default=100)
parser.add_argument('--draw', action='store_true', default=False)
args = parser.parse_args()
return args
def get_dataset(dataset_name, K_shots):
if dataset_name == 'sin':
from dataset.SinDataGenerator import dataset
else:
ValueError("Invalid dataset")
return dataset(K_shots)
def main(args):
np.random.seed(args.seed)
dataset = get_dataset(args.dataset, args.K)
model = MAML(dataset,
args.model_type,
args.loss_type,
dataset.dim_input,
dataset.dim_output,
args.alpha,
args.beta,
args.K,
args.batch_size,
args.is_train,
args.num_updates,
args.norm
)
if args.is_train:
model.learn(args.batch_size, dataset, args.max_steps)
else:
model.evaluate(dataset, args.test_sample, args.draw,
restore_checkpoint=args.restore_checkpoint,
restore_dir=args.restore_dir)
if __name__ == '__main__':
args = argsparser()
main(args)