forked from heatherbaier/wm-asu-caoe-portal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlstm.py
175 lines (113 loc) · 5.14 KB
/
lstm.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import pandas as pd
import torch
import json
import ast
class LSTM(torch.nn.Module):
"""
input_size - will be 1 in this example since we have only 1 predictor (a sequence of previous values)
hidden_size - Can be chosen to dictate how much hidden "long term memory" the network will have
output_size - This will be equal to the prediciton_periods input to get_x_y_pairs
"""
def __init__(self, input_size, hidden_size, output_size):
super(LSTM, self).__init__()
self.hidden_size = hidden_size
self.lstm = torch.nn.LSTM(input_size, hidden_size)
self.linear = torch.nn.Linear(hidden_size, output_size)
# print(self.lstm.device)
# print(self.linear.device)
def forward(self, x, rank, hidden = None):
if hidden == None:
self.hidden = (torch.zeros(1,1,self.hidden_size).to(rank),
torch.zeros(1,1,self.hidden_size).to(rank))
else:
self.hidden = hidden
"""
inputs need to be in the right shape as defined in documentation
- https://pytorch.org/docs/stable/generated/torch.nn.LSTM.html
lstm_out - will contain the hidden states from all times in the sequence
self.hidden - will contain the current hidden state and cell state
"""
lstm_out, self.hidden = self.lstm(x.view(len(x),1,-1),
self.hidden)
predictions = self.linear(lstm_out.view(len(x), -1))
return predictions[-1]
class LSTM_CPU(torch.nn.Module):
"""
input_size - will be 1 in this example since we have only 1 predictor (a sequence of previous values)
hidden_size - Can be chosen to dictate how much hidden "long term memory" the network will have
output_size - This will be equal to the prediciton_periods input to get_x_y_pairs
"""
def __init__(self, input_size, hidden_size, output_size):
super(LSTM_CPU, self).__init__()
self.hidden_size = hidden_size
self.lstm = torch.nn.LSTM(input_size, hidden_size)
self.linear = torch.nn.Linear(hidden_size, output_size)
# print(self.lstm.device)
# print(self.linear.device)
def forward(self, x, hidden = None):
if hidden == None:
self.hidden = (torch.zeros(1,1,self.hidden_size),
torch.zeros(1,1,self.hidden_size))
else:
self.hidden = hidden
"""
inputs need to be in the right shape as defined in documentation
- https://pytorch.org/docs/stable/generated/torch.nn.LSTM.html
lstm_out - will contain the hidden states from all times in the sequence
self.hidden - will contain the current hidden state and cell state
"""
lstm_out, self.hidden = self.lstm(x.view(len(x),1,-1),
self.hidden)
predictions = self.linear(lstm_out.view(len(x), -1))
return predictions[-1]
class AverageMeter:
def __init__(self):
self.reset()
def reset(self):
self.val = 0
self.avg = 0
self.sum = 0
self.count = 0
def update(self, val, n = 1):
self.val = val
self.sum += val * n
self.count += n
self.avg = self.sum / self.count
if __name__ == "__main__":
model = LSTM(input_size = 512,
hidden_size = 128,
output_size = 12)
criterion = torch.nn.MSELoss()
optimizer = torch.optim.Adam(model.parameters(), lr = 0.001)
with open("/sciclone/geograd/heather_data/temporal_features/jsons/484005031.json", "r") as f:
data = json.load(f)
features = list(data.values())
migs = [torch.tensor([ast.literal_eval(i["migrants"])]) for i in features]
features = [torch.tensor([ast.literal_eval(i["features: "])]) for i in features]
num_steps = 12
x, y = [], []
for i in range(len(features)):
x.append(torch.cat(features[i:i+num_steps]))
y.append(torch.cat(migs[i:i+num_steps]))
x = torch.cat([i.unsqueeze(0) for i in x if i.shape[0] == 12])
# y = [i for i in y if i.shape[0] == 12]
train_num = int(x.shape[0] * .75)
print("Train num: ", train_num)
import random
train_indices = random.sample(range(x.shape[0]), train_num)
val_indices = [i for i in range(x.shape[0]) if i not in train_indices]
x_train = torch.index_select(x, 0, torch.tensor(train_indices))
x_val = torch.index_select(x, 0, torch.tensor(val_indices))
print(x_train.shape, x_val.shape)
# train_tracker = AverageMeter()
# for epoch in range(0, 100):
# train_tracker.reset()
# for input, target in zip(x_train, y_train):
# if input.shape[0] == 12:
# output = model(input)
# loss = criterion(target, output)
# train_tracker.update(loss.item())
# optimizer.zero_grad()
# loss.backward()
# optimizer.step()
# print(train_tracker.avg)