Python chainer.functions.sigmoid() Examples

The following are 30 code examples of chainer.functions.sigmoid(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module chainer.functions , or try the search function .
Example #1
Source File: multi_label_classifier.py    From models with MIT License 6 votes vote down vote up
def calc_accuracy(pred_scores, gt_labels):
    # https://arxiv.org/pdf/1612.03663.pdf
    # end of section 3.1
    pred_probs = F.sigmoid(pred_scores).data

    accs = []
    n_pos = []
    n_pred = []
    for pred_prob, gt_label in zip(pred_probs, gt_labels):
        gt_label = chainer.cuda.to_cpu(gt_label)
        pred_prob = chainer.cuda.to_cpu(pred_prob)
        pred_label = np.where(pred_prob > 0.5)[0]

        correct = np.intersect1d(gt_label, pred_label)
        diff_gt = np.setdiff1d(gt_label, correct)
        diff_pred = np.setdiff1d(pred_label, correct)
        accs.append(
            len(correct) / (len(correct) + len(diff_gt) + len(diff_pred)))
        n_pos.append(len(gt_label))
        n_pred.append(len(pred_label))
    return {
        'accuracy': np.mean(accs),
        'n_pos': np.mean(n_pos),
        'n_pred': np.mean(n_pred)} 
Example #2
Source File: test_replace_func.py    From chainer with MIT License 6 votes vote down vote up
def test_fake_as_funcnode_without_replace():

    class Model(chainer.Chain):
        def _init__(self):
            super().__init__()

        def add(self, xs, value=0.01):
            return xs.array + value

        def __call__(self, xs):
            return F.sigmoid(self.add(xs))

    model = Model()
    x = input_generator.increasing(3, 4)

    onnx_model = export(model, x)
    sigmoid_nodes = [
        node for node in onnx_model.graph.node if node.op_type == 'Sigmoid']
    assert len(sigmoid_nodes) == 1
    # sigmoid node should be expected to connect with input
    # but the connection is cut because `add` method takes array.
    assert not sigmoid_nodes[0].input[0] == 'Input_0' 
Example #3
Source File: centernet.py    From imgclsmob with MIT License 6 votes vote down vote up
def __init__(self,
                 in_channels,
                 out_channels,
                 do_nms,
                 **kwargs):
        super(CenterNetHeatmapBlock, self).__init__(**kwargs)
        self.do_nms = do_nms

        with self.init_scope():
            self.head = CenterNetHeadBlock(
                in_channels=in_channels,
                out_channels=out_channels)
            self.sigmoid = F.sigmoid
            if self.do_nms:
                self.pool = partial(
                    F.max_pooling_2d,
                    ksize=3,
                    stride=1,
                    pad=1,
                    cover_all=False) 
Example #4
Source File: sinet.py    From imgclsmob with MIT License 6 votes vote down vote up
def __init__(self,
                 channels,
                 reduction=16,
                 round_mid=False,
                 mid_activation=(lambda: F.relu),
                 out_activation=(lambda: F.sigmoid)):
        super(SEBlock, self).__init__()
        self.use_conv2 = (reduction > 1)
        mid_channels = channels // reduction if not round_mid else round_channels(float(channels) / reduction)

        with self.init_scope():
            self.fc1 = L.Linear(
                in_size=channels,
                out_size=mid_channels)
            if self.use_conv2:
                self.activ = get_activation_layer(mid_activation)
                self.fc2 = L.Linear(
                    in_size=mid_channels,
                    out_size=channels)
            self.sigmoid = get_activation_layer(out_activation) 
Example #5
Source File: faster_gru.py    From knmt with GNU General Public License v3.0 6 votes vote down vote up
def faster_call2(self, h, x):
        r_z_h_x = self.W_r_z_h(x)

        r_z_h = self.U_r_z(h)

        r_x, z_x, h_x = split_axis(r_z_h_x, (self.n_units, self.n_units * 2), axis=1)
        assert r_x.data.shape[1] == self.n_units
        assert z_x.data.shape[1] == self.n_units
        assert h_x.data.shape[1] == self.n_units

        r_h, z_h = split_axis(r_z_h, (self.n_units,), axis=1)
#         r = sigmoid.sigmoid(r_x + r_h)
#         z = sigmoid.sigmoid(z_x + z_h)
#         h_bar = tanh.tanh(h_x + self.U(sigm_a_plus_b_by_h(r_x, r_h, h)))
#         h_new = (1 - z) * h + z * h_bar
#         return h_new

        return compute_output_GRU(z_x, z_h, h_x, h, self.U(sigm_a_plus_b_by_h_fast(r_x, r_h, h))) 
Example #6
Source File: region_loss.py    From models with MIT License 6 votes vote down vote up
def region_loss(output, gt_points):
    # rpoints lie in [0, feat_size]
    # points lie in [0, 1]
    # anchors = [0.1067, 0.9223]
    B, C, H, W = output.shape

    det_confs = F.sigmoid(output[:, 18])
    rpoints = output[:, :18].reshape(B, 9, 2, H, W)
    rpoints0 = F.sigmoid(rpoints[:, 0])
    rpoints = F.concat(
        (rpoints0[:, None], rpoints[:, 1:]), axis=1)
    rpoints_data = rpoints.data

    points_data = rpoints_to_points(rpoints_data)
    gt_rpoints, gt_confs, coord_mask, conf_mask = create_target(
        points_data, gt_points)

    point_loss = F.sum(
        coord_mask[:, None, None] * (rpoints - gt_rpoints) ** 2) / (2 * B)
    conf_loss = F.sum(conf_mask * (det_confs - gt_confs) ** 2) / (2 * B)
    return point_loss, conf_loss 
Example #7
Source File: eval_voc07.py    From models with MIT License 6 votes vote down vote up
def __call__(self, imgs):
        with chainer.using_config('train', False), \
                chainer.function.no_backprop_mode():
            transform = BatchTransform(self.model.mean)
            imgs = transform(imgs)
            imgs = self.model.xp.array(imgs)
            scores = self.model(imgs)
            probs = chainer.cuda.to_cpu(F.sigmoid(scores).data)

        labels = []
        scores = []
        for prob in probs:
            label = np.where(prob >= self.thresh)[0]
            labels.append(label)
            scores.append(prob[label])
        return labels, scores 
Example #8
Source File: highway.py    From models with MIT License 6 votes vote down vote up
def forward(self, inputs):
        current_input = inputs
        for layer in self._layers:
            projected_input = layer(current_input)
            linear_part = current_input
            # NOTE: if you modify this, think about whether you should modify the initialization
            # above, too.
            nonlinear_part = projected_input[:,
                                             (0 * self._input_dim):
                                             (1 * self._input_dim)]
            gate = projected_input[:,
                                   (1 * self._input_dim):
                                   (2 * self._input_dim)]
            nonlinear_part = self._activation(nonlinear_part)
            gate = F.sigmoid(gate)
            current_input = gate * linear_part + (1 - gate) * nonlinear_part
        return current_input 
Example #9
Source File: net.py    From tensorboardX with MIT License 6 votes vote down vote up
def get_loss_func(self, C=1.0, k=1):
        """Get loss function of VAE.

        The loss value is equal to ELBO (Evidence Lower Bound)
        multiplied by -1.

        Args:
            C (int): Usually this is 1.0. Can be changed to control the
                second term of ELBO bound, which works as regularization.
            k (int): Number of Monte Carlo samples used in encoded vector.
        """
        def lf(x):
            mu, ln_var = self.encode(x)
            batchsize = len(mu.data)
            # reconstruction loss
            rec_loss = 0
            for l in six.moves.range(k):
                z = F.gaussian(mu, ln_var)
                rec_loss += F.bernoulli_nll(x, self.decode(z, sigmoid=False)) \
                    / (k * batchsize)
            self.rec_loss = rec_loss
            self.loss = self.rec_loss + \
                C * gaussian_kl_divergence(mu, ln_var) / batchsize
            return self.loss
        return lf 
Example #10
Source File: test_replace_func.py    From onnx-chainer with MIT License 6 votes vote down vote up
def test_fake_as_funcnode_without_replace():

    class Model(chainer.Chain):
        def _init__(self):
            super().__init__()

        def add(self, xs, value=0.01):
            return xs.array + value

        def __call__(self, xs):
            return F.sigmoid(self.add(xs))

    model = Model()
    x = input_generator.increasing(3, 4)

    onnx_model = export(model, x)
    sigmoid_nodes = [
        node for node in onnx_model.graph.node if node.op_type == 'Sigmoid']
    assert len(sigmoid_nodes) == 1
    # sigmoid node should be expected to connect with input
    # but the connection is cut because `add` method takes array.
    assert not sigmoid_nodes[0].input[0] == 'Input_0' 
Example #11
Source File: generator.py    From chainer-gqn with MIT License 6 votes vote down vote up
def __call__(self, prev_hg, prev_cg, prev_z, v, r, prev_u):
        v = self.broadcast_v(v)
        if r.shape[2] == 1:
            r = self.broadcast_r(r)

        lstm_input = cf.concat((prev_hg, v, r, prev_z), axis=1)
        gate_inputs = self.lstm(lstm_input)

        forget_gate_input, input_gate_input, tanh_input, output_gate_input = cf.split_axis(
            gate_inputs, 4, axis=1)

        forget_gate = cf.sigmoid(forget_gate_input)
        input_gate = cf.sigmoid(input_gate_input)
        next_c = forget_gate * prev_cg + input_gate * cf.tanh(tanh_input)
        output_gate = cf.sigmoid(output_gate_input)
        next_h = output_gate * cf.tanh(next_c)

        next_u = self.upsample_h(next_h) + prev_u

        return next_h, next_c, next_u 
Example #12
Source File: inference.py    From chainer-gqn with MIT License 6 votes vote down vote up
def __call__(self, prev_hg, prev_he, prev_ce, x, v, r, u):
        xu = cf.concat((x, u), axis=1)
        xu = self.downsample_xu(xu)
        v = self.broadcast_v(v)
        if r.shape[2] == 1:
            r = self.broadcast_r(r)

        lstm_input = cf.concat((prev_he, prev_hg, xu, v, r), axis=1)
        gate_inputs = self.lstm(lstm_input)

        if self.use_cuda_kernel:
            next_h, next_c = CoreFunction()(gate_inputs, prev_ce)
        else:
            forget_gate_input, input_gate_input, tanh_input, output_gate_input = cf.split_axis(
                gate_inputs, 4, axis=1)

            forget_gate = cf.sigmoid(forget_gate_input)
            input_gate = cf.sigmoid(input_gate_input)
            next_c = forget_gate * prev_ce + input_gate * cf.tanh(tanh_input)
            output_gate = cf.sigmoid(output_gate_input)
            next_h = output_gate * cf.tanh(next_c)

        return next_h, next_c 
Example #13
Source File: model_py.py    From models with MIT License 5 votes vote down vote up
def swish(x):
    return x * F.sigmoid(x) 
Example #14
Source File: net.py    From chainer with MIT License 5 votes vote down vote up
def __call__(self, z):
        h = F.reshape(F.relu(self.bn0(self.l0(z))),
                      (len(z), self.ch, self.bottom_width, self.bottom_width))
        h = F.relu(self.bn1(self.dc1(h)))
        h = F.relu(self.bn2(self.dc2(h)))
        h = F.relu(self.bn3(self.dc3(h)))
        x = F.sigmoid(self.dc4(h))
        return x 
Example #15
Source File: images.py    From models with MIT License 5 votes vote down vote up
def logit(self, x):
        h = self.l1(x)
        h = F.sigmoid(h)
        h = self.lout(h)
        return h 
Example #16
Source File: nnpu_risk.py    From pywsl with MIT License 5 votes vote down vote up
def __init__(self, prior, loss=(lambda x: F.sigmoid(-x)), gamma=1, beta=0, nnPU=True):
        check.in_range(prior, 0, 1, "prior")
        self.prior = prior
        self.gamma = gamma
        self.beta = beta
        self.loss_func = loss
        self.nnPU = nnPU
        self.positive = 1
        self.unlabeled = 0
#        self.unlabeled = -1 
Example #17
Source File: mesh.py    From neural_renderer with MIT License 5 votes vote down vote up
def get_batch(self, batch_size):
        # broadcast for minibatch
        vertices = cf.broadcast_to(self.vertices, [batch_size] + list(self.vertices.shape))
        faces = cf.broadcast_to(self.faces, [batch_size] + list(self.faces.shape)).data
        textures = cf.sigmoid(cf.broadcast_to(self.textures, [batch_size] + list(self.textures.shape)))
        return vertices, faces, textures 
Example #18
Source File: test_mlp_convolution_2d.py    From chainer with MIT License 5 votes vote down vote up
def test_init(self):
        self.assertIs(self.mlp.activation, functions.sigmoid)

        self.assertEqual(len(self.mlp), 3)
        for i, conv in enumerate(self.mlp):
            self.assertIsInstance(conv, links.Convolution2D)
            if i == 0:
                self.assertEqual(conv.W.data.shape, (96, 3, 11, 11))
            else:
                self.assertEqual(conv.W.data.shape, (96, 96, 1, 1)) 
Example #19
Source File: test_mlp_convolution_2d.py    From chainer with MIT License 5 votes vote down vote up
def setUp(self):
        self.mlp = links.MLPConvolution2D(
            3, (96, 96, 96), 11, activation=functions.sigmoid)
        self.x = numpy.zeros((10, 3, 20, 20), dtype=numpy.float32) 
Example #20
Source File: test_replace_func.py    From onnx-chainer with MIT License 5 votes vote down vote up
def test_output(self):
        class Model(chainer.Chain):
            def __init__(self, value):
                super().__init__()
                self.value = value

            @as_funcnode('NumpyFull')
            def full(self, xs, value=0):
                # not support `def full(self, xs_shape, value=0)`
                # wrapped function node cannot handle shape directly yet.
                return np.full(xs.array.shape, value, dtype=np.float32)

            def __call__(self, xs):
                return F.sigmoid(self.full(xs, value=self.value))

        model = Model(value=5)
        x = input_generator.increasing(2, 3, 4)

        def numpy_full_converter(params):
            gb = onnx_helper.GraphBuilder()
            output = gb.op('Shape', params.input_names)
            value = onnx.helper.make_tensor(
                'value', onnx.TensorProto.FLOAT, [1], [params.func.value])
            gb.op_output_named(
                'ConstantOfShape', [output], params.output_names, value=value)
            return gb.nodes()

        addon_converters = {'NumpyFull': numpy_full_converter}

        self.expect(
            model, x, skip_opset_version=[7, 8],
            external_converters=addon_converters) 
Example #21
Source File: test_caffe.py    From chainer with MIT License 5 votes vote down vote up
def test_Sigmoid(self):
        class Link(chainer.Chain):
            def forward(self, x):
                return F.sigmoid(x)

        assert_export_import_match(Link(), self.x) 
Example #22
Source File: test_reporter.py    From chainer with MIT License 5 votes vote down vote up
def test_keep_graph(self):
        x = chainer.Variable(numpy.array([1], numpy.float32))
        y = functions.sigmoid(x)
        reporter = chainer.Reporter()
        with self._scope(True):
            reporter.report({'y': y})
        assert reporter.observation['y'].creator is not None 
Example #23
Source File: test_reporter.py    From chainer with MIT License 5 votes vote down vote up
def test_keep_graph_default(self):
        x = chainer.Variable(numpy.array([1], numpy.float32))
        y = functions.sigmoid(x)
        reporter = chainer.Reporter()
        with self._scope(None):
            reporter.report({'y': y})
        self.assertIsNone(reporter.observation['y'].creator) 
Example #24
Source File: net.py    From chainer with MIT License 5 votes vote down vote up
def forward(self, z):
        h = F.reshape(F.relu(self.bn0(self.l0(z))),
                      (len(z), self.ch, self.bottom_width, self.bottom_width))
        h = F.relu(self.bn1(self.dc1(h)))
        h = F.relu(self.bn2(self.dc2(h)))
        h = F.relu(self.bn3(self.dc3(h)))
        x = F.sigmoid(self.dc4(h))
        return x 
Example #25
Source File: cbamresnet.py    From imgclsmob with MIT License 5 votes vote down vote up
def __call__(self, x):
        att1 = F.expand_dims(F.max(x, axis=1), axis=1)
        att2 = F.expand_dims(F.mean(x, axis=1), axis=1)
        att = F.concat((att1, att2), axis=1)
        att = self.conv(att)
        att = F.broadcast_to(F.sigmoid(att), x.shape)
        x = x * att
        return x 
Example #26
Source File: modules.py    From chainer with MIT License 5 votes vote down vote up
def forward(self, x, condition):
        length = x.shape[2]
        h = self.conv(x)
        h = h[:, :, :length]  # crop
        h += condition
        tanh_z, sig_z = F.split_axis(h, 2, axis=1)
        z = F.tanh(tanh_z) * F.sigmoid(sig_z)
        if x.shape[2] == z.shape[2]:
            residual = self.res(z) + x
        else:
            residual = self.res(z) + x[:, :, -1:]  # crop
        skip_conenection = self.skip(z)
        return residual, skip_conenection 
Example #27
Source File: net.py    From tensorboardX with MIT License 5 votes vote down vote up
def __call__(self, z):
        h = F.reshape(F.relu(self.bn0(self.l0(z))),
                      (len(z), self.ch, self.bottom_width, self.bottom_width))
        h = F.relu(self.bn1(self.dc1(h)))
        h = F.relu(self.bn2(self.dc2(h)))
        h = F.relu(self.bn3(self.dc3(h)))
        x = F.sigmoid(self.dc4(h))
        return x 
Example #28
Source File: net.py    From tensorboardX with MIT License 5 votes vote down vote up
def decode(self, z, sigmoid=True):
        h1 = F.tanh(self.ld1(z))
        h2 = self.ld2(h1)
        if sigmoid:
            return F.sigmoid(h2)
        else:
            return h2 
Example #29
Source File: net.py    From tensorboardX with MIT License 5 votes vote down vote up
def __call__(self, x, sigmoid=True):
        """AutoEncoder"""
        return self.decode(self.encode(x)[0], sigmoid) 
Example #30
Source File: common.py    From imgclsmob with MIT License 5 votes vote down vote up
def __call__(self, x):
        w = F.average_pooling_2d(x, ksize=x.shape[2:])
        if not self.use_conv:
            w = F.reshape(w, shape=(w.shape[0], -1))
        w = self.conv1(w) if self.use_conv else self.fc1(w)
        w = self.activ(w)
        w = self.conv2(w) if self.use_conv else self.fc2(w)
        w = self.sigmoid(w)
        if not self.use_conv:
            w = F.expand_dims(F.expand_dims(w, axis=2), axis=3)
        x = x * w
        return x