PyTorchClassification/data_loader.py [401:432]:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
            self.relationship = np.zeros((self.get_num_classes(), self.get_num_classes()))
            for tax_level, levelname in list(enumerate(self.tax_levels))[::-1]:
                assingm = tax_assignments[tax_level]
                self.relationship[assingm[:,None] == assingm[None,:]] = len(self.tax_levels) - tax_level

            # Compute the probability mass to be distributed for same genus, same family, etc
            # Start with the relative weights for each level: 2**(level)
            prob_per_tax_level = np.array([2**i for i in range(len(self.tax_levels) - 1)])
            # Distribute (1 - label_smoothing) according to these weights
            prob_per_tax_level = label_smoothing * prob_per_tax_level / np.sum(prob_per_tax_level)
            # Prob mass for unrelated classes as all probabilities have to be non-zero
            eps = prob_per_tax_level[0] * 0.1
            prob_per_tax_level[0] -= eps
            # Add probability mass for the same class predicition and unrelated class prediciton
            prob_per_tax_level = [eps] + prob_per_tax_level.tolist() + [1 - label_smoothing] 

            # Now convert the numbers in the matrix to probabilities
            self.targets = np.zeros_like(self.relationship, dtype=np.float64)
            # We will distribute prob_per_tax_level[LEVEL] across all entries in a row with entry LEVEL
            # Accumulate left over prob mass in case a class does not have any other classes with same 
            # family across the next level 
            # We will start from to most specific tax level and go more and more generic
            leftover_prob_mass = np.zeros((len(self.targets),))
            for tax_level in range(len(prob_per_tax_level))[::-1]:
                per_row_count = np.sum(self.relationship==tax_level, axis=1)
                prob_mass = prob_per_tax_level[tax_level] + leftover_prob_mass
                self.targets[self.relationship==tax_level] = np.repeat(prob_mass / per_row_count, per_row_count)
                leftover_prob_mass = prob_per_tax_level[tax_level] * (per_row_count==0)
            # Create a memory efficient target representation for each sample
            assert not np.any(np.isclose(0, self.targets))
            self.targets = self.targets.tolist()
            self.targets = [np.array(aa) for aa in self.targets]
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -



PyTorchClassification/data_loader_cv.py [358:389]:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
            self.relationship = np.zeros((self.get_num_classes(), self.get_num_classes()))
            for tax_level, levelname in list(enumerate(self.tax_levels))[::-1]:
                assingm = tax_assignments[tax_level]
                self.relationship[assingm[:,None] == assingm[None,:]] = len(self.tax_levels) - tax_level
            
            # Compute the probability mass to be distributed for same genus, same family, etc
            # Start with the relative weights for each level: 2**(level)
            prob_per_tax_level = np.array([2**i for i in range(len(self.tax_levels) - 1)])
            # Distribute (1 - label_smoothing) according to these weights
            prob_per_tax_level = label_smoothing * prob_per_tax_level / np.sum(prob_per_tax_level)
            # Prob mass for unrelated classes as all probabilities have to be non-zero
            eps = prob_per_tax_level[0] * 0.1
            prob_per_tax_level[0] -= eps
            # Add probability mass for the same class predicition and unrelated class prediciton
            prob_per_tax_level = [eps] + prob_per_tax_level.tolist() + [1 - label_smoothing] 

            # Now convert the numbers in the matrix to probabilities
            self.targets = np.zeros_like(self.relationship, dtype=np.float64)
            # We will distribute prob_per_tax_level[LEVEL] across all entries in a row with entry LEVEL
            # Accumulate left over prob mass in case a class does not have any other classes with same 
            # family across the next level 
            # We will start from to most specific tax level and go more and more generic
            leftover_prob_mass = np.zeros((len(self.targets),))
            for tax_level in range(len(prob_per_tax_level))[::-1]:
                per_row_count = np.sum(self.relationship==tax_level, axis=1)
                prob_mass = prob_per_tax_level[tax_level] + leftover_prob_mass
                self.targets[self.relationship==tax_level] = np.repeat(prob_mass / per_row_count, per_row_count)
                leftover_prob_mass = prob_per_tax_level[tax_level] * (per_row_count==0)
            # Create a memory efficient target representation for each sample
            assert not np.any(np.isclose(0, self.targets))
            self.targets = self.targets.tolist()
            self.targets = [np.array(aa) for aa in self.targets]
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -



