Ejemplo n.º 1
0
def create_torch_stft(samples, FFT=False):
    '''
        ARGUMENTS
            > samples - a pytorch tensor of dimensions (# samples, sample length)
            > FFT - if True, FFT is calculated instead of STFT
        RETURNS
            > spec - a tensor of dimension (# samples, frequency bins)
        '''
    if not FFT:
        # Create Spectrogram
        spec = torch.stft(samples,
                          n_fft=2048,
                          hop_length=512,
                          normalized=True,
                          return_complex=True).abs()
        # Collapse spectrogram by summing along the time axis
        spec = torch.sum(spec, dim=2)
    else:
        spec = torch.fft.fft(samples, dim=1).abs()
        spec -= spec.min(1, keepdim=True)[0]
        spec /= spec.max(1, keepdim=True)[0]

    # Normalize
    spec_max = torch.max(spec, axis=1)[0].unsqueeze(1).repeat(1, spec.shape[1])
    spec_min = torch.min(spec, axis=1)[0].unsqueeze(1).repeat(1, spec.shape[1])
    spec = torch.subtract(spec, spec_min)
    spec = torch.divide(spec, torch.subtract(spec_max, spec_min))
    spec = spec[:, :11025]
    return spec.type(torch.float32)
Ejemplo n.º 2
0
def influence_cos(model1, model2, aggregated):
    cos = nn.CosineSimilarity(dim=0, eps=1e-6)
    center = torch.flatten(aggregated["linear.weight"])
    p1 = torch.flatten(model1["linear.weight"])
    p2 = torch.flatten(model2["linear.weight"])
    p1 = torch.subtract(center, p1)
    p2 = torch.subtract(center, p2)
    return cos(p1, p2).numpy().min()
Ejemplo n.º 3
0
def var(input: Tensor,
        dim: DimOrDims = None,
        unbiased: Optional[bool] = False,
        *,
        keepdim: Optional[bool] = False,
        dtype: Optional[DType] = None,
        mask: Optional[Tensor] = None) -> Tensor:
    """\
{reduction_signature}

{reduction_descr}

The identity value of sample variance operation is undefined.  The
elements of output tensor with strided layout, that correspond to
fully masked-out elements, have ``nan`` values.

{reduction_args}

{reduction_example}"""
    if dtype is None:
        dtype = input.dtype
        if not (dtype.is_floating_point or dtype.is_complex):
            dtype = torch.float32
    compute_dtype = dtype
    if not (compute_dtype.is_floating_point or compute_dtype.is_complex):
        compute_dtype = torch.float32
    if input.layout == torch.strided:
        inmask = _input_mask(input, mask=mask)
        count = sum(inmask.new_ones(input.shape, dtype=torch.int64),
                    dim,
                    keepdim=True,
                    mask=inmask)
        sample_total = sum(input, dim, keepdim=True, dtype=dtype, mask=inmask)
        # TODO: replace torch.subtract/divide/square/maximum with
        # masked subtract/divide/square/maximum when these will be
        # available.
        sample_mean = torch.divide(sample_total, count)
        x = torch.subtract(input, sample_mean)
        total = sum(x * x.conj(),
                    dim,
                    keepdim=keepdim,
                    dtype=compute_dtype,
                    mask=inmask)
        if not keepdim:
            count = count.reshape(total.shape)
        if unbiased:
            count = torch.subtract(count, 1)
            count = torch.maximum(count, count.new_zeros([]))
        return torch.divide(total, count).to(dtype=dtype)
    else:
        raise ValueError(
            f'masked var expects strided tensor (got {input.layout} tensor)')
Ejemplo n.º 4
0
def fit(train_x,
        train_y,
        num_iterations,
        loss_interval=None,
        batch_size=4,
        weights=None):
    loss_interval = (num_iterations //
                     10 if loss_interval is None else loss_interval)
    num_y_classes = train_y.shape[1]
    num_images = train_x.shape[0]
    weights = (t.rand(
        (train_x.shape[1],
         num_y_classes), dtype=t.float64) if weights is None else weights)
    grad = t.zeros_like(weights)
    y_predicted = t.zeros((batch_size, num_y_classes), dtype=t.float64)
    softmax_y_holder = t.zeros_like(y_predicted)
    cross_entropy_y_holder = t.zeros_like(y_predicted)
    x_holder = t.zeros((batch_size, train_x.shape[1]), dtype=t.float64)

    # try:

    start = time.time()

    for iter in range(num_iterations):

        loss = 0

        for img_idx in range(0, num_images, batch_size):

            t.matmul(train_x[img_idx:img_idx + batch_size],
                     weights,
                     out=y_predicted)
            softmax_y_holder[:] = y_predicted[:]
            softmax(softmax_y_holder)

            y_true = train_y[img_idx:img_idx + batch_size]
            cross_entropy_y_holder[:] = softmax_y_holder[:]
            loss += t.sum(cross_entropy(cross_entropy_y_holder, y_true))

            t.subtract(y_true, softmax_y_holder, out=softmax_y_holder)
            t.matmul(train_x[img_idx:img_idx + batch_size].T,
                     softmax_y_holder,
                     out=grad)
            weights += LEARNING_RATE / num_images * grad

        if iter % loss_interval == 0: print(loss)

    end = time.time()
    print("Running", num_iterations, "took", end - start, "seconds!")
    # finally:
    return weights
