Recommand · November 24, 2021 0

Is there a way to optimize the calculation of Bernoulli Log-Likelihoods for many multivariate samples?

I currently have two Torch Tensors, p and x, which both have the shape of (batch_size, input_size).

I would like to calculate the Bernoulli log likelihoods for the given data, and return a tensor of size (batch_size)

Here’s an example of what I’d like to do:
I have the formula for log likelihoods of Bernoulli Random variables:

\sum_i^d x_{i} ln(p_i) + (1-x_i) ln (1-p_i)

Say I have p Tensor:

[[0.6 0.4 0],

[0.33 0.34 0.33]]

And say I have the x tensor for the binary inputs based on those probabilities:

[[1 1 0],

[0 1 1]]

And I want to calculate the log likelihood for every sample, which would result in:



Would it be possible to do this computation without the use of for loops?
I know I could use torch.sum(axis=1) to do the final summation between the logs, but is it possible to do the Bernoulli log-likelihood computation without the use of for loops? or use at most 1 for loop? I am trying to vectorize this operation as much as possible. I could’ve sworn we could use LaTeX for equations before, did something change or is it another website?

Thank you