-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathmain_mrnn.py
144 lines (121 loc) · 3.86 KB
/
main_mrnn.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
"""Main function for MRNN
Reference: Jinsung Yoon, William R. Zame and Mihaela van der Schaar,
"Estimating Missing Data in Temporal Data Streams Using
Multi-Directional Recurrent Neural Networks,"
in IEEE Transactions on Biomedical Engineering,
vol. 66, no. 5, pp. 1477-1490, May 2019.
Paper Link: https://ieeexplore.ieee.org/document/8485748
Contact: jsyoon0823@gmail.com
--------------------------------------------------
(1) Load the data
(2) Train MRNN model
(3) Impute missing data
(4) Evaluate the imputation performance
"""
# Necessary packages
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import argparse
import warnings
warnings.filterwarnings("ignore")
import numpy as np
import shutil
import os
from data_loader import data_loader
from mrnn import mrnn
from utils import imputation_performance
def main (args):
"""MRNN main function.
Args:
- file_name: dataset file name
- seq_len: sequence length of time-series data
- missing_rate: the rate of introduced missingness
- h_dim: hidden state dimensions
- batch_size: the number of samples in mini batch
- iteration: the number of iteration
- learning_rate: learning rate of model training
- metric_name: imputation performance metric (mse, mae, rmse)
Returns:
- output:
- x: original data with missing
- ori_x: original data without missing
- m: mask matrix
- t: time matrix
- imputed_x: imputed data
- performance: imputation performance
"""
## Load data
x, m, t, ori_x = data_loader(args.file_name,
args.seq_len,
args.missing_rate)
## Train M-RNN
# Remove 'tmp/mrnn_imputation' directory if exist
if os.path.exists('tmp/mrnn_imputation'):
shutil.rmtree('tmp/mrnn_imputation')
# mrnn model parameters
model_parameters = {'h_dim': args.h_dim,
'batch_size': args.batch_size,
'iteration': args.iteration,
'learning_rate': args.learning_rate}
# Fit mrnn_model
mrnn_model = mrnn(x, model_parameters)
mrnn_model.fit(x, m, t)
# Impute missing data
imputed_x = mrnn_model.transform(x, m, t)
# Evaluate the imputation performance
performance = imputation_performance (ori_x, imputed_x, m, args.metric_name)
# Report the result
print(args.metric_name + ': ' + str(np.round(performance, 4)))
# Return the output
output = {'x': x, 'ori_x': ori_x, 'm': m, 't': t, 'imputed_x': imputed_x,
'performance': performance}
if os.path.exists('tmp/mrnn_imputation'):
shutil.rmtree('tmp/mrnn_imputation')
return output
##
if __name__ == '__main__':
# Inputs for the main function
parser = argparse.ArgumentParser()
parser.add_argument(
'--file_name',
default='data/google.csv',
type=str)
parser.add_argument(
'--seq_len',
help='sequence length of time-series data',
default=7,
type=int)
parser.add_argument(
'--missing_rate',
help='the rate of introduced missingness',
default=0.2,
type=float)
parser.add_argument(
'--h_dim',
help='hidden state dimensions',
default=10,
type=int)
parser.add_argument(
'--batch_size',
help='the number of samples in mini batch',
default=128,
type=int)
parser.add_argument(
'--iteration',
help='the number of iteration',
default=2000,
type=int)
parser.add_argument(
'--learning_rate',
help='learning rate of model training',
default=0.01,
type=float)
parser.add_argument(
'--metric_name',
help='imputation performance metric',
default='rmse',
type=str)
args = parser.parse_args()
# Call main function
output = main(args)