3-2 Sigmoid 구현

2024. 3. 29. 02:49·Deep Learning/Deep learning Bacis Mathematics
CH03_03_[구현강의]_Binary_Classifiers

The Graphs of Odds and Logit¶

In [12]:
import numpy as np
import tensorflow as tf

import matplotlib.pyplot as plt
plt.style.use('_classic_test_patch')

p_np = np.linspace(0.01, 0.99, 100)
p_tf = tf.linspace(0.01, 0.99, 100)

# 오즈 계산
odds_np = p_np/(1 - p_np)
odds_tf = p_tf/(1 - p_tf)

# 로짓
logit_np = np.log(odds_np)
logit_tf = tf.math.log(odds_tf)

fig, axes = plt.subplots(2, 1, figsize=(15, 10),
                         sharex=True)

axes[0].plot(p_np, odds_tf.numpy())
axes[1].plot(p_np, logit_tf.numpy())

xticks = np.arange(0, 1.1, 0.1)

axes[0].tick_params(labelsize=15)
axes[0].set_xticks(xticks)
axes[0].set_ylabel('Odds', fontsize=20, color='darkblue')
axes[1].tick_params(labelsize=15)
axes[1].set_xticks(xticks)
axes[1].set_ylabel('Logit', fontsize=20, color='darkblue')
axes[1].set_xlabel('Probability', fontsize=20, color='darkblue')
Out[12]:
Text(0.5, 0, 'Probability')

The Graphs of Sigmoid¶

In [13]:
import tensorflow as tf

from tensorflow.keras.layers import Activation

X = tf.linspace(-30, 30, 100)
sigmoid = Activation("sigmoid")(X)

fig, ax = plt.subplots(figsize=(10, 5))
ax.plot(X.numpy(), sigmoid.numpy())
Out[13]:
[<matplotlib.lines.Line2D at 0x1274274e690>]

Single-variate Logistic Regression Models¶

In [19]:
import matplotlib.pyplot as plt

import tensorflow as tf
from tensorflow.keras.layers import Dense

plt.style.use("_classic_test_patch")

X = tf.random.normal(shape=(100, 1))
dense = Dense(units=1, activation="sigmoid")

Y = dense(X)
print(Y.shape)

fig, ax = plt.subplots(figsize=(7, 7))
ax.scatter(X.numpy().flatten(), Y.numpy().flatten())
(100, 1)
Out[19]:
<matplotlib.collections.PathCollection at 0x127420838f0>

Multi-variate Logistic Regression Models¶

In [21]:
import matplotlib.pyplot as plt

import tensorflow as tf
from tensorflow.keras.layers import Dense

plt.style.use("_classic_test_patch")

X = tf.random.normal(shape=(100, 5))
dense = Dense(units=1, activation="sigmoid")

Y = dense(X)
print(Y.shape)
(100, 1)

Binary Classifier with Dense Layers¶

In [22]:
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense

model = Sequential()
model.add(Dense(units=10, activation="relu"))
model.add(Dense(units=5, activation="relu"))
model.add(Dense(units=1, activation="sigmoid"))
In [ ]:
 

'Deep Learning > Deep learning Bacis Mathematics' 카테고리의 다른 글

3-3 Softmax 구현  (0) 2024.03.29
3-1 Sigmoid & Softmax 이론  (0) 2024.03.29
2-4 Model Implementation with Dense layer 구현  (0) 2024.03.28
2-3 Cascaded_Dense_Layers 구현  (0) 2024.03.28
2-2 Dense Layer 구현  (0) 2024.03.28
'Deep Learning/Deep learning Bacis Mathematics' 카테고리의 다른 글
  • 3-3 Softmax 구현
  • 3-1 Sigmoid & Softmax 이론
  • 2-4 Model Implementation with Dense layer 구현
  • 2-3 Cascaded_Dense_Layers 구현
Juson
Juson
  • Juson
    Juson의 데이터 공부
    Juson
  • 전체
    오늘
    어제
    • 분류 전체보기 (95)
      • RAG (2)
      • AI (2)
        • NLP (0)
        • Generative Model (0)
        • Deep Reinforcement Learning (2)
        • LLM (0)
      • Logistic Optimization (0)
      • Machine Learning (37)
        • Linear Regression (2)
        • Logistic Regression (2)
        • Decision Tree (5)
        • Naive Bayes (1)
        • KNN (2)
        • SVM (2)
        • Clustering (4)
        • Dimension Reduction (3)
        • Boosting (6)
        • Abnomaly Detection (2)
        • Recommendation (4)
        • Embedding & NLP (4)
      • Reinforcement Learning (5)
      • Deep Learning (10)
        • Deep learning Bacis Mathema.. (10)
      • Optimization (2)
        • OR Optimization (0)
        • Convex Optimization (0)
        • Integer Optimization (0)
      • SNA 분석 (0)
      • 포트폴리오 최적화 공부 (0)
        • 최적화 기법 (0)
        • 금융 베이스 (0)
      • Finanancial engineering (0)
      • 프로그래머스 데브코스(Boot camp) (15)
        • SQL (9)
        • Python (5)
        • Machine Learning (1)
      • Python (22)
      • Project (0)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.4
Juson
3-2 Sigmoid 구현
상단으로

티스토리툴바