面膜有什么用? [英] What is the use of Mask?

查看:46
本文介绍了面膜有什么用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经常看到使用掩码(在 C、C++、Python、Java 中),例如掩码 softmax 或其他东西.我无法理解它的用途(我不是在谈论计算机视觉中的掩码,而是一般编程中的掩码).

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).

我将掩码理解为您在诸如 x & 之类的按位运算中使用的东西.1 :这里的 1 是一个掩码,它在按位运算中执行某些操作.

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. 什么是掩码变量(就像这里为什么将第二个变量称为掩码,这里有什么特别之处)?

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

它们在哪里使用?

如何创建掩码变量?

谢谢,干杯:)

推荐答案

什么是掩码变量?

What are mask variables?

按位 AND 和 OR 运算具有以下特性:

Bitwise AND and OR operation have following properties:

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

因此,如果您 AND 任何位与 1 都不会改变它的值.如果你 AND 任何位与 0 它重置为 0.因此结合一个值的 1 和 0 并执行操作 AND 你可以将一些位重置为 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

另一方面,如果您将任何位与 1 OR 设置为 1.如果您将任何位 OR 与 0 进行OR,它不会改变它的值.所以你可以设置一些位,同时保持其他位不变.

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.

它们在哪里使用?

将多个位组合成一个变量会创建掩码,您可以将其与 AND 一起使用以重置某些位或与 OR 一起使用以设置它们.

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

如何创建掩码变量?

您可以确定需要设置或重置的位,然后相应地进行计算.因为支持二进制符号的语言并不多,而且它们通常用十六进制编写过于冗长,因为从二进制到十六进制的映射很简单,反之亦然(4 位映射到一个十六进制数字).

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).

这篇关于面膜有什么用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