Skip to content

Notebook 03: What is the dimension that I should pass to softmax? #465

Answered by mrdbourke
vence-andersen asked this question in Q&A
Discussion options

You must be logged in to vote

Hi @vence-andersen ,

You'll want to perform the softmax operation on the logits dimension.

For example, with batch_size=1, you can perform it on dim=0 (there is only one value).

But with batch_size=32, you'll want to perform it across dim=1 (assuming your tensor shape is [batch_size, logits]).

Changing the softmax code in Notebook 03 to in the make_predictions() function from dim=0 to dim=1 will error:

def make_predictions(model: torch.nn.Module, data: list, device: torch.device = device):
    pred_probs = []
    model.eval()
    with torch.inference_mode():
        for sample in data:
            # Prepare sample
            sample = torch.unsqueeze(sample, dim=0).to(device) # Add an ext…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by vence-andersen
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants