Skip to content
Advertisement

Tag: array-broadcasting

np.where for 2d array, manipulate whole rows

I want to rebuild the following logic with numpy broadcasting function such as np.where: From a 2d array check per row if the first element satisfies a condition. If the condition is true then return the first three elements as a row, else the last three elements. A short MWE in form of a for-loop which I want to circumvent:

How does pytorch broadcasting work?

produces a Tensor with size: torch.Size([4,4]). Can someone provide a logic behind this? Answer PyTorch broadcasting is based on numpy broadcasting semantics which can be understood by reading numpy broadcasting rules or PyTorch broadcasting guide. Expounding the concept with an example would be intuitive to understand it better. So, please see the example below: Now for torch.add(t_rand, t_ones), visualize it

Advertisement