Ejemplo n.º 5
0
    def forward(ctx, X, rank: int = 100):

        U, S, V = torch.svd(X, compute_uv=True, some=False)

        S = torch.diag(S[0:(rank - 1)])
        U = torch.matmul(U[:, 0:(rank - 1)], S)
        V = torch.transpose(V, 0, 1)[0:(rank - 1), :]

        x, y = X.shape

        Unew = U[:, 0]
        Vnew = V[0, :]

        __U = torch.where(torch.less(torch.min(V[0, :]), torch.min(-V[0, :])),
                          -(Unew.view(x, 1)), Unew.view(x, 1))
        __V = torch.where(torch.less(torch.min(V[0, :]), torch.min(-V[0, :])),
                          -(Vnew.view(1, y)), Vnew.view(1, y))
        if rank > 2:
            for i in range(1, rank - 1):
                Unew = Unew.view(x, 1)
                Vnew = Vnew.view(1, y)
                __U = torch.where(
                    torch.less(torch.min(V[0, :]), torch.min(-V[0, :])),
                    torch.cat((__U, -Unew), dim=1),
                    torch.cat((__U, Unew), dim=1))
                __V = torch.where(
                    torch.less(torch.min(V[0, :]), torch.min(-V[0, :])),
                    torch.cat((__V, -Vnew), dim=0),
                    torch.cat((__V, Vnew), dim=0))

        if rank == 2:
            A = torch.cat((U, -U), dim=1)
        else:
            Un = torch.transpose(-(torch.sum(U, dim=1)), 0, -1).view(x, 1)
            A = torch.cat((U, Un), dim=1)

        B = torch.cat((V, torch.zeros((1, y))), dim=0)

        if rank >= 3:
            b, _ = torch.min(V, dim=0)
            B = torch.subtract(B, torch.minimum(torch.tensor(0.), b))
        else:
            B = torch.subtract(B, torch.minimum(torch.tensor(0.), V))
        x = torch.tensor(x)
        y = torch.tensor(y)
        normalize = torch.sqrt(torch.multiply(x, y).type(torch.FloatTensor))
        norm = torch.norm(A)

        return torch.multiply(torch.div(A, norm),
                              normalize), torch.div(torch.multiply(B, norm),
                                                    normalize)
Ejemplo n.º 6
0
def simple_loss(candidate, target, loss_weight, weights, balance, q_z, free_bits, beta_rate, global_step, max_beta, device, l1_barrier):
    '''
    A differentiable loss function

    =========== ARGUMENTS ===========  
    > candidate - averaged STFT of model output, truncated to same dimension as model input
    > target - model input (local_batch)
        Recall, the FFT is computed during dataset initialization, and the log attack time is appended then as well. Therefore,
        only the part of the input pertaining to the FFT (local_batch[:, :-1]) should be passed as the target.
    > loss_weight - the overarching weight for the loss. This could change each epoch, necessitating its input here.
    > balance - array. [MSE, KL, l1]
        weighs each individual loss type.

    ============ RETURNS ============
    > loss - overall loss for the batch
    > MSE - portion of the loss attributable to MSE between FFTs
        Probably going to remove this
    > l1 - portion of the loss attributable to l1 regularization
    '''
    batch_size = target.shape[0]

    # l1 loss
    # l1 = torch.sum(weights.abs()) * LAMBDA
    # x = batch_size/(torch.sum(torch.pow(weights, 2)))
    # l1 = (torch.log10(1 + torch.exp(x*2-l1_barrier)))
    l1 = torch.tensor(0)

    # MSE loss
    MSE = torch.sum(torch.pow(torch.subtract(candidate, target), 2))*(1 / torch.numel(target))

    # KL Divergence
    KL, bits = kl_loss_beta_vae(q_z, free_bits, beta_rate, global_step, max_beta, device)

    loss = l1*balance[2] + (MSE*balance[0])*loss_weight + KL*balance[1]
    return loss, MSE, l1, KL, bits
