M[M > 0.001] = 1.0 M[M < 0.5] = 0.0
时间: 2024-04-19 08:27:45 浏览: 21
这段代码对名为M的张量进行操作。首先,通过M > 0.001生成一个布尔型的掩码张量,其中大于0.001的元素为True,小于等于0.001的元素为False。然后,将这个掩码张量应用到M上,将满足条件的元素赋值为1.0。接着,通过M < 0.5生成另一个布尔型的掩码张量,其中小于0.5的元素为True,大于等于0.5的元素为False。最后,将这个掩码张量应用到M上,将满足条件的元素赋值为0.0。这样,代码就完成了将M中大于0.001的元素设为1.0,小于0.5的元素设为0.0的操作。
相关问题
将 czm_material czm_getMaterial(czm_materialInput materialInput) { czm_material material = czm_getDefaultMaterial(materialInput); material.diffuse = 1.5 * color.rgb; vec2 st = materialInput.st; vec3 str = materialInput.str; float dis = distance(st, vec2(0.5, 0.5)); float per = fract(time); if (abs(str.z) > 0.001) { discard; } if (dis > 0.5) { discard; } else { float perDis = 0.5 / count; float disNum; float bl = .0; for (int i = 0; i <= 9; i++) { if (float(i) <= count) { disNum = perDis *float(i) - dis + per / count; if (disNum > 0.0) { if (disNum < perDis) { bl = 1.0 - disNum / perDis; } else if(disNum - perDis < perDis) { bl = 1.0 - abs(1.0 - disNum / perDis); } material.alpha = pow(bl, gradient); } } } } return material; } 翻译成成cesium webgl2标准
Here is the translation of the code into Cesium WebGL2 standard:
```javascript
function czm_getMaterial(materialInput) {
var material = czm_getDefaultMaterial(materialInput);
material.diffuse = color.rgb.multiply(1.5);
var st = materialInput.st;
var str = materialInput.str;
var dis = Cesium.distance(st, new Cesium.Cartesian2(0.5, 0.5));
var per = Cesium.fract(time);
if (Math.abs(str.z) > 0.001) {
discard;
}
if (dis > 0.5) {
discard;
} else {
var perDis = 0.5 / count;
var disNum;
var bl = 0.0;
for (var i = 0; i <= 9; i++) {
if (i <= count) {
disNum = perDis * i - dis + per / count;
if (disNum > 0.0) {
if (disNum < perDis) {
bl = 1.0 - disNum / perDis;
} else if (disNum - perDis < perDis) {
bl = 1.0 - Math.abs(1.0 - disNum / perDis);
}
material.alpha = Math.pow(bl, gradient);
}
}
}
}
return material;
}
```
class DropBlock_Ske(nn.Module): def __init__(self, num_point, block_size=7): super(DropBlock_Ske, self).__init__() self.keep_prob = 0.0 self.block_size = block_size self.num_point = num_point self.fc_1 = nn.Sequential( nn.Linear(in_features=25, out_features=25, bias=True), nn.ReLU(inplace=True), nn.Linear(in_features=25, out_features=25, bias=True), ) self.fc_2 = nn.Sequential( nn.Linear(in_features=25, out_features=25, bias=True), nn.ReLU(inplace=True), nn.Linear(in_features=25, out_features=25, bias=True), ) self.sigmoid = nn.Sigmoid() def forward(self, input, keep_prob, A): # n,c,t,v self.keep_prob = keep_prob if not self.training or self.keep_prob == 1: return input n, c, t, v = input.size() input_attention_mean = torch.mean(torch.mean(input, dim=2), dim=1).detach() # 32 25 input_attention_max = torch.max(input, dim=2)[0].detach() input_attention_max = torch.max(input_attention_max, dim=1)[0] # 32 25 avg_out = self.fc_1(input_attention_mean) max_out = self.fc_2(input_attention_max) out = avg_out + max_out input_attention_out = self.sigmoid(out).view(n, 1, 1, self.num_point) input_a = input * input_attention_out input_abs = torch.mean(torch.mean( torch.abs(input_a), dim=2), dim=1).detach() input_abs = input_abs / torch.sum(input_abs) * input_abs.numel() gamma = 0.024 M_seed = torch.bernoulli(torch.clamp( input_abs * gamma, min=0, max=1.0)).to(device=input.device, dtype=input.dtype) M = torch.matmul(M_seed, A) M[M > 0.001] = 1.0 M[M < 0.5] = 0.0 mask = (1 - M).view(n, 1, 1, self.num_point) return input * mask * mask.numel() / mask.sum()
这段代码定义了一个名为DropBlock_Ske的PyTorch模型类。该类继承自nn.Module基类,并实现了模型的前向传播逻辑。
在初始化方法中,定义了一些模型的属性,包括keep_prob、block_size、num_point等。然后,定义了两个全连接网络层fc_1和fc_2,并使用nn.Sequential组织网络层的结构。最后,使用nn.Sigmoid定义了一个Sigmoid激活函数。
在前向传播方法中,首先根据输入的keep_prob值判断是否需要执行DropBlock操作,如果不需要则直接返回输入。接着,获取输入张量的形状,并计算出输入张量的均值和最大值。通过两个全连接网络层对均值和最大值进行处理,将处理结果相加得到out。然后,使用Sigmoid激活函数对out进行处理,得到一个形状为(n, 1, 1, num_point)的张量input_attention_out。将input_attention_out与输入张量input相乘得到input_a。
接下来,计算input_a的绝对值的平均值,并将其除以总数并乘以元素个数,得到一个形状为(n,)的张量input_abs。定义了一个gamma值,并将input_abs与gamma相乘并经过torch.clamp函数进行限制,再经过torch.bernoulli函数进行伯努利采样,得到一个形状与输入相同的二值张量M_seed。使用torch.matmul函数将M_seed与A矩阵相乘得到M。
然后,将M中大于0.001的元素赋值为1.0,小于0.5的元素赋值为0.0。接着,将1减去M得到mask,将mask乘以输入张量input,并除以mask中的元素个数与总和,得到最终的输出张量。
这个模型类实现了DropBlock_Ske操作,其中包含了一些全连接网络层和一些基于概率的操作。它的具体功能和用途可能需要根据上下文来确定。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)