Practice in Artificial Intelligence: Notes on Tensorflow ii

The article directories

  • preface
  • 1. Building an Internet sequential system
    • 1. Function introduction
    • 2.6 One-step method to achieve iris classification
  • Two, build network eight class
    • 1. Create your own neural network template:
    • 2. Call the model object you created
  • 3. MNIST data set
    • 1. Handwritten digit recognition using sequential network
    • 2. Use class to build a network to realize handwritten number recognition
  • 4. FASHION data set
    • Trousers recognition using sequential network
  • conclusion

preface

Objective of this lecture: Use eight-strand neural network to build eight-strand IRIS code to reproduce MNIST data set and train MNIST data set Fashion data set


1. Building an Internet sequential system

Using the six-step method, using TensorFlow’s API: 2. Tell what the training set and test set to be fed into the network are. Also is to specify the training set and testing set of input features and training set label. 3, the model = tf keras. Models. The Sequential in Sequential (in) building the network structure, step by step description of each layer of network, 4, model.compile configures the training method in compile, tells which optimizer to choose, which loss function to choose, which evaluation index to choose 5, model.fit implements the training process in FIT, tells the input characteristics and labels of training set and test set, Tell each batch what it is and how many data sets to iterate over

1. Function introduction

Sequential () usage: model = tf. Keras. Models. The sequential (# [network]) to describe the layers of network structure, for example: straight layer: tf. Keras. The layers. The Flatter () the connection layer: Tf.keras.layers.Dense(Number of neurons,activation= “activation function”, Kernel_regularizer = which regularization) Activation (string given) optional: Relu, Softmax, SignoID, tanh Kernel_regularizer Optional: Kernel_regularizer.L1 (), Kernel_regularizer.l2 () Convolutional layer: Tf.keras.layers.conv2d (filters= number of convolution kernel_size= size,strides= convolution step,padding= “vaild” or “same”) LSTM layer: tf.kreas.layers.LSTM()

Compile () : compile() : compile() : compile() : compile() : compile() : compile() : compile() : compile() ‘SGD or tf. Keras. Optimizers. SGD (lr = vector, momentum = momentum parameters)’ adagrad or tf. Keras. Optimizers. Adagrad vector (lr =) ‘adadelta or Tf. Keras. Optimizers. Adadelta vector (lr =). “Adam” or tf keras. Optimizers. Adam (lr = vector, beta_1 = 0.9, beta_2 = 0.999) loss optional: ‘mse or tf. Keras. Losses. MeanSquaredError ()’ sparse_categorical_crossentropy or Tf. Keras. Losses. SparseCategoricalCrossentropy (from_logits = False) (some of the neural network output is through the function such as softmax probability distribution, others without directly output probability distribution, From_logits is asking if it is the original output. ‘Accuracy’ :y_pred and y are numbers, For example, y_pred=[1] y=[1] ‘categorical_accuracy’ :y_pred and y are unique heat codes (probability distributions). For example, y_pred=[0,1,0], Y = [0.5, 0.5, 0.5] ‘sparse_categorical_accuracy: y_pred is numerical, y is the sole hot coding, y_pred = [1], y = [0.5, 0.5, 0.5]

Fit () usage: Model.fit (Training set input characteristics, training set label, batCH_size =, EPOchs =, validATION_data =(test set input characteristics, test set label), Validation_split = how much of the training set is split into the test set, validation_freq = how many times is the epoch tested)

The model () usage:

model.summary()

2.6 One-step method to achieve iris classification

The code is as follows:

import tensorflow as tf
from sklearn import datasets
import numpy as np

There is no need to import test separately
x_train = datasets.load_iris().data
y_train = datasets.load_iris().target
# out of order
np.random.seed(116)
np.random.shuffle(x_train)
np.random.seed(116)
np.random.shuffle(y_train)
tf.random.set_seed(116)
#3 neurons, softmax activated, L2 regularized
model = tf.keras.models.Sequential([
    tf.keras.layers.Dense(3, activation='softmax', kernel_regularizer=tf.keras.regularizers.l2())
])
Vector # SGD optimizer, 0.1, use SparseCategoricalCrossentropy as the loss function, due to the end of the neural network using softmax function, output as a probability distribution, so from_logits to false
# iris data set is labeled 0,1,2. The output of neural network forward propagation is probability distribution, and sparse_categorical_accuracy is used as the accuracy rate
model.compile(optimizer=tf.keras.optimizers.SGD(lr=0.1),
              loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False),
              metrics=['sparse_categorical_accuracy'])
# Input training data, feed 32 sets of data at a time, iterate 500 times, divide 20% of the training set into test set, verify the accuracy in the test set every 20 iterations
model.fit(x_train, y_train, batch_size=32, epochs=500, validation_split=0.2, validation_freq=20)
Print network structure and parameter statistics
model.summary()
Copy the code

The print result is as follows:

Two, build network eight class

Sequential networks can be constructed in which the upper output is the lower input, but it is not possible to write some non-sequential networks with hops. In this case, we can choose to build the neural network structure with the class. Using the six-step method, we can use the TensorFlow API: 1, Import 2, train, test 3, Class MyMode(Model) Model =MyModel 4, model.compile 5, model.fit 6, model.summary

1. Create your own neural network template:

The pseudocode is as follows:

class MyModel(Model) :
	def _init_(self) :
		super(MyModel,self).init_() defines the network structure blockdef call(self,x) :Call the network structure block to achieve forward propagationreturn y

model=MyModel()
Copy the code

The code is as follows:

class IrisModel(Model) :
    def __init__(self) :
        super(IrisModel, self).__init__()
        The single layer network of iris classification is a full connection with 3 neurons
        self.d1 = Dense(3, activation='softmax', kernel_regularizer=tf.keras.regularizers.l2())

    def call(self, x) :
        y = self.d1(x)
        return y
Object with the alias model
model = IrisModel()
Copy the code

2. Call the model object you created

The code is as follows:

import tensorflow as tf
from tensorflow.keras.layers import Dense
from tensorflow.keras import Model
from sklearn import datasets
import numpy as np

x_train = datasets.load_iris().data
y_train = datasets.load_iris().target

np.random.seed(116)
np.random.shuffle(x_train)
np.random.seed(116)
np.random.shuffle(y_train)
tf.random.set_seed(116)

class IrisModel(Model) :
    def __init__(self) :
        super(IrisModel, self).__init__()
        self.d1 = Dense(3, activation='softmax', kernel_regularizer=tf.keras.regularizers.l2())

    def call(self, x) :
        y = self.d1(x)
        return y

model = IrisModel()

model.compile(optimizer=tf.keras.optimizers.SGD(lr=0.1),
              loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False),
              metrics=['sparse_categorical_accuracy'])

