啟用函式

Python 的 TensorFlow 入門

Isaiah Hull

Visiting Associate Professor of Finance, BI Norwegian Business School

什麼是啟用函式?

  • 典型隱藏層的組成
    • 線性:矩陣相乘
    • 非線性:啟用函式
Python 的 TensorFlow 入門

為何需要非線性

此圖示範用帳單金額與年齡來預測是否違約的簡單網路。

Python 的 TensorFlow 入門

為何需要非線性

此圖顯示 30 歲及以下借款人之信用卡帳單金額與違約的關係。

Python 的 TensorFlow 入門

簡單範例

import numpy as np
import tensorflow as tf

# Define example borrower features
young, old = 0.3, 0.6
low_bill, high_bill = 0.1, 0.5
# Apply matrix multiplication step for all feature combinations
young_high = 1.0*young + 2.0*high_bill
young_low = 1.0*young + 2.0*low_bill
old_high = 1.0*old + 2.0*high_bill
old_low = 1.0*old + 2.0*low_bill
Python 的 TensorFlow 入門

簡單範例

# Difference in default predictions for young
print(young_high - young_low)

# Difference in default predictions for old
print(old_high - old_low)
0.8 
0.8
Python 的 TensorFlow 入門

簡單範例

# Difference in default predictions for young
print(tf.keras.activations.sigmoid(young_high).numpy() - 
tf.keras.activations.sigmoid(young_low).numpy())

# Difference in default predictions for old
print(tf.keras.activations.sigmoid(old_high).numpy() - 
tf.keras.activations.sigmoid(old_low).numpy())
0.16337568
0.14204389
Python 的 TensorFlow 入門

Sigmoid 啟用函式

  • Sigmoid 啟用函式
    • 二元分類
    • 低階:tf.keras.activations.sigmoid()
    • 高階:sigmoid

此圖顯示 -10 到 10 區間的 sigmoid 啟用函式曲線。

Python 的 TensorFlow 入門

ReLU 啟用函式

  • ReLU 啟用函式
    • 隱藏層
    • 低階:tf.keras.activations.relu()
    • 高階:relu

此圖顯示 -10 到 10 區間的 relu 啟用函式曲線。

Python 的 TensorFlow 入門

Softmax 啟用函式

  • Softmax 啟用函式
    • 輸出層(>2 類)
    • 低階:tf.keras.activations.softmax()
    • 高階:softmax
Python 的 TensorFlow 入門

神經網路中的啟用函式

import tensorflow as tf
# Define input layer
inputs = tf.constant(borrower_features, tf.float32)
# Define dense layer 1
dense1 = tf.keras.layers.Dense(16, activation='relu')(inputs)
# Define dense layer 2
dense2 = tf.keras.layers.Dense(8, activation='sigmoid')(dense1)
# Define output layer 
outputs = tf.keras.layers.Dense(4, activation='softmax')(dense2)
Python 的 TensorFlow 入門

一起來練習吧!

Python 的 TensorFlow 入門

Preparing Video For Download...