本文整理汇总了Python中torch.sigmoid函数的典型用法代码示例。如果您正苦于以下问题:Python sigmoid函数的具体用法?Python sigmoid怎么用?Python sigmoid使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了sigmoid函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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
开发者ID:chriscremer,项目名称:Other_Code,代码行数:28,代码来源:distributions.py
示例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
开发者ID:chriscremer,项目名称:Other_Code,代码行数:30,代码来源:distributions.py
示例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
开发者ID:Joyce94,项目名称:sentence_classification,代码行数:29,代码来源:bnlstm.py
示例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
开发者ID:Unbabel,项目名称:OpenNMT-py,代码行数:29,代码来源:average_attn.py
示例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)
开发者ID:simonjmendelsohn,项目名称:StackNN,代码行数:31,代码来源:recurrent.py
示例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
开发者ID:gtesei,项目名称:fast-furious,代码行数:26,代码来源:test.py
示例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, :, :]),
}
开发者ID:wkflyerman,项目名称:pytorch-3d-point-cloud-generation,代码行数:28,代码来源:trainer.py
示例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)
开发者ID:chu-data-lab,项目名称:GOGGLES,代码行数:10,代码来源:loss.py
示例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
开发者ID:chriscremer,项目名称:Other_Code,代码行数:55,代码来源:vae_discrete.py
示例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
开发者ID:lextoumbourou,项目名称:study-notes,代码行数:53,代码来源:util.py
示例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
开发者ID:anoopsarkar,项目名称:nlp-class-hw,代码行数:15,代码来源:models.py
示例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)
开发者ID:Northrend,项目名称:pytorch,代码行数:26,代码来源:test_jit.py
示例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
开发者ID:guitarmind,项目名称:DME,代码行数:30,代码来源:embedders.py
示例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
开发者ID:praveenkumarchandaliya,项目名称:pytorch_convlstm,代码行数:16,代码来源:conv_lstm.py
示例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))
开发者ID:anihamde,项目名称:cs287-s18,代码行数:7,代码来源:gan_models.py
示例16: forward
def forward(self, x):
x = self.fc1(x)
x = F.relu(x, inplace=True)
x = self.fc2(x)
x = torch.sigmoid(x)
#print(x.size())
return x
开发者ID:Diyago,项目名称:Machine-Learning-scripts,代码行数:7,代码来源:unet_models.py
示例17: _step
def _step(self, tok, states, attention):
prev_states, prev_out = states
lstm_in = torch.cat(
[self._embedding(tok).squeeze(1), prev_out],
dim=1
)
states = self._lstm(lstm_in, prev_states)
lstm_out = states[0][-1]
query = torch.mm(lstm_out, self._attn_w)
attention, attn_mask, extend_src, extend_vsize = attention
context, score = step_attention(
query, attention, attention, attn_mask)
dec_out = self._projection(torch.cat([lstm_out, context], dim=1))
# extend generation prob to extended vocabulary
gen_prob = self._compute_gen_prob(dec_out, extend_vsize)
# compute the probabilty of each copying
copy_prob = torch.sigmoid(self._copy(context, states[0][-1], lstm_in))
# add the copy prob to existing vocab distribution
lp = torch.log(
((-copy_prob + 1) * gen_prob
).scatter_add(
dim=1,
index=extend_src.expand_as(score),
source=score * copy_prob
) + 1e-8) # numerical stability for log
return lp, (states, dec_out), score
开发者ID:ShawnXiha,项目名称:fast_abs_rl,代码行数:27,代码来源:copy_summ.py
示例18: make_score_map
def make_score_map(self, img, mode='sigmoid'):
"""
"""
img = img/255
# The offset is inserted so that the final size of the score map matches
# the search image. To know more see "How to overlay the search img with
# the score map" in Trello/Report. It is half of the dimension of the
# Smallest Class Equivalent of the Ref image.
offset = (((self.ref.shape[0] + 1)//4)*4 - 1)//2
img_mean = img.mean()
img_padded = np.pad(img, ((offset, offset), (offset, offset), (0, 0)),
mode='constant', constant_values=img_mean)
img_padded = numpy_to_torch_var(img_padded, device)
srch_emb = self.net.get_embedding(img_padded)
score_map = self.net.match_corr(self.ref_emb, srch_emb)
dimx = score_map.shape[-1]
dimy = score_map.shape[-2]
score_map = score_map.view(-1, dimy, dimx)
if mode == 'sigmoid':
score_map = sigmoid(score_map)
elif mode == 'norm':
score_map = score_map - score_map.min()
score_map = score_map/score_map.max()
score_map = score_map.unsqueeze(0)
# We upscale 4 times, because the total stride of the network is 4
score_map = F.interpolate(score_map, scale_factor=4, mode='bilinear',
align_corners=False)
score_map = score_map.cpu()
score_map = torch_var_to_numpy(score_map)
return score_map
开发者ID:vero1925,项目名称:Pytorch-SiamFC,代码行数:32,代码来源:producer.py
示例19: test
def test(img_dir, split_test, split_name, model, batch_size, img_size, crop_size):
since = time.time()
# -------------------- SETTINGS: DATA TRANSFORMS
normalizer = [[0.485, 0.456, 0.406], [0.229, 0.224, 0.225]]
data_transforms = {split_name: transforms.Compose([
transforms.Resize(img_size),
transforms.CenterCrop(crop_size),
transforms.ToTensor(),
transforms.Normalize(normalizer[0], normalizer[1])])}
# -------------------- SETTINGS: DATASET BUILDERS
datasetTest = DataGenerator(img_dir=img_dir, split_file=split_test,
transform=data_transforms[split_name])
dataLoaderTest = DataLoader(dataset=datasetTest, batch_size=batch_size,
shuffle=False, num_workers=32)
dataloaders = {}
dataloaders[split_name] = dataLoaderTest
print('Number of testing CXR images: {}'.format(len(datasetTest)))
dataset_sizes = {split_name: len(datasetTest)}
# -------------------- TESTING
model.train(False)
running_corrects = 0
output_list = []
# label_list = []
preds_list = []
# Iterate over data.
for data in dataloaders[split_name]:
inputs, img_names = data
# wrap them in Variable
inputs = Variable(inputs.cuda(), volatile=True)
# forward
outputs = model(inputs)
score = torch.sigmoid(outputs)
score_np = score.data.cpu().numpy()
preds = score>0.5
preds_np = preds.data.cpu().numpy()
preds = preds.type(torch.cuda.LongTensor)
outputs = outputs.data.cpu().numpy()
for j in range(len(img_names)):
print(str(img_names[j]) + ': ' + str(score_np[j]))
img_name = str(img_names[j]).rsplit('/',1)[1]
if score_np[j] > 0.5:
copyfile(str(img_names[j]), './images-own-90/'+img_name)
if score_np[j] < 0.5:
copyfile(str(img_names[j]), './images-own-0/'+img_name)
for i in range(outputs.shape[0]):
output_list.append(outputs[i].tolist())
preds_list.append(preds_np[i].tolist())
开发者ID:rsummers11,项目名称:CADLab,代码行数:59,代码来源:run_test_own.py
示例20: forward
def forward(self, x):
x = self.pre_layer(x)
x = x.view(x.size(0), -1)
x = self.conv4(x)
x = self.prelu4(x)
det = torch.sigmoid(self.conv5_1(x))
box = self.conv5_2(x)
return det, box
开发者ID:Saiuz,项目名称:autokeras,代码行数:8,代码来源:face_detector.py
注:本文中的torch.sigmoid函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论