Six steps to build a neural network using TensorFlow

Use TensorFlow API: tf.keras to build a neural network

Six steps to build a neural network:

1. Import a third-party library: import
2. Import and understand the data, divide the training set and test set: train test
3. Build the network structure in Sequential(). Describe each layer of the network layer by layer, which is equivalent to forward propagation. : model=tf.keras.models.Sequential
4. Configure the training method in compile. That is, which optimizer to choose, which loss function to choose, and which evaluation indicator to choose. model.compile
5. Train in fit. Inform the input features and labels of the training set and test set. How much is each betch, and how many iterations of the data set: model.fit
6. Use model.summary to print out the structure and parameters of the network.

Function usage introduction

1.model=tf.keras.models.Sequential

The Sequential function is a container that encapsulates the network structure of the neural network and describes the network structure of the input parameters of the Sequential function from the input layer to the output layer.
Such as:

Straightening layer: The tf.keras.layers.Flatten()
straightening layer can transform the size of the tensor and straighten the input features into a one-dimensional array. It is a layer without calculation parameters.

Fully connected layer:tf.keras.layers.Dense( 神经元个数,activation=”激活函数”, kernel_regularizer=”正则化方式”)

Among them:
activation (given by string) can be relu, softmax, sigmoid, tanh, etc., kernel_regularizer can be tf.keras.regularizers.l1(),
tf.keras.regularizers.l2()
Convolutional layer: tf.keras. layers.Conv2D( filter = number of convolution kernels, kernel_size = convolution kernel size,
strides = convolution step size, padding = “valid” or “same”)

LSTM layer:tf.keras.layers.LSTM()。

2.Model.compile

Compile is used to configure the training method of the neural network, and inform the optimizer, loss function and accuracy evaluation standard used during training.

Model.compile( optimizer = 优化器, loss = 损失函数, metrics = [“准确率”])
(1) The optimizer can be the name of the optimizer given in string form, or it can be in function form. The learning rate, momentum, and hyperparameters can be set using the function form.
The options are:
'sgd'or tf.optimizers.SGD( lr=learning rate, decay=learning rate decay rate, momentum=momentum parameter)

'adagrad'or tf.keras.optimizers.Adagrad(lr=learning rate,decay=learning rate decay rate)

'adadelta'or tf.keras.optimizers.Adadelta(lr=learning rate, decay=learning rate decay rate)

'adam'or tf.keras.optimizers.Adam (lr=learning rate, decay=learning rate decay rate)
(2) Loss can be the name of the loss function given in string form, or it can be in functional form.
Options include:
'mse'or tf.keras.losses.MeanSquaredError()
'sparse_categorical_crossentropy or tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False)

The loss function often needs functions such as softmax to convert the output into a probability distribution. from_logits is used to mark whether the loss function needs to be converted into a probability form. If it is False, it means that it is converted into a probability distribution. If it is True, it means it is not converted into a probability distribution and output directly.
(3) Metrics mark network evaluation indicators.
Options include:
'accuracy': y_ and y are both numerical values, such as y_=[1] y=[1].
'categorical_accuracy': y_ and y are both represented by one-hot codes and probability distributions.
For example, y_=[0, 1, 0], y=[0.256, 0.695, 0.048].
'sparse_ categorical_accuracy': y_ is given in numerical form, and y is given in one-hot code form
. For example, y_=[1], y=[0.256, 0.695, 0.048].

3.model.fit()

The fit function is used to perform the training process.
——Model.fit (input feature of training set, label of training set, batch_size, epochs, validation_data = (input feature of test set, label of test set), validataion_split = how much ratio is divided from test set to training set, validation_freq = The number of epoch intervals tested)

4.model.summary()

The summary function is used to print the network structure and parameter statistics. The
Insert picture description herefigure above is the network structure and parameter statistics of the iris classification network of model.summary(). For a fully connected network with 4 inputs and 3 outputs, there are 15 parameters in total.

Guess you like

Origin blog.csdn.net/waner_jiaki/article/details/109903373