def lwta(p, block_size):
 batch_size = p.shape[0]
 num_filters = p.shape[1]
 num_blocks = (num_filters // block_size)
 w = p.reshape((batch_size, num_blocks, block_size))
 block_max = (w.max(axis=2).dimshuffle(0, 1, 'x') * T.ones_like(w))
 max_mask = T.cast((w >= block_max), 'float32')
 indices = numpy.array(range(1, (block_size + 1)))
 max_mask2 = (max_mask * indices)
 block_max2 = (max_mask2.max(axis=2).dimshuffle(0, 1, 'x') * T.ones_like(w))
 max_mask3 = T.cast((max_mask2 >= block_max2), 'float32')
 w2 = (w * max_mask3)
 w3 = w2.reshape((p.shape[0], p.shape[1]))
 return w3

def lwta ( p , block_size ) : batch_size = p . shape [ 0 ] num_filters = p . shape [ 1 ] num_blocks = ( num_filters // block_size ) w = p . reshape ( ( batch_size , num_blocks , block_size ) ) block_max = ( w . max ( axis = 2 ) . dimshuffle ( 0 , 1 , 'x' ) * T . ones_like ( w ) ) max_mask = T . cast ( ( w >= block_max ) , 'float32' ) indices = numpy . array ( range ( 1 , ( block_size + 1 ) ) ) max_mask2 = ( max_mask * indices ) block_max2 = ( max_mask2 . max ( axis = 2 ) . dimshuffle ( 0 , 1 , 'x' ) * T . ones_like ( w ) ) max_mask3 = T . cast ( ( max_mask2 >= block_max2 ) , 'float32' ) w2 = ( w * max_mask3 ) w3 = w2 . reshape ( ( p . shape [ 0 ] , p . shape [ 1 ] ) ) return w3
apply hard local winner-take-all on every rows of a theano matrix .