vault backup: 2025-02-10 17:27:57

This commit is contained in:
2025-02-10 17:27:57 +08:00
parent 60a1c02c6a
commit 5752038f86
159 changed files with 1 additions and 1 deletions

View 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]]

View 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打亂。

View 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/)

View 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]]