model.fit(x_train, y_train, batch_size=32, epochs=500, validation_split=0.2, validation_freq=20)
model.summary()
Copy the code

The print result is as follows:

3. MNIST data set

MNIST data set

60,000 handwritten digital images and labels of 28×28 pixels from 0 to 9 are provided for training. 10 000 handwritten digital images and labels of 28×28 pixels from 0 to 9 are provided for testing.

Import data set

mnist =tf.keras.datasets.mnist

(x_train,y_train),(x_test,y_test)=mnist.load_data()

As input features, the neural network is fed by stretching the data into a one-dimensional array

tf.keras.layers.Flatter()

1. Handwritten digit recognition using sequential network

code:

import tensorflow as tf

mnist = tf.keras.datasets.mnist
(x_train, y_train), (x_test, y_test) = mnist.load_data()
# Normalize the characteristics of the input network, so that the original gray values of 0~255 are converted into decimal values of 0~1.
# Reducing the value of input features is more conducive to neural network absorption
x_train, x_test = x_train / 255.0, x_test / 255.0
# Sequential networking
model = tf.keras.models.Sequential([
    tf.keras.layers.Flatten(),                          Straighten the input features into a 1-dimensional array, that is, 784 (28*28) values
    tf.keras.layers.Dense(128, activation='relu'),      The first layer network has 128 neurons, and RELU is the activation function
    tf.keras.layers.Dense(10, activation='softmax')     The second layer network has 10 neurons. Softmax makes the output conform to the probability distribution
])
Configure training methods with compile
model.compile(optimizer='adam',                         
              loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False),
              metrics=['sparse_categorical_accuracy'])
