Skip to content

Commit 9cac0a7

Browse files
committed
small fixes
1 parent e955ba7 commit 9cac0a7

File tree

4 files changed

+66
-61
lines changed

4 files changed

+66
-61
lines changed

.DS_Store

0 Bytes
Binary file not shown.

requirements.txt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
1-
tensorflow>=1.0.0
2-
keras>=2.1.2
1+
numpy>=1.15.3
2+
sklearn>=0.20.0
3+
cPickle>=1.71
4+
tensorflow==1.0.0
5+
keras==2.1.2

src/multivariate_example.py

Lines changed: 45 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,40 @@
1-
import pickle
2-
import random
3-
import keras
41
import argparse
52
import numpy as np
63
import tensorflow as tf
7-
84
from sklearn.model_selection import StratifiedKFold
95
from sklearn.metrics import average_precision_score as auprc
106
from sklearn.metrics import roc_auc_score as auc_score
7+
import keras
118
from keras.utils import multi_gpu_model
12-
from keras.layers import Input, Dense, Masking, GRU, Dropout, Lambda, Permute
13-
from keras.models import load_model, Model, Sequential
9+
from keras.layers import Input, Dense, GRU, Lambda, Permute
10+
from keras.models import Model
1411
from interpolation_layer import single_channel_interp, cross_channel_interp
1512

1613
np.random.seed(10)
1714
tf.set_random_seed(10)
1815

