#NEWLINE# def lwta( p , block_size ) : #NEWLINE# #INDENT# '\n    Apply hard local winner-take-all on every rows of a theano matrix.\n\n    Parameters\n    ----------\n    p: theano matrix\n        Matrix on whose rows LWTA will be applied.\n    block_size: int\n        Number of units in each block.\n    ' #NEWLINE# batch_size = p . shape [ 0 ] #NEWLINE# num_filters = p . shape [ 1 ] #NEWLINE# num_blocks = ( num_filters // block_size ) #NEWLINE# w = p . reshape ( ( batch_size , num_blocks , block_size ) ) #NEWLINE# block_max = ( w . max ( axis = 2 ) . dimshuffle ( 0 , 1 , 'x' ) * T . ones_like ( w ) ) #NEWLINE# max_mask = T . cast ( ( w >= block_max ) , 'float32' ) #NEWLINE# indices = numpy . array ( range ( 1 , ( block_size + 1 ) ) ) #NEWLINE# max_mask2 = ( max_mask * indices ) #NEWLINE# block_max2 = ( max_mask2 . max ( axis = 2 ) . dimshuffle ( 0 , 1 , 'x' ) * T . ones_like ( w ) ) #NEWLINE# max_mask3 = T . cast ( ( max_mask2 >= block_max2 ) , 'float32' ) #NEWLINE# w2 = ( w * max_mask3 ) #NEWLINE# w3 = w2 . reshape ( ( p . shape [ 0 ] , p . shape [ 1 ] ) ) #NEWLINE# return w3