def predict(pred_input, seq_length, input_size, hidden_size, num_layers,
            num_classes, bidirectional, PATH):
    ''' Function to predict the heat demand needed for the next hour.
    
     Args:
        pred_input:     input data for the prediction model.
        seq_length:     the number of pass input points which needed 
                            for predicting the future value. 
        
        num_epochs:     number of times all the data has passed to the model.
        
        learning_rate:  step size at each iteration while moving 
                            toward a minimum of a loss function
      input_size:       number of input features.
      hidden_size:      number of hidden layer.
      num_classes:      number of outputs.
      bidirectional:    True or False.
      PATH:             name of the model *.pt (pytorch model)
    
    Returns:
       
        lstm:           A train lstm models with the structure define as input.   
    
    '''

    model = LSTM(num_classes, input_size, hidden_size, num_layers,
                 bidirectional, seq_length)
    model.load_state_dict(torch.load(PATH))

    #dataX, dataY, trainX, trainY, testX, testY = data_preprocess(filename,seq_length)

    train_predict = model(pred_input)
    data_predict = train_predict.data.numpy()

    return data_predict
Beispiel #2
0
def train(filename,seq_length,num_epochs,learning_rate,
          input_size,hidden_size,num_layers,num_classes,bidirectional,PATH):
    
    ''' Function to train and save the model for prediction.
    
     Args:
        
        filename:       name of the data set.
        seq_length:     the number of pass input points which needed 
                            for predicting the future value. 
        
        num_epochs:     number of times all the data has passed to the model.
        
        learning_rate:  step size at each iteration while moving 
                            toward a minimum of a loss function
      input_size:       number of input features.
      hidden_size:      number of hidden layer.
      num_classes:      number of outputs.
      bidirectional:    True or False.
      PATH:             name of the save model *.pt (pytorch model)
    
    Returns:
       
        lstm:           A train lstm models with the structure define as input.   
    
    '''
    
    
    dataX, dataY, trainX, trainY, testX, testY = data_preprocess(filename,seq_length)
      
    lstm = LSTM(num_classes, input_size, hidden_size, num_layers,bidirectional,seq_length)
    
    criterion = torch.nn.MSELoss()    # mean-squared error for regression
    optimizer = torch.optim.Adam(lstm.parameters(), lr=learning_rate)
    #optimizer = torch.optim.SGD(lstm.parameters(), lr=learning_rate)
    
    # Train the model
    for epoch in range(num_epochs):
        outputs = lstm(trainX)
        optimizer.zero_grad()
        
        # obtain the loss function
        loss = criterion(outputs, trainY)
        
        loss.backward()
        
        optimizer.step()
        if epoch % 100 == 0:
            print("Epoch: %d, loss: %1.5f" % (epoch, loss.item()))
    
    # Save the model for prediction.
    
    #PATH = PATH
    torch.save(lstm.state_dict(), PATH)
    
    return lstm
Beispiel #3
0
def multi_steps_prediction(pred_input,pred_hour,seq_length,input_size,hidden_size,
                           num_layers,num_classes,bidirectional,PATH):
    
    ''' Function to predict the heat demand needed for the next hours (more than 1 hours).
    
     Args:
        pred_input:     input data for the prediction model.
        pred_hour :     number of predicted hours ahead of time
        seq_length:     the number of pass input points which needed 
                            for predicting the future value. 
        
        num_epochs:     number of times all the data has passed to the model.
        
        learning_rate:  step size at each iteration while moving 
                            toward a minimum of a loss function
      input_size:       number of input features.
      hidden_size:      number of hidden layer.
      num_classes:      number of outputs.
      bidirectional:    True or False.
      PATH:             name of the model *.pt (pytorch model)
    
    Returns:
       
        lstm:           A train lstm models with the structure define as input.   
    
    '''
    
    model = LSTM(num_classes, input_size, hidden_size, num_layers,bidirectional,seq_length)
    model.load_state_dict(torch.load(PATH))
    
    #dataX, dataY, trainX, trainY, testX, testY = data_preprocess(filename,seq_length)
        
    # Copy a test set to a new set
    #testX.shape
    #Create an empty array
    
    predict =np.array([])
    
    for i in range(pred_hour):
        # Select a subset of 12 hour data from the test set
        indices = torch.tensor([i])
        subtest= torch.index_select(pred_input,0,indices)
        # predict the next hour
        temp    = model(subtest)
        # Replace the next value in the test set with a predicted value
        # for predicting the next time step
        pred_input[i+1,-1,0]= temp
        # Add all prediction value together. 
        temp = temp.detach().numpy()
        predict = np.append(predict,temp)
        
    
    
    return predict
