本文整理汇总了Python中torch.nn.functional.sigmoid方法的典型用法代码示例。如果您正苦于以下问题:Python functional.sigmoid方法的具体用法?Python functional.sigmoid怎么用?Python functional.sigmoid使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类torch.nn.functional
的用法示例。
在下文中一共展示了functional.sigmoid方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: node_forward
# 需要导入模块: from torch.nn import functional [as 别名]
# 或者: from torch.nn.functional import sigmoid [as 别名]
def node_forward(self, inputs, child_c, child_h):
child_h_sum = torch.sum(child_h, dim=0, keepdim=True)
iou = self.ioux(inputs) + self.iouh(child_h_sum)
i, o, u = torch.split(iou, iou.size(1) // 3, dim=1)
i, o, u = F.sigmoid(i), F.sigmoid(o), F.tanh(u)
f = F.sigmoid(
self.fh(child_h) +
self.fx(inputs).repeat(len(child_h), 1)
)
fc = torch.mul(f, child_c)
c = torch.mul(i, u) + torch.sum(fc, dim=0, keepdim=True)
h = torch.mul(o, F.tanh(c))
return c, h
示例2: forward
# 需要导入模块: from torch.nn import functional [as 别名]
# 或者: from torch.nn.functional import sigmoid [as 别名]
def forward(self, x, hidden):
# input shiddenape is N*C*H*W, C is 1
x = x.squeeze() # get rid of C
x = x.transpose(1,2).transpose(0,1) # make it W*N*H
r, hidden = self.gru(x, hidden)
r = r.transpose(1,0).transpose(2,1) # make it N*H*W
r = r.contiguous()
r = r.unsqueeze(1) # make it N*C*H*W, C is 1
#print(r.size())
r = self.conv(r)
#print(r.size())
r = self.avg(r)
#print(r.size())
r = r.view(r.size(0), -1)
#print(r.size())
r = self.fc(r)
return F.sigmoid(r), hidden
示例3: forward
# 需要导入模块: from torch.nn import functional [as 别名]
# 或者: from torch.nn.functional import sigmoid [as 别名]
def forward(self, x):
#print('1:', x.size())
x = self.conv1(x)
#print('2:', x.size())
x = self.rb1(x)
#print('3:', x.size())
x = self.mpool1(x)
#print('4:', x.size())
x = self.features(x)
#print('5:', x.size())
x = self.classifier(x)
#print('6:', x.size())
x = self.mpool2(x)
#print('7:', x.size())
x = x.view(x.size(0), -1)
#print('8:', x.size())
x = self.fc(x)
return F.sigmoid(x)
示例4: _concatenation
# 需要导入模块: from torch.nn import functional [as 别名]
# 或者: from torch.nn.functional import sigmoid [as 别名]
def _concatenation(self, x, g):
input_size = x.size()
batch_size = input_size[0]
assert batch_size == g.size(0)
# theta => (b, c, t, h, w) -> (b, i_c, t, h, w) -> (b, i_c, thw)
# phi => (b, g_d) -> (b, i_c)
theta_x = self.theta(x)
theta_x_size = theta_x.size()
# g (b, c, t', h', w') -> phi_g (b, i_c, t', h', w')
# Relu(theta_x + phi_g + bias) -> f = (b, i_c, thw) -> (b, i_c, t/s1, h/s2, w/s3)
phi_g = F.upsample(self.phi(g), size=theta_x_size[2:], mode=self.upsample_mode)
f = F.relu(theta_x + phi_g, inplace=True)
# psi^T * f -> (b, psi_i_c, t/s1, h/s2, w/s3)
sigm_psi_f = F.sigmoid(self.psi(f))
# upsample the attentions and multiply
sigm_psi_f = F.upsample(sigm_psi_f, size=input_size[2:], mode=self.upsample_mode)
y = sigm_psi_f.expand_as(x) * x
W_y = self.W(y)
return W_y, sigm_psi_f
示例5: _concatenation_debug
# 需要导入模块: from torch.nn import functional [as 别名]
# 或者: from torch.nn.functional import sigmoid [as 别名]
def _concatenation_debug(self, x, g):
input_size = x.size()
batch_size = input_size[0]
assert batch_size == g.size(0)
# theta => (b, c, t, h, w) -> (b, i_c, t, h, w) -> (b, i_c, thw)
# phi => (b, g_d) -> (b, i_c)
theta_x = self.theta(x)
theta_x_size = theta_x.size()
# g (b, c, t', h', w') -> phi_g (b, i_c, t', h', w')
# Relu(theta_x + phi_g + bias) -> f = (b, i_c, thw) -> (b, i_c, t/s1, h/s2, w/s3)
phi_g = F.upsample(self.phi(g), size=theta_x_size[2:], mode=self.upsample_mode)
f = F.softplus(theta_x + phi_g)
# psi^T * f -> (b, psi_i_c, t/s1, h/s2, w/s3)
sigm_psi_f = F.sigmoid(self.psi(f))
# upsample the attentions and multiply
sigm_psi_f = F.upsample(sigm_psi_f, size=input_size[2:], mode=self.upsample_mode)
y = sigm_psi_f.expand_as(x) * x
W_y = self.W(y)
return W_y, sigm_psi_f
示例6: forward
# 需要导入模块: from torch.nn import functional [as 别名]
# 或者: from torch.nn.functional import sigmoid [as 别名]
def forward(self, x):
device_id = x.get_device() if torch.cuda.is_available() else None
feature = self.dnn(x)
rows, cols = feature.size()[-2:]
cells = rows * cols
_feature = feature.permute(0, 2, 3, 1).contiguous().view(feature.size(0), cells, self.anchors.size(0), -1)
sigmoid = F.sigmoid(_feature[:, :, :, :3])
iou = sigmoid[:, :, :, 0]
ij = torch.autograd.Variable(utils.ensure_device(meshgrid(rows, cols).view(1, -1, 1, 2), device_id))
center_offset = sigmoid[:, :, :, 1:3]
center = ij + center_offset
size_norm = _feature[:, :, :, 3:5]
anchors = torch.autograd.Variable(utils.ensure_device(self.anchors.view(1, 1, -1, 2), device_id))
size = torch.exp(size_norm) * anchors
size2 = size / 2
yx_min = center - size2
yx_max = center + size2
logits = _feature[:, :, :, 5:] if _feature.size(-1) > 5 else None
return feature, iou, center_offset, size_norm, yx_min, yx_max, logits
示例7: _pos
# 需要导入模块: from torch.nn import functional [as 别名]
# 或者: from torch.nn.functional import sigmoid [as 别名]
def _pos(self, p):
pos_fn = self.pos_fn.lower()
if pos_fn == 'softmax':
p_sz = p.size()
p = p.view(p_sz[0],p_sz[1], -1)
p = F.softmax(p, -1)
return p.view(p_sz)
elif pos_fn == 'exp':
return torch.exp(p)
elif pos_fn == 'softplus':
return F.softplus(p, beta=10)
elif pos_fn == 'sigmoid':
return F.sigmoid(p)
else:
print('Undefined positive function!')
return
示例8: forward
# 需要导入模块: from torch.nn import functional [as 别名]
# 或者: from torch.nn.functional import sigmoid [as 别名]
def forward(self, inputs, dx_labels=None, rx_labels=None):
# inputs (B, 2, max_len)
# bert_pool (B, hidden)
_, dx_bert_pool = self.bert(inputs[:, 0, :], torch.zeros(
(inputs.size(0), inputs.size(2))).long().to(inputs.device))
_, rx_bert_pool = self.bert(inputs[:, 1, :], torch.zeros(
(inputs.size(0), inputs.size(2))).long().to(inputs.device))
dx2dx, rx2dx, dx2rx, rx2rx = self.cls(dx_bert_pool, rx_bert_pool)
# output logits
if rx_labels is None or dx_labels is None:
return F.sigmoid(dx2dx), F.sigmoid(rx2dx), F.sigmoid(dx2rx), F.sigmoid(rx2rx)
else:
loss = F.binary_cross_entropy_with_logits(dx2dx, dx_labels) + \
F.binary_cross_entropy_with_logits(rx2dx, dx_labels) + \
F.binary_cross_entropy_with_logits(dx2rx, rx_labels) + \
F.binary_cross_entropy_with_logits(rx2rx, rx_labels)
return loss, F.sigmoid(dx2dx), F.sigmoid(rx2dx), F.sigmoid(dx2rx), F.sigmoid(rx2rx)
示例9: inference
# 需要导入模块: from torch.nn import functional [as 别名]
# 或者: from torch.nn.functional import sigmoid [as 别名]
def inference(self, images=None, outputs=None, labels=None, **_):
if outputs is None:
assert images is not None
outputs = self.model(images)
num_outputs = LandmarkDetector.NUM_OUTPUTS
outputs = outputs.view(-1,num_outputs,self.num_anchors,self.feature_size,self.feature_size)
anchors = self._get_anchors()
B,C,A,H,W = outputs.size()
outputs = outputs.view(B,C,A*H*W)
anchors = torch.stack([anchors]*B, dim=0)
anchors = anchors.view(B,-1,A*H*W)
scores, indices = torch.max(outputs[:,0], dim=1)
outputs = outputs[torch.arange(B), :, indices]
anchors = anchors[torch.arange(B), :, indices]
boxes = self._targets_to_boxes(outputs[:,1:5], anchors)
landmarks = self._targets_to_landmarks(outputs[:,5:], anchors)
probabilities = F.sigmoid(scores)
return {'boxes': boxes, 'landmarks': landmarks, 'probabilities': probabilities}
示例10: generate_detections
# 需要导入模块: from torch.nn import functional [as 别名]
# 或者: from torch.nn.functional import sigmoid [as 别名]
def generate_detections(self, proposal_bboxes: Tensor, proposal_classes: Tensor, image_width: int, image_height: int) -> Tuple[Tensor, Tensor, Tensor, Tensor]:
batch_size = proposal_bboxes.shape[0]
#print("detection_bboxes:",proposal_bboxes)
detection_bboxes = BBox.clip(proposal_bboxes, left=0, top=0, right=image_width, bottom=image_height)
#print("detection_bboxes_clip:", detection_bboxes)
detection_probs = F.sigmoid(proposal_classes)
detection_zheng=detection_probs>=EvalConfig.KEEP
all_detection_classes=[]
all_detection_probs=[]
for label,prob in zip(detection_zheng,detection_probs):
detection_classes = []
detection_p=[]
for index,i in enumerate(label):
if i==1:
detection_classes.append(index)
detection_p.append(prob[index].item())
all_detection_classes.append(detection_classes)
all_detection_probs.append(detection_p)
#print('all_detection_classes:',all_detection_classes)
return detection_bboxes, all_detection_classes, all_detection_probs
示例11: forward
# 需要导入模块: from torch.nn import functional [as 别名]
# 或者: from torch.nn.functional import sigmoid [as 别名]
def forward(self, e1, rel):
e1_embedded_real = self.inp_drop(self.emb_e_real(e1)).view(Config.batch_size, -1)
rel_embedded_real = self.inp_drop(self.emb_rel_real(rel)).view(Config.batch_size, -1)
e1_embedded_img = self.inp_drop(self.emb_e_img(e1)).view(Config.batch_size, -1)
rel_embedded_img = self.inp_drop(self.emb_rel_img(rel)).view(Config.batch_size, -1)
e1_embedded_real = self.inp_drop(e1_embedded_real)
rel_embedded_real = self.inp_drop(rel_embedded_real)
e1_embedded_img = self.inp_drop(e1_embedded_img)
rel_embedded_img = self.inp_drop(rel_embedded_img)
# complex space bilinear product (equivalent to HolE)
realrealreal = torch.mm(e1_embedded_real*rel_embedded_real, self.emb_e_real.weight.transpose(1,0))
realimgimg = torch.mm(e1_embedded_real*rel_embedded_img, self.emb_e_img.weight.transpose(1,0))
imgrealimg = torch.mm(e1_embedded_img*rel_embedded_real, self.emb_e_img.weight.transpose(1,0))
imgimgreal = torch.mm(e1_embedded_img*rel_embedded_img, self.emb_e_real.weight.transpose(1,0))
pred = realrealreal + realimgimg + imgrealimg - imgimgreal
pred = F.sigmoid(pred)
return pred
示例12: process
# 需要导入模块: from torch.nn import functional [as 别名]
# 或者: from torch.nn.functional import sigmoid [as 别名]
def process(eval_img, device='cpu'):
(img, origin, unpadder), file_name = eval_img
with torch.no_grad():
out = model(img.to(device))
prob = F.sigmoid(out)
mask = prob > 0.5
mask = torch.nn.MaxPool2d(kernel_size=(3, 3), padding=(1, 1), stride=1)(mask.float()).byte()
mask = unpadder(mask)
mask = mask.float().cpu()
save_image(mask, file_name + ' _mask.jpg')
origin_np = np.array(to_pil_image(origin[0]))
mask_np = to_pil_image(mask[0]).convert("L")
mask_np = np.array(mask_np, dtype='uint8')
mask_np = draw_bounding_box(origin_np, mask_np, 500)
mask_ = Image.fromarray(mask_np)
mask_.save(file_name + "_contour.jpg")
# ret, mask_np = cv2.threshold(mask_np, 127, 255, 0)
# dst = cv2.inpaint(origin_np, mask_np, 1, cv2.INPAINT_NS)
# out = Image.fromarray(dst)
# out.save(file_name + ' _box.jpg')
示例13: forward
# 需要导入模块: from torch.nn import functional [as 别名]
# 或者: from torch.nn.functional import sigmoid [as 别名]
def forward(self, x):
x1 = self.conv1(x)
x2 = self.mp1(x1)
x3 = self.conv2(x2)
x4 = self.mp2(x3)
x5 = self.conv3(x4)
x6 = self.mp3(x5)
# Bottom
x7 = self.conv4(x6)
# Up-sampling
x8 = self.up1(x7, x5)
x9 = self.up2(x8, x3)
x10 = self.up3(x9, x1)
x11 = self.conv9(x10)
preds = F.sigmoid(x11)
return preds
示例14: forward
# 需要导入模块: from torch.nn import functional [as 别名]
# 或者: from torch.nn.functional import sigmoid [as 别名]
def forward(self, obs_variable, actions_variable, target):
"""
Compute the cross entropy loss using the logit, this is more numerical
stable than first apply sigmoid function and then use BCELoss.
As in discriminator, we only want to discriminate the expert from
learner, thus this is a binary classification problem.
Parameters
----------
obs_variable (Variable): state wrapped in Variable
actions_variable (Variable): action wrapped in Variable
target (Variable): 1 or 0, mark the real and fake of the
samples
Returns
-------
loss (Variable):
"""
logits = self.get_logits(obs_variable, actions_variable)
loss_fn = nn.BCEWithLogitsLoss()
loss = loss_fn(logits, target)
return loss
示例15: prediction
# 需要导入模块: from torch.nn import functional [as 别名]
# 或者: from torch.nn.functional import sigmoid [as 别名]
def prediction(self, observation, action):
"""
Make the prediction of the class label
Parameters
----------
observation (numpy.ndarray): state
action (numpy.ndarray): action
Returns
-------
prob (numpy.ndarray):
"""
obs_variable = Variable(torch.from_numpy(observation),
volatile=True).type(torch.FloatTensor)
# obs_variable sets volatile to True, thus, we do not set
# it here
action_variable = Variable(torch.from_numpy(action)).type(
torch.FloatTensor)
logits = self.get_logits(obs_variable, action_variable)
probs = F.sigmoid(logits)
return probs.data.numpy()