translate from pyrotch to tensorflow
What does this line of code in PyTorch do?

normA = A.mul(A).sum(dim=1).sum(dim=1).sqrt()
Y = A.div(normA.view(batchSize, 1, 1).expand_as(A))


Normally it should be a second term like this:

torch.div(input, value, out=None) → Tensor


Your question is a little unclear because you didn't mention what is the shape of tensor A and what is normA. But I guess the following:


A is a tensor of shape (batchSize, X, Y)
normA is a tensor of norms of all batch elements of A and its' shape is (batchSize).


So, you normalize the tensor A with the following statement.

A.div(normA.view(batchSize, 1, 1).expand_as(A))


Where, normA.view(batchSize, 1, 1).expand_as(A) is first converted into a tensor of shape (batchSize, X, Y) and then you divide A by the resulting tensor.

An example (created from my guess):

batchSize = 8

A = torch.randn(batchSize, 5, 5)
normA = A.norm(dim=-1).norm(dim=-1)
print(normA.size()) # torch.Size([8])

normA = normA.view(batchSize, 1, 1).expand_as(A)
print(normA.size()) # torch.Size([8, 5, 5])

A = A.div(normA)