def main():
    ''' An example for running the prediction with an assumption that Q_solar and
        internal heat gain for the next hour are not known, all the pass values are known
        and will be used as input for the next predicted hour.
    
    '''

    #filename = 'Heavy_weight.txt'
    filename = 'Light_weight.txt'
    seq_length = 12

    # Prepare the data.
    dataX, dataY, trainX, trainY, testX, testY = data_preprocess(
        filename, seq_length)

    Xtsq = testX.clone().detach()

    # Construct a predicted uncertainties array and add to the predicted set
    # Assume that Solar and internal heat gain are variables with uncertainties.
    #tempset = Variable(torch.Tensor(np.zeros([Xtsq.shape[0],Xtsq.shape[1],2])))
    tempset = Variable(torch.Tensor(np.array([[0, 0]])))

    # Replace Solar and internal heat gain with zero
    # (only replace the last values of the sequences)
    #Xtsq[:,:,3:5]= tempset
    Xtsq[:, -1, 3:5] = tempset

    #Define and load the model.
    input_size = 6
    hidden_size = 20
    num_layers = 1
    num_classes = 1
    bidirectional = True

    # name of the save model
    PATH = "heat_demand.pt"

    # load the model.
    model = LSTM(num_classes, input_size, hidden_size, num_layers,
                 bidirectional, seq_length)
    model.load_state_dict(torch.load(PATH))

    # call prediction function
    predict_test = model(Xtsq)
    predict_data = predict_test.data.numpy()

    # Transform the data to its original form.
    sc_Y = load('sc_Y.bin')
    predict = sc_Y.inverse_transform(predict_data)
    predict[predict < 0] = 0
    Y_val = testY.data.numpy()
    Y_val_plot = sc_Y.inverse_transform(Y_val)

    # Plot the results

    fig, axs = plt.subplots(2, figsize=(20, 12))
    axs[0].plot(Y_val_plot, label='measured')
    axs[0].plot(predict, label='predicted')
    axs[1].plot(Y_val_plot, label='measured')
    axs[1].plot(predict, label='predicted')
    axs[0].title.set_text('Zoom_in')
    axs[1].title.set_text('Heat demand')
    #plt.figure(figsize=(17,6)) #plotting
    axs[0].set_xlim([0, 2000])
    #axs[1].set_xlim([500,1000])
    #plt.xlim([0,0])
    #plt.plot(dataY_plot[:,0],label='measured')
    #plt.plot(data_predict[:,0],label = 'predict')
    #plt.suptitle('Heat-Demand Prediction')
    axs[0].legend()
    axs[1].legend()
    plt.show()
def main():
    ''' An example for running the prediction with an assumption that Q_solar and
        internal heat gain are not known. The model will predict several hours ahead of time.
    
    '''

    #filename = 'Heavy_weight.txt'
    filename = 'Light_weight.txt'
    seq_length = 12

    # Prepare the data.
    dataX, dataY, trainX, trainY, testX, testY = data_preprocess(
        filename, seq_length)

    #Define and load the model.
    input_size = 6
    hidden_size = 20
    num_layers = 1
    num_classes = 1
    bidirectional = True

    # name of the saved model
    PATH = "heat_demand.pt"

    # load the model.
    model = LSTM(num_classes, input_size, hidden_size, num_layers,
                 bidirectional, seq_length)
    model.load_state_dict(torch.load(PATH))

    Xtsq = testX.clone().detach()
    # Construct a predicted uncertainties array and add to the predicted set
    # Assume that Solar and internal heat gain are variables with uncertainties.
    tempset = Variable(
        torch.Tensor(np.zeros([Xtsq.shape[0], Xtsq.shape[1], 2])))
    #tempset = Variable(torch.Tensor(np.array([[0,0]])))

    # Replace Solar and internal heat gain with zero
    # (only replace the last values of the sequences)
    Xtsq[:, :, 3:5] = tempset
    #Xtsq[:,-1,3:5]= tempset

    predict = np.array([])
    pred_hour = 24 * 50

    for i in range(pred_hour):
        # Select a subset of 12 hour data from the test set
        indices = torch.tensor([i])
        subtestX = torch.index_select(Xtsq, 0, indices)
        # predict the next hour

        temp = model(subtestX)

        # Replace the next hour value in the set with a predicted value
        # for predicting the next time step
        Xtsq[i + 1, -1, 0] = temp
        # Add all prediction value together.
        temp = temp.detach().numpy()
        predict = np.append(predict, temp)

    # Transform the data to its original form.
    sc_Y = load('sc_Y.bin')
    predict = sc_Y.inverse_transform(predict)
    predict[predict < 0] = 0
    Y_val = testY.data.numpy()
    Y_val_plot = sc_Y.inverse_transform(Y_val)

    # Plot the results

    fig, axs = plt.subplots(2, figsize=(20, 12))
    axs[0].plot(Y_val_plot, label='measured')
    axs[0].plot(predict, label='predicted')
    axs[1].plot(Y_val_plot, label='measured')
    axs[1].plot(predict, label='predicted')
    axs[0].title.set_text('Zoom_in')
    axs[1].title.set_text('Heat demand')
    axs[0].set_xlim([0, pred_hour])
    axs[1].set_xlim([0, pred_hour])
    axs[0].legend()
    axs[1].legend()
    plt.show()