-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrun_FFN.py
84 lines (75 loc) · 3.04 KB
/
run_FFN.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 json
import numpy as np
import pandas as pd
import tensorflow as tf
from src.utils import deco_print
from src.data.data_layer import DataInRamInputLayer
from src.model.model_IPCA_naive import ModelIPCA_Naive
from src.model.model_IPCA_FFN import ModelIPCA_FFN
tf.flags.DEFINE_string('config', '', 'Path to the file with configurations')
tf.flags.DEFINE_string('logdir', '', 'Path to save logs and checkpoints')
tf.flags.DEFINE_integer('nFactor', 1, 'Number of Factors')
tf.flags.DEFINE_boolean('isTrain', True, 'True if train, False if evaluate')
tf.flags.DEFINE_boolean('randomInitFactors', True, 'Initialize factors randomly')
FLAGS = tf.flags.FLAGS
def main(_):
with open(FLAGS.config, 'r') as file:
config = json.load(file)
deco_print('Read the following in config: ')
print(json.dumps(config, indent=4))
deco_print('Creating data layer')
dl_train = DataInRamInputLayer(config['individual_feature_file'])
dl_valid = DataInRamInputLayer(config['individual_feature_file_valid'])
dl_test = DataInRamInputLayer(config['individual_feature_file_test'])
deco_print('Data layer created')
model = ModelIPCA_FFN(individual_feature_dim=config['individual_feature_dim'],
tSize=config['tSize_train'],
hidden_dims=config['hidden_dims'],
nFactor=FLAGS.nFactor,
lr=config['lr'],
dropout=config['dropout'],
logdir=FLAGS.logdir,
dl=dl_train,
is_train=True)
gpu_options = tf.GPUOptions(allow_growth=True)
sess_config = tf.ConfigProto(gpu_options=gpu_options)
sess = tf.Session(config=sess_config)
if FLAGS.isTrain:
model.randomInitialization(sess)
if FLAGS.randomInitFactors:
initial_F = None
else:
model_naive = ModelIPCA_Naive(46, 'model/IPCA_naive')
model_naive.loadSavedModel(FLAGS.nFactor)
initial_F = model_naive.getFactors(dl_train)
loss_epoch_list = model.train(sess, initial_F=initial_F,
numEpoch=config['num_epoch'], maxIter=config['max_iter'], tol=config['tol'])
model_valid = ModelIPCA_FFN(individual_feature_dim=config['individual_feature_dim'],
tSize=config['tSize_valid'],
hidden_dims=config['hidden_dims'],
nFactor=FLAGS.nFactor,
lr=config['lr'],
dropout=config['dropout'],
logdir=FLAGS.logdir,
dl=dl_valid,
is_train=False,
force_var_reuse=True)
model_test = ModelIPCA_FFN(individual_feature_dim=config['individual_feature_dim'],
tSize=config['tSize_test'],
hidden_dims=config['hidden_dims'],
nFactor=FLAGS.nFactor,
lr=config['lr'],
dropout=config['dropout'],
logdir=FLAGS.logdir,
dl=dl_test,
is_train=False,
force_var_reuse=True)
model.loadSavedModel(sess)
w = model.getMarkowitzWeight(sess)
stats = pd.DataFrame(np.zeros((4,3)), columns=['train', 'valid', 'test'], index=['SR', 'UV', 'Alpha', 'Alpha_weighted'])
stats.loc[:,'train'] = model.calculateStatistics(sess, w)
stats.loc[:,'valid'] = model_valid.calculateStatistics(sess, w)
stats.loc[:,'test'] = model_test.calculateStatistics(sess, w)
print(stats)
if __name__ == '__main__':
tf.app.run()