TensorFlow2.0 -- Sequential model and functional API to construct neural network structure

1, Introduction to data sets and models

The source of the data set is the Fashion MNIST data set, which is the clothing map data. The data set contains 70000 gray images in 10 categories. We use this data to build a neural network model and train it. The structure of the model is input=784, layer1=128, output=10.

The image of the dataset shows a single piece of clothing in low resolution (28x28 pixels), as follows:

 

 

2, Sequential serialization model

1. Construction of network structure

One of the methods of constructing network structure in tensorflow is to use sequential serialization model. Sequential can be regarded as a sequence container, which encapsulates the structure of neural network. Let's take a look at the example code:

# use keras.Sequential To construct the structure of neural network, method 1: directly construct it with list
model = tf.keras.Sequential([
    tf.keras.layers.Flatten(input_shape=(28, 28)),  # The pixel of the input picture is 28*28,Convert it to 1D as input
    tf.keras.layers.Dense(128, activation='relu'),  # 128 neurons in hidden layer
    tf.keras.layers.Dense(10)   # The output is 10 categories. There is no need to activate the function here for subsequent loss calculation softmax And
])

# use keras.Sequential Constructing the structure of neural network, method 2, using add()Methods add neural network layer
model=tf.keras.Sequential()
model.add(tf.keras.layers.Flatten(input_shape=(28, 28)))
model.add(tf.keras.layers.Dense(128, activation='relu'))
model.add(tf.keras.layers.Dense(10))

The above is TF keras. Sequential package with TF keras. Layers build the network layer. The following two functions are introduced:

 

 

  1),tf.keras.Sequential(layers=None,name=None)
  • Layers: a TF keras. list or tuple of layers.

Used to create a Sequential model. You can use the add() method to add the layer structure and the summary() method to view the model structure.

 

  2),tf.keras.layers.Flatten(input_shape)
  • input_shape: enter the shape.

It is used to convert multidimensional data into one bit data, which is equivalent to NP of numpy Reval() function.

 

  3),tf.keras.layers.Dense(units,activation)
  • units: number of neurons in hidden layer
  • activation: activate function

It is used to build the internal structure of the hidden layer and output layer. For other detailed parameters, please see the official description.

 

2. View model structure

After we have created the model, we'd better see how the network structure of the model looks. At this time, there is a method TF keras. Sequential(). Summary () can view the structure of our model.

Now build a model with code and see its structure:

# use keras.Sequential Constructing the structure of neural network, method 2, using add()Methods add neural network layer
model=tf.keras.Sequential(layers=None,name=None)
model.add(tf.keras.layers.Flatten(input_shape=(28, 28)))
model.add(tf.keras.layers.Dense(128, activation='relu'))
model.add(tf.keras.layers.Dense(10))

# Output network results
model.summary()

Output result:

Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
flatten (Flatten)            (None, 784)               0         
_________________________________________________________________
dense (Dense)                (None, 128)               100480    
_________________________________________________________________
dense_1 (Dense)              (None, 10)                1290      
=================================================================
Total params: 101,770
Trainable params: 101,770
Non-trainable params: 0
_________________________________________________________________

 

3. Compiling model

After the structure of the model is built, we also need to build the training content of the model, such as optimizer, loss function and evaluation criteria. At this time, we can compile it with the complie() method

# use sequential Class compile Method compilation model: optimizer is adam,
model.compile(
    optimizer='adam',
    loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True), # from_logits=True Indicates that the loss is due to softmax Functional
    metrics=['accuracy']    # The evaluation method is accuracy
)

 

4. Incoming training data training model

After the model is compiled, the model can be trained. The fit() method is used for model training. fit() method can be used for epoch, batch and validation during model training_ Data to adjust.

# Training model, through fit Methods the incoming data were trained for 10 generations, and the batch size of each generation was 200,Take training set 0 for each batch.1 Validation set of data
model.fit(x=train_images, y=train_labels, epochs=10,batch_size=200,validation_split=0.1)

 

 

3, Summary of Sequential construction model

The following is a summary of the steps of Sequential construction of neural network mechanism:

  1. model=tf.keras.Sequential encapsulates a neural network structure, model Summary () is used to view the model structure.
  2. The. compile method compiles the model.
  3. . fit method to train the model.

 

  

 

  

Posted by Ange52 on Sat, 07 May 2022 03:16:50 +0300