Steps to build a neural network

Six steps to build a neural network

1. Import related modules (import). Modules such as tensorflow, numpy, etc.
2. Tell the training set and test set (train, test) to be fed to the network, you need to specify the input feature x_train of the training set and the label y_train of the training set, and the input feature x_test of the test set and the test set Label y_test.
3. Building a network structure in Sequential(), describing each layer of the network layer by layer, which is equivalent to going through the forward propagation. (Sequential() can build a sequential network structure in which the output of the upper layer is the input of the lower layer, but it cannot write a non-sequential network structure with jump connections. You can use a custom class to build a neural network structure) model=tf.keras.models.Sequential
You can think of Sequential() as a container that encapsulates a neural network structure.
model=tf.keras.models.Sequential([网络结构])#
Sequential is used to describe the network structure of each layer. The
network structure includes:
Straighten layer: tf.keras.layers.Flattern()
This layer does not contain calculations, only shapes Conversion, straighten the input features into a one-dimensional array
Fully connected layer: tf.keras.layers.Dense (number of neurons, activation='activation function', kernel_regularize=what kind of regularization)
Activation includes: relu, softmax, sigmoid ,tanh
Kernel_regularizer optional: tf.keras.regularizers.l1(), tf.keras.regularizers.l2()
Convolutional layer: tf.keras.layers.Conv2D(filters=number of convolution kernels, kernel_size=convolution kernel size, strides=convolution step length, padding='valid' or'same')
LSTM (long short-term memory) Layer: tf.keras.layers.LSTM()
4. Configure the training method in compile, tell the computer to choose which optimizer, which loss function, and which evaluation index to choose. model.compile
model.compile(optimizer=optimizer, loss=loss function, metrics=["accuracy"] (p evaluation index))
optimizer optional:
'sgd' or tf.keras.optimizers.SGD(lr=learn Rate, momentum=momentum parameter)
'adagrad' or tf.keras.optimizers.Adagrad(lr=learning rate)
'adadelta' or tf.keras.optimizers.Adadelta(lr=learning rate)
'adam' or tf.keras.optimizers .Adam(lr=learning rate, beta_1=0.9,beta_2=0.99)
loss optional:
'mse' or tf.keras.losses.MeanSquareError()
'sparse_categorical_crossentropy' or tf.keras.losses.SparseCategoricalCrossEntropy(from_logits=False)
#from_logits=True indicates that the data is original data, that is, the parameter has not passed the probability distribution of softmax, and from_logits=False indicates that the data has been disrupted and is no longer the original data.
Metrics optional:
'accuracy': y_ and y are both numeric values, such as y_=[1] y=[1]
'categorical_accuracy': y_ and y are both one-hot codes (probability distribution), such as y_=[0 ,1,0] y=[0.256,0.695,0.049]
'sparse_categorical_accuracy': y_s is a number, y is a one-hot code (probability distribution), such as y_=[1],y=[0.256,0.695,0.049]
5. Perform the training process in fit, tell the input features and labels of the training set and test set, tell the number of each batch, and tell how many data sets to iterate. model.fit()
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),
Validation_split=How much is the proportion of the training set divided to Test set,
Validation_freq=How many epochs are tested once)
6. Use summary() to print out the network structure and parameter statistics.

Guess you like

Origin blog.csdn.net/weixin_45187794/article/details/108111233