def forward(self, input, target):
    assert (self.cluster_means is not None)
    assert (self.cluster_classes is not None)
    assert (self.variance is not None)
    GPU_INT_DTYPE = torch.cuda.IntTensor
    GPU_LONG_DTYPE = torch.cuda.LongTensor
    GPU_FLOAT_DTYPE = torch.cuda.FloatTensor
    num_classes = (np.max(self.cluster_classes) + 1)
    cluster_means = torch.from_numpy(self.cluster_means).type(GPU_FLOAT_DTYPE)
    cluster_classes = torch.from_numpy(self.cluster_classes).type(GPU_LONG_DTYPE)
    sample_costs = compute_euclidean_distance(cluster_means, expand_dims(input, 1))
    if (self.style == 'closest'):
        (_, pred) = sample_costs.min(1)
        pred = cluster_classes[pred]
        acc = pred.eq(target).float()
        return (torch.zeros(1), torch.zeros(1), pred, acc)
    else:
        num_clusters = cluster_means.size()[0]
        (sorted_sample_costs, indices) = torch.sort(sample_costs)
        sorted_sample_costs = sorted_sample_costs.squeeze()
        indices = indices.type(GPU_LONG_DTYPE).squeeze()
        sorted_cluster_classes = cluster_classes[indices]
        if (self.L < num_clusters):
            sorted_sample_costs = sorted_sample_costs[:self.L]
            sorted_cluster_classes = sorted_cluster_classes[:self.L]
            num_clusters = self.L
        var_normalizer = ((- 1) / (2 * (self.variance ** 2)))
        normalised_costs = torch.exp((var_normalizer * sorted_sample_costs)).type(GPU_FLOAT_DTYPE)
        per_class_costs = torch.zeros(num_classes, num_clusters).type(GPU_FLOAT_DTYPE)
        per_class_costs = per_class_costs.scatter_(0, sorted_cluster_classes.unsqueeze(0), normalised_costs.unsqueeze(0))
        numerator = per_class_costs.sum(1)
        denominator = torch.sum(normalised_costs)
        epsilon = 1e-08
        probs = (numerator / (denominator + epsilon))
        (_, pred) = probs.max(0)
        acc = pred.eq(target).float()
        return (torch.zeros(1), torch.zeros(1), pred, acc)
def forward(self, input, target):
    assert (self.cluster_means is not None)
    assert (self.cluster_classes is not None)
    assert (self.variance is not None)
    GPU_INT_DTYPE = torch.cuda.IntTensor
    GPU_LONG_DTYPE = torch.cuda.LongTensor
    GPU_FLOAT_DTYPE = torch.cuda.FloatTensor
    num_classes = (np.max(self.cluster_classes) + 1)
    cluster_means = torch.from_numpy(self.cluster_means).type(GPU_FLOAT_DTYPE)
    cluster_classes = torch.from_numpy(self.cluster_classes).type(GPU_LONG_DTYPE)
    sample_costs = compute_euclidean_distance(cluster_means, expand_dims(input, 1))
    if (self.style == 'closest'):
        (_, pred) = sample_costs.min(1)
        pred = cluster_classes[pred]
        acc = pred.eq(target).float()
        return (torch.zeros(1), torch.zeros(1), pred, acc)
    else:
        num_clusters = cluster_means.size()[0]
        (sorted_sample_costs, indices) = torch.sort(sample_costs)
        sorted_sample_costs = sorted_sample_costs.squeeze()
        indices = indices.type(GPU_LONG_DTYPE).squeeze()
        sorted_cluster_classes = cluster_classes[indices]
        if (self.L < num_clusters):
            sorted_sample_costs = sorted_sample_costs[:self.L]
            sorted_cluster_classes = sorted_cluster_classes[:self.L]
            num_clusters = self.L
        var_normalizer = ((- 1) / (2 * (self.variance ** 2)))
        normalised_costs = torch.exp((var_normalizer * sorted_sample_costs)).type(GPU_FLOAT_DTYPE)
        per_class_costs = torch.zeros(num_classes, num_clusters).type(GPU_FLOAT_DTYPE)
        per_class_costs = per_class_costs.scatter_(0, sorted_cluster_classes.unsqueeze(0), normalised_costs.unsqueeze(0))
        numerator = per_class_costs.sum(1)
        denominator = torch.sum(normalised_costs)
        epsilon = 1e-08
        probs = (numerator / (denominator + epsilon))
        (_, pred) = probs.max(0)
        acc = pred.eq(target).float()
        return (torch.zeros(1), torch.zeros(1), pred, acc)