Как уменьшить среднее значение игнорирования дополненных строк в 3D-тензоре ⇐ Python
Как уменьшить среднее значение игнорирования дополненных строк в 3D-тензоре
I have and a 3D tensor A has shape (batch_size, N, dim) and a 3D tensor B has shape (batch_size, N, 2). In which B has some padding row to fill to N (which is not a zero vector, as it already passed in some functions). To know which row is padded, I have to look up tensor B, if row k is padded, the value at the k-row in tensor B is [0, 0]. I want to filter out these padded rows in A before calculating the mean.
After reducing A to its mean along dim=1, the result has a shape of (batch_size, dim).
Edit: I figured out one solution. Any other solution is welcome!
# squeeze to 2D A = A.view(-1, A.size(-1)) B = B.view(-1, B.size(-1)) # mask out padded row mask = torch.sum(B, dim=-1) mask[mask!=0] = 1 # the point is I need to assign padded row in A by zero A = mask.unsqueeze(-1)*A # calculate the real size of each cluster. mask = mask.view(batch_size, -1) batch_cluster_size = torch.sum(mask, dim=-1, keepdim=True) # convert back to original shape A = A.view(batch_size, -1, A.size(-1)) output = torch.sum(A, dim=1)/batch_cluster_size
Источник: https://stackoverflow.com/questions/780 ... -3d-tensor
I have and a 3D tensor A has shape (batch_size, N, dim) and a 3D tensor B has shape (batch_size, N, 2). In which B has some padding row to fill to N (which is not a zero vector, as it already passed in some functions). To know which row is padded, I have to look up tensor B, if row k is padded, the value at the k-row in tensor B is [0, 0]. I want to filter out these padded rows in A before calculating the mean.
After reducing A to its mean along dim=1, the result has a shape of (batch_size, dim).
Edit: I figured out one solution. Any other solution is welcome!
# squeeze to 2D A = A.view(-1, A.size(-1)) B = B.view(-1, B.size(-1)) # mask out padded row mask = torch.sum(B, dim=-1) mask[mask!=0] = 1 # the point is I need to assign padded row in A by zero A = mask.unsqueeze(-1)*A # calculate the real size of each cluster. mask = mask.view(batch_size, -1) batch_cluster_size = torch.sum(mask, dim=-1, keepdim=True) # convert back to original shape A = A.view(batch_size, -1, A.size(-1)) output = torch.sum(A, dim=1)/batch_cluster_size
Источник: https://stackoverflow.com/questions/780 ... -3d-tensor
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение