def __matmul__(self, inp: CombT) -> RkhsObject: if isinstance(inp, FiniteMap): G = inner(self.inp_feat, inp.outp_feat) if not inp.debias_outp: matr = self.matr @ G @ inp.matr inp_bias = (matr @ inp.bias.T).T else: matr = self.matr @ (G - G @ inp.bias.T) @ inp.matr inp_bias = (self.matr @ G @ inp.bias.T).T rval = FiniteMap(inp.inp_feat, self.outp_feat, matr, outp_bias=self.bias + inp_bias) rval.mean_center_inp = inp.mean_center_inp return rval else: if isinstance(inp, DeviceArray): inp = FiniteVec(self.inp_feat.k, np.atleast_2d(inp)) lin_map = (self.matr @ inner(self.inp_feat, inp)).T if self.debias_outp: r = [DecenterOutFeat(lin_map)] else: if self._normalize: lin_map = lin_map / lin_map.sum(1, keepdims=True) r = [LinearReduce(lin_map + self.bias)] if len(inp) == 1: r.append(Sum()) rval = self.outp_feat.extend_reduce(r) return rval
def __init__(self, inp_feat: Vec, outp_feat: Vec, ref_feat: Vec, regul=0.01): if True: op = multiply( CovOp(ref_feat, regul).inv(), Cmo(inp_feat, outp_feat, regul)) (self.inp_feat, self.outp_feat, self.matr) = (op.inp_feat, op.outp_feat, op.matr) else: self.inp_feat = inp_feat self.outp_feat = ref_feat cmo_matr = np.linalg.inv( inner(self.inp_feat) + regul * tf.eye(len(inp_feat))) assert np.allclose(cmo_matr, Cmo(inp_feat, outp_feat, regul).matr) inv_gram = np.linalg.inv( inner(ref_feat) + regul * np.eye(len(ref_feat), dtype=ref_feat.prefactors.dtype)) preimg_factor = ( np.diag(ref_feat.prefactors**2) @ inv_gram @ inv_gram) #assert np.allclose(preimg_factor, CovOp(ref_feat, regul).inv().matr) self.matr = preimg_factor @ inner(ref_feat, outp_feat) @ cmo_matr
def __init__(self, start_feat: InpVecT, timelagged_feat: InpVecT, regul=0.01, embedded=True, koopman=False): self.embedded = embedded self.koopman = koopman assert (start_feat.k == timelagged_feat.k) if (embedded is True and koopman is False) or (embedded is False and koopman is True): self.matr = np.linalg.inv( inner(start_feat) + timelagged_feat * regul * np.eye(len(start_feat))) else: G_xy = inner(start_feat, timelagged_feat) G_x = inner(start_feat) matr = (np.linalg.pinv(G_xy) @ np.linalg.pinv(G_x + len(timelagged_feat) * regul * np.eye(len(timelagged_feat))) @ G_xy) if koopman is True: matr = self.matr.T if koopman is False: inp_feat = start_feat outp_feat = timelagged_feat else: inp_feat = timelagged_feat outp_feat = start_feat super().__init__(inp_feat, outp_feat, matr)
def multiply(A:FiniteOp[IntermVecT, OutVecT], B:CombT) -> RkhsObject: # "T = TypeVar("T"); multiply(A:FiniteOp, B:T) -> T" if isinstance(B, FiniteOp): return FiniteOp(B.inp_feat, A.outp_feat, A.matr @ inner(A.inp_feat, B.outp_feat) @ B.matr) else: if len(B) == 1: return FiniteVec.construct_RKHS_Elem(A.outp_feat.k, A.outp_feat.inspace_points, np.squeeze(A.matr @ inner(A.inp_feat, B))) else: pref = A.matr @ inner(A.inp_feat, B) return FiniteVec(A.outp_feat.k, np.tile(A.outp_feat.inspace_points, (pref.shape[1], 1)), np.hstack(pref.T), points_per_split=pref.shape[0])
def inv(self, regul: float = None) -> "CovOp[InpVecT, InpVecT]": """Compute the inverse of this covariance operator with a certain regularization. Args: regul (float, optional): Regularization parameter to be used. Defaults to self.regul. Returns: CovOp[InpVecT, InpVecT]: The inverse operator """ set_inv = False rval = self._inv if regul is None: set_inv = True regul = self.regul print("regul=", regul) if self._inv is None or set_inv: inv_gram = np.linalg.inv( inner(self.inp_feat) + regul * np.eye(len(self.inp_feat), dtype=self.matr.dtype)) matr = (self.matr @ self.matr @ inv_gram @ inv_gram) rval = CovOp(self.inp_feat, regul) rval.matr = matr rval._inv = self if set_inv: self._inv = rval return rval
def inv(self): if self._inv is None: inv_gram = np.linalg.inv(inner(self.inp_feat) + self.regul * np.eye(len(self.inp_feat), dtype = self.matr.dtype)) matr = (self.matr**2 @ inv_gram @ inv_gram) self._inv = CovOp(self.inp_feat, self.regul) self._inv.matr = matr self._inv._inv = self return self._inv
def solve(self, result: FiniteVec): if np.all(self.outp_feat.inspace_points == result.inspace_points): s = np.linalg.solve( self.matr @ inner(self.inp_feat, self.inp_feat), result.prefactors) return FiniteVec.construct_RKHS_Elem(result.k, result.inspace_points, s) else: assert ()
def __init__(self, inp_feat:InpVecT, outp_feat:OutVecT, regul = 0.01): self.inp_feat = inp_feat self.outp_feat = outp_feat regul = np.array(regul, dtype=np.float32) if False: op = multiply(CrossCovOp(inp_feat, outp_feat), CovOp(inp_feat, regul).inv()) (self.inp_feat, self.outp_feat, self.matr) = (op.inp_feat, op.outp_feat, op.matr) else: self.matr = np.linalg.inv(inner(self.inp_feat) + regul * np.eye(len(inp_feat)))
def multiply( A: FiniteOp, B: RkhsObject, copy_tensors=True ) -> RkhsObject: # "T = TypeVar("T"); multiply(A:FiniteOp, B:T) -> T" assert (copy_tensors is False, "copy_tensors == True is not implemented yet") try: return FiniteOp(B.inp_feat, A.outp_feat, A.matr @ inner(A.inp_feat, B.outp_feat) @ B.matr) except AttributeError: if len(B) == 1: #print("len 1") return FiniteVec.construct_RKHS_Elem( A.outp_feat.k, A.outp_feat.inspace_points, np.squeeze(A.matr @ inner(A.inp_feat, B))) else: # print("len "+str(len(B))) pref = A.matr @ inner(A.inp_feat, B) return FiniteVec(A.outp_feat.k, np.tile(A.outp_feat.inspace_points, (pref.shape[1], 1)), np.hstack(pref.T), points_per_split=pref.shape[0])
def __init__(self, inp_feat: InpVecT, outp_feat: OutVecT, regul: float = 0.01, center=False, regul_func=None): regul = np.array(regul, dtype=np.float32) #we do not center the output features - this still leads to the correct results in the output of the CME c_inp_feat = inp_feat.centered() if center else inp_feat G = inner(c_inp_feat) if regul_func is None: assert regul.squeeze().size == 1 or regul.squeeze( ).shape[0] == len(inp_feat) matr = np.linalg.inv(G + regul * np.eye(len(inp_feat))) else: matr = regul_func(G) super().__init__(inp_feat, outp_feat, matr, mean_center_inp=center, decenter_outp=center)