1916
# Loading dataset
20-
"""
21-
y : (N,) discrete for classification, real values for regression
22-
x : (N, D, tn) input multivariate time series data with dimension
23-
where N is number of data cases, D is the dimension of
24-
sparse and irregularly sampled time series and tn is the union
25-
of observed time stamps in all the dimension for a data case n.
26-
Since each tn is of variable length, we pad them with zeros to
27-
have an array representation.
28-
m : (N, D, tn) where m[i,j,k] = 0 means that x[i,j,k] is not observed.
29-
T : (N, D, tn) represents the actual time stamps of observation;
30-
"""
31-
32-
"""To implement the autoencoder component of the loss, we introduce a set
33-
of masking variables mr (and mr1) for each data point. If drop_mask = 0, then we remove
34-
the data point as an input to the interpolation network, and include
35-
the predicted value at this time point when assessing
36-
the autoencoder loss. In practice, we randomly select 20% of the
17+
# y : (N,) discrete for classification, real values for regression
18+
# x : (N, D, tn) input multivariate time series data with dimension
19+
# where N is number of data cases, D is the dimension of
20+
# sparse and irregularly sampled time series and tn is the union
21+
# of observed time stamps in all the dimension for a data case n.
22+
# Since each tn is of variable length, we pad them with zeros to
23+
# have an array representation.
24+
# m : (N, D, tn) where m[i,j,k] = 0 means that x[i,j,k] is not observed.
25+
# T : (N, D, tn) represents the actual time stamps of observation;
26+
27+
28+
"""To implement the autoencoder component of the loss, we introduce a set
29+
of masking variables mr (and mr1) for each data point. If drop_mask = 0,
30+
then we removecthe data point as an input to the interpolation network,
31+
and includecthe predicted value at this time point when assessing
32+
the autoencoder loss. In practice, we randomly select 20% of the
3733
observed data points to hold out from
3834
every input time series."""
3935

4036

41-
def drop_mask(mask, perc=0.2):
37+
def hold_out(mask, perc=0.2):
4238
drop_mask = np.ones_like(mask)
4339
drop_mask *= mask
4440
for i in range(mask.shape[0]):
@@ -56,8 +52,8 @@ def drop_mask(mask, perc=0.2):
5652
return drop_mask
5753

5854

59-
x = np.concatenate((x, m, T, drop_mask(m)), axis=1) # input format
60-
print(x.shape, y.shape)
55+
x = np.concatenate((x, m, T, hold_out(m)), axis=1) # input format
56+
print x.shape, y.shape
6157

6258
ap = argparse.ArgumentParser()
6359
ap.add_argument("-g", "--gpus", type=int, default=4,
@@ -75,7 +71,7 @@ def drop_mask(mask, perc=0.2):
7571

7672
args = vars(ap.parse_args())
7773
gpu_num = args["gpus"]
78-
iter = args["epochs"]
74+
epoch = args["epochs"]
7975
hid = args["hidden_units"]
8076
timestamp = x.shape[2]
8177
num_features = x.shape[1]/4
@@ -86,8 +82,8 @@ def drop_mask(mask, perc=0.2):
8682
else:
8783
batch = args["batch_size"]
8884

89-
# Autoencoder loss
90-
85+
""" Autoencoder loss
86+
"""
9187

9288
def customloss(ytrue, ypred):
9389
# standard deviation of each feature mentioned in paper for MIMIC_III data
@@ -141,7 +137,7 @@ def interp_net():
141137
model = multi_gpu_model(orig_model, gpus=gpu_num)
142138
else:
143139
model = orig_model
144-
print(orig_model.summary())
140+
print orig_model.summary()
145141
return model
146142

147143

@@ -154,19 +150,30 @@ def interp_net():
154150
i = 0
155151
kfold = StratifiedKFold(n_splits=5, shuffle=True, random_state=seed)
156152
for train, test in kfold.split(np.zeros(len(y)), y):
157-
print("Running Fold:", i+1)
153+
print "Running Fold:", i+1
158154
model = interp_net() # re-initializing every time
159-
model.compile(optimizer='adam', loss={'main_output': 'binary_crossentropy', 'aux_output': customloss},
160-
loss_weights={'main_output': 1., 'aux_output': 1.}, metrics={'main_output': 'accuracy'})
161-
model.fit({'input': x[train]}, {'main_output': y[train], 'aux_output': x[train]},
162-
batch_size=batch, callbacks=callbacks_list, nb_epoch=iter, validation_split=0.20, verbose=2)
155+
model.compile(
156+
optimizer='adam',
157+
loss={'main_output': 'binary_crossentropy', 'aux_output': customloss},
158+
loss_weights={'main_output': 1., 'aux_output': 1.},
159+
metrics={'main_output': 'accuracy'})
160+
model.fit(
161+
{'input': x[train]}, {'main_output': y[train], 'aux_output': x[train]},
162+
batch_size=batch,
163+
callbacks=callbacks_list,
164+
nb_epoch=epoch,
165+
validation_split=0.20,
166+
verbose=2)
163167
y_pred = model.predict(x[test], batch_size=batch)
164168
y_pred = y_pred[0]
165-
total_loss, score, reconst_loss, acc = model.evaluate(
166-
{'input': x[test]}, {'main_output': y[test], 'aux_output': x[test]}, batch_size=batch, verbose=0)
169+
total_loss, score, reconst_loss, acc = model.evaluate(
170+
{'input': x[test]},
171+
{'main_output': y[test], 'aux_output': x[test]},
172+
batch_size=batch,
173+
verbose=0)
167174
results['loss'].append(score)
168175
results['acc'].append(acc)
169176
results['auc'].append(auc_score(y[test], y_pred))
170177
results['auprc'].append(auprc(y[test], y_pred))
171-
print(results)
178+
print results
172179
i += 1

src/univariate_example.py

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
import argparse
2-
import numpy as np
3-
import pandas as pd
42
import cPickle as pickle
5-
import matplotlib.pyplot as plt
6-
import keras
3+
import numpy as np
74
import tensorflow as tf
8-
9-
from keras.models import load_model, Model, Sequential
10-
from keras.layers import Input, Dense, Embedding, Masking, GRU, Dropout, Lambda, Permute
5+
import keras
6+
from keras.models import Model
7+
from keras.layers import Input, Dense, GRU, Lambda, Permute
118
from sklearn.preprocessing import MultiLabelBinarizer
12-
from interpolation_layer import single_channel_interp, cross_channel_interp
9+
from interpolation_layer import single_channel_interp
1310

1411
ap = argparse.ArgumentParser()
1512
ap.add_argument("-batch", "--batch_size", type=int, default=256,
@@ -22,7 +19,7 @@
2219
default=128, help="# of refpoints")
2320
args = vars(ap.parse_args())
2421
batch = args["batch_size"]
25-
iter = args["epochs"]
22+
epoch = args["epochs"]
2623
hid = args["hidden_units"]
2724
ref_points = args["ref_points"]
2825
hours_look_ahead = 100 # same as the input time stamps range
@@ -31,8 +28,7 @@
3128

3229

3330
# Loading Dataset
34-
file = 'Dataset/UWaveGestureLibraryAll-10.pkl'
35-
with open(file, 'rb') as f:
31+
with open('Dataset/UWaveGestureLibraryAll-10.pkl', 'rb') as f:
3632
x_train, y_train, x_test, y_test, l_train, l_test = pickle.load(f)
3733

3834
x_train = np.array(x_train)
@@ -50,13 +46,13 @@
5046
print(x_train.shape, y_train.shape, x_test.shape,
5147
y_test.shape, l_train.shape, l_test.shape)
5248

53-
"""To implement the autoencoder component of the loss, we introduce a set
54-
of masking variables mr (and mr1) for each data point. If mr = 0, then we remove
55-
the data point as an input to the interpolation network, and include
56-
the predicted value at this time point when assessing
57-
the autoencoder loss. In practice, we randomly select 20% of the
58-
observed data points to hold out from
59-
every input time series."""
49+
# To implement the autoencoder component of the loss, we introduce a set
50+
# of masking variables mr (and mr1) for each data point. If mr = 0, then we remove
51+
# the data point as an input to the interpolation network, and include
52+
# the predicted value at this time point when assessing
53+
# the autoencoder loss. In practice, we randomly select 20% of the
54+
# observed data points to hold out from
55+
# every input time series.
6056

6157
m = np.ones_like(x_train) # for one dimensional time series m is all ones
6258
mr = np.ones_like(x_train)
@@ -80,10 +76,9 @@ def customloss(ytrue, ypred):
8076
wc = 1
8177
y = ytrue[:, :num_features, :]
8278
m2 = ytrue[:, 3*num_features:4*num_features, :]
83-
m = 1 - m2
8479
ypred = ypred[:, :num_features, :]
8580
x = (y - ypred)*(y - ypred)
86-
x = x*m
81+
x = x * (1 - m2)
8782
count = tf.reduce_sum(m, axis=2)
8883
count = tf.where(count > 0, count, tf.ones_like(count))
8984
x = tf.reduce_sum(x, axis=2)/count
@@ -112,5 +107,5 @@ def customloss(ytrue, ypred):
112107

113108
print('Train...')
114109
model.fit({'input': x_test}, {'main_output': l_test, 'aux_output': x_test}, batch_size=batch,
115-
callbacks=callbacks_list, epochs=iter, validation_split=0.3, shuffle=True)
110+
callbacks=callbacks_list, epochs=epoch, validation_split=0.3, shuffle=True)
116111
print(model.evaluate(x_train, [l_train, x_train], batch_size=batch))

0 commit comments

Comments
 (0)