匯入包:
import matplotlib as mpl
import matplotlib.pyplot as plt
%matplotlib inline
import numpy as np
import sklearn
import pandas as pd
import os
import sys
import time
import tensorflow as tf
from tensorflow import keras
print(tf.__version__)
print(sys.version_info)
for module in mpl, np, pd, sklearn, tf, keras:
print(module.__name__, module.__version__)
輸出:
1.13.1
sys.version_info(major=3, minor=7, micro=4, releaselevel=’final’, serial=0)
matplotlib 3.1.3
numpy 1.18.1
pandas 1.0.1
sklearn 0.22.1
tensorflow 1.13.1
tensorflow._api.v1.keras 2.2.4-tf
fashion_mnist = keras.datasets.fashion_mnist
(x_train_all, y_train_all), (x_test, y_test) = fashion_mnist.load_data()
x_valid, x_train = x_train_all[:5000], x_train_all[5000:]
y_valid, y_train = y_train_all[:5000], y_train_all[5000:]
print(x_valid.shape, y_valid.shape)
print(x_train.shape, y_train.shape)
print(x_test.shape, y_test.shape)
輸出:
(5000, 28, 28) (5000,)
(55000, 28, 28) (55000,)
(10000, 28, 28) (10000,)
歸一化:
from sklearn.preprocessing import StandardScaler
# x_train: [None, 28, 28] -> [None, 784]
scaler = StandardScaler()
x_train_scaled = scaler.fit_transform(
x_train.astype(np.float32).reshape(-1, 1)).reshape(-1, 28 * 28)
x_valid_scaled = scaler.transform(
x_valid.astype(np.float32).reshape(-1, 1)).reshape(-1, 28 * 28)
x_test_scaled = scaler.transform(
x_test.astype(np.float32).reshape(-1, 1)).reshape(-1, 28 * 28)
y_train = np.asarray(y_train, dtype = np.int64)
y_valid = np.asarray(y_valid, dtype = np.int64)
y_test = np.asarray(y_test, dtype = np.int64)
dataset.make_initializable_iterator:
def make_dataset(images, labels, epochs, batch_size, shuffle=True):
dataset = tf.data.Dataset.from_tensor_slices((images, labels))
if shuffle:
dataset = dataset.shuffle(10000)
dataset = dataset.repeat(epochs).batch(batch_size)
return dataset
batch_size = 20
epochs = 10
images_placeholder = tf.placeholder(tf.float32, [None,28 * 28])
labels_placeholder = tf.placeholder(tf.int64, (None,))
dataset = make_dataset(images_placeholder, labels_placeholder,
epochs = epochs,
batch_size = batch_size)
dataset_iter = dataset.make_initializable_iterator()
x, y = dataset_iter.get_next()
with tf.Session() as sess:
sess.run(dataset_iter.initializer,
feed_dict = {
images_placeholder: x_train_scaled,
labels_placeholder: y_train
})
x_val, y_val = sess.run([x, y])
print(x_val.shape)
print(y_val.shape)
sess.run(dataset_iter.initializer,
feed_dict = {
images_placeholder: x_train_scaled,
labels_placeholder: y_train
})
x_val, y_val = sess.run([x, y])
print(x_val.shape)
print(y_val.shape)
輸出:
(20, 784)
(20,)
(20, 784)
(20,)
本作品採用《CC 協議》,轉載必須註明作者和本文連結