tensorflow 2.1 six-step method to build Keras neural network model

1. Six-step method to build the Keras neural network model
Step 1: Import library functions
Step 2: Divide the data set
Step 3: Build the tf.keras.models.Sequential of the neural network model, build the model
Step 4 : compile select loss function, optimizer, accuracy expression form...
·Step 5: fit select input features, labels, number of iterations, batch size, data set division ratio, how many epochs to verify a data set
· Step 6: Print out the model summery and verify the model


2. Neural Network Layer

Sequential is the container of the neural network layer:
the Flatten layer does not include calculations, but straightens the data into a one-dimensional array. The
Dense fully connected layer includes calculations. You can set the number of neurons, activation functions, regularization methods (l1, l2), etc.
The Conv2 convolution layer has the number of convolution kernels, the size of the convolution kernel, the step size of the convolution kernel, and padding to fill
the LSTM layer, etc.


3. Introduction to network layer creation functions

       1. Introduction to sequential functions

Where regularization corresponds to L1, and L2 regularization method

         2. compile function

tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False)的from_logits参数是在询问输出是否是原始输出或者概率分布输出,若是经过了softmax的概率分布输出,那么该参数设置为False

         3. fit function

4. Examples

import tensorflow as tf
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)

model = tf.keras.models.Sequential([
    tf.keras.layers.Dense(3, activation='softmax', kernel_regularizer=tf.keras.regularizers.l2())
])

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()

Firstly, get the dataset of iris classification and scramble it, create a neural network layer container and add a fully connected layer, set the regularization and activation functions at the same time, compile and set the SGD optimizer and loss function with a learning rate of 0.1 (after softmax Probability distribution output, then this parameter is set to False), fit feeds the data set and label and sets batch and epoch, splits the data set 0.8:0.2, and 20 epochs will verify the accuracy once, and finally print the neural network model as follows :

result: 

The above is a simple six-step method tf.keras to create a neural network model

Reference link:

https://www.icourse163.org/learn/PKU-1002536002?tid=1452937471#/learn/content?type=detail&id=1231348069&cid=1249387128

Guess you like

Origin blog.csdn.net/qq_46006468/article/details/119607501