一、數據集簡介
二、MNIST數據集介紹
三、CIFAR 10/100數據集介紹
四、tf.data.Dataset.from_tensor_slices()
五、shuffle()隨機打散
六、map()數據預處理
七、實戰
import tensorflow as tf import tensorflow.keras as keras import os os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' def prepare_mnist_features_and_labels(x,y): x = tf.cast(x, tf.float32) / 255.0 y = tf.cast(y, tf.int64) return x,y def mnist_dataset(): (x,y), (x_test,y_test) = keras.datasets.fashion_mnist.load_data() #numpy中的格式 y = tf.one_hot(y, depth=10) #[10k] ==> [10k,10]的tensor y_test = tf.one_hot(y_test, depth=10) ds = tf.data.Dataset.from_tensor_slices((x,y)) ds = ds.map(prepare_mnist_features_and_labels) #數據預處理,注意:tf.map中傳進的參數 ds = ds.shuffle(60000).batch(100) #隨機打散,讀取一個batch的樣本 ds_val = tf.data.Dataset.from_tensor_slices((x_test,y_test)) ds_val = ds_val.map(prepare_mnist_features_and_labels) ds_val = ds_val.shuffle(10000).batch(100) return ds, ds_val def main(): ds, ds_val = mnist_dataset() print("訓練集信息如下:") iteration_ds = iter(ds) iter_ds = next(iteration_ds) print(iter_ds[0].shape, iter_ds[1].shape) print("測試集信息如下:") iteration_ds_val = iter(ds_val) iter_ds_val = next(iteration_ds_val) print(iter_ds_val[0].shape, iter_ds_val[1].shape) if __name__ == '__main__': main()