Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.10 KB | None | 0 0
  1. import torch.nn as nn
  2. import torch.nn.functional as F
  3.  
  4.  
  5. class Model(nn.Module):
  6. def __init__(self):
  7. super(Model, self).__init__()
  8.  
  9. self.rnn = nn.LSTM(input_size=1,hidden_size=50,num_layers=3,batch_first=True,dropout=0.2)
  10. self.out = nn.Linear(50, 1)
  11.  
  12. def forward(self, x):
  13.  
  14. r_out, (h_n, h_c) = self.rnn(x, None)
  15. x = r_out[:,-1,:] #last hidden output!
  16. x = self.out(x)
  17. return x
  18.  
  19. model = Model()
  20.  
  21. if cuda:
  22. model.cuda()
  23.  
  24. mse = mean_squared_error(dataset['y'].iloc[60:].values,predicted)
  25. print("RMSE:", np.sqrt(mse))
  26.  
  27. RMSE: 0.10680269716212222
  28.  
  29. #array --> List
  30. today = total_x[-1].reshape(-1).tolist()
  31.  
  32. # scaling last price
  33. last_price = scaler.transform(infered['y_pred'][-1].reshape(-1, 1))
  34.  
  35. # adding last price to list.
  36. today.append(last_price[0])
  37.  
  38. # Exclude first(0th index) element
  39. today = today[1:]
  40.  
  41. #reshape
  42. today = np.array(today).reshape(-1,60,1)
  43.  
  44. today = torch.Tensor(list(today))
  45.  
  46. #predict!
  47. tomorrow = predict_with_pytorch(model, today)
  48.  
  49. #inverse transform.
  50. tomorrow = scaler.inverse_transform(tomorrow)[0]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement