Optimize LSTM model

I have created an LSTM model to predict the heating power needed to maintain a certain temperature inside a room. I don’t have a real dataset, so I took some weather data and I’m simulating the passage of time. So at each step I want the model to predict the power needed for the next 15 minutes and then calculate the internal temperature based on that predicted value.

Here the code:

lass LSTMModel(nn.Module):
    def __init__(self, input_size=10, hidden_size=96, num_layers=2, output_size=1):
        super(LSTMModel, self).__init__()
        self.hidden_size = hidden_size
        self.num_layers = num_layers
        
        self.lstm = nn.LSTM(input_size, hidden_size, num_layers, batch_first=True)

        self.fc = nn.Linear(hidden_size, output_size)
        self.relu = nn.ReLU()

    def forward(self, x, room):

        # reset hn and cn at each batch
        hn = torch.zeros(self.num_layers, batch_size, self.hidden_size).to(device)
        cn = torch.zeros(self.num_layers, batch_size, self.hidden_size).to(device)
        
        # Define t0 tensor for power and internal temp
        power = [torch.zeros((batch_size,1,1), dtype=torch.float32).to(device)] # shape (16,1,1)
        int_temp = [torch.tensor([NormilizeTemp(dfmin, dfmax, 10.0)], dtype=torch.float32).to(device).unsqueeze(1).expand(batch_size, 1, -1)] # shape (16,1,1)
        
        # Go throw all the 96 timestamp (15 minutes * 24)
        # -1 because the line 95 is 23:45. But I don't wont to predict the 
        # 00:00 because it corrispond to the next day
        for i in range(0, window_size-1):

            lstm_input = torch.cat((x[:,i:i+1,:], power[i], int_temp[i]),dim=2) # shape (16,1,8)+(16,1,1)+(16,1,1) = (16,1,10)
            
            # svae hn and cn at each step to feed again in the next step
            lstm_out, (hn, cn) = self.lstm(lstm_input, (hn, cn)) # shape out (16,96,1)
            lstm_out = self.relu(lstm_out) 
            output = self.fc(lstm_out) # shape (16,96,1) to (16,1,1)
            output = self.relu(output)

            power.append(output) # append each power to the list
            int_temp.append(room.temp_update(x[:,i:i+1,1:2],power[i+1],int_temp[i])) # append each internal temp to the list

        return torch.cat(power, dim=1), torch.cat(int_temp, dim=1) # return 2 list of shape (16,96,1)

As you can se, the loop is havy dependent on computing each time. In a real state I would have sensors that detect the values live while the model decide.
At each time I feed a batch of size 16 (where each tensor is a day), with 96 timestamps (24h splitted in 15 minutes), and 10 features (where feature 9 and 10 are dependent on the previous step). Also, room is just a class with the characteristics (height, width, length, etc.) of the room the model is running on, the function “temp_update” compute the temperature using some thermodynamic formulas.

  1. The reasoning I’m doing seems fair. If someone has something to suggest Is appreciated.
  2. Is there a way to make the loop faster? Because right now I’m doing 10 iterations for second. Maybe I should increase from 15 minutes to 20, or even 30. I was thinking to compute 1 day at time. But maybe is even worst.

Thank you for your attention, I hope someone could help me. If you have question, just ask.

1 Like