def compute_distances_two_loops(x_train, x_test):
    """
  Computes the squared Euclidean distance between each element of the training
  set and each element of the test set. Images should be flattened and treated
  as vectors.

  This implementation uses a naive set of nested loops over the training and
  test data.

  The input data may have any number of dimensions -- for example this function
  should be able to compute nearest neighbor between vectors, in which case
  the inputs will have shape (num_{train, test}, D); it should alse be able to
  compute nearest neighbors between images, where the inputs will have shape
  (num_{train, test}, C, H, W). More generally, the inputs will have shape
  (num_{train, test}, D1, D2, ..., Dn); you should flatten each element
  of shape (D1, D2, ..., Dn) into a vector of shape (D1 * D2 * ... * Dn) before
  computing distances.

  The input tensors should not be modified.

  NOTE: Your implementation may not use `torch.norm`, `torch.dist`,
  `torch.cdist`, or their instance method variants x.norm / x.dist / x.cdist.
  You may not use any functions from torch.nn or torch.nn.functional.

  Inputs:
  - x_train: Torch tensor of shape (num_train, D1, D2, ...)
  - x_test: Torch tensor of shape (num_test, D1, D2, ...)

  Returns:
  - dists: Torch tensor of shape (num_train, num_test) where dists[i, j] is the
    squared Euclidean distance between the ith training point and the jth test
    point. It should have the same dtype as x_train.
  """
    # Initialize dists to be a tensor of shape (num_train, num_test) with the
    # same datatype and device as x_train
    num_train = x_train.shape[0]
    num_test = x_test.shape[0]
    dists = x_train.new_zeros(num_train, num_test)
    ##############################################################################
    # TODO: Implement this function using a pair of nested loops over the        #
    # training data and the test data.                                           #
    #                                                                            #
    # You may not use torch.norm (or its instance method variant), nor any       #
    # functions from torch.nn or torch.nn.functional.                            #
    ##############################################################################
    # Replace "pass" statement with your code
    x_train = x_train.view(num_train, -1)
    x_test = x_test.view(num_test, -1)

    for i in range(num_train):
        for j in range(num_test):
            temp = torch.subtract(x_train[i], x_test[j])
            temp = torch.square(temp)
            temp = temp.sum()
            dists[i][j] = torch.sqrt(temp)

    ##############################################################################
    #                             END OF YOUR CODE                               #
    ##############################################################################
    return dists
Ejemplo n.º 8
0
def compute_norm():
    global norms
    block_norms = np.empty((0, time_steps))
    start = timer()
    for x in range(len(forward_batch)):
        step_norms = np.array([])
        for y in range(time_steps):
            print(str(x) + " , " + str(y))
            diff = torch.subtract(
                torch.from_numpy(forward_batch[x][y]),
                torch.from_numpy(backward_batch[len(forward_batch) - x -
                                                1][time_steps - y - 1]))
            diffnp = diff.numpy()
            diffrs = np.reshape(diffnp, (1, diffnp.size))
            diffvec = torch.from_numpy(diffrs)
            diffnorm = torch.norm(diffvec)
            step_norms = np.append(step_norms, [diffnorm.item()])
            print("step norm shape : " + str(step_norms.shape))
            print("step norm " + str(step_norms))
        block_norms = np.append(block_norms,
                                np.expand_dims(step_norms, axis=0),
                                axis=0)
        print("block norms shape : " + str(block_norms.shape))
        print("block norms " + str(block_norms))

    print("norms shape : " + str(norms.shape))
    print("norms " + str(norms))
    norms = np.append(norms, np.expand_dims(block_norms, axis=0), axis=0)
    compute_time = timer() - start
    print("compute norm time : " + str(compute_time))
