def forward(self, x):
"""
:param x: tensor with shape of [batch_size, size]
:return: tensor with shape of [batch_size, size]
applies ?(x) ? (f(G(x))) + (1 - ?(x)) ? (Q(x)) transformation | G and Q is affine transformation,
f is non-linear transformation, ?(x) is affine transformation with sigmoid non-linearition
and ? is element-wise multiplication
"""
for layer in range(self.num_layers):
gate = F.sigmoid(self.gate[layer](x))
nonlinear = self.f(self.nonlinear[layer](x))
linear = self.linear[layer](x)
x = gate * nonlinear + (1 - gate) * linear
return x
python类sigmoid()的实例源码
def __init__(self, mode, anchors=9, classes=80, depth=4,
base_activation=F.relu,
output_activation=F.sigmoid):
super(SubNet, self).__init__()
self.anchors = anchors
self.classes = classes
self.depth = depth
self.base_activation = base_activation
self.output_activation = output_activation
self.subnet_base = nn.ModuleList([conv3x3(256, 256, padding=1)
for _ in range(depth)])
if mode == 'boxes':
self.subnet_output = conv3x3(256, 4 * self.anchors, padding=1)
elif mode == 'classes':
# add an extra dim for confidence
self.subnet_output = conv3x3(256, (1 + self.classes) * self.anchors, padding=1)
self._output_layer_init(self.subnet_output.bias.data)
def forward(self, mid_input, global_input):
w = mid_input.size()[2]
h = mid_input.size()[3]
global_input = global_input.unsqueeze(2).unsqueeze(2).expand_as(mid_input)
fusion_layer = torch.cat((mid_input, global_input), 1)
fusion_layer = fusion_layer.permute(2, 3, 0, 1).contiguous()
fusion_layer = fusion_layer.view(-1, 512)
fusion_layer = self.bn1(self.fc1(fusion_layer))
fusion_layer = fusion_layer.view(w, h, -1, 256)
x = fusion_layer.permute(2, 3, 0, 1).contiguous()
x = F.relu(self.bn2(self.conv1(x)))
x = self.upsample(x)
x = F.relu(self.bn3(self.conv2(x)))
x = F.relu(self.bn4(self.conv3(x)))
x = self.upsample(x)
x = F.sigmoid(self.bn5(self.conv4(x)))
x = self.upsample(self.conv5(x))
return x
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
nn_semisupervised_resnet_50.py 文件源码
项目:KagglePlanetPytorch
作者: Mctigger
项目源码
文件源码
阅读 24
收藏 0
点赞 0
评论 0
def generate_model():
class MyModel(nn.Module):
def __init__(self, pretrained_model):
super(MyModel, self).__init__()
self.pretrained_model = pretrained_model
self.layer1 = pretrained_model.layer1
self.layer2 = pretrained_model.layer2
self.layer3 = pretrained_model.layer3
self.layer4 = pretrained_model.layer4
pretrained_model.avgpool = nn.AvgPool2d(8)
classifier = [
nn.Linear(pretrained_model.fc.in_features, 17),
]
self.classifier = nn.Sequential(*classifier)
pretrained_model.fc = self.classifier
def forward(self, x):
return F.sigmoid(self.pretrained_model(x))
return MyModel(torchvision.models.resnet50(pretrained=True))
def generate_model():
class MyModel(nn.Module):
def __init__(self, pretrained_model):
super(MyModel, self).__init__()
self.pretrained_model = pretrained_model
self.layer1 = pretrained_model.layer1
self.layer2 = pretrained_model.layer2
self.layer3 = pretrained_model.layer3
self.layer4 = pretrained_model.layer4
pretrained_model.avgpool = nn.AvgPool2d(8)
classifier = [
nn.Linear(pretrained_model.fc.in_features, 17),
]
self.classifier = nn.Sequential(*classifier)
pretrained_model.fc = self.classifier
def forward(self, x):
return F.sigmoid(self.pretrained_model(x))
return MyModel(torchvision.models.resnet34(pretrained=True))
nn_semisupervised_resnet_34.py 文件源码
项目:KagglePlanetPytorch
作者: Mctigger
项目源码
文件源码
阅读 25
收藏 0
点赞 0
评论 0
def generate_model():
class MyModel(nn.Module):
def __init__(self, pretrained_model):
super(MyModel, self).__init__()
self.pretrained_model = pretrained_model
self.layer1 = pretrained_model.layer1
self.layer2 = pretrained_model.layer2
self.layer3 = pretrained_model.layer3
self.layer4 = pretrained_model.layer4
pretrained_model.avgpool = nn.AvgPool2d(8)
classifier = [
nn.Linear(pretrained_model.fc.in_features, 17),
]
self.classifier = nn.Sequential(*classifier)
pretrained_model.fc = self.classifier
def forward(self, x):
return F.sigmoid(self.pretrained_model(x))
return MyModel(torchvision.models.resnet34(pretrained=True))
def generate_model():
class MyModel(nn.Module):
def __init__(self, pretrained_model):
super(MyModel, self).__init__()
self.pretrained_model = pretrained_model
self.layer1 = pretrained_model.layer1
self.layer2 = pretrained_model.layer2
self.layer3 = pretrained_model.layer3
self.layer4 = pretrained_model.layer4
pretrained_model.avgpool = nn.AvgPool2d(8)
classifier = [
nn.Linear(pretrained_model.fc.in_features, 17),
]
self.classifier = nn.Sequential(*classifier)
pretrained_model.fc = self.classifier
def forward(self, x):
return F.sigmoid(self.pretrained_model(x))
return MyModel(torchvision.models.resnet101(pretrained=True))
nn_semisupervised_resnet_101.py 文件源码
项目:KagglePlanetPytorch
作者: Mctigger
项目源码
文件源码
阅读 22
收藏 0
点赞 0
评论 0
def generate_model():
class MyModel(nn.Module):
def __init__(self, pretrained_model):
super(MyModel, self).__init__()
self.pretrained_model = pretrained_model
self.layer1 = pretrained_model.layer1
self.layer2 = pretrained_model.layer2
self.layer3 = pretrained_model.layer3
self.layer4 = pretrained_model.layer4
pretrained_model.avgpool = nn.AvgPool2d(8)
classifier = [
nn.Linear(pretrained_model.fc.in_features, 17),
]
self.classifier = nn.Sequential(*classifier)
pretrained_model.fc = self.classifier
def forward(self, x):
return F.sigmoid(self.pretrained_model(x))
return MyModel(torchvision.models.resnet101(pretrained=True))
def forward(self, x):
"""
:param x: tensor with shape of [batch_size, size]
:return: tensor with shape of [batch_size, size]
applies ?(x) ? (f(G(x))) + (1 - ?(x)) ? (Q(x)) transformation | G and Q is affine transformation,
f is non-linear transformation, ?(x) is affine transformation with sigmoid non-linearition
and ? is element-wise multiplication
"""
for layer in range(self.num_layers):
gate = F.sigmoid(self.gate[layer](x))
nonlinear = self.f(self.nonlinear[layer](x))
linear = self.linear[layer](x)
x = gate * nonlinear + (1 - gate) * linear
return x
def forward(self, x):
down1 = self.down1(x)
x = self.pool1(down1)
down2 = self.down2(x)
x = self.pool2(down2)
down3 = self.down3(x)
x = self.pool3(down3)
down4 = self.down4(x)
x = self.pool4(down4)
down5 = self.down5(x)
up4 = self.up4(down4, down5)
up3 = self.up3(down3, up4)
up2 = self.up2(down2, up3)
up1 = self.up1(down1, up2)
out = self.classify(up1)
return F.sigmoid(out)
def forward(self, x):
down1 = self.down1(x)
x = self.pool1(down1)
down2 = self.down2(x)
x = self.pool2(down2)
down3 = self.down3(x)
x = self.pool3(down3)
down4 = self.down4(x)
x = self.pool4(down4)
down5 = self.down5(x)
up4 = self.up4(down4, down5)
up3 = self.up3(down3, up4)
up2 = self.up2(down2, up3)
up1 = self.up1(down1, up2)
out = self.classify(up1)
return F.sigmoid(out)
def forward(self, x):
down_outputs = []
for i in range(len(self.down)):
down_output = self.down[i](x)
down_output = self.dropout2d(down_output)
down_outputs.append(down_output)
if i < len(self.pool):
x = self.pool[i](down_output)
x = down_outputs[-1]
for i in reversed(range(len(self.up))):
x = self.up[i](down_outputs[i], x)
x = self.dropout2d(x)
out = self.classify(x)
return F.sigmoid(out)
def KrauseLSTMCell(input, hidden, w_ih, w_hh, b_ih=None, b_hh=None):
# Terminology matchup:
# - This implementation uses the trick of having all gates concatenated
# together into a single tensor, so you can do one matrix multiply to
# compute all the gates.
# - Thus, w_ih holds W_hx, W_ix, W_ox, W_fx
# and w_hh holds W_hh, W_ih, W_oh, W_fh
# - Notice that the indices are swapped, because F.linear has swapped
# arguments. "Cancelling" indices are always next to each other.
hx, cx = hidden
gates = F.linear(input, w_ih, b_ih) + F.linear(hx, w_hh, b_hh)
ingate, forgetgate, hiddengate, outgate = gates.chunk(4, 1)
ingate = F.sigmoid(ingate)
outgate = F.sigmoid(outgate)
forgetgate = F.sigmoid(forgetgate)
cy = (forgetgate * cx) + (ingate * hiddengate)
hy = F.tanh(cy * outgate)
return hy, cy
def MultiplicativeLSTMCell(input, hidden, w_xm, w_hm, w_ih, w_mh, b_xm=None, b_hm=None, b_ih=None, b_mh=None):
# w_ih holds W_hx, W_ix, W_ox, W_fx
# w_mh holds W_hm, W_im, W_om, W_fm
hx, cx = hidden
# Key difference:
m = F.linear(input, w_xm, b_xm) * F.linear(hx, w_hm, b_hm)
gates = F.linear(input, w_ih, b_ih) + F.linear(m, w_mh, b_mh)
ingate, forgetgate, hiddengate, outgate = gates.chunk(4, 1)
ingate = F.sigmoid(ingate)
outgate = F.sigmoid(outgate)
forgetgate = F.sigmoid(forgetgate)
cy = (forgetgate * cx) + (ingate * hiddengate)
hy = F.tanh(cy * outgate)
return hy, cy
def _generate_pred_bbox(self, bbox_delta, anchors):
"""get predictions boxes from bbox_delta and anchors.
Args:
bbox_delta: (dcx, dcy, dw, dh)
shape:(H*W*num_anchor, 4)
anchor: (cx, cy, h, w)
shape:(H*W*num_anchor, 4)
Output:
output: (x_min, y_min, x_max, y_max)
"""
assert bbox_delta.dim() == anchors.dim(), "dim is not equal"
pred_xy = torch.sigmoid(bbox_delta[:, :2]) + anchors[:, :2]
pred_wh = torch.exp(bbox_delta[:, 2:]) * anchors[:, 2:]
pred_bbox = torch.cat((pred_xy, pred_wh), dim=1).contiguous()
# change (cx, xy, h, w) to (x_min, y_min, x_max, y_max)
pred_bbox[:, 0:2] = pred_bbox[:, 0:2] - pred_bbox[:, 2:4] / 2
pred_bbox[:, 2:4] = pred_bbox[:, 0:2] + pred_bbox[:, 2:4]
pred_bbox[:, 0::2] = pred_bbox[:, 0::2] / self.W
pred_bbox[:, 1::2] = pred_bbox[:, 1::2] / self.H
return pred_bbox
def forward(self, x, tags, hn_tags, chunks, hn_chunks, deps, hn_deps):
tags = tags.view(1, -1, nb_postags)
chunks = chunks.view(1, -1, nb_chunktags)
deps = deps.view(1, deps.size(0), deps.size(1))
gt = torch.cat([hn_chunks, hn_tags, hn_deps, x, tags, chunks, deps], dim=2)
pad = torch.zeros(1, x.size(1), self.input_size - gt.size(2))
pad = Variable(pad)
gt = torch.cat([gt, pad], dim=2)
out, hn = self.bi_lstm(gt, (self.h[:,:x.size(1),:],
self.w[:,:x.size(1),:]))
sentiment = self.fc(out[0,-1].view(1,-1))
sentiment = F.sigmoid(sentiment)
return sentiment, out
def forward(self, x):
xs = []
for i, down in enumerate(self.down):
if i == 0:
x_in = x
elif i == 1:
x_in = self.pool_top(xs[-1])
else:
x_in = self.pool(xs[-1])
x_out = down(x_in)
x_out = self.dropout2d(x_out)
xs.append(x_out)
x_out = xs[-1]
for i, (x_skip, up) in reversed(list(enumerate(zip(xs[:-1], self.up)))):
upsample = self.upsample_top if i == 0 else self.upsample
x_out = up(torch.cat([upsample(x_out), x_skip], 1))
x_out = self.dropout2d(x_out)
x_out = self.conv_final(x_out)
b = self.hps.patch_border
return F.sigmoid(x_out[:, :, b:-b, b:-b])
def forward(self, x):
# Input
x = self.input_conv(x)
# Encoder
x = self.enc_1(x)
x = self.pool(x)
x = self.enc_2(x)
x = self.pool(x)
x = self.enc_3(x)
x = self.pool(x)
x = self.enc_4(x)
# Decoder
x = self.dec_4(x)
x = self.upsample(x)
x = self.dec_3(x)
x = self.upsample(x)
x = self.dec_2(x)
x = self.upsample(x)
x = self.dec_1(x)
# Output
x = self.conv_final(x)
b = self.hps.patch_border
return F.sigmoid(x[:, :, b:-b, b:-b])
def forward(self, x):
# Input
x = self.input_conv(x)
# Network
skips = []
for i, (block, scale) in enumerate(zip(self.blocks, self.scales)):
if i < self.n_layers:
x = concat([block(x), x])
skips.append(x)
x = scale(x)
elif i == self.n_layers:
x = block(x)
else:
x = block(concat([scale(x), skips[2 * self.n_layers - i]]))
# Output
x = self.output_conv(x)
b = self.hps.patch_border
return F.sigmoid(x[:, :, b:-b, b:-b])
def forward(self, embedding):
def act(x):
return F.relu(x, inplace=True)
def up(x):
m = nn.UpsamplingNearest2d(scale_factor=2)
return m(x)
x_ae = embedding # Bx256
x_ae = act(self.ae_fc1_bn(self.ae_fc1(x_ae))) # 128x3x5
x_ae = x_ae.view(-1, 128, 3, 5)
x_ae = up(x_ae) # 6x10
x_ae = act(self.ae_c1_bn(self.ae_c1(x_ae))) # 6x10
x_ae = up(x_ae) # 12x20
x_ae = act(self.ae_c2_bn(self.ae_c2(x_ae))) # 12x20 -> 10x20
x_ae = F.pad(x_ae, (0, 0, 1, 0)) # 11x20
x_ae = up(x_ae) # 22x40
x_ae = act(self.ae_c3_bn(self.ae_c3(x_ae))) # 22x40
x_ae = up(x_ae) # 44x80
x_ae = F.pad(x_ae, (0, 0, 1, 0)) # add 1px at top (from 44 to 45)
x_ae = F.sigmoid(self.ae_c4(x_ae))
return x_ae
def forward(self, inp):
#if inp.dim() > 2:
# inp = inp.permute(0, 2, 1)
#inp = inp.contiguous().view(-1, self.L)
if not (type(inp) == Variable):
inp = Variable(inp[0])
if hasattr(self.arguments, 'pack_num'):
N = inp.size(0)
Ncut = int(N/self.arguments.pack_num)
split = torch.split(inp, Ncut, dim=0)
inp = torch.cat(split, dim=1)
h1 = F.tanh((self.l1(inp)))
#h2 = F.tanh(self.l2_bn(self.l2(h1)))
if self.arguments.tr_method == 'adversarial_wasserstein':
output = (self.l3(h1))
else:
output = F.sigmoid(self.l3(h1))
return output, h1
def SkipConnectLSTMCell(input, hidden, hidden_skip, w_ih, w_hh, b_ih=None, b_hh=None, noise_in=None, noise_hidden=None):
input = input.expand(4, *input.size()) if noise_in is None else input.unsqueeze(0) * noise_in
hx, cx = hidden
hx = torch.cat([hx, hidden_skip], dim=1)
hx = hx.expand(4, *hx.size()) if noise_hidden is None else hx.unsqueeze(0) * noise_hidden
gates = torch.baddbmm(b_ih.unsqueeze(1), input, w_ih) + torch.baddbmm(b_hh.unsqueeze(1), hx, w_hh)
ingate, forgetgate, cellgate, outgate = gates
ingate = F.sigmoid(ingate)
forgetgate = F.sigmoid(forgetgate)
cellgate = F.tanh(cellgate)
outgate = F.sigmoid(outgate)
cy = (forgetgate * cx) + (ingate * cellgate)
hy = outgate * F.tanh(cy)
return hy, cy
def SkipConnectGRUCell(input, hidden, hidden_skip, w_ih, w_hh, b_ih=None, b_hh=None, noise_in=None, noise_hidden=None):
input = input.expand(3, *input.size()) if noise_in is None else input.unsqueeze(0) * noise_in
hx = torch.cat([hidden, hidden_skip], dim=1)
hx = hx.expand(3, *hx.size()) if noise_hidden is None else hx.unsqueeze(0) * noise_hidden
gi = torch.baddbmm(b_ih.unsqueeze(1), input, w_ih)
gh = torch.baddbmm(b_hh.unsqueeze(1), hx, w_hh)
i_r, i_i, i_n = gi
h_r, h_i, h_n = gh
resetgate = F.sigmoid(i_r + h_r)
inputgate = F.sigmoid(i_i + h_i)
newgate = F.tanh(i_n + resetgate * h_n)
hy = newgate + inputgate * (hidden - newgate)
return hy
def SkipConnectFastGRUCell(input, hidden, hidden_skip, w_ih, w_hh, b_ih=None, b_hh=None, noise_in=None, noise_hidden=None):
if noise_in is not None:
input = input * noise_in
hx = torch.cat([hidden, hidden_skip], dim=1)
if noise_hidden is not None:
hx = hx * noise_hidden
if input.is_cuda:
gi = F.linear(input, w_ih)
gh = F.linear(hx, w_hh)
state = fusedBackend.GRUFused()
return state(gi, gh, hidden) if b_ih is None else state(gi, gh, hidden, b_ih, b_hh)
gi = F.linear(input, w_ih, b_ih)
gh = F.linear(hx, w_hh, b_hh)
i_r, i_i, i_n = gi.chunk(3, 1)
h_r, h_i, h_n = gh.chunk(3, 1)
resetgate = F.sigmoid(i_r + h_r)
inputgate = F.sigmoid(i_i + h_i)
newgate = F.tanh(i_n + resetgate * h_n)
hy = newgate + inputgate * (hidden - newgate)
return hy
def VarLSTMCell(input, hidden, w_ih, w_hh, b_ih=None, b_hh=None, noise_in=None, noise_hidden=None):
input = input.expand(4, *input.size()) if noise_in is None else input.unsqueeze(0) * noise_in
hx, cx = hidden
hx = hx.expand(4, *hx.size()) if noise_hidden is None else hx.unsqueeze(0) * noise_hidden
gates = torch.baddbmm(b_ih.unsqueeze(1), input, w_ih) + torch.baddbmm(b_hh.unsqueeze(1), hx, w_hh)
ingate, forgetgate, cellgate, outgate = gates
ingate = F.sigmoid(ingate)
forgetgate = F.sigmoid(forgetgate)
cellgate = F.tanh(cellgate)
outgate = F.sigmoid(outgate)
cy = (forgetgate * cx) + (ingate * cellgate)
hy = outgate * F.tanh(cy)
return hy, cy
def VarFastGRUCell(input, hidden, w_ih, w_hh, b_ih=None, b_hh=None, noise_in=None, noise_hidden=None):
if noise_in is not None:
input = input * noise_in
hx = hidden if noise_hidden is None else hidden * noise_hidden
if input.is_cuda:
gi = F.linear(input, w_ih)
gh = F.linear(hx, w_hh)
state = fusedBackend.GRUFused()
return state(gi, gh, hidden) if b_ih is None else state(gi, gh, hidden, b_ih, b_hh)
gi = F.linear(input, w_ih, b_ih)
gh = F.linear(hx, w_hh, b_hh)
i_r, i_i, i_n = gi.chunk(3, 1)
h_r, h_i, h_n = gh.chunk(3, 1)
resetgate = F.sigmoid(i_r + h_r)
inputgate = F.sigmoid(i_i + h_i)
newgate = F.tanh(i_n + resetgate * h_n)
hy = newgate + inputgate * (hidden - newgate)
return hy
def forward(self, xt, state):
all_input_sums = self.i2h(xt) + self.h2h(state[0][-1])
sigmoid_chunk = all_input_sums.narrow(1, 0, 3 * self.rnn_size)
sigmoid_chunk = F.sigmoid(sigmoid_chunk)
in_gate = sigmoid_chunk.narrow(1, 0, self.rnn_size)
forget_gate = sigmoid_chunk.narrow(1, self.rnn_size, self.rnn_size)
out_gate = sigmoid_chunk.narrow(1, self.rnn_size * 2, self.rnn_size)
in_transform = torch.max(\
all_input_sums.narrow(1, 3 * self.rnn_size, self.rnn_size),
all_input_sums.narrow(1, 4 * self.rnn_size, self.rnn_size))
next_c = forget_gate * state[1][-1] + in_gate * in_transform
next_h = out_gate * F.tanh(next_c)
next_h = self.dropout(next_h)
output = next_h
state = (next_h.unsqueeze(0), next_c.unsqueeze(0))
return output, state
def forward(self, xt, fc_feats, att_feats, p_att_feats, state):
att_res = self.attention(state[0][-1], att_feats, p_att_feats)
all_input_sums = self.i2h(xt) + self.h2h(state[0][-1])
sigmoid_chunk = all_input_sums.narrow(1, 0, 3 * self.rnn_size)
sigmoid_chunk = F.sigmoid(sigmoid_chunk)
in_gate = sigmoid_chunk.narrow(1, 0, self.rnn_size)
forget_gate = sigmoid_chunk.narrow(1, self.rnn_size, self.rnn_size)
out_gate = sigmoid_chunk.narrow(1, self.rnn_size * 2, self.rnn_size)
in_transform = all_input_sums.narrow(1, 3 * self.rnn_size, 2 * self.rnn_size) + \
self.a2c(att_res)
in_transform = torch.max(\
in_transform.narrow(1, 0, self.rnn_size),
in_transform.narrow(1, self.rnn_size, self.rnn_size))
next_c = forget_gate * state[1][-1] + in_gate * in_transform
next_h = out_gate * F.tanh(next_c)
output = self.dropout(next_h)
state = (next_h.unsqueeze(0), next_c.unsqueeze(0))
return output, state
def node_forward(self, inputs, child_c, child_h):
child_h_sum = F.torch.sum(torch.squeeze(child_h, 1), 0)
i = F.sigmoid(self.ix(inputs) + self.ih(child_h_sum))
o = F.sigmoid(self.ox(inputs) + self.oh(child_h_sum))
u = F.tanh(self.ux(inputs) + self.uh(child_h_sum))
# add extra singleton dimension
fx = F.torch.unsqueeze(self.fx(inputs), 1)
f = F.torch.cat([self.fh(child_hi) + fx for child_hi in child_h], 0)
f = F.sigmoid(f)
# removing extra singleton dimension
f = F.torch.unsqueeze(f, 1)
fc = F.torch.squeeze(F.torch.mul(f, child_c), 1)
c = F.torch.mul(i, u) + F.torch.sum(fc, 0)
h = F.torch.mul(o, F.tanh(c))
return c, h