-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.py
78 lines (55 loc) · 2.34 KB
/
run.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
import torch
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from time import strftime
import networks.zeoliteDNN as dnn # other networks to import, only import DNNs here
from parser import parse_args, write_config_log
from predict import predict
from train import train, train_valid
from utils.data import read_chan_data, read_grid_data, load_data
def run_train(net, time_stamp, args):
"""
Train a neural network.
Args:
net: initialized neural network
time_stamp: time stamp for documentation purposes
args: training arguments
Returns:
net: trained neural network
"""
if args.validate.lower() in ['true', 't', 'yes', 'y']:
train_data, valid_data = load_data(args)
net = train_valid(net, train_data=train_data, valid_data=valid_data, time_stamp=time_stamp, args=args)
elif args.validate.lower() in ['false', 'f', 'no', 'n']:
train_data = load_data(args)
net = train(net, train_data=train_data, time_stamp=time_stamp, args=args)
else:
raise ValueError('Unrecognized validation argument -- options: [True/False, T/F, Y/N, Yes/No]')
return net
def run_predict(net, time_stamp, args):
"""
Make predictions using a neural network.
Args:
net: trained neural network
time_stamp: time stamp for documentation purposes
args: training arguments
"""
# Currently set to use validation data to avoid fitting to test data.
# Should only touch test data as the very last step for model evaluation i.e. don't validate model on test data.
valid_data = load_data(args)
predict(net, data=valid_data, time_stamp=time_stamp, args=args)
def run():
"""Run NN operation."""
time_stamp = strftime('-%Y_%m_%d-%H_%M_%S') # time stamp for documentation
# Perhaps there would be a better way of passing an arg for what network to use but I had too many
# networks I was testing to do something in the likes of `build_optimizer` or `build_loss`
args = parse_args()
if args.mode.lower() == 'train':
net = dnn.ThreeLayerNet_V4()
run_train(net, time_stamp, args)
if args.mode.lower() == 'predict':
net = dnn.FourLayerNet_V3()
net.load_state_dict(torch.load(args.net_path))
run_predict(net, time_stamp, args)
write_config_log(net, time_stamp, args)