[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] sample1 - 手寫數字辨識

MNIST NIST(National Insitute of Standards and Technology) 是美國國家標準與技術研究院,MNIST 是由 NIST 所提供的一組經典的機器學習測資,可以想成是深度學習中的「Hello World!」,它由 60000張 訓練圖片與 10000 張測試圖片所組成,為手寫數字的灰階圖片,大小為 28 * 28 像素,分類 0 到 9 共 10 個數字。 可透過 keras 模組直接取得資料 >>> from tensorflow.keras.datasets import mnist 輸入 mnist.load_data() 可取得 mnist 資料集,回傳值為 2*2 的 tuple of ndarray。 >>> (train_images, train_labels), (test_images, test_labels) = mnist.load_data() tuple 裡面裝載的是 NumPy 的 ndarray 物件,我們可以利用 o.shape 來取得 ndarray 的屬性 len(o) 來取得陣列的個數 >>> train_images.shape (60000, 28, 28) # 3 軸陣列,其大小為 60000 * 28 * 28 >>> test_images....

<span title='2023-10-28 14:11:35 +0800 +0800'>October 28, 2023</span>&nbsp;·&nbsp;3 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