Skip to content
The Chat Archive
gpt-3.5-turbo-0301· OpenAI WildChat-4.8M (AI2)

以python语言编写一个程序,分别用RNN、LSTM、GRU网络建立训练模型,并对模型进行评估,最后分别使用这些模型进行预测。

  1. User

    以python语言编写一个程序,分别用RNN、LSTM、GRU网络建立训练模型,并对模型进行评估,最后分别使用这些模型进行预测。

  2. Assistant

    由于本问题属于机器学习/deep learning问题,我们需要导入一些机器学习/deep learning相关的库,如下所示:

    import numpy as np
    from keras.models import Sequential
    from keras.layers import Dense, SimpleRNN, LSTM, GRU
    from sklearn.metrics import mean_squared_error
    

    接下来,我们可以定义一些超参数,如:

    # Define hyperparameters
    n_input = 5 # Number of input features
    n_output = 1 # Number of output features
    n_steps = 10 # Number of time steps to use in each sample
    n_samples = 10000 # Number of samples to generate
    n_epochs = 10 # Number of epochs to train the model
    n_neurons = 64 # Number of neurons in the RNN/LSTM/GRU layer
    

    接下来,我们可以生成一些随机数据来用于训练和测试模型。这里,我们生成一个包含10个特征,每个特征都是一个正弦函数的时间序列。我们从这个时间序列中生成一些有标签的样本,每个样本包含10个时间步长,即我们把时间序列切割成了10段,每段长度为1。这样,我们就得到了10000个带有10个时间步长和1个标签的样本。代码如下:

    # Generate random dataset
    X = np.zeros((n_samples, n_steps, n_input))
    y = np.zeros((n_samples, n_output))
    
    for i in range(n_samples):
        freq = np.random.uniform(low=0.1, high=1.0)
        phase = np.random.uniform(low=0, high=1.0)
        amplitude = np.random.uniform(low=0.1, high=1.0)
        x = np.linspace(0, 2*np.pi, n_steps)
        X[i,:,0] = amplitude * np.sin(freq*x + phase)
        for j in range(1, n_input):
            X[i,:,j] = np.random.normal(loc=0, scale=1, size=(n_steps,))
        y[i,:] = np.average(X[i,:,0])
    

    接下来,我们可以使用这些数据和我们定义的超参数来构建三个不同的模型:RNN、LSTM和GRU。代码如下:

    # Construct RNN model
    model_rnn = Sequential()
    model_rnn.add(SimpleRNN(n_neurons, input_shape=(n_steps, n_input), activation='relu'))
    model_rnn.add(Dense(n_output))
    model_rnn.compile(loss='mse', optimizer='adam')
    
    # Construct LSTM model
    model_lstm = Sequential()
    model_lstm.add(LSTM(n_neurons, input_shape=(n_steps, n_input), activation='relu'))
    model_lstm.add(Dense(n_output))
    model_lstm.compile(loss='mse', optimizer='adam')
    
    # Construct GRU model
    model_gru = Sequential()
    model_gru.add(GRU(n_neurons, input_shape=(n_steps, n_input), activation='relu'))
    model_gru.add(Dense(n_output))
    model_gru.compile(loss='mse', optimizer='adam')
    

    接下来,我们可以使用这些模型对数据进行训练和评估。我们将数据分为训练集和测试集,使用训练集训练模型,然后使用测试集评估模型的性能。代码如下:

    # Split data into training and test sets
    split_ratio = 0.8
    n_train = int(n_samples * split_ratio)
    X_train = X[:n_train,:,:]
    y_train = y[:n_train,:]
    X_test = X[n_train:,:,:]
    y_test = y[n_train:,:]
    
    # Fit RNN model
    model_rnn.fit(X_train, y_train, epochs=n_epochs, batch_size=64, verbose=1)
    
    # Fit LSTM model
    model_lstm.fit(X_train, y_train, epochs=n_epochs, batch_size=64, verbose=1)
    
    # Fit GRU model
    model_gru.fit(X_train, y_train, epochs=n_epochs, batch_size=64, verbose=1)
    
    # Evaluate models
    y_pred_rnn = model_rnn.predict(X_test)
    rmse_rnn = np.sqrt(mean_squared_error(y_test, y_pred_rnn))
    print('RNN RMSE:', rmse_rnn)
    
    y_pred_lstm = model_lstm.predict(X_test)
    rmse_lstm = np.sqrt(mean_squared_error(y_test, y_pred_lstm))
    print('LSTM RMSE:', rmse_lstm)
    
    y_pred_gru = model_gru.predict(X_test)
    rmse_gru = np.sqrt(mean_squared_error(y_test, y_pred_gru))
    print('GRU RMSE:', rmse_gru)
    

    最后,我们可以使用训练好的模型来进行预测。我们可以使用相同的方式生成一些新的时间序列数据,并把它们输入到模型中进行预测。代码如下:

    # Generate new data and make predictions
    X_new = np.zeros((1, n_steps, n_input))
    freq = np.random.uniform(low=0.1, high=1.0)
    phase = np.random.uniform(low=0, high=1.0)
    amplitude = np.random.uniform(low=0.1, high=1.0)
    x = np.linspace(0, 2*np.pi, n_steps)
    X_new[0,:,0] = amplitude * np.sin(freq*x + phase)
    for j in range(1, n_input):
        X_new[0,:,j] = np.random.normal(loc=0, scale=1, size=(n_steps,))
    print('Input:', X_new[0,:,0])
    print('True output:', np.average(X_new[0,:,0]))
    
    y_pred_rnn = model_rnn.predict(X_new)
    print('RNN prediction:', y_pred_rnn[0,0])
    
    y_pred_lstm = model_lstm.predict(X_new)
    print('LSTM prediction:', y_pred_lstm[0,0])
    
    y_pred_gru = model_gru.predict(X_new)
    print('GRU prediction:', y_pred_gru[0,0])
    

    完整代码如下:

    import numpy as np
    from keras.models import Sequential
    from keras.layers import Dense, SimpleRNN, LSTM, GRU
    from sklearn.metrics import mean_squared_error
    
    # Define hyperparameters
    n_input = 5 # Number of input features
    n_output = 1 # Number of output features
    n_steps = 10 # Number of time steps to use in each sample
    n_samples = 10000 # Number of samples to generate
    n_epochs = 10 # Number of epochs to train the model
    n_neurons = 64 # Number of neurons in the RNN/LSTM/GRU layer
    
    # Generate random dataset
    X = np.zeros((n_samples, n_steps, n_input))
    y = np.zeros((n_samples, n_output))
    
    for i in range(n_samples):
        freq = np.random.uniform(low=0.1, high=1.0)
        phase = np.random.uniform(low=0, high=1.0)
        amplitude = np.random.uniform(low=0.1, high=1.0)
        x = np.linspace(0, 2*np.pi, n_steps)
        X[i,:,0] = amplitude * np.sin(freq*x + phase)
        for j in range(1, n_input):
            X[i,:,j] = np.random.normal(loc=0, scale=1, size=(n_steps,))
        y[i,:] = np.average(X[i,:,0])
    
    # Construct RNN model
    model_rnn = Sequential()
    model_rnn.add(SimpleRNN(n_neurons, input_shape=(n_steps, n_input), activation='relu'))
    model_rnn.add(Dense(n_output))
    model_rnn.compile(loss='mse', optimizer='adam')
    
    # Construct LSTM model
    model_lstm = Sequential()
    model_lstm.add(LSTM(n_neurons, input_shape=(n_steps, n_input), activation='relu'))
    model_lstm.add(Dense(n_output))
    model_lstm.compile(loss='mse', optimizer='adam')
    
    # Construct GRU model
    model_gru = Sequential()
    model_gru.add(GRU(n_neurons, input_shape=(n_steps, n_input), activation='relu'))
    model_gru.add(Dense(n_output))
    model_gru.compile(loss='mse', optimizer='adam')
    
    # Split data into training and test sets
    split_ratio = 0.8
    n_train = int(n_samples * split_ratio)
    X_train = X[:n_train,:,:]
    y_train = y[:n_train,:]
    X_test = X[n_train:,:,:]
    y_test = y[n_train:,:]
    
    # Fit RNN model
    model_rnn.fit(X_train, y_train, epochs=n_epochs, batch_size=64, verbose=1)
    
    # Fit LSTM model
    model_lstm.fit(X_train, y_train, epochs=n_epochs, batch_size=64, verbose=1)
    
    # Fit GRU model
    model_gru.fit(X_train, y_train, epochs=n_epochs, batch_size=64, verbose=1)
    
    # Evaluate models
    y_pred_rnn = model_rnn.predict(X_test)
    rmse_rnn = np.sqrt(mean_squared_error(y_test, y_pred_rnn))
    print('RNN RMSE:', rmse_rnn)
    
    y_pred_lstm = model_lstm.predict(X_test)
    rmse_lstm = np.sqrt(mean_squared_error(y_test, y_pred_lstm))
    print('LSTM RMSE:', rmse_lstm)
    
    y_pred_gru = model_gru.predict(X_test)
    rmse_gru = np.sqrt(mean_squared_error(y_test, y_pred_gru))
    print('GRU RMSE:', rmse_gru)
    
    # Generate new data and make predictions
    X_new = np.zeros((1, n_steps, n_input))
    freq = np.random.uniform(low=0.1, high=1.0)
    phase = np.random.uniform(low=0, high=1.0)
    amplitude = np.random.uniform(low=0.1, high=1.0)
    x = np.linspace(0, 2*np.pi, n_steps)
    X_new[0,:,0] = amplitude * np.sin(freq*x + phase)
    for j in range(1, n_input):
        X_new[0,:,j] = np.random.normal(loc=0, scale=1, size=(n_steps,))
    print('Input:', X_new[0,:,0])
    print('True output:', np.average(X_new[0,:,0]))
    
    y_pred_rnn = model_rnn.predict(X_new)
    print('RNN prediction:', y_pred_rnn[0,0])
    
    y_pred_lstm = model_lstm.predict(X_new)
    print('LSTM prediction:', y_pred_lstm[0,0])
    
    y_pred_gru = model_gru.predict(X_new)
    print('GRU prediction:', y_pred_gru[0,0])