[Python] How to divide 2d matrix according to specified position

Discussão em 'Python' iniciado por Stack, Dezembro 16, 2024.

  1. Stack

    Stack Membro Participativo

    I have a feature map that needs to be divied with another 2d constant, the feature map has shape [H, W, ...] , the 2d constant has shape [H, W] , however when I use np.divide it says these two tensor can not be broadcast to a common shape:

    import numpy as np

    a = np.random.rand(4, 3 ,3)
    b = np.random.rand(4, 3)

    c = np.divide(a, b, where=b > 0)


    as the numpy broadcast rules say b will be broadcast to shape [1, 4, 3] instead of [4, 3, 1] , thus I can't do this operation with boradcast.

    I would like to divide my feature map with that constant tensor where the constant is not 0 to avoid zero-div error, what should I do ?

    Continue reading...

Compartilhe esta Página