def estimate_u(self, y, y_dot, gy=None): if gy is None: gy = generalized_gradient(y) A, B = y.shape n = A * B K = self.H.shape[0] H = self.H # H = (K x 2 x H x W ) # gy = (2 x H x W ) Hgy = np.tensordot(H * gy, [1, 1], axes=(1, 0)) # This was the slow system # a = np.zeros((n, K)) # b = np.zeros(n) # s = 0 # for i, j in itertools.product(range(y.shape[0]), # range(y.shape[1])): # a[s, :] = Hgy[:, i, j] # b[s] = y_dot[i, j] # s += 1 # assert s == n a = Hgy.reshape((K, n)).T b = y_dot.reshape(n) u_est, _, _, _ = np.linalg.lstsq(a, b) return u_est
def estimate_y_dot(self, y, u, gy=None): if gy is None: gy = generalized_gradient(y) H = self.H uH = np.tensordot(u, H, axes=(0, 0)) y_dot = (uH * gy).sum(axis=0) return y_dot
def update(self, y, y_dot, u, w): self.once = True M = y.shape[0] check_all_finite(y) check_all_finite(y_dot) check_all_finite(u) # TODO: check shape is conserved self.is1D = y.ndim == 1 self.is2D = y.ndim == 2 gy = generalized_gradient(y) y_dot_w = w u_w = np.ones(u.shape) gy_w = w.reshape((1, M)) assert gy.shape == gy_w.shape Qi = outer(u, u) Qi_w = outer(u_w, u_w) self.Q.update(Qi, Qi_w) Pi = outer_first_dim(gy) Pi_w = outer_first_dim(gy_w) self.P.update(Pi, Pi_w) self.R_needs_update = True Gi = outer(u, gy * y_dot) Gi_w = outer(u_w, gy_w * y_dot_w) self.G.update(Gi, Gi_w) self.H_needs_update = True Bk = outer(u, y_dot) Bk_w = outer(u_w, y_dot_w) self.B.update(Bk, Bk_w) self.C_needs_update = True self.last_y = y self.last_gy = gy self.last_y_dot = y_dot self.last_u = u self.last_w = w
def get_importance(self, y, y_dot): self.once = True gy = generalized_gradient(y) # gy='array[1xN]', y_valid = np.logical_and(y > self.min_y, y < self.max_y) gy0 = gy[0, :] gy_valid = np.abs(gy0) < self.max_gy y_dot_valid = np.abs(y_dot) < self.max_y_dot w = y_valid * 1.0 * gy_valid * y_dot_valid self.w_stats.update(w) self.last_w = w self.last_y = y self.last_y_valid = y_valid self.last_y_dot = y_dot self.last_y_dot_valid = y_dot_valid self.last_gy = gy self.last_gy_valid = gy_valid return w