Ejemplo n.º 9
0
    def loss_function(self,
                      neg_rat,
                      neg_prob,
                      pos_rat,
                      reduction=False,
                      weighted=False,
                      pos_rank=None,
                      **kwargs):
        """
        Bpr loss
        """
        pred = torch.subtract(pos_rat.unsqueeze(1), neg_rat)

        if weighted:
            importance = F.softmax(torch.negative(pred) - neg_prob, dim=1)
        else:
            importance = F.softmax(torch.ones_like(pred), dim=1)

        if pos_rank is not None:
            importance = importance * pos_rank

        weight_loss = torch.multiply(importance.detach(),
                                     torch.negative(F.logsigmoid(pred)))
        if reduction:
            return torch.sum(weight_loss, dim=-1).mean(-1)
        else:
            return torch.sum(weight_loss, dim=-1).sum(-1)
    def _calculate_loss(self, minibatch):
        # NOTE: when just training on a single example on each iteration, the NumPy array (and Torch tensor) still needs to have two dimensions: the mini-batch dimension, and the data dimension. And in this case, the mini-batch dimension would be 1, instead of 5. This can be done by using the torch.unsqueeze() function.
        # Convert the NumPy array into a Torch tensor
        #input is state position
        minibatch_states = minibatch[0]
        minibatch_rewards = minibatch[2]
        minibatch_actions = minibatch[1]
        minibatch_nextstates = minibatch[3]
        minibatch_states_tensor = torch.tensor(minibatch_states, dtype=torch.float32)
        minibatch_rewards_tensor = torch.tensor(minibatch_rewards, dtype=torch.float32)
        minibatch_actions_tensor = torch.tensor(minibatch_actions, dtype=torch.int64)
        minibatch_nextstates_tensor = torch.tensor(minibatch_nextstates, dtype=torch.float32)
        #print(minibatch_rewards_tensor)
        # Do a forward pass of the network using the inputs batch # NOTE: when training a Q-network, you will need to find the prediction for a particular action. This can be done using the "torch.gather()" function.
        state_q_values = self.q_network.forward(minibatch_states_tensor)
        nextstate_q_values_target = self.target_network.forward(minibatch_nextstates_tensor)

        nextstate_maxq_values_target = torch.max(nextstate_q_values_target,1)[0]
        #print(nextstate_maxq_values)
        nextstate_maxq_values_target = nextstate_maxq_values_target.detach()

        state_action_q_values = torch.gather(state_q_values,1,minibatch_actions_tensor.unsqueeze(-1)).squeeze(-1)
        #print(state_action_q_values)
        target_tensor = torch.add(minibatch_rewards_tensor, nextstate_maxq_values_target, alpha = 0.9)
        weight = torch.abs(torch.subtract(target_tensor, state_action_q_values))
        #print(target_tensor)
        #print(target_tensor.dtype)
        #raise TypeError("Stop")
        #nextstate_prediction = torch.gather(nextstate_q_values,1,minibatch_actions_tensor.unsqueeze(-1)).squeeze(-1)
        #print(network_prediction)
        # Compute the loss based on the label's batch
        loss = torch.nn.MSELoss()(target_tensor, state_action_q_values)
        #print(loss)
        return loss, delta
Ejemplo n.º 11
0
    def create_torch_stft(self, samples):
        '''
        samples is a pytorch tensor of dimensions (# samples, sample length)
        returns a tensor of dimension (# samples, frequency bins), as the time dimension is collapsed.
        '''
        # Create Spectrogram
        spec = torch.stft(samples, n_fft=2048, hop_length=512, normalized=True, return_complex=True).abs()
        
        # Collapse spectrogram by summing along the time axis
        spec = torch.sum(spec, dim=2)

        # Normalize
        spec_max = torch.max(spec, axis=1)[0].unsqueeze(1).repeat(1,spec.shape[1])
        spec_min = torch.min(spec, axis=1)[0].unsqueeze(1).repeat(1,spec.shape[1])
        spec = torch.subtract(spec, spec_min)
        spec = torch.divide(spec, torch.subtract(spec_max, spec_min))
        return spec
Ejemplo n.º 12
0
def tanimoto_loss(label, pred):
    square = torch.square(pred)
    sum_square = torch.sum(square)
    product = torch.multiply(pred, label)
    sum_product = torch.sum(product)
    denomintor = torch.subtract(torch.add(sum_square, 1), sum_product)
    loss = torch.divide(sum_product, denomintor)
    loss = torch.reduce_mean(loss)
    return 1.0 - loss
Ejemplo n.º 13
0
def std_var(input: Tensor,
            dim: DimOrDims = None,
            unbiased: Optional[bool] = False,
            *,
            keepdim: Optional[bool] = False,
            dtype: Optional[DType] = None,
            mask: Optional[Tensor] = None,
            take_sqrt: Optional[bool] = False) -> Tensor:
    if dtype is None:
        dtype = input.dtype
        if not (dtype.is_floating_point or dtype.is_complex):
            dtype = torch.float32
    compute_dtype = dtype
    if not (compute_dtype.is_floating_point or compute_dtype.is_complex):
        compute_dtype = torch.float32
    if input.layout == torch.strided:
        if mask is None:
            # TODO: compute count analytically
            count = sum(torch.ones(input.shape, dtype=torch.int64, device=input.device), dim, keepdim=True)
            sample_total = sum(input, dim, keepdim=True, dtype=dtype)
        else:
            inmask = _input_mask(input, mask=mask)
            count = sum(inmask.new_ones(input.shape, dtype=torch.int64), dim, keepdim=True, mask=inmask)
            sample_total = sum(input, dim, keepdim=True, dtype=dtype, mask=inmask)
        # TODO: replace torch.subtract/divide/square/maximum with
        # masked subtract/divide/square/maximum when these will be
        # available.
        sample_mean = torch.divide(sample_total, count)
        x = torch.subtract(input, sample_mean)
        if mask is None:
            total = sum(x * x.conj(), dim, keepdim=keepdim, dtype=compute_dtype)
        else:
            total = sum(x * x.conj(), dim, keepdim=keepdim, dtype=compute_dtype, mask=inmask)
        if not keepdim:
            count = count.reshape(total.shape)
        if unbiased:
            count = torch.subtract(count, 1)
            count = torch.maximum(count, count.new_zeros([]))
        output = torch.divide(total, count).to(dtype=dtype)
        if take_sqrt:
            output = torch.sqrt(output)
        return output
    else:
        raise ValueError(f'masked std/var expects strided tensor (got {input.layout} tensor)')
