What is the use of Mask? [closed]

瘦欲@ 提交于 2019-12-25 20:01:49

问题


I often see use of masks (in C, C++, Python, Java), like masked softmax or something. I can't understand its use (I am not talking about masks in computer vision but about masks in programming in general).

I understood mask as something you used in a bitwise operation like x & 1 : here 1 is a mask that does something in the bitwise operation.

But, I see more and more usage of masks like below:

import theano
import theano.tensor as tt

    # The context document (2D matrix of long type called context.)
    context_bt = tt.lmatrix('context')

    # Context document mask used to distinguish real symbols from the sequence and padding symbols that are at the end.

    context_mask_bt = tt.matrix('context_mask')

I would greatly appreciate if you could help me in understanding:

  1. What are mask variables (like here why call the second var a mask, anything special here)?

  2. Where are they used?

  3. How can I create a mask variable?

Thanks, Cheers :).


回答1:


Rather than calling it just a mask, let's call it a bit mask as that's the full term.

A bit mask is usually used to examine if bits on an integer are set, or to set them. It allows people to do something of the form

if (time & CHEESE_MASK) eat()
if (time & CAKE_MASK) tea()

you may note here that it may be both time for cheese and cake in this example. This provides a very useful way of having a 'configuration' in a small space in memory, or in a nice easy to read format for a function argument - for example

shop (CHEESE_MASK | CAKE_MASK)

would suggest it's time to shop for cheese and cake.




回答2:


What are mask variables?

Bitwise AND and OR operation have following properties:

X & 1 == X
X & 0 == 0

So if you AND any bit with 1 it does not change it's value. If you AND any bit with 0 it resets to 0. So combining in one value ones and zeros and do operation AND you can reset some bits to 0, while others are untouched.

X | 1 == 1
X | 0 == X

On another side if you OR any bit with 1 it is set to 1. If you OR any bit with 0 it does not change it's value. So you can set some bits while keep other untouched.

Where are they used?

Combining multiple bits into one variable creates mask that you can use with AND to reset some bits or with OR to set them.

How can I create a mask variable?

You determine which bits you need to set or reset and just calculate it accordingly. Because not many languages support binary notation and it is too verbose they usually written in hex, as there is simple mapping from binary to hex and vice versa ( 4bits map to one hex digit).



来源:https://stackoverflow.com/questions/39980136/what-is-the-use-of-mask

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!