vault backup: 2025-02-10 17:27:57
This commit is contained in:
10
21.01. Programming/Keras/Keras.tensorflow - Dataset.md
Normal file
10
21.01. Programming/Keras/Keras.tensorflow - Dataset.md
Normal file
@@ -0,0 +1,10 @@
|
||||
可以使用 `tensorflow.keras.utils.image_dataset_from_directory` 來建立 dataset。
|
||||
dataset 會有 `data_batch` 與 `label_batch` 這兩個 member,分別代表資料與標籤。
|
||||
可以用 `dataset.batch(32)` 改變 batch size。
|
||||
還有一些其他的有用function:
|
||||
- `shuffle(buffer_size)`: 打亂順序,可參考[[Keras.tensorflow - shuffle#^832c8c]]
|
||||
- `prefetch(buffer_size)`: 設定預讀的大小
|
||||
- `map(callback_func)`: 用 callback_func 來處理資料
|
||||
- `take(N)`: 取出第N筆的批次資料,注意這一筆是一個批次資料,裡面可能有32筆資料(或其他數量,看你的 `dataset.batch(N)` 怎麼設定)。
|
||||
|
||||
打亂data的方法,請看[[Keras.tensorflow - shuffle]]
|
||||
37
21.01. Programming/Keras/Keras.tensorflow - shuffle.md
Normal file
37
21.01. Programming/Keras/Keras.tensorflow - shuffle.md
Normal file
@@ -0,0 +1,37 @@
|
||||
如果想用同時打亂x_train與y_train,可以參考這2個方法。
|
||||
|
||||
## 1. 用 `tf.random.shuffle`
|
||||
```python
|
||||
indices = tf.range(start=0, limit=tf.shape(x_data)[0], dtype=tf.int32)
|
||||
idx = tf.random.shuffle(indices)
|
||||
x_data = tf.gather(x_data, idx)
|
||||
y_data = tf.gather(y_data, idx)
|
||||
```
|
||||
|
||||
先建立一個跟array一樣大的list,然後打亂它,再用這個已打亂的list當作索引來建立一個新的data list。
|
||||
|
||||
## 2. 用 `Dataset.shuffle`
|
||||
|
||||
^832c8c
|
||||
|
||||
```python
|
||||
x_train = tf.data.Dataset.from_tensor_slices(x)
|
||||
y_train = tf.data.Dataset.from_tensor_slices(y)
|
||||
|
||||
x_train, y_train = x_train.shuffle(buffer_size=2, seed=2), y_train.shuffle(buffer_size=2, seed=2)
|
||||
dataset = tf.data.Dataset.from_tensor_slices((x_train, y_train))
|
||||
```
|
||||
|
||||
或者
|
||||
|
||||
```python
|
||||
BF = 2
|
||||
SEED = 2
|
||||
def shuffling(dataset, bf, seed_number):
|
||||
return dataset.shuffle(buffer_size=bf, seed=seed_number)
|
||||
|
||||
x_train, y_train = shuffling(x_train, BF, SEED), shuffling(y_train, BF, SEED)
|
||||
dataset = tf.data.Dataset.from_tensor_slices((x_train, y_train))
|
||||
```
|
||||
|
||||
概念跟第一點是一樣的,但是這是先轉成 `tf.data.Dataset`,然後把x_train跟y_train都用同樣的seed打亂。
|
||||
14
21.01. Programming/Keras/Machine Learning.md
Normal file
14
21.01. Programming/Keras/Machine Learning.md
Normal file
@@ -0,0 +1,14 @@
|
||||
|
||||
## 問題分類
|
||||
|
||||
| 問題類型 | 啟動函數 | 損失函數 | 範例 |
|
||||
|:--------------:|:--------:|:------------------------------------------------------------------------------:|:------------------------------------------------------------------:|
|
||||
| 二元分類 | sigmoid | binary_crossentropy(二元交叉熵) | |
|
||||
| 單標籤多元分類 | softmax | [[categorical_crossentropy]](分類交叉熵)<br> sparse_categorical_crossentropy | 範例:[[An example that use categorical_crossentropy and softmax]] |
|
||||
| 多標籤分類 | sigmoid | binary_crossentropy | |
|
||||
| 回歸求值 | None | mse(均方誤差) | |
|
||||
| 回歸求0~1值 | sigmoid | mse或binary_crossentropy | |
|
||||
|
||||
|
||||
## Callback
|
||||
- [ReduceLROnPlateau](https://keras.io/api/callbacks/reduce_lr_on_plateau/)
|
||||
3
21.01. Programming/Keras/categorical_crossentropy.md
Normal file
3
21.01. Programming/Keras/categorical_crossentropy.md
Normal file
@@ -0,0 +1,3 @@
|
||||
- 僅適用於 one-hot 編碼。
|
||||
- 如果輸出不是 one-hot,而是整數標籤,也就是直接輸出 0、1、2,而不是一個array(`[0, 0, 0, 1, 0]` 之類),那就需要 sparse_categorical_crossentropy。
|
||||
- 範例:[[An example that use categorical_crossentropy and softmax]]
|
||||
Reference in New Issue
Block a user