def smooth_l1(deltas, targets, sigma=3.0):

    sigma2 = sigma * sigma
    diffs = torch.subtract(deltas, targets)
    smooth_l1_signs = torch.less(torch.abs(diffs), 1.0 / sigma2).float32

    smooth_l1_option1 = torch.mul(diffs, diffs) * 0.5 * sigma2
    smooth_l1_option2 = torch.abs(Diffs) - 0.5 / sigma2
    smooth_l1_add = torch.mul(smooth_l1_option1, smooth_l1_signs) + torch.mul(
        smooth_l1_option2, 1 - smooth_l1_signs)

    smooth_l1 = smooth_l1_add

    return smooth_l1
def mb_outliers_mean_entropy(out_layer_1: torch.Tensor,
                             out_layer_2: torch.Tensor,
                             out_layer_3: torch.Tensor,
                             sample_size: int) -> np.array:
    """Combined uncertainty and diversity sampling technique from https://towardsdatascience.com/advanced-active-learning-cheatsheet-d6710cba7667"""

    # Part 1: pre-sample with least confidence

    # convert final layer to probabilities
    probabilities = torch.nn.functional.softmax(out_layer_3, dim=-1)
    # calculate entropy
    probslogs = probabilities * torch.log2(probabilities)
    summed = probslogs.sum(dim=1)
    entropy_scores = torch.subtract(torch.zeros_like(summed),
                                    summed) / np.log2(probslogs.size()[1])

    # define pre_sample_size
    pre_sample_size = 4 * sample_size
    # make sure sample size doesn't exceed scores
    pre_sample_size = np.min([len(entropy_scores), pre_sample_size])

    # pick k examples with highest uncertainty scores
    _, entropy_idxs = torch.topk(entropy_scores, pre_sample_size, largest=True)
    entropy_idxs = entropy_idxs.detach().cpu().numpy()

    # Part 2: sample from lease confidence pool using model based outliers

    # calculate mean activations for each layer's activations
    mean_layer_1 = torch.mean(out_layer_1, dim=-1)
    mean_layer_2 = torch.mean(out_layer_2, dim=-1)
    mean_layer_3 = torch.mean(out_layer_3, dim=-1)

    # get average scores across layers
    scores = (mean_layer_1 + mean_layer_2 + mean_layer_3) / 3

    # select most uncertain examples
    scores = scores[entropy_idxs]

    # make sure sample size doesn't exceed scores
    sample_size = np.min([len(scores), sample_size])

    # pick k examples with lowest activation scores
    _, idxs = torch.topk(scores, sample_size, largest=False)
    idxs = idxs.detach().cpu().numpy()

    return idxs
Ejemplo n.º 16
0
def rs_distance(z: Tensor, reg_weight: float):
    Nf = torch.tensor(z.shape[0],dtype=torch.float32)
    Df = torch.tensor(z.shape[1],dtype=torch.float32)

    f0 =(math.sqrt(2.)-1.)*torch.exp(torch.lgamma(Df/2.0+0.5)-torch.lgamma(Df/2.0))
            
    ddf0= torch.exp(torch.lgamma(.5+Df/2.)-torch.lgamma(1.+Df/2.))/math.sqrt(2.)
    c0=f0 - 1./ddf0 #first constant in function
    c1=1/(ddf0**2) #second constant

    dist_real_Z = euclidean_norm_squared(z, dim=1)
    distZZ = euclidean_norm_squared( torch.subtract(torch.unsqueeze(z, 0), torch.unsqueeze(z, 1)) , dim=2)    

    #set diag = 1 because of the singularity of sqrt'(x) in zero. This is a numerical
    #issue due to low precision of the GPU computations; it is compensated
    #exactly in the return value
    #distZZ = tf.matrix_set_diag(distZZ,tf.ones([ tf.stop_gradient(tf.shape(Z)[0]) ]),name=None) 
    distZZ=distZZ.fill_diagonal_(1.)
    smalleps=1e-4
    return reg_weight*( c0+torch.mean(torch.sqrt(dist_real_Z+c1)) - 0.5*torch.mean(torch.sqrt(distZZ+1e-6))+0.5/Nf+2.*math.sqrt(smalleps) )
