How can I normalize after masking?

Suppose I have a tensor like [0.6, 0.7, 0.4] and a mask like: [1, 0,0] How can I normalize it to: [1,0,0] my try: normalized_attn_scores = F.softmax(attn_scores, 1) normalized_attn_scores = normalized_attn_scores.mul(attn_mask) But it doesn’t produce the desired output >Solution : You can normalize after masking by dividing the masked tensor by its sum, like this:… Read More How can I normalize after masking?

Understanding gradient computation using backward() in PyTorch

I’m trying to understand the basic pytorch autograd system: x = torch.tensor(10., requires_grad=True) print(‘tensor:’,x) x.backward() print(‘gradient:’,x.grad) output: tensor: tensor(10., requires_grad=True) gradient: tensor(1.) since x is a scalar constant and no function is applied to it, I expected 0. as the gradient output. Why is the gradient 1. instead? >Solution : Whenever you are using value.backward(),… Read More Understanding gradient computation using backward() in PyTorch

Is it possible to auto-size the subsequent input of a layer following torch.nn.Flatten within torch.nn.Sequential in PyTorch?

If I have the following model class for example: class MyTestModel(nn.Module): def __init__(self): super(MyTestModel, self).__init__() self.seq1 = nn.Sequential( nn.Conv2d(3, 6, 3), nn.MaxPool2d(2, 2), nn.Conv2d(6, 16, 3), nn.MaxPool2d(2, 2), nn.Flatten(), nn.Linear(myflattendinput(), 120), # how to automate this? nn.ReLU(), nn.Linear(120, 84), nn.ReLU(), nn.Linear(84, 2), ) self.softmax = nn.Softmax(dim=1) def forward(self, x): x = self.seq1(x) x = self.softmax(x)… Read More Is it possible to auto-size the subsequent input of a layer following torch.nn.Flatten within torch.nn.Sequential in PyTorch?

Understanding broadcasting and arithmetic operations on different dimension tensors

I’m currently working on computing various similarity metrics between vectors such as cosine similarity, euclidean distance, mahalanobis distance, etc. As I’m working with vectors that can be very large, I need compute time to be minimal. I’m struggling to understand how to work with vectors of different dimensions (they, do, however, share one dimension) and… Read More Understanding broadcasting and arithmetic operations on different dimension tensors