A: Use tf.keras.model.Sequential build a classification model includes seven steps:
- Import packet module
- Loading a data set (used here keras.datasets.fashion_mnist packet)
- Segmentation training and validation sets
- Data normalization
- Build a classification model
- Trainer
- The model is applied to the test set
II: Import Package
There will be a one-time import machine learning to use the library frequently.
1 import numpy as np 2 import pandas as pd 3 import matplotlib as mpl 4 import matplotlib.pyplot as plt 5 import sklearn 6 import tensorflow as tf 7 import tensorflow.keras as keras
Print information about the library
1 for module in np, pd, mpl, sklearn, tf, keras: 2 print (module.__name__, module.__version__)
Three: Load data set
As used herein, the keras carrying data set for identifying the image data
1 fashion_mnist = keras.datasets.fashion_mnist 2 (x_train_all, y_train_all), (x_test, y_test) = fashion_mnist.load_data()
Before processing the data, we should look at the data in the dataset sample, we define two functions to see what the data set of pictures
1 # -defined function, see picture 2 DEF show_single_img (img_arr): . 3 plt.imshow (img_arr, CMap = ' binary ' ) . 4 plt.show () . 5 show_single_img (x_train_all [0])
Showing results:
1 # -defined functions, display multiple rows and columns picture. N_rows x_data display before the pictures * n_cols 2 DEF show_images (n_rows, n_cols, x_data, y_data, class_names): . 3 Assert len (x_data) == len (y_data) # is the number of image data to be set and the number of its category same . 4 Assert n_rows n_cols * <len (x_data) # amount of data to be displayed must be less than the amount of data in the data set . 5 plt.figure (figsize = (* n_rows for 1.5, for 1.5 n_cols * )) . 6 for Row in Range (n_rows): . 7 for COL in Range (n_cols): . 8 index = n_cols * Row + COL . 9 plt.subplot(n_rows, n_cols, index+1) 10 plt.imshow(x_data[index], cmap='binary', interpolation='nearest') 11 plt.axis('off')# 不显示坐标轴 12 plt.title(class_names[y_data[index]]) 13 plt.show() 14 class_names = ['T-shirt', 'Trouser', 'Pullover', 'Dress', 'Coat ' , ' of Sandal ' , ' Shirt ' , 15 ' of Sandal ' , ' Shirt ' , ' the Sneaker ' , ' Bag ' , ' Ankle_boot ' ] # fashion_mnist data set of image categories 16 show_images (. 3,. 3, x_test, android.permission.FACTOR., class_names)
Showing results:
Four: segmentation training and validation sets