Python code to convert 1D tensor to 2D tensor
I am trying to use Binary Cross Entropy Loss (BCE loss) for Simese network.
I have two inputs for BCE loss function:
output (input_dy) → tensor of size [4] , output of neural network
true_labels (y_true) → tensor of size [4], target (true value)

For BCE loss, the input parameters must be of the dimension:
output (input_dy) → [Batch_size, no. of classes]
true_labels (y_true) → [Batch_size]

The following diagram explains the query:enter image description here
I need a function in python using pytorch to convert the dy matrix to a 2D matrix with the output probabilities that sum to 1. [To note: dy should be iterated through length of it, as it is the output of the network for every input ]
Further a 2D array must be represented into one hot encoding, which will be true_labels (that will represent Binary classes with 0 &amp; 1)
I need both output matrix and true_labels matrix for BCE Loss with following dimensions:
output dimension → [4, 2]
true_labels → [4]

Any help is most appreciated!
Thank you in advance.

It looks to me that your output is binary so you don't really need a 2D matrix for that task.
Also, I'm not quite sure that the BCE Loss (nor BCEWithLogits) requires tensors of different dimensions, they should both have shape (N, *) as far as I know.
Apart from that, for the sake of the question: if you have p(x), you can obtain the other column by simply computing 1 - p(x).
There are many ways to obtain that, a method could be:
# suppose we have a tensor/batch of probabilities
a = torch.tensor([0.4691, 0.9589, 0.7529, 0.9564])
# this gives a 2D matrix with two columns, (1 - p), p
b = torch.stack((1 - p, p), dim=-1)

And that's it!
