def Rational_PYTORCH_C_F(x, weight_numerator, weight_denominator, training): # P(X) / Q(X) = a_0 + a_1 * X + ... + a_n * X^n / # eps + |b_0 + b1 * X + b_2 * X^2 + ... + b_m*X^m| z = x.view(-1) len_num, len_deno = len(weight_numerator), len(weight_denominator) xps = torch.vander(z, max(len_num, len_deno), increasing=True) numerator = xps.mul(weight_numerator).sum(1) denominator = xps[:, :len_deno].mul(weight_denominator).sum(1).abs() return numerator.div(0.1 + denominator).view(x.shape)
def Rational_PYTORCH_A_F(x, weight_numerator, weight_denominator, training): # P(X) / Q(X) = a_0 + a_1 * X + ... + a_n * X^n / # 1 + | b_1 * X | + | b_2 * X^2| + ... + | b_m * X ^m| z = x.view(-1) len_num, len_deno = len(weight_numerator), len(weight_denominator) xps = torch.vander(z, max(len_num, len_deno), increasing=True) numerator = xps.mul(weight_numerator).sum(1) expanded_dw = torch.cat([torch.tensor([1.]), weight_denominator, \ torch.zeros(len_num - len_deno - 1)]) denominator = xps.mul(expanded_dw).abs().sum(1) return numerator.div(denominator).view(x.shape)
def Rational_PYTORCH_D_F(x, weight_numerator, weight_denominator, training, random_deviation=0.1): # P(X)/Q(X) = noised(a_0) + noised(a_1) * X +noised(a_2) * X^2 + ... + noised(a_n) * X^n / # # 1 + |noised(b_1) * X + noised(b_2) * X^2 + ... + noised(b_m)*X^m| # # Noised parameters have uniform noise to be in range [(1-random_deviation)*parameter,(1+random_deviation)*parameter]. if not training: # do not add noise return Rational_PYTORCH_B_F(x, weight_numerator, weight_denominator, training) z = x.view(-1) len_num, len_deno = len(weight_numerator), len(weight_denominator) xps = torch.vander(z, max(len_num, len_deno), increasing=True) numerator = xps.mul(weight_numerator.mul( torch.FloatTensor(len_num).uniform_(1-random_deviation, 1+random_deviation)) ).sum(1) denominator = xps[:, 1:len_deno+1].mul(weight_denominator).sum(1).abs() return numerator.div(1 + denominator).view(x.shape)
def other_ops(self): a = torch.randn(4) b = torch.randn(4) c = torch.randint(0, 8, (5, ), dtype=torch.int64) e = torch.randn(4, 3) f = torch.randn(4, 4, 4) size = [0, 1] dims = [0, 1] return ( torch.atleast_1d(a), torch.atleast_2d(a), torch.atleast_3d(a), torch.bincount(c), torch.block_diag(a), torch.broadcast_tensors(a), torch.broadcast_to(a, (4)), # torch.broadcast_shapes(a), torch.bucketize(a, b), torch.cartesian_prod(a), torch.cdist(e, e), torch.clone(a), torch.combinations(a), torch.corrcoef(a), # torch.cov(a), torch.cross(e, e), torch.cummax(a, 0), torch.cummin(a, 0), torch.cumprod(a, 0), torch.cumsum(a, 0), torch.diag(a), torch.diag_embed(a), torch.diagflat(a), torch.diagonal(e), torch.diff(a), torch.einsum("iii", f), torch.flatten(a), torch.flip(e, dims), torch.fliplr(e), torch.flipud(e), torch.kron(a, b), torch.rot90(e), torch.gcd(c, c), torch.histc(a), torch.histogram(a), torch.meshgrid(a), torch.lcm(c, c), torch.logcumsumexp(a, 0), torch.ravel(a), torch.renorm(e, 1, 0, 5), torch.repeat_interleave(c), torch.roll(a, 1, 0), torch.searchsorted(a, b), torch.tensordot(e, e), torch.trace(e), torch.tril(e), torch.tril_indices(3, 3), torch.triu(e), torch.triu_indices(3, 3), torch.vander(a), torch.view_as_real(torch.randn(4, dtype=torch.cfloat)), torch.view_as_complex(torch.randn(4, 2)), torch.resolve_conj(a), torch.resolve_neg(a), )
def dynamics(self) -> pt.Tensor: return pt.diag(self.amplitude) @ pt.vander(self.eigvals, self._dm.shape[-1], True)