Update modeling_super_linear.py
Browse files292 unmodified lines293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356292 unmodified linesself.gating_network = nn.Linear(input_dim, self.num_experts, bias=True)def get_periodogram(self, inputs, ker_len=50, con=1, n=10000):n_fft = 128ker_len =12if inputs.ndim == 2: # (B, L)B, L = inputs.shapeC = 1x = inputs.unsqueeze(2) # → (B, L, 1)time_first = True # time is dim-1elif inputs.ndim == 3:B, d1, d2 = inputs.shapeif d1 < d2: # (B, L, C)L, C = d1, d2x = inputstime_first = Trueelse: # (B, C, L)C, L = d1, d2x = inputs.transpose(1, 2) # → (B, L, C)time_first = Falseelse:raise ValueError("Input must be (B,L), (B,L,C) or (B,C,L)")# ---------- centre the signal ----------x = x - x.mean(dim=1, keepdim=True)# ---------- parameter defaults ----------if n_fft is None:n_fft = 1 << (L - 1).bit_length()if ker_len is None:ker_len = min(L // 4, 50)ker_half = ker_len // 2# ---------- high-pass detrend ----------if con and ker_len > 0:x_perm = x.permute(0, 2, 1) # (B, C, L)ker = torch.ones(1, 1, ker_len, device=x.device) / ker_lentrend = F.conv1d(x_perm, ker, padding="same")left = min(ker_half, L - 1)right = min(ker_half, L - 1)trend[:, :, :left] = trend[:, :, left:left+1]trend[:, :, -right:] = trend[:, :, -(right+1):-right]x = (x_perm - trend).permute(0, 2, 1) # back to (B, L, C)# ---------- FFT ----------dft = torch.fft.fft(x, n=n_fft, dim=1) / np.sqrt(n_fft)I = (dft[:, : n_fft//2, :]).abs() ** 2# ---------- normalise ----------I_sum = I.sum(dim=1, keepdim=True)I_sum[I_sum == 0] = 1I /= I_sum# ---------- restore original layout ----------if inputs.ndim == 2: # wanted (B, … )return I.squeeze(2)if time_first: # original was (B, L, C)return I # already (B, F, C)else: # original was (B, C, L) → (B, C, F)return I.transpose(1, 2)def fourier_interp_dim1(self,x, target_len: int = 512):292 unmodified lines293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332292 unmodified linesself.gating_network = nn.Linear(input_dim, self.num_experts, bias=True)def get_periodogram(self, inputs, ker_len=50, con=1, n=10000):if inputs.dim() == 2:x_0 = inputs.unsqueeze(2)else:x_0 = inputsx_0 = x_0 - torch.mean(x_0, dim=1, keepdim=True)v = torch.arange(0, n) / nif con:if ker_len is None:ker_len = n // 4ker_len = min(ker_len, 50)x_0 = x_0.permute(0, 2, 1)ker = (torch.ones(1, 1, ker_len) / ker_len).to(x_0.device)x_c = F.conv1d(x_0, ker, padding="same")x_c[:, :, :ker_len // 2] = x_c[:, :, ker_len // 2:ker_len // 2 + 1]x_c[:, :, -ker_len // 2:] = x_c[:, :, -ker_len // 2 - 1:-ker_len // 2]x_0 = x_0 - x_cx_0 = x_0.permute(0, 2, 1)dft = torch.fft.fft(x_0, dim=1, n=n) / np.sqrt(n)dft = dft[:, :n//2, :]I = torch.abs(dft) ** 2I_sum = torch.sum(I, dim=1, keepdim=True)I_sum[I_sum == 0] = 1I = I / I_sumif torch.any(I_sum == 0):print("Zeros in the sum")raise ValueErrorif inputs.dim() == 2:I = I.squeeze(2)def fourier_interp_dim1(self,x, target_len: int = 512):
modeling_super_linear.py+28-52