Python code to convert 1D tensor to 2D tensor

Hello,

I am trying to use Binary Cross Entropy Loss (BCE loss) for Simese network.
I have two inputs for BCE loss function:

  1. output (input_dy) → tensor of size [4] , output of neural network
  2. true_labels (y_true) → tensor of size [4], target (true value)

For BCE loss, the input parameters must be of the dimension:

  1. output (input_dy) → [Batch_size, no. of classes]
  2. true_labels (y_true) → [Batch_size]

The following diagram explains the query:

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 & 1)

I need both output matrix and true_labels matrix for BCE Loss with following dimensions:

  1. output dimension → [4, 2]
  2. true_labels → [4]

Any help is most appreciated!
Thank you in advance. @ptrblck

There are many ways to do this, but here is a simple and straightforward version:

input_dy = torch.empty(dy.size(0), 2)
input_dy[:,0] = dy
input_dy[:,1] = 1-dy

y_true = torch.round(input_dy)

I don’t understand the second part of the question; is the “one-hot” encoding supposed to have a shape of [4] as well? If it was truly “one-hot” I would expect the shape to be [4, 2] as well. The posted code snippet implements a true-false encoding rather than a one-hot.

Yes, y_true should have the dimension [4].
y_true matrix should contain either 0 or 1, for e.g. y_true first element is 1 because it is the index of
input_dy maximum value, 0.6607 is greater than 0.3393, and index of 0.6607 is 1, so y_true is 1.
@eqy