def entropy_pt(predictions, sample_size: int) -> np.array:
    """Uncertainty sampling technique from https://towardsdatascience.com/uncertainty-sampling-cheatsheet-ec57bc067c0b"""

    # convert np array to torch tensor
    probabilities = torch.Tensor(predictions)

    # calculate entropy
    probslogs = probabilities * torch.log2(probabilities)
    summed = probslogs.sum(dim=1)
    scores = torch.subtract(torch.zeros_like(summed), summed) / np.log2(
        probslogs.size()[1])

    # make sure sample size doesn't exceed scores
    sample_size = np.min([len(scores), sample_size])

    # pick k examples with highest entropy scores
    _, idxs = torch.topk(scores, sample_size, largest=True)
    idxs = idxs.detach().cpu().numpy()

    return idxs
def entropy_mc(probabilities: torch.Tensor, sample_size: int) -> np.array:
    """Uncertainty sampling technique from https://towardsdatascience.com/uncertainty-sampling-cheatsheet-ec57bc067c0b
    adjusted for Bayesian Active Learning"""

    # calculate average probabilities from multiple inference runs
    probabilities = torch.mean(probabilities, dim=2)

    # calculate entropy
    probslogs = probabilities * torch.log2(probabilities)
    summed = probslogs.sum(dim=1)
    scores = torch.subtract(torch.zeros_like(summed), summed) / np.log2(
        probslogs.size()[1])

    # make sure sample size doesn't exceed scores
    sample_size = np.min([len(scores), sample_size])

    # pick k examples with highest entropy scores
    _, idxs = torch.topk(scores, sample_size, largest=True)
    idxs = idxs.detach().cpu().numpy()

    return idxs
Ejemplo n.º 19
0
    def validation_step(self, batch, batch_idx):
        ##### Validate images
        x, _ = batch
        z, latent_shape = self.encode_batch(x)
        x_hat = self.decode_latent(z, latent_shape)
        x_hat = torch.clip(x_hat, 0., 1.)
        ##### Get Losses
        elbo, kl, recon_lkh = self.compute_losses(x, z, x_hat)
        ##### Create dictionary
        val_dictionary = self.create_dictionary(elbo, kl, recon_lkh)
        ##### Include PSNR into dictionary
        res_orig_image = torch.round(255 * x[0])
        res_rec_image = torch.round(255 * x_hat[0])
        psnr = 10 * math.log10((255**2) / (torch.mean(
            torch.square(torch.subtract(res_orig_image, res_rec_image)))))
        val_dictionary.update({"psnr": psnr})
        ##### Save sample as example.
        if self.test_idx == batch_idx:
            self.orig_validation_sample = x[0]
            self.rec_validation_sample = x_hat[0]
            self.psnr_sample = psnr

        return val_dictionary
def linearmdpfrequency(mdp_data, p, initD):
    [states, actions, transitions] = mdp_data['sa_p'].size()
    D = torch.zeros(states, 1)
    diff = 1.0
    threshold = 0.00001

    while (diff >= threshold):
        # Dp = torch.tensor(D.clone().detach().requires_grad_(True), dtype=torch.float64)
        Dp = D
        #left_hold = torch.tensor(np.tile(p[...,None], (1,1,transitions)))
        #LHS = torch.mul(left_hold, mdp_data['sa_p'])
        #RHS = torch.tensor(np.tile(Dp[...,None], (1,actions,transitions)))
        #RHS = RHS0*mdp_data['discount']

        Dpi = torch.mul(
            torch.mul(torch.tensor(np.tile(p[..., None], (1, 1, transitions))),
                      mdp_data['sa_p'].type('torch.DoubleTensor')),
            torch.tensor(np.tile(Dp[..., None], (1, actions, transitions))) *
            mdp_data['discount'])

        D_CSR = sps.csc_matrix(
            (Dpi.transpose(1, 0).flatten(), mdp_data['sa_s'].transpose(
                1, 0).flatten().type('torch.DoubleTensor'),
             torch.arange(states * actions * transitions + 1)),
            shape=(states, states * actions * transitions))
        D_CSR.eliminate_zeros()
        D_mx = torch.tensor(D_CSR.todense()) @ torch.ones(
            states * actions * transitions, 1)
        D_s = torch.sum(D_mx, 1)
        D_s = D_s.view(len(D_s), 1)
        D = initD + D_s
        #D *= 1/D.max() #normalize between 0 and 1

        diff = torch.max(torch.abs(torch.subtract(D, Dp)))

    return D
