本文整理汇总了Python中torch.sigmoid函数的典型用法代码示例。如果您正苦于以下问题:Python sigmoid函数的具体用法?Python sigmoid怎么用?Python sigmoid使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了sigmoid函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: forward_flow
def forward_flow(self, z, xenc):
B = z.shape[0]
C = z.shape[1]
f = self.flows
logdet = 0.
for i in range(self.n_flows):
z = z[:,f[str(i)]['perm']]
z1 = z[:,:C//2]
z2 = z[:,C//2:]
sig2 = torch.sigmoid(f[str(i)]['f1_sig'](torch.cat([z2,xenc],1)))
mu2 = f[str(i)]['f1_mu'](torch.cat([z2,xenc],1))
z1 = z1*sig2 + mu2
mu1 = f[str(i)]['f2_mu'](torch.cat([z1,xenc],1))
sig1 = torch.sigmoid(f[str(i)]['f2_sig'](torch.cat([z1,xenc],1)))
z2 = z2*sig1 + mu1
z = torch.cat([z1,z2],1)
sig1 = sig1.view(B, -1)
sig2 = sig2.view(B, -1)
logdet += torch.sum(torch.log(sig1), 1)
logdet += torch.sum(torch.log(sig2), 1)
return z, logdet
示例2: reverse_flow
def reverse_flow(self, z):
B = z.shape[0]
C = z.shape[1]
f = self.flows
logdet = 0.
reverse_ = list(range(self.n_flows))[::-1]
for i in reverse_:
z1 = z[:,:C//2]
z2 = z[:,C//2:]
sig1 = torch.sigmoid(f[str(i)]['f2_sig'](z1))
mu1 = f[str(i)]['f2_mu'](z1)
z2 = (z2 - mu1) / sig1
sig2 = torch.sigmoid(f[str(i)]['f1_sig'](z2))
mu2 = f[str(i)]['f1_mu'](z2)
z1 = (z1 - mu2) / sig2
z = torch.cat([z1,z2],1)
z = z[:,f[str(i)]['inv_perm']]
sig1 = sig1.view(B, -1)
sig2 = sig2.view(B, -1)
logdet += torch.sum(torch.log(sig1), 1)
logdet += torch.sum(torch.log(sig2), 1)
return z, logdet
示例3: forward
def forward(self, input_, hx):
"""
Args:
input_: A (batch, input_size) tensor containing input
features.
hx: A tuple (h_0, c_0), which contains the initial hidden
and cell state, where the size of both states is
(batch, hidden_size).
time: The current timestep value, which is used to
get appropriate running statistics.
Returns:
h_1, c_1: Tensors containing the next hidden and cell state.
"""
h_0, c_0 = hx
batch_size = h_0.size(0)
bias_batch = (self.bias.unsqueeze(0)
.expand(batch_size, *self.bias.size()))
wh = torch.mm(h_0, self.weight_hh)
wh = torch.mm(h_0, self.weight_hh)
wi = torch.mm(input_, self.weight_ih)
bn_wh = self.bn_hh(wh)
bn_wi = self.bn_ih(wi)
f, i, o, g = torch.split(bn_wh + bn_wi + bias_batch,
split_size=self.hidden_size, dim=1)
c_1 = torch.sigmoid(f)*c_0 + torch.sigmoid(i)*torch.tanh(g)
h_1 = torch.sigmoid(o) * torch.tanh(self.bn_c(c_1))
return h_1, c_1
示例4: forward
def forward(self, inputs, mask=None, layer_cache=None, step=None):
"""
Args:
inputs (FloatTensor): ``(batch_size, input_len, model_dim)``
Returns:
(FloatTensor, FloatTensor):
* gating_outputs ``(batch_size, input_len, model_dim)``
* average_outputs average attention
``(batch_size, input_len, model_dim)``
"""
batch_size = inputs.size(0)
inputs_len = inputs.size(1)
device = inputs.device
average_outputs = self.cumulative_average(
inputs, self.cumulative_average_mask(batch_size,
inputs_len).to(device).float()
if layer_cache is None else step, layer_cache=layer_cache)
average_outputs = self.average_layer(average_outputs)
gating_outputs = self.gating_layer(torch.cat((inputs,
average_outputs), -1))
input_gate, forget_gate = torch.chunk(gating_outputs, 2, dim=2)
gating_outputs = torch.sigmoid(input_gate) * inputs + \
torch.sigmoid(forget_gate) * average_outputs
return gating_outputs, average_outputs
示例5: forward
def forward(self, x, r):
"""
Computes an output and data structure instructions using a
single linear layer.
:type x: Variable
:param x: The input to this Controller
:type r: Variable
:param r: The previous item read from the neural data structure
:rtype: tuple
:return: A tuple of the form (y, (v, u, d)), interpreted as
follows:
- output y
- pop a strength u from the data structure
- push v with strength d to the data structure
"""
self._hidden = self._rnn(torch.cat([x, r], 1), self._hidden)
nn_output = self._linear(self._hidden)
output = nn_output[:, self._n_args + self._read_size:].contiguous()
read_params = torch.sigmoid(nn_output[:, :self._n_args + self._read_size])
v = read_params[:, self._n_args:].contiguous()
instructions = tuple(read_params[:, j].contiguous()
for j in xrange(self._n_args))
self._log(x, torch.sigmoid(output), v, *instructions)
return output, ((v,) + instructions)
示例6: predict
def predict(model, batch, flipped_batch, use_gpu):
image_ids, inputs = batch['image_id'], batch['input']
if use_gpu:
inputs = inputs.cuda()
outputs, _, _ = model(inputs)
probs = torch.sigmoid(outputs)
if flipped_batch is not None:
flipped_image_ids, flipped_inputs = flipped_batch['image_id'], flipped_batch['input']
# assert image_ids == flipped_image_ids
if use_gpu:
flipped_inputs = flipped_inputs.cuda()
flipped_outputs, _, _ = model(flipped_inputs)
flipped_probs = torch.sigmoid(flipped_outputs)
probs += torch.flip(flipped_probs, (3,)) # flip back and add
probs *= 0.5
probs = probs.squeeze(1).cpu().numpy()
if args.resize:
probs = np.swapaxes(probs, 0, 2)
probs = cv2.resize(probs, (orig_img_size, orig_img_size), interpolation=cv2.INTER_LINEAR)
probs = np.swapaxes(probs, 0, 2)
else:
probs = probs[:, y0:y1, x0:x1]
return probs
示例7: _make_images_board
def _make_images_board(self, model):
model.eval()
num_imgs = 64
fuseTrans = self.cfg.fuseTrans
batch = next(iter(self.data_loaders[1]))
input_images, renderTrans, depthGT, maskGT = utils.unpack_batch_novel(batch, self.cfg.device)
with torch.set_grad_enabled(False):
XYZ, maskLogit = model(input_images)
# ------ build transformer ------
XYZid, ML = transform.fuse3D(
self.cfg, XYZ, maskLogit, fuseTrans) # [B,3,VHW],[B,1,VHW]
newDepth, newMaskLogit, collision = transform.render2D(
self.cfg, XYZid, ML, renderTrans) # [B,N,1,H,W]
return {'RGB': utils.make_grid( input_images[:num_imgs]),
'depth': utils.make_grid(
((1-newDepth)*(collision==1).float())[:num_imgs, 0, 0:1, :, :]),
'depthGT': utils.make_grid(
1-depthGT[:num_imgs, 0, 0:1, :, :]),
'mask': utils.make_grid(
torch.sigmoid(maskLogit[:num_imgs, 0:1,:, :])),
'mask_rendered': utils.make_grid(
torch.sigmoid(newMaskLogit[:num_imgs, 0, 0:1, :, :])),
'maskGT': utils.make_grid(
maskGT[:num_imgs, 0, 0:1, :, :]),
}
示例8: custom_cross_entropy
def custom_cross_entropy(x, y):
sigmoid_x = torch.sigmoid(x)
sigmoid_x2 = torch.sigmoid(x ** 2)
neg_log_sigmoid_x = -1 * torch.log(sigmoid_x)
neg_log_1_minus_sigmoid_x2 = -1 * torch.log(1 - sigmoid_x2)
l1 = torch.mul(y, neg_log_sigmoid_x)
l2 = torch.mul(1 - y, neg_log_1_minus_sigmoid_x2)
return torch.sum(l1 + l2)
示例9: forward
def forward(self, x=None, warmup=1., inf_net=None): #, k=1): #, marginf_type=0):
outputs = {}
B = x.shape[0]
if inf_net is None:
# mu, logvar = self.inference_net(x)
z, logits = self.q.sample(x)
else:
# mu, logvar = inf_net.inference_net(x)
z, logqz = inf_net.sample(x)
# print (z[0])
# b = harden(z)
# print (b[0])
# logpz = torch.sum( self.prior.log_prob(b), dim=1)
# print (logpz[0])
# print (logpz.shape)
# fdasf
probs_q = torch.sigmoid(logits)
probs_q = torch.clamp(probs_q, min=.00000001, max=.9999999)
probs_p = torch.ones(B, self.z_size).cuda() *.5
KL = probs_q*torch.log(probs_q/probs_p) + (1-probs_q)*torch.log((1-probs_q)/(1-probs_p))
KL = torch.sum(KL, dim=1)
# print (z.shape)
# Decode Image
x_hat = self.generator.forward(z)
alpha = torch.sigmoid(x_hat)
beta = Beta(alpha*self.beta_scale, (1.-alpha)*self.beta_scale)
x_noise = torch.clamp(x + torch.FloatTensor(x.shape).uniform_(0., 1./256.).cuda(), min=1e-5, max=1-1e-5)
logpx = beta.log_prob(x_noise) #[120,3,112,112] # add uniform noise here
logpx = torch.sum(logpx.view(B, -1),1) # [PB] * self.w_logpx
# print (logpx.shape,logpz.shape,logqz.shape)
# fsdfda
log_ws = logpx - KL #+ logpz - logqz
outputs['logpx'] = torch.mean(logpx)
outputs['x_recon'] = alpha
# outputs['welbo'] = torch.mean(logpx + warmup*( logpz - logqz))
outputs['welbo'] = torch.mean(logpx + warmup*(KL))
outputs['elbo'] = torch.mean(log_ws)
outputs['logws'] = log_ws
outputs['z'] = z
outputs['logpz'] = torch.zeros(1) #torch.mean(logpz)
outputs['logqz'] = torch.mean(KL)
# outputs['logvar'] = logvar
return outputs
示例10: predict_transform
def predict_transform(
prediction, inp_dim, anchors, num_classes, CUDA=True
):
batch_size = prediction.size(0)
stride = inp_dim // prediction.size(2)
grid_size = inp_dim // stride
bbox_attrs = 5 + num_classes
num_anchors = len(anchors)
prediction = prediction.view(
batch_size, bbox_attrs * num_anchors, grid_size * grid_size)
prediction = prediction.transpose(1, 2).contiguous()
prediction = prediction.view(
batch_size, grid_size * grid_size * num_anchors, bbox_attrs)
anchors = [(a[0] / stride, a[1] / stride) for a in anchors]
# Sigmoid the center_X, center_Y and object confidence
prediction[:,:,0] = torch.sigmoid(prediction[:,:,0])
prediction[:,:,1] = torch.sigmoid(prediction[:,:,1])
prediction[:,:,4] = torch.sigmoid(prediction[:,:,4])
# Add the centre offsets
grid = np.arange(grid_size)
a, b = np.meshgrid(grid, grid)
x_offset = torch.FloatTensor(a).view(-1, 1)
y_offset = torch.FloatTensor(b).view(-1, 1)
if CUDA:
x_offset = x_offset.cuda()
y_offset = y_offset.cuda()
x_y_offset = torch.cat((x_offset, y_offset), 1).repeat(1, num_anchors).view(-1, 2).unsqueeze(0)
prediction[:, :, :2] += x_y_offset
# log space transform height and the width
anchors = torch.FloatTensor(anchors)
if CUDA:
anchors = anchors.cuda()
anchors = anchors.repeat(grid_size * grid_size, 1).unsqueeze(0)
prediction[:,:,2:4] = torch.exp(prediction[:,:,2:4]) * anchors
prediction[:,:,5:5 + num_classes] = (
torch.sigmoid((prediction[:, :, 5:5 + num_classes])))
prediction[:, :, 4] *= stride
return prediction
示例11: forward
def forward(self, data, last_hidden):
hx, cx = last_hidden
m = self.wmx(data) * self.wmh(hx)
gates = self.wx(data) + self.wh(m)
i, f, o, u = gates.chunk(4, 1)
i = torch.sigmoid(i)
f = torch.sigmoid(f)
u = torch.tanh(u)
o = torch.sigmoid(o)
cy = f * cx + i * u
hy = o * torch.tanh(cy)
return hy, cy
示例12: test_autograd_closure
def test_autograd_closure(self):
x = Variable(torch.Tensor([0.4]), requires_grad=True)
y = Variable(torch.Tensor([0.7]), requires_grad=True)
trace = torch._C._tracer_enter((x, y), 1)
z = torch.sigmoid(x * (x + y))
w = torch.abs(x * x * x + y) + Variable(torch.ones(1))
torch._C._tracer_exit((z, w))
torch._C._jit_pass_lint(trace)
(z * w).backward()
torch._C._jit_pass_dce(trace)
torch._C._jit_pass_lint(trace)
x_grad = x.grad.data.clone()
x.grad.data.zero_()
function = torch._C._jit_createAutogradClosure(trace)
torch._C._jit_pass_lint(trace)
z2, w2 = function()(x, y)
(z2 * w2).backward()
self.assertEqual(z, z2)
self.assertEqual(w, w2)
self.assertEqual(x.grad.data, x_grad)
示例13: forward
def forward(self, words):
projected = [self.projectors[name](self.embedders[name](words)) for name in self.emb_names]
if self.args.attnnet == 'none':
out = sum(projected)
else:
projected_cat = torch.cat([p.unsqueeze(2) for p in projected], 2)
s_len, b_size, _, emb_dim = projected_cat.size()
attn_input = projected_cat
if self.args.attnnet.startswith('dep_'):
attn_input = attn_input.view(s_len, b_size * self.n_emb, -1)
self.m_attn = self.attn_1(self.attn_0(attn_input)[0])
self.m_attn = self.m_attn.view(s_len, b_size, self.n_emb)
elif self.args.attnnet.startswith('no_dep_'):
self.m_attn = self.attn_1(self.attn_0(attn_input)).squeeze(3)
if self.args.attnnet.endswith('_gating'):
self.m_attn = torch.sigmoid(self.m_attn)
elif self.args.attnnet.endswith('_softmax'):
self.m_attn = F.softmax(self.m_attn, dim=2)
attended = projected_cat * self.m_attn.view(s_len, b_size, self.n_emb, 1).expand_as(projected_cat)
out = attended.sum(2)
if self.args.nonlin == 'relu':
out = F.relu(out)
if self.args.emb_dropout > 0.0:
out = self.dropout(out)
return out
示例14: forward
def forward(self, input, hidden_state):
hidden,c=hidden_state#hidden and c are images with several channels
#print 'hidden ',hidden.size()
#print 'input ',input.size()
combined = torch.cat((input, hidden), 1)#oncatenate in the channels
#print 'combined',combined.size()
A=self.conv(combined)
(ai,af,ao,ag)=torch.split(A,self.num_features,dim=1)#it should return 4 tensors
i=torch.sigmoid(ai)
f=torch.sigmoid(af)
o=torch.sigmoid(ao)
g=torch.tanh(ag)
next_c=f*c+i*g
next_h=o*torch.tanh(next_c)
return next_h, next_c
示例15: forward
def forward(self, x):
out = F.leaky_relu(self.conv1(x), 0.05) # (?, 32, 14, 14)
out = F.leaky_relu(self.conv2(out), 0.05) # (?, 64, 7, 7)
out = F.leaky_relu(self.conv3(out), 0.05) # (?, 128, 3, 3)
out = F.leaky_relu(self.conv4(out), 0.05) # (?, 256, 1, 1)
out = out.squeeze()
return torch.sigmoid(self.linear(out))