Files
Obsidian-Main/20.01. Programming/Keras/Keras.tensorflow - shuffle.md

38 lines
1.1 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
如果想用同時打亂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打亂。