<jupyter_start><jupyter_text>Prepare train and test data<jupyter_code>y = data['S4248SM144NCEN'].values.astype(float)

test_size = 12 ### 12 is for the 12 months in a year

train_data = y[:-test_size]
test_data = y[-test_size:]
test_data
from sklearn.preprocessing import MinMaxScaler
scaler = MinMaxScaler(feature_range=(-1,1))

train_norm = scaler.fit_transform(train_data.reshape(-1,1))
train_norm.min()
train_norm.max()
train_norm.mean()
type(train_norm)<jupyter_output><empty_output><jupyter_text>Prepare data for LSTM<jupyter_code>train_norm = torch.FloatTensor(train_norm).view(-1)

window_size = 12

def input_data(seq, ws):
    out = []
    L = len(seq)
    
    for i in range(L-ws):
        window = seq[i:i+ws]
        label = seq[i+ws:i+ws+1]
        
        out.append((window,label))
    return out

train_data = input_data(train_norm, window_size)
    
train_data[0]
train_data[1]<jupyter_output><empty_output><jupyter_text>Define Model<jupyter_code>class LSTMTSModel(nn.Module):
    
    def __init__(self, input_size=1, hidden_size=100, output_size=1):
        super().__init__()
        
        self.hidden_size = hidden_size
        
        self.lstm = nn.LSTM(input_size, hidden_size)
        self.fc1 = nn.Linear(hidden_size, output_size)
        
        self.hidden = (torch.zeros(1,1,self.hidden_size), torch.zeros(1,1,self.hidden_size)) ## intialisation
        
    def forward(self, x):
        lstm_out, self.hidden = self.lstm(x.view(len(x),1,-1), self.hidden)
        pred = self.fc1(lstm_out.view(len(x),-1))
        
        return pred[-1]
model = LSTMTSModel()
model
criterion = nn.MSELoss()
optimizer = torch.optim.SGD(model.parameters(), lr=0.001)<jupyter_output><empty_output><jupyter_text>Train the model<jupyter_code>epochs = 100

for epoch in range(epochs):   
    
    for seq, y_train in train_data:
        
        optimizer.zero_grad()
        model.hidden = (torch.zeros(1,1,model.hidden_size),
                        torch.zeros(1,1,model.hidden_size))
        
        y_pred = model(seq)
        
        loss = criterion(y_pred, y_train)
        loss.backward()
        optimizer.step()
        
    if epoch%10 == 0:
        print(f'Epoch: {epoch} Loss: {loss.item():10.8f}')<jupyter_output>Epoch: 0 Loss: 0.01289018
Epoch: 10 Loss: 0.00679644
Epoch: 20 Loss: 0.18631664
Epoch: 30 Loss: 0.35237628
Epoch: 40 Loss: 0.38022545
Epoch: 50 Loss: 0.38105014
Epoch: 60 Loss: 0.37891001
Epoch: 70 Loss: 0.37647125
Epoch: 80 Loss: 0.37400016
Epoch: 90 Loss: 0.37152395
{'fraction_non_alphanumeric': 0.12882703777335985, 'fraction_numerical': 0.06520874751491054, 'mean_word_length': 3.3156089193825045, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '395939', 'license': 'Apache-2.0', 'n_tokens_mistral': 1062, 'n_tokens_neox': 960, 'n_words': 170, 'path': 'torch/12. RNN - Time Series - LSTM.ipynb', 'repo_name': 'rishuatgithub/MLPy', 'size': '2515'}