forked from DiegoAE/HMMLFM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
283 lines (210 loc) · 9.21 KB
/
test.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
__author__ = 'diego'
from hmm.continuous.LFMHMM import LFMHMM
from hmm.continuous.LFMHMMcontinuousMO import LFMHMMcontinuousMO
from matplotlib import pyplot as plt
import numpy as np
seed = np.random.random_integers(10000)
seed = 6490
np.random.seed(seed)
print "USED SEED", seed
pi = np.array([0.3, 0.3, 0.4])
print "initial state distribution", pi
A = np.array([[0.1, 0.5, 0.4], [0.6, 0.1, 0.3], [0.4, 0.5, 0.1]])
print "hidden state transition matrix\n", A
number_lfm = 3
outputs = 1
start_t = 0.1 # finding: it's problematic to choose 0 as starting point.
end_t = 5.1 # finding: it's problematic to choose long times.
# since the cov's tend to be the same.
locations_per_segment = 20
# list of lists in case of multiple outputs
damper_constants = np.asarray([[1.], [3.], [6.]])
spring_constants = np.asarray([[3.], [1.], [5.]])
# implicitly assuming there is only one latent force governing the system.
# lengthscales = np.asarray([8., 10., 12.])
lengthscales = np.asarray([[10.], [10.], [10.]])
# it seems to be quite problematic when you choose big lenghtscales
noise_var = np.array([0.0005]) # Viterbi starts failing when this noise is set.
lfm_hmm = LFMHMMcontinuousMO(outputs, number_lfm, locations_per_segment, start_t,
end_t, verbose=True)
lfm_hmm.set_params(A, pi, damper_constants, spring_constants, lengthscales,
noise_var)
# Testing packing and unpacking
print "Testing packing and unpacking: ",
test_unit_1 = lfm_hmm.unpack_params(lfm_hmm.pack_params(lfm_hmm.LFMparams))
test_unit_2 = lfm_hmm.LFMparams
for k in test_unit_2.keys():
assert np.allclose(test_unit_2[k], test_unit_1[k])
print "Accepted!"
# Plotting
# segments = 10
# obs_1, _ = lfm_hmm.generate_observations(segments)
# last_value = 0
# for i in xrange(segments):
# plt.axvline(x=last_value, color='red', linestyle='--')
# sl = lfm_hmm.sample_locations
# plt.plot(last_value + sl - sl[0], obs_1[i])
# # print last_value
# # print last_value + sl - sl[0]
# last_value += end_t - start_t
# plt.show()
obs = []
n_training_sequences = 10
hidden_states = np.zeros(n_training_sequences, dtype=object)
for i in xrange(n_training_sequences):
segments = np.random.randint(1, 100)
print "The %d-th sequence has length %d" % (i, segments)
output, hidden = lfm_hmm.generate_observations(segments)
obs.append(output)
hidden_states[i] = hidden
# plotting covariance matrix
# plt.figure(1)
# plt.subplot(131)
# plt.imshow(lfm_hmm.get_cov_function(0))
# plt.subplot(132)
# plt.imshow(lfm_hmm.get_cov_function(1))
# plt.subplot(133)
# plt.imshow(lfm_hmm.get_cov_function(2))
# plt.show()
lfm_hmm.set_observations(obs)
lfm_hmm.reset(emissions_reset=True) # Reset to A and pi
print lfm_hmm.pi
print lfm_hmm.A
print lfm_hmm.LFMparams
print "start training"
train_flag = True
if train_flag:
lfm_hmm.train()
lfm_hmm.save_params("/home/diego/tmp/Parameters", "pruebaSO")
else:
lfm_hmm.read_params("/home/diego/tmp/Parameters", "pruebaSO")
print "after training"
print lfm_hmm.pi
print lfm_hmm.A
print lfm_hmm.LFMparams
recovered_paths = lfm_hmm._viterbi()
# Testing GP-LFM fitting from the Viterbi estimation
one_observation = obs[0][0]
one_hidden_state = recovered_paths[0][0]
# print one_observation, one_hidden_state
t_test = np.linspace(start_t, end_t, 600)
mean_pred, cov_pred = lfm_hmm.predict(t_test, one_hidden_state, one_observation)
diag_cov = np.diag(cov_pred)
print recovered_paths - hidden_states
plt.scatter(lfm_hmm.sample_locations, one_observation)
plt.plot(t_test, mean_pred)
plt.plot(t_test, mean_pred.flatten() - 2 * np.sqrt(diag_cov), 'k--')
plt.plot(t_test, mean_pred.flatten() + 2 * np.sqrt(diag_cov), 'k--')
plt.show()
# Recomendaciones de mauricio para los problemas numericos
# 1. Mirar el caso en en que el kernel no funciona bien (overdamped, underdamped, critically damped).
# There is not system critically dammped.
# 2. Asegurarse que el lengthscale sea mas grande que las distancias entre samples.
# Asegurado
# 3. Sumar un jitter para la matriz de covarianza.
# 4. Mirar el codigo de Matlab por que no funciona?
# 5. Graficar la matriz de covarianza obtenida para visualizar que puede estar fallando. Done. Good hint!
# First: experiment Validation of the viterbi algorithm
# now the lfm_hmm has the estimated pi and A
# it's necessary to create a new instance of LFMHMM
lfm_hmm_reference = LFMHMMcontinuousMO(outputs, number_lfm, locations_per_segment,
start_t, end_t, verbose=True)
lfm_hmm_reference.set_params(A, pi, damper_constants, spring_constants,
lengthscales, noise_var)
obs = []
n_training_sequences = 20
hidden_states_reference = np.zeros(n_training_sequences, dtype=object)
for i in xrange(n_training_sequences):
segments = np.random.randint(1, 100)
print "The %d-th sequence has length %d" % (i, segments)
output, hidden = lfm_hmm_reference.generate_observations(segments)
obs.append(output)
hidden_states_reference[i] = hidden
recovered_paths = lfm_hmm._viterbi(obs)
diff = recovered_paths - hidden_states_reference
mismatchs = 0
total_segments = 0
for i in xrange(len(diff)):
mismatchs += np.count_nonzero(diff[i])
total_segments += len(diff[i])
print "the viterbi algorithm failed in %d from %d" % (mismatchs, total_segments)
# Second experiment: Regression
regression_observation = [obs[0]]
hidden_states_ground_truth = np.array(hidden_states_reference[0])
lfm_hmm.set_observations(regression_observation)
regression_hidden_states = lfm_hmm._viterbi()[0]
# This measure doesn't take into account that the hidden states can be permuted
# and the parameters might be potentially unidentifiable.
print "The number of wrong predicted motor primitives is %d" % \
np.count_nonzero(regression_hidden_states - hidden_states_ground_truth)
considered_segments = min(len(regression_hidden_states), 20) # a few segments
number_testing_points = 100
# Plotting the priors
# last_value = 0
# plt.axvline(x=last_value, color='red', linestyle='--')
# for i in xrange(considered_segments):
# c_hidden_state = regression_hidden_states[i]
# # predicting more time steps
# t_test = np.linspace(start_t, end_t, locations_per_segment)
# mean_prior = np.zeros(len(t_test))
# cov_prior = lfm_hmm.lfms[c_hidden_state].Kyy()
# plt.plot(last_value + t_test - t_test[0], mean_prior, color='green')
# diag_cov = np.diag(cov_prior)
# plt.plot(last_value + t_test - t_test[0], mean_prior.flatten() - 2 * np.sqrt(diag_cov), 'k--')
# plt.plot(last_value + t_test - t_test[0], mean_prior.flatten() + 2 * np.sqrt(diag_cov), 'k--')
# last_value = last_value + end_t - start_t
# plt.axvline(x=last_value, color='red', linestyle='--')
#
# plt.title("Plotting priors.")
# plt.show()
last_value = 0
plt.axvline(x=last_value, color='red', linestyle='--')
means = np.zeros((considered_segments, number_testing_points))
for i in xrange(considered_segments):
c_hidden_state = regression_hidden_states[i]
c_obv = regression_observation[0][i]
# predicting more time steps
t_test = np.linspace(start_t, end_t, number_testing_points)
mean_pred, cov_pred = lfm_hmm.predict(t_test, c_hidden_state, c_obv)
means[i, :] = mean_pred.flatten()
sl = lfm_hmm.sample_locations
plt.scatter(last_value + sl - sl[0], c_obv, facecolors='none',
label=[None, 'observations'][i == 0])
plt.plot(last_value + t_test - t_test[0], mean_pred, color='green',
label=[None, 'predicted mean'][i == 0])
diag_cov = np.diag(cov_pred)
plt.plot(last_value + t_test - t_test[0], mean_pred.flatten() - 2 * np.sqrt(diag_cov), 'k--')
plt.plot(last_value + t_test - t_test[0], mean_pred.flatten() + 2 * np.sqrt(diag_cov), 'k--')
last_value = last_value + end_t - start_t
plt.axvline(x=last_value, color='red', linestyle='--')
print "*****"
print "HS Ground truth", hidden_states_ground_truth[:considered_segments]
print "HS own model ", regression_hidden_states[:considered_segments]
print "*****"
plt.title("Fitting of the model given an observation sequence.")
plt.legend(loc='upper left')
plt.show()
#print regression_hidden_states
# Third experiment
number_testing_points = 191
lfm_validation = LFMHMMcontinuousMO(outputs, number_lfm, number_testing_points,
start_t, end_t, verbose=True)
lfm_validation.set_params(A, pi, damper_constants, spring_constants,
lengthscales, noise_var)
n_segments = 5
full_observation, reference_states = lfm_validation.generate_observations(n_segments)
sampled_observation = np.zeros((n_segments, 20))
for i in xrange(n_segments):
for j in xrange(20):
sampled_observation[i][j] = full_observation[i][j * 10]
obtained_states = lfm_hmm._viterbi([sampled_observation])
rmse = 0
for i in xrange(n_segments):
c_hidden_state = obtained_states[0][i]
t_test = np.linspace(start_t, end_t, number_testing_points) # predicting more time steps
mean_pred, cov_pred = lfm_hmm.predict(t_test, c_hidden_state,
sampled_observation[i])
rmse += np.power(mean_pred.flatten() - full_observation[i].flatten(), 2).sum()
print "The RMSE error in regression is %f" % np.sqrt(rmse)
# the same motor primitive were recovered.
print "USED SEED", seed