# Test set evaluation is performed for each iteration of training set. With the increase of iteration rounds, the recognition accuracy of handwritten digits is constantly improved (using test sets)
model.fit(x_train, y_train, batch_size=32, epochs=5, validation_data=(x_test, y_test), validation_freq=1)
model.summary()
Copy the code

print result:

Train on 60000 samples, validate on 10000 samples Epoch 1/5 60000/60000 [] – 4s 62us/sample – loss: 0.2589 – sparse_categorical_accuracy: 0.9262 – val_loss: 0.1373 – val_SPARse_categorical_accuracy: 0.9607 Epoch 2/5 60000/60000 [] -2s 40US/sample-loss: 0.1114-SPARse_categorical_accuracy: 0.9676-val_loss: 0.1027-Val_sparse_categorical_accuracy: 0.9699 Epoch 3/5 60000/60000 [] -3s 43us/ sample-loss: 0.0762-sparse_categorical_accuracy: 0.9775-val_loss: 0.0898-val_sparse_categorical_accuracy: 0.9722 Epoch 4/5 60000/60000 [] -2s 41us/ sample-loss: 0.0573-SPARse_categorical_accuracy: 0.9822-val_loss: 0.0851-val_sparse_categorical_accuracy: 0.9752 Epoch 5/5 60000/60000 [] -2s 41us/ sample-loss: 0.0450-sparse_categorical_accuracy: 0.9858 – val_loss: 0.0846-val_sparse_categorical_accuracy: 0.9738 Model: “Sequential” Layer (type) the Output Shape Param # = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = flatten (Flatten) Multiple 0 dense (dense) Multiple 100480 Total Params: 101,770 Trainable Params: 101770 Non – trainable params: 0


It can be observed that with the increase of iteration rounds, the accuracy rate also keeps improving. The parameters of training are also extremely large, reaching more than 100,000.

2. Use class to build a network to realize handwritten number recognition

However, the method of model instantiation is different, which is consistent with the realization of handwritten number recognition by sequential network. The init function defines the layer used in the call function, which passes forward from input X to output Y and returns output y

import tensorflow as tf
from tensorflow.keras.layers import Dense, Flatten
from tensorflow.keras import Model

mnist = tf.keras.datasets.mnist
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0


class MnistModel(Model) :
    def __init__(self) :
        super(MnistModel, self).__init__()
        self.flatten = Flatten()
        self.d1 = Dense(128, activation='relu')
        self.d2 = Dense(10, activation='softmax')

    def call(self, x) :
        x = self.flatten(x)
        x = self.d1(x)
        y = self.d2(x)
        return y


model = MnistModel()

model.compile(optimizer='adam',
              loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False),
              metrics=['sparse_categorical_accuracy'])

model.fit(x_train, y_train, batch_size=32, epochs=5, validation_data=(x_test, y_test), validation_freq=1)
model.summary()
Copy the code

4. FASHION data set

FASHION data set

Provide 60,000 28×28 pixel clothing and other pictures and labels for training.

Provide 10,000 28×28 pixels of clothing and other images and labels for testing.

Import data set

fashion = tf.keras.datasets.fashion_mnist

(x_train, y_train),(x_test, y_test) = fashion.load_data()

Trousers recognition using sequential network

Loading data takes a long time and requires patience

import tensorflow as tf

fashion = tf.keras.datasets.fashion_mnist
(x_train, y_train),(x_test, y_test) = fashion.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0

model = tf.keras.models.Sequential([
    tf.keras.layers.Flatten(),
    tf.keras.layers.Dense(128, activation='relu'),
    tf.keras.layers.Dense(10, activation='softmax')
])

model.compile(optimizer='adam',
              loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False),
              metrics=['sparse_categorical_accuracy'])

model.fit(x_train, y_train, batch_size=32, epochs=5, validation_data=(x_test, y_test), validation_freq=1)
model.summary()
Copy the code

Class method can also be achieved, here do not do repeated expansion, use eight template.


conclusion

This unit went through the framework of the whole training, and made a summary in the form of eight strands, and gained a lot.

MOOC Ai practices: TensorFlow Notes 2