LoginSignup
2
2

More than 5 years have passed since last update.

Pythonでシグモイド関数

Last updated at Posted at 2018-12-04
import numpy as np

def sigmoid(x):
    return 1 / (1 + np.exp(-x))

返り値の特性。xが0のとき、0.5を返す。xが負の数のときは 0より大きく0.5より小さい数、正の数のときは0.5よりも大きく1より小さい数を返す。

>>> sigmoid(0)
0.5
>>> sigmoid(-6)
0.0024726231566347743
>>> sigmoid(6)
0.9975273768433653

追記。ラムダ式で書けば1行で書ける。

import numpy as np
sigmoid = lambda x : 1 / (1 + np.exp(-x))

おまけ。Kotlinでシグモイド関数。

import kotlin.math.exp

fun sigmoid(x: Double): Double = 1 / (1 + exp(-x))

シグモイド関数の微分

def derivative_sigmoid(x):
    return sigmoid(x) * (1 - sigmoid(x)
2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2