(self, X: pd.DataFrame)
Learn the mixture probability, mean and covariance for each component k.
        Store the computed energy based on the training data and the aforementioned parameters.
Learn the mixture probability, mean and covariance for each component k.
        Store the computed energy based on the training data and the aforementioned parameters.
Learn the mixture probability mean and covariance for each component k . Store the computed energy based on the training data and the aforementioned parameters .
def fit(self, X: pd.DataFrame):
        """Learn the mixture probability, mean and covariance for each component k.
        Store the computed energy based on the training data and the aforementioned parameters."""
        X.interpolate(inplace=True)
        X.bfill(inplace=True)
        data = X.values
        sequences = [data[i:i + self.sequence_length] for i in range(X.shape[0] - self.sequence_length + 1)]
        data_loader = DataLoader(dataset=sequences, batch_size=self.batch_size, shuffle=True, drop_last=True)
        self.hidden_size = 5 + int(X.shape[1] / 20)
        autoencoder = self.autoencoder_type(X.shape[1], hidden_size=self.hidden_size, **self.autoencoder_args)
        self.dagmm = DAGMMModule(autoencoder, n_gmm=self.gmm_k, latent_dim=self.hidden_size + 2,
                                 seed=self.seed, gpu=self.gpu)
        self.to_device(self.dagmm)
        self.optimizer = torch.optim.Adam(self.dagmm.parameters(), lr=self.lr)

        for _ in trange(self.num_epochs):
            for input_data in data_loader:
                input_data = self.to_var(input_data)
                self.dagmm_step(input_data.float())

        self.dagmm.eval()
        n = 0
        mu_sum = 0
        cov_sum = 0
        gamma_sum = 0
        for input_data in data_loader:
            input_data = self.to_var(input_data)
            _, _, z, gamma = self.dagmm(input_data.float())
            phi, mu, cov = self.dagmm.compute_gmm_params(z, gamma)

            batch_gamma_sum = torch.sum(gamma, dim=0)

            gamma_sum += batch_gamma_sum
            mu_sum += mu * batch_gamma_sum.unsqueeze(-1)  # keep sums of the numerator only
            cov_sum += cov * batch_gamma_sum.unsqueeze(-1).unsqueeze(-1)  # keep sums of the numerator only

            n += input_data.size(0)
def fit ( self , X : pd . DataFrame ) : X . interpolate ( inplace = True ) X . bfill ( inplace = True ) data = X . values sequences = [ data [ i : i + self . sequence_length ] for i in range ( X . shape [ 0 ] - self . sequence_length + 1 ) ] data_loader = DataLoader ( dataset = sequences , batch_size = self . batch_size , shuffle = True , drop_last = True ) self . hidden_size = 5 + int ( X . shape [ 1 ] / 20 ) autoencoder = self . autoencoder_type ( X . shape [ 1 ] , hidden_size = self . hidden_size , * * self . autoencoder_args ) self . dagmm = DAGMMModule ( autoencoder , n_gmm = self . gmm_k , latent_dim = self . hidden_size + 2 , seed = self . seed , gpu = self . gpu ) self . to_device ( self . dagmm ) self . optimizer = torch . optim . Adam ( self . dagmm . parameters ( ) , lr = self . lr ) for _ in trange ( self . num_epochs ) : for input_data in data_loader : input_data = self . to_var ( input_data ) self . dagmm_step ( input_data . float ( ) ) self . dagmm . eval ( ) n = 0 mu_sum = 0 cov_sum = 0 gamma_sum = 0 for input_data in data_loader : input_data = self . to_var ( input_data ) _ , _ , z , gamma = self . dagmm ( input_data . float ( ) ) phi , mu , cov = self . dagmm . compute_gmm_params ( z , gamma ) batch_gamma_sum = torch . sum ( gamma , dim = 0 ) gamma_sum += batch_gamma_sum mu_sum += mu * batch_gamma_sum . unsqueeze ( - 1 ) # keep sums of the numerator only cov_sum += cov * batch_gamma_sum . unsqueeze ( - 1 ) . unsqueeze ( - 1 ) # keep sums of the numerator only n += input_data . size ( 0 )