-
Notifications
You must be signed in to change notification settings - Fork 1
/
TrainIndividGPFlow.py
212 lines (176 loc) · 9.11 KB
/
TrainIndividGPFlow.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import sys
import csv
import numpy as np
import gpflow
import os
import pandas as pd
import h5py
from sklearn.model_selection import train_test_split
import tensorflow as tf
from scipy.cluster.vq import kmeans
tf.set_random_seed(1234)
import pickle
import argparse
#Model to train the individual Policy and Value Function models for Penalty Shot
def train_model(**kwargs):
subID = kwargs['subID']
npseed = kwargs['npseed']
iters = kwargs['iterations']
gpu = kwargs['gpu']
numInducingPoints = kwargs['IP']
whichModel = kwargs['whichModel']
print("subID: " + str(subID))
print("npseed: " + str(npseed))
print("iterations: " + str(iters))
print("gpu: " + str(gpu))
print("IPs: " + str(numInducingPoints))
print("Model Requested: " + str(whichModel))
os.environ["CUDA_DEVICE_ORDER"]="PCI_BUS_ID"
os.environ["CUDA_VISIBLE_DEVICES"]=str(gpu)
print("Loading Data for Subject " + str(subID) + "....")
data = h5py.File('penaltykickdata.h5','r')
subID1HE = np.array(data.get('subID')).astype('float32')
otherdata = np.array(data.get('otherfeatures')).astype('float32')
switchBool = np.array(data.get('targets')).astype('float32') #did they switch at time t+1
trialidx = np.array(data.get('trialidx')).astype('float32')
time = np.array(data.get('time')).astype('int32')
if whichModel == 'PSwitch':
targets = np.array(data.get('targets')).astype('float32')
elif whichModel == 'ExtraEV':
targets = np.array(data.get('EVtargets').value).astype('int32')
otherdata = np.hstack((otherdata, switchBool))
Xfeatures_totaldata = np.hstack((otherdata, subID1HE))
Xfeatures_totaldata = pd.DataFrame(Xfeatures_totaldata)
offset = otherdata.shape[1]
subdata = Xfeatures_totaldata[Xfeatures_totaldata[offset+subID]==1]
subtargets = pd.DataFrame(targets).iloc[subdata.index]
X = pd.DataFrame(otherdata).iloc[subdata.index]
#Make within-opponent experience percentage variable, but only for the PSwitch model (not ExtraEV) because this is confounded
if whichModel == 'PSwitch':
X["superindex"] = pd.DataFrame(trialidx).iloc[subdata.index]
progressvar = []
humantrialindex = X[X[5]==1]['superindex'].unique()
cputrialindex = X[X[5]==0]['superindex'].unique()
numHuman = len(humantrialindex)
numCPU = len(cputrialindex)
for _, row in X.iterrows():
if row[5] == 1: #if human observation
progressvar.append((np.where(humantrialindex == row['superindex'])[0][0] / numHuman))
elif row[5] == 0: #if cpu observation
progressvar.append((np.where(cputrialindex == row['superindex'])[0][0] / numCPU))
X['progressvar'] = progressvar
del X['superindex']
X_train, X_test = train_test_split(X, test_size=0.2, random_state=1)
y_train, y_test = train_test_split(subtargets, test_size=0.2, random_state=1)
optimizer = 'Adam'
mb = 256
np.random.seed(npseed)
Ms = numInducingPoints
X = np.array(X_train, dtype=float)
Y = np.array(y_train, dtype=float)
Z = kmeans(X_train, Ms, iter=1)[0]
Z = np.array(Z, dtype=float)
dimsize = X.shape[1]
kernel = gpflow.kernels.RBF(input_dim=dimsize, ARD=True)
m = gpflow.models.SVGP(
X,Y, kern=kernel,
likelihood=gpflow.likelihoods.Bernoulli(), Z=Z, minibatch_size=mb)
m.feature.set_trainable(True)
global_step = tf.get_variable("global_step", (), tf.int32, tf.zeros_initializer(), trainable=False)
learning_rate = 0.001 #adam default
if whichModel == 'PSwitch':
experstring = 'fulldatatrim_sub' + str(subID) + '_iters' + str(iters) + '_inducingpts' + str(numInducingPoints) + '_' + "_npseed" + str(npseed)
fw = tf.summary.FileWriter("finalsubjtrainindivid_logs/{}".format(experstring), m.graph)
elif whichModel == 'ExtraEV':
experstring = 'ExtraEV_sub' + str(subID) + '_iters' + str(iters) + '_inducingpts' + str(numInducingPoints) + '_' + "_npseed" + str(npseed)
fw = tf.summary.FileWriter("ExtraEVfinalsubjtrainindivid_logs/{}".format(experstring), m.graph)
#define summary scalars for examination in tensorboard
tf.summary.scalar("likelihood", m._build_likelihood())
tf.summary.scalar("ELBO", m.likelihood_tensor)
tf.summary.scalar("lengthscales_goalieposy", tf.gather(m.kern.lengthscales._constrained_tensor, 0))
tf.summary.scalar("lengthscales_shooterposx", tf.gather(m.kern.lengthscales._constrained_tensor, 1))
tf.summary.scalar("lengthscales_shooterposy", tf.gather(m.kern.lengthscales._constrained_tensor, 2))
tf.summary.scalar("lengthscales_goalievely", tf.gather(m.kern.lengthscales._constrained_tensor, 3))
tf.summary.scalar("lengthscales_shootervely", tf.gather(m.kern.lengthscales._constrained_tensor, 4))
tf.summary.scalar("lengthscales_opp", tf.gather(m.kern.lengthscales._constrained_tensor, 5))
tf.summary.scalar("lengthscales_timesincelastchange", tf.gather(m.kern.lengthscales._constrained_tensor, 6))
if whichModel == 'PSwitch':
tf.summary.scalar("lengthscales_oppexperience", tf.gather(m.kern.lengthscales._constrained_tensor, 7))
elif whichModel == 'ExtraEV':
tf.summary.scalar("lengthscales_currtswitch", tf.gather(m.kern.lengthscales._constrained_tensor,7))
mysum = tf.summary.merge_all()
def loss_callback(summary):
fw.add_summary(summary, loss_callback.iter)
loss_callback.iter += 1
loss_callback.iter=0
print("Training Model...")
gpflow.train.AdamOptimizer(learning_rate).minimize(m, maxiter=iters, var_list=[global_step], global_step=global_step, summary_op=mysum, file_writer=fw)
#save model
param_dict = {p[0].full_name.replace('SGPR', 'SGPU'): p[1] for p in zip(m.trainable_parameters, m.read_trainables())}
if whichModel == 'PSwitch':
with open('finalindividsubjGPs/pswitchmodel_fulltrimdata_'+str(numInducingPoints)+'IP_sub'+str(subID)+'_np'+str(npseed)+ '_iters' + str(iters) + '.pickle', 'wb') as handle:
pickle.dump(param_dict, handle, protocol=pickle.HIGHEST_PROTOCOL)
m.as_pandas_table().to_pickle('finalindividsubjGPs/pswitchmodelparams_fulltrimdata_'+str(numInducingPoints)+'IP_sub'+str(subID)+'_np'+str(npseed) + '_iters'+str(iters))
elif whichModel == 'ExtraEV':
with open('ExtraEVfinalindividsubjGPs/fulltrimdata_'+str(numInducingPoints)+'IP_sub'+str(subID)+'_np'+str(npseed)+ '_iters' + str(iters) + '.pickle', 'wb') as handle:
pickle.dump(param_dict, handle, protocol=pickle.HIGHEST_PROTOCOL)
m.as_pandas_table().to_pickle('ExtraEVfinalindividsubjGPs/params_fulltrimdata_'+str(numInducingPoints)+'IP_sub'+str(subID)+'_np'+str(npseed) + '_iters'+str(iters))
print("Calculating Goalie Gradient Metric....")
inds = np.array([0, 3], dtype=np.int32) #for goalie position and goalie y-velocity variables
Xph = tf.placeholder(m.X.dtype, shape=(inputdata.shape[0], inputdata.shape[1]))
data = np.array(inputdata, dtype=m.X.dtype)
fd = {Xph: data}
Xdata = tf.data.Dataset.from_tensor_slices(Xph).batch(mb)
iterator = Xdata.make_initializable_iterator()
next_element = iterator.get_next()
df_white = kelsey_calc_whitened_indices(m, next_element, inds)
dfs = []
with tf.Session() as sess:
m.initialize()
sess.run(iterator.initializer, feed_dict=fd)
while True:
try:
dfs.append(sess.run([df_white], feed_dict=fd)[0])
except tf.errors.OutOfRangeError:
break
result = np.concatenate(dfs).squeeze()
mywhitemetric = result[:,0]**2 + result[:,1]**2
if whichModel == 'PSwitch':
np.save("finalindividsubjGPs/pswitchgradmetric_trimsub" + str(subID) + "_" + str(numInducingPoints) + "IPs_" + "npseed" + str(npseed) + '_' + str(iters) + 'iters.npy', mywhitemetric)
elif whichModel == 'ExtraEV':
np.save("ExtraEVfinalindividsubjGPs/gradmetric_trimsub" + str(subID) + "_" + str(numInducingPoints) + "IPs_" + "npseed" + str(npseed) + '_' + str(iters) + 'iters.npy', mywhitemetric)
print("Subject " + str(subID) + " Complete")
def kelsey_calc_whitened_indices(m, X, inds):
f = m.q_mu._constrained_tensor
Z = m.feature.Z._constrained_tensor
K = m.kern.K(Z) + tf.eye(tf.shape(Z)[0],
dtype=gpflow.settings.float_type) * gpflow.settings.numerics.jitter_level
LK = tf.cholesky(K)
lenscales = tf.gather(m.kern.lengthscales._constrained_tensor, inds)
kvec = m.kern.K(X, Z)
kscal = m.kern.variance.constrained_tensor
dX = (tf.expand_dims(tf.gather(Z, inds, axis=1), 0)
- tf.expand_dims(tf.gather(X, inds, axis=1), 1))
dk = lenscales**(-2) * dX * tf.expand_dims(kvec, 2)
# first piece of covariance
ddk = tf.diag(kscal * lenscales**(-2))
# second piece of covariance
LKinvdk = tf.stack(tf.map_fn(lambda x: tf.matrix_triangular_solve(LK, x, lower=True), dk))
dkKinvdk = tf.matmul(LKinvdk, LKinvdk, transpose_a=True)
# mean
dmu = tf.einsum('bmd,mj->bd', LKinvdk, f)
# put it all together and invert
dSigma = (ddk - dkKinvdk)
L = tf.cholesky(dSigma)
df_white = tf.matrix_triangular_solve(L, tf.expand_dims(dmu,2), lower=True)
return df_white
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('--subID', type=int)
parser.add_argument('--npseed',default=1, type=int)
parser.add_argument('--iterations',default=200000,type=int)
parser.add_argument('--gpu',default=0, type=int)
parser.add_argument('--IP', default=500, type=int)
parser.add_argument('--whichModel', default='PSwitch', type=str) #PSwitch or ExtraEV
args = parser.parse_args()
train_model(**vars(args))