Ejemplo n.º 21
0
    def walk_pair(model, label, z_mod1, z_mod2):
        walk_zs = []
        for i in range(DFLAGS.num_steps):
            im_noise = torch.randn_like(z_mod1).detach()

            im_noise.normal_()
            z_mod1 = z_mod1 + DFLAGS.step_noise * im_noise

            z_mod1.requires_grad_(requires_grad=True)

            energy_next_z1 = model.forward_top(z_mod1, label)
            gaussian_distance = torch.multiply(
                torch.tensor(-DFLAGS.step_valley_depth * model.depth_mod),
                torch.exp(
                    torch.divide(
                        torch.sum(torch.square(torch.subtract(z_mod1, z_mod2)),
                                  dim=model.embedded_dims),  # (1, 2, 3)
                        torch.tensor(-DFLAGS.step_valley_sigma *
                                     model.sigma_mod)).double()))

            if FLAGS.verbose > 1:
                print("step: ", i, energy_next_z1.mean(), "gd: ",
                      gaussian_distance.mean())

            im_grad1 = torch.autograd.grad(
                [torch.add(energy_next_z1.sum(), gaussian_distance.sum())],
                [z_mod1])[0]

            z_mod1 = z_mod1 - DFLAGS.step_lr * model.lr_mod * torch.clamp(
                im_grad1, -FLAGS.gradient_clip, FLAGS.gradient_clip)
            z_mod1 = z_mod1.detach()

            if FLAGS.plot_walks:
                walk_zs.append(z_mod1.cpu().numpy())

        return z_mod1, z_mod2, walk_zs
Ejemplo n.º 22
0
def attention_consistency_loss(attention_weights, reattention_weights):
    """Returns Attention consistency loss.

    Attention consistency loss is defined as the distance between the visual
    attention maps learned in the attention layer (learned from the fusion of
    the question and image features) and the attention map learned in the
    re-attention layer(learned from the fusion of the gleaned answer
    representation and image features). It is implemented here as defined in
    'Re-attention for Visual Question Answering' by Guo et al
    (https://ojs.aaai.org//index.php/AAAI/article/view/5338). Attention
    consistency loss is used to re-learn the importance of the visual features
    guided by which objects correspond to the learned answer,

    Args:
        attention_weights: visual attention weights learned in the attention
            layer, prior to obtaining the answer representation.
        reattention_weights: visual attention weights learned in the
            re-attention layer.

    Returns:
        attention consistency loss.
    """
    return torch.sum(
        torch.square(torch.subtract(attention_weights, reattention_weights)))
Ejemplo n.º 23
0
myspacers(mystr, len(mystr), "-")
print(b)
# c = a + b
c = torch.add(a, b)
mystr = f"------ c = a + b ------"
myspacers(mystr, len(mystr), "-")
print(c)

d = c * c
d = torch.multiply(c, c)
mystr = f"------ d = c * c ------"
myspacers(mystr, len(mystr), "-")
print(d)

e = d - b
e = torch.subtract(d, b)
mystr = f"------ e = d - b ------"
myspacers(mystr, len(mystr), "-")
print(e)

f = e / (b + 4)
f = torch.divide(e, (b + 4))
mystr = f"------ f = c / b(+4 ) ------"
myspacers(mystr, len(mystr), "-")
print(f)

# ==============================================================================

# %%
# ==============================================================================
# In place Operations ----------------------------------------------------------
Ejemplo n.º 24
0
def simple_loss(candidate, target, weight):
    MSE = (weight / torch.numel(target)) * torch.sum(
        torch.pow(torch.subtract(candidate, target), 2))
    return MSE
Ejemplo n.º 25
0
def cross_entropy(y_predicted, y_true):
    y_predicted = y_true * t.subtract(t.tensor(0), (t.log(y_predicted))) \
     + y_predicted * (1-y_true)
    return y_predicted
Ejemplo n.º 26
0
def subtract(addr, a, b):
    return torch.subtract(a, b)
Ejemplo n.º 27
0
def compute_log_pi(alpha_k):
    # Bishop eq 10.66

    alpha_hat = torch.sum(alpha_k)
    return torch.subtract(torch.digamma(alpha_k), torch.digamma(alpha_hat))
Ejemplo n.º 28
0
def calculate_euclidean_distance(feature_vector_1, feature_vector_2):
    return torch.sum(
        torch.square(torch.subtract(feature_vector_1,
                                    feature_vector_2)))**(0.5)
