python / Sigmoid 함수 구현

Geewon Kim·2024년 1월 17일

Python

목록 보기
7/7

Sigmoid 시그모이드 함수 구현

코드

import numpy as np
import matplotlib.pyplot as plt

# sigmoid = 1 / (1 + e^-x(wx+b))
def sigmoid(x):
    return 1/(1+np.exp(-x))

if __name__ == '__main__':

    x = np.arange(-5.0, 5.0, 0.1)
    # w = 1, b = 0
    # --> sigmoid(x)
    y = sigmoid(x)

    plt.figure(figsize=(12,7))
    plt.plot(x, y , 'g')
    plt.plot([0,0], [1,0], ":")
    plt.plot('Sigmoid Function')
    # plt.show()

    # b 값은 고정, w만 변경
    # w 값이 클수록 경사도가 커짐
    y1 = sigmoid(0.5 * x)
    y2 = sigmoid(3 * x)

    # b 값 변경
    y3 = sigmoid(x + 2)

    plt.plot(x, y1, 'r', linestyle="--")
    plt.plot(x, y2, 'b', linestyle="--")
    plt.plot(x, y3, 'orange', linestyle="--")
    plt.show()
profile
내 지식의 외장하드

0개의 댓글