我想知道如何通过Pytorch中的遮罩选择通道。 [channel1 channel2 channel3 channel4] x [1,0,0,1]-> [channel1,channel4] 我尝试了torch.masked_select(),但没有用。
如果输入的形状类似于[B,C,H,W]
,则输出的形状应为[B,masked_C,H,W]
,
import torch
from torch import nn
input = torch.randn((1,5,3,3))
pool = nn.AdaptiveAvgPool2d(1)
w = torch.sigmoid(pool(input)).view(1,-1)
mask = torch.gt(w,0.5)
print(input)
print(w)
print(mask)
输出如下:
tensor([[[[ 0.9129, -0.9763, 1.4460],
[ 0.3608, 0.5561, -1.4612],
[ 1.4953, -1.2474, 0.4069]],
[[-0.9121, 0.1261, 0.4661],
[-1.1624, -1.0266, -1.5419],
[ 1.0644, 1.0039, -0.4022]],
[[-1.8454, -0.2150, 2.3703],
[ 0.5224, 0.3366, 1.7545],
[-0.4624, 1.2639, 1.8032]],
[[-1.1558, -1.9985, -1.1336],
[-0.4400, -0.2092, 0.0677],
[-0.4172, -0.3614, -1.3193]],
[[-0.9441, -0.2944, 0.3381],
[ 1.6562, -0.5623, 0.0599],
[ 0.7229, 0.0472, -0.5122]]]])
tensor([[0.5414, 0.4341, 0.6489, 0.3156, 0.5142]])
tensor([[1, 0, 1, 0, 1]], dtype=torch.uint8)
我想要的结果是这样的:
tensor([[[[ 0.9129, -0.9763, 1.4460],
[ 0.3608, 0.5561, -1.4612],
[ 1.4953, -1.2474, 0.4069]],
[[-1.8454, -0.2150, 2.3703],
[ 0.5224, 0.3366, 1.7545],
[-0.4624, 1.2639, 1.8032]],
[[-0.9441, -0.2944, 0.3381],
[ 1.6562, -0.5623, 0.0599],
[ 0.7229, 0.0472, -0.5122]]]])
答案 0 :(得分:0)
我相信您可以做到:
import org.springframework.data.jpa.repository.Query;
顺便说一句。您无需先致电 //Controller
@yourMapping
public returnType method1()
{
myService.performBusinessLoginonMethod1(...) //if you are using services
myReposiroty.DBOpertaion(...) //as per above case
}
//Service: myService
returnType performBusinessLoginonMethod1(...)
{
myRepository.DBOpertaion(...)
}
//Repository: myRepository
@Modifying // import org.springframework.data.jpa.repository.Modifying;
@Query(...)
returnType DBOpertaion(...)
,然后再致电input[mask]
。您可以直接执行sigmoid
,而无需调用Sigmoid。