Ejemplo n.º 29
0
def resampler_with_unstacked_warp(
    data,
    warp_x,
    warp_y,
    safe=True,
):
    """Resamples input data at user defined coordinates.

  The resampler functions in the same way as `resampler` above, with the
  following differences:
  1. The warp coordinates for x and y are given as separate tensors.
  2. If warp_x and warp_y are known to be within their allowed bounds, (that is,
     0 <= warp_x <= width_of_data - 1, 0 <= warp_y <= height_of_data - 1) we
     can disable the `safe` flag.

  Args:
    data: Tensor of shape `[batch_size, data_height, data_width,
      data_num_channels]` containing 2D data that will be resampled.
    warp_x: Tensor of shape `[batch_size, dim_0, ... , dim_n]` containing the x
      coordinates at which resampling will be performed.
    warp_y: Tensor of the same shape as warp_x containing the y coordinates at
      which resampling will be performed.
    safe: A boolean, if True, warp_x and warp_y will be clamped to their bounds.
      Disable only if you know they are within bounds, otherwise a runtime
      exception will be thrown.
    name: Optional name of the op.

  Returns:
     Tensor of resampled values from `data`. The output tensor shape is
    `[batch_size, dim_0, ... , dim_n, data_num_channels]`.

  Raises:
    ValueError: If warp_x, warp_y and data have incompatible shapes.
  """

    # warp_x = torch.tensor(warp_x)
    # warp_y = torch.tensor(warp_y)
    # data = torch.tensor(data)
    if not warp_x.shape == warp_y.shape:
        raise ValueError(
            'warp_x and warp_y are of incompatible shapes: %s vs %s ' %
            (str(warp_x.shape), str(warp_y.shape)))
    warp_shape = torch.tensor(warp_x.shape).to(uflow_gpu_utils.device)

    if warp_x.shape[0] != data.shape[0]:
        raise ValueError(
            '\'warp_x\' and \'data\' must have compatible first '
            'dimension (batch size), but their shapes are %s and %s ' %
            (str(warp_x.shape[0]), str(data.shape[0])))
    # Compute the four points closest to warp with integer value.
    warp_floor_x = torch.floor(warp_x)
    warp_floor_y = torch.floor(warp_y)
    # Compute the weight for each point.
    right_warp_weight = warp_x - warp_floor_x
    down_warp_weight = warp_y - warp_floor_y

    warp_floor_x = warp_floor_x.type(torch.int32)
    warp_floor_y = warp_floor_y.type(torch.int32)
    warp_ceil_x = torch.ceil(warp_x).type(torch.int32)
    warp_ceil_y = torch.ceil(warp_y).type(torch.int32)

    left_warp_weight = torch.subtract(
        torch.tensor(1.0, dtype=right_warp_weight.dtype), right_warp_weight)
    up_warp_weight = torch.subtract(
        torch.tensor(1.0, dtype=down_warp_weight.dtype), down_warp_weight)

    # Extend warps from [batch_size, dim_0, ... , dim_n, 2] to
    # [batch_size, dim_0, ... , dim_n, 3] with the first element in last
    # dimension being the batch index.

    # A shape like warp_shape but with all sizes except the first set to 1:
    # warp_batch_shape = torch.cat(
    #     [warp_shape[0:1], torch.ones_like(warp_shape[1:])], 0)

    warp_batch = torch.arange(warp_shape[0], dtype=torch.int32).reshape(
        warp_shape[0:1],
        *torch.ones_like(warp_shape[1:]).tolist()).to(uflow_gpu_utils.device)

    # Broadcast to match shape:

    warp_batch = warp_batch + torch.zeros_like(warp_y, dtype=torch.int32)
    left_warp_weight = torch.unsqueeze(left_warp_weight, dim=1)
    down_warp_weight = torch.unsqueeze(down_warp_weight, dim=1)
    up_warp_weight = torch.unsqueeze(up_warp_weight, dim=1)
    right_warp_weight = torch.unsqueeze(right_warp_weight, dim=1)

    up_left_warp = torch.stack([warp_batch, warp_floor_y, warp_floor_x], dim=1)
    up_right_warp = torch.stack([warp_batch, warp_floor_y, warp_ceil_x], dim=1)
    down_left_warp = torch.stack([warp_batch, warp_ceil_y, warp_floor_x],
                                 dim=1)
    down_right_warp = torch.stack([warp_batch, warp_ceil_y, warp_ceil_x],
                                  dim=1)

    def gather(params, indices):
        return safe_gather_nd(params, indices) if safe else gather_nd(
            params, indices)

    # gather data then take weighted average to get resample result.

    result = (
        (gather(data, up_left_warp) * left_warp_weight +
         gather(data, up_right_warp) * right_warp_weight) * up_warp_weight +
        (gather(data, down_left_warp) * left_warp_weight +
         gather(data, down_right_warp) * right_warp_weight) * down_warp_weight)

    return result


# a = torch.arange(1*40*40*32, dtype = torch.float32).reshape(1,32,40,40)
# b = torch.arange(1*40*40*2, dtype = torch.float32).reshape(1,2,40,40)
#
# c = resampler(a,b)
# print(c.shape)
Ejemplo n.º 30
0
def subtract(alpha,a,b):
    return torch.subtract(a,b)