[tensorflow] continuous input neural network model training code

[tensorflow] continuous input neural network model training code

  Check out the three models in this series: [tensorflow] Continuous input linear regression model training code [tensorflow] Continuous input neural network model training code [tensorflow] Continuous input + discrete input neural network model training code
  
  
  

Full code - copy and play

from sklearn.model_selection import train_test_split
import tensorflow as tf
import numpy as np
from keras import Input, Model, Sequential
from keras.layers import Dense, concatenate, Embedding, LSTM
from sklearn.preprocessing import StandardScaler
from tensorflow import keras

def get_data():
    # 设置随机种子,以确保结果可复现(可选)
    np.random.seed(0)

    # 生成随机数据
    data = np.random.rand(10000, 10)
    
    # 正则化数据
    scaler = StandardScaler()
    data = scaler.fit_transform(data)

    # 生成随机数据
    target = np.random.rand(10000, 1)

    return train_test_split(data, target, test_size=0.1, random_state=42)


data_train, data_val, target_train, target_val = get_data()

# 迭代轮次
train_epochs = 10
# 学习率
learning_rate = 0.0001
# 批大小
batch_size = 200


model = keras.models.Sequential([
    keras.layers.Dense(64, activation="relu", input_shape=[10]),
    keras.layers.Dense(64, activation="relu"),
    keras.layers.Dense(1)
])

model.summary()

model.compile(loss="mse", optimizer=keras.optimizers.Adam(lr=learning_rate))
history = model.fit(data_train, target_train, epochs=train_epochs, batch_size=batch_size, validation_data=(data_val, target_val))

training output

  The model structure is as follows:
insert image description here

  The output during model training is as follows:

insert image description here

code introduction

  The get_data function is used to generate random training and validation datasets. First use np.random.rand to generate a random data set of shape (10000, 10) to simulate a 10-dimensional continuous input, and then use StandardScaler to standardize the data. Then generate a target of (10000, 1), which represents the final fitting target score. Finally, use the train_test_split function to divide the dataset into a training set and a validation set.

  Since the target is a floating point number, our task is a regression task.

  Use keras.models.Sequential to build a sequential model. The model consists of a sequence of layers connected in sequence. In this example, the model consists of three fully connected layers.

  The first hidden layer (keras.layers.Dense) has 64 neurons, uses the ReLU activation function, and specifies the input shape as [10]. The input shape represents the dimensionality of the input data.
  The second hidden layer is also a fully connected layer with 64 neurons, also using the ReLU activation function.
  The last layer is the output layer, which consists of one neuron and does not use an activation function.
  The structure of the model is input layer (10 dimensions) → hidden layer (64 neurons, ReLU activation function) → hidden layer (64 neurons, ReLU activation function) → output layer (1 neuron).

  Finally, configure the model's loss function and optimizer using the model.compile method. In this example, the loss function is set to mean squared error (Mean Squared Error, MSE), the optimizer selects the Adam optimization algorithm, and sets the learning rate to learning_rate.

  Use the model.fit method to train the model. Pass in training data data_train and target data target_train, set training rounds train_epochs, batch size batch_size, and validation set data (data_val, target_val).

  During the training process, the model will update the parameters according to the given training data and target data, and optimize the weight and bias of the model through the back propagation algorithm. Each training epoch performs a complete training on the entire training dataset. The training process also evaluates the model using the validation set data to monitor the model's performance and loss on the validation set.

  The loss value and other indicators during the training process will be recorded in the history object, which can be used for subsequent visualization and analysis.

Guess you like

Origin blog.csdn.net/qq_43592352/article/details/131258632
Recommended