def forward(self, input, target):
    GPU_INT_DTYPE = torch.cuda.IntTensor
    GPU_LONG_DTYPE = torch.cuda.LongTensor
    GPU_FLOAT_DTYPE = torch.cuda.FloatTensor
    self.r = input
    classes = target.cpu().numpy()
    self.classes = torch.from_numpy(classes).type(GPU_LONG_DTYPE)
    (self.clusters, _) = torch.sort(torch.arange(0, float(self.n_clusters)).repeat(self.examples_per_cluster))
    self.clusters = self.clusters.type(GPU_INT_DTYPE)
    self.cluster_classes = self.classes[0:(self.n_clusters * self.examples_per_cluster):self.examples_per_cluster]
    cluster_examples = dynamic_partition(self.r, self.clusters, self.n_clusters)
    cluster_means = torch.stack([torch.mean(x, dim=0) for x in cluster_examples])
    sample_costs = compute_euclidean_distance(cluster_means, expand_dims(self.r, 1))
    self.sample_costs = sample_costs
    clusters_tensor = self.clusters.type(GPU_FLOAT_DTYPE)
    n_clusters_tensor = torch.arange(0, self.n_clusters).type(GPU_FLOAT_DTYPE)
    intra_cluster_mask = Variable(comparison_mask(clusters_tensor, n_clusters_tensor).type(GPU_FLOAT_DTYPE))
    intra_cluster_costs = torch.sum((intra_cluster_mask * sample_costs), dim=1)
    N = self.r.size()[0]
    variance = (torch.sum(intra_cluster_costs) / float((N - 1)))
    self.variances = torch.cat((self.variances, variance.unsqueeze(0).cpu()), 0)
    var_normalizer = ((- 1) / (2 * (variance ** 2)))
    numerator = torch.exp(((var_normalizer * intra_cluster_costs) - self.alpha))
    classes_tensor = self.classes.type(GPU_FLOAT_DTYPE)
    cluster_classes_tensor = self.cluster_classes.type(GPU_FLOAT_DTYPE)
    diff_class_mask = Variable(comparison_mask(classes_tensor, cluster_classes_tensor).type(GPU_FLOAT_DTYPE))
    diff_class_mask = (1 - diff_class_mask)
    denom_sample_costs = torch.exp((var_normalizer * sample_costs))
    denominator = torch.sum((diff_class_mask * denom_sample_costs), dim=1)
    epsilon = 1e-08
    losses = F.relu((- torch.log(((numerator / (denominator + epsilon)) + epsilon))))
    total_loss = torch.mean(losses)
    if (self.style == 'closest'):
        (_, pred) = sample_costs.min(1)
        acc = pred.eq(clusters_tensor.type(GPU_LONG_DTYPE)).float().mean()
    else:
        raise NotImplementedError
    return (total_loss, losses, pred, acc)
def forward(self, input, target):
    GPU_INT_DTYPE = torch.cuda.IntTensor
    GPU_LONG_DTYPE = torch.cuda.LongTensor
    GPU_FLOAT_DTYPE = torch.cuda.FloatTensor
    self.r = input
    classes = target.cpu().numpy()
    self.classes = torch.from_numpy(classes).type(GPU_LONG_DTYPE)
    (self.clusters, _) = torch.sort(torch.arange(0, float(self.n_clusters)).repeat(self.examples_per_cluster))
    self.clusters = self.clusters.type(GPU_INT_DTYPE)
    self.cluster_classes = self.classes[0:(self.n_clusters * self.examples_per_cluster):self.examples_per_cluster]
    cluster_examples = dynamic_partition(self.r, self.clusters, self.n_clusters)
    cluster_means = torch.stack([torch.mean(x, dim=0) for x in cluster_examples])
    sample_costs = compute_euclidean_distance(cluster_means, expand_dims(self.r, 1))
    self.sample_costs = sample_costs
    clusters_tensor = self.clusters.type(GPU_FLOAT_DTYPE)
    n_clusters_tensor = torch.arange(0, self.n_clusters).type(GPU_FLOAT_DTYPE)
    intra_cluster_mask = Variable(comparison_mask(clusters_tensor, n_clusters_tensor).type(GPU_FLOAT_DTYPE))
    intra_cluster_costs = torch.sum((intra_cluster_mask * sample_costs), dim=1)
    N = self.r.size()[0]
    variance = (torch.sum(intra_cluster_costs) / float((N - 1)))
    self.variances = torch.cat((self.variances, variance.unsqueeze(0).cpu()), 0)
    var_normalizer = ((- 1) / (2 * (variance ** 2)))
    numerator = torch.exp(((var_normalizer * intra_cluster_costs) - self.alpha))
    classes_tensor = self.classes.type(GPU_FLOAT_DTYPE)
    cluster_classes_tensor = self.cluster_classes.type(GPU_FLOAT_DTYPE)
    diff_class_mask = Variable(comparison_mask(classes_tensor, cluster_classes_tensor).type(GPU_FLOAT_DTYPE))
    diff_class_mask = (1 - diff_class_mask)
    denom_sample_costs = torch.exp((var_normalizer * sample_costs))
    denominator = torch.sum((diff_class_mask * denom_sample_costs), dim=1)
    epsilon = 1e-08
    losses = F.relu((- torch.log(((numerator / (denominator + epsilon)) + epsilon))))
    total_loss = torch.mean(losses)
    if (self.style == 'closest'):
        (_, pred) = sample_costs.min(1)
        acc = pred.eq(clusters_tensor.type(GPU_LONG_DTYPE)).float().mean()
    else:
        raise NotImplementedError
    return (total_loss, losses, pred, acc)