[ML] 選擇 loss function/ optimizer/ metrics

建構模型 Dense(units, activation) units 為 output_size,keras 已經處理好自動計算 input_size 的部分。 activation function relu: Rectified Linear Unit, ReLU softmax: 對陣列中所有元素做自然對數取值後,在做 normalize,目的是放大最大權重的元素,並且將所有值換成 0~1 的值,意義類似機率。 model.keras.Sequential([ Dense(32, activation="relu"), Dense(64, activation="relu"), Dense(32, activation="relu"), Dense(10, activation="softmax"), ]) 編譯 損失函數(目標函數) loss function CategoricalCrossentropy SparseCategoricalCrossentropy BinaryCrossentropy MeanSquareError KLDivergence CosineSimilarity … 優化器 optimizer SGD (可搭配 momemtum) RMSprop Adam Adagrad … 評量指標 metrics CategoricalAccuracy SparseCategoricalAccuracy BinaryAccuracy AUC Precision Recall … 以下範例兩種型式都可以。其中物件的用法可以使用客製化的條件。 model.compile(optimizer="rmsprop", loss="mean_square_error", metics=["accuracy"]) model.compile(optimizer=keras.optimizers.RMSprop(learning_rate=1e-4), loss=keras.meanSquaredError(), metrics=[keras.metrics.BinaryAccuracy]) 洗牌 收集完資料之後,我們目的並非只在訓練資料上取得良好的模型,而是要取得在大部分狀況下都表現良好的模型。 故我們需要將收集完的資料分成訓練集與驗證集。 以下透過 np....

<span title='2024-02-14 15:39:25 +0800 +0800'>February 14, 2024</span>&nbsp;·&nbsp;1 min&nbsp;·&nbsp;Rain Hu

[ML] General guide on ML

Loss on training data large: model bias -> add features optimization -> change optimization methods small: loss on testing data large: overfitting: (1) more training data, data augmentation (2) make model simpler small: mismatch

<span title='2024-01-14 14:31:56 +0800 +0800'>January 14, 2024</span>&nbsp;·&nbsp;1 min&nbsp;·&nbsp;Rain Hu

[ML] Start Tensorflow Environment with Conda

環境建置 安裝 Anaconda 創建虛擬環境 conda create -n tensorflow 進入虛擬環境 (macOS/Linux) source activate tensorflow 在環境內安裝 tensorflow pip install tensorflow 在環境內安裝 jupyter notebook pip install jupyter notebook 在環境內安裝 pandas pip install pandas 開啟 jupyter notebook jupyter notebook For terminal user 開始 Anaconda.Navigator 在 Environments 中安裝指定模組 ex.tensorflow, keras 在 terminal 中輸入 conda activate {環境名稱} conda activate tensorflow 開啟 python python 若成功便會顯示 python 安裝資訊 Python 3.11.5 (main, Sep 11 2023, 08:17:37) [Clang 14.0.6 ] on darwin Type "help", "copyright", "credits" or "license" for more information....

<span title='2023-10-11 20:48:34 +0800 +0800'>October 11, 2023</span>&nbsp;·&nbsp;1 min&nbsp;·&nbsp;Rain Hu

[ML] 01. 機器學習基本概念簡介

前言 什麼是機器學習 機器學習(Machine Learning),就是利用機器的力量幫忙找出函式。 Input 可以是 vector matrix sequence Output 可以是 Regression Classification Structed Learning(令機器產生有結構的東西 eg. text, image) 示意圖 什麼是深度學習 深度學習(Deep Learning),就是利用神經網路(neural network)的方式來產生函數。 機器如何學習 1. 基本原理(訓練三步驟) Step 1: 使用合適的 Model \(y=f(\text{\red{data}})\) Function with unknown parameters Model: \(\boxed{y=b+wx_1}\) \(w: \text{weight}\) \(b: \text{bias}\) \(x: \text{feature}\) Step 2: 定義 Loss function Define loss from training data 以 Model 的參數 \(w,b\) 來計算 Loss 物理意義:Loss 愈大代表參數愈不好,Loss 愈小代表參數愈好。 計算方法:求估計的值與實際的值(label)之間的差距 Loss function: \(\boxed{L=\frac{1}{N}\sum_ne_n}\) MAE (mean absolute error): \(e=|y-\hat{y}|\) MSE (mean square error): \(e=(y-\hat{y})^2\) Cross-entropy: 計算機率分布之間的差距 Error Surface: 根據不同的參數,計算出 loss 所畫出來的等高線圖。 Step 3: Optimization 找到 loss 最小的參數組合 \((w,b)\) 方法:Gradient Descent \(\boxed{w’ = w - \red{\eta}\frac{\partial L}{\partial w}|_{w=w^0,b=b^0}}\) \(\boxed{b’ = b - \red{\eta}\frac{\partial L}{\partial b}|_{w=w^0,b=b^0}}\) \(\red{\eta}\): 學習率 learning rate, 決定 gradient descent 的一步有多大步 2....

<span title='2023-08-02 23:56:25 +0800 +0800'>August 2, 2023</span>&nbsp;·&nbsp;2 min&nbsp;·&nbsp;Rain Hu

[ML] 簡單實作測試

線性迴歸建模 載入資料 import pandas as pd import matplotlib.pyplot as plt import matplotlib as mlp url = "sample.csv" data = pd.read_csv(url) x = data["x-axis"] y = data["y-axis"] 畫圖 def plot(x, y, w, b): line = w * x + b plt.plot(x, line, color="red", label="prediction") plt.scatter(x, y, color="blue", label="data", marker="x") plt.title("Title") plt.xlabel("x Axis") plt.ylabel("y Axis") plt.xlim([0,12]) plt.ylim([20,140]) plt.show() plot(x, y, 10, 20) 定義 cost function def cost_function(x, y, w, b): y2 = w * x + b cost = (y - y2) ** 2 return cost....

<span title='2023-04-30 00:35:59 +0800 +0800'>April 30, 2023</span>&nbsp;·&nbsp;3 min&nbsp;·&nbsp;Rain Hu