-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathNetwork.py
More file actions
executable file
·119 lines (102 loc) · 4.63 KB
/
Network.py
File metadata and controls
executable file
·119 lines (102 loc) · 4.63 KB
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
import torch
import torch.nn as nn
class RegressiveCNN(nn.Module):
def __init__(self):
super(RegressiveCNN, self).__init__()
# LeNet structure!
# immagine rgb -> 3 canali input iniziale 12 out vuol dire
# identificare 12 features
self.conv1 = nn.Conv2d(in_channels=3, out_channels=3, kernel_size=3,
stride=1, padding=1)
self.bn1 = nn.BatchNorm2d(num_features=3)
self.relu1 = nn.ReLU()
self.pool1 = nn.MaxPool2d(kernel_size=2) # 3x64x64
self.conv2 = nn.Conv2d(in_channels=3, out_channels=6, kernel_size=3,
stride=1, padding=1)
self.bn2 = nn.BatchNorm2d(num_features=6)
self.relu2 = nn.ReLU()
self.pool2 = nn.MaxPool2d(kernel_size=2) # 6x32x32
# self.conv3 = nn.Conv2d(in_channels=24, out_channels=24, kernel_size=3, stride=1, padding=1)
# self.bn3 = nn.BatchNorm2d(num_features=24) # output.shape = 24x32x32
# self.relu3 = nn.ReLU()
# self.pool3 = nn.MaxPool2d(kernel_size=2)
# self.conv4 = nn.Conv2d(in_channels=24, out_channels=12, kernel_size=3, stride=1, padding=1)
# self.bn4 = nn.BatchNorm2d(num_features=12) # output.shape = 12x16x16
# self.relu4 = nn.ReLU()
# self.pool4 = nn.MaxPool2d(kernel_size=2)
# weather MLP
# self.w1 = nn.Linear(in_features=8, out_features=16)
# self.w_bn1 = nn.BatchNorm1d(num_features=16)
# self.w2 = nn.Linear(in_features=16, out_features=32)
# self.w_bn2 = nn.BatchNorm1d(num_features=32)
# self.w3 = nn.Linear(in_features=32, out_features=64)
# self.w_bn3 = nn.BatchNorm1d(num_features=64)
# self.w4 = nn.Linear(in_features=64, out_features=32)
# self.w_bn4 = nn.BatchNorm1d(num_features=32)
# self.w5 = nn.Linear(in_features=32, out_features=8)
# layer Bilinear, B(x1, x2) = x1^t * M * x2 + b
# M, b imparati da Bilinear, x1=feature map
# x2=dati, dati = dati_meteo + coord_cella + indice_temporale
self.bilinear = nn.Bilinear(6*32*32, 8, 128)
self.bilin_bn = nn.BatchNorm1d(num_features=128)
self.dropout1 = nn.Dropout(0.2)
self.fc1 = nn.Linear(in_features=128, out_features=64)
self.mlp_bn1 = nn.BatchNorm1d(num_features=64)
self.dropout2 = nn.Dropout(0.2)
self.fc2 = nn.Linear(in_features=64, out_features=32)
self.mlp_bn2 = nn.BatchNorm1d(num_features=32)
self.fc3 = nn.Linear(in_features=32, out_features=16)
self.mlp_bn3 = nn.BatchNorm1d(num_features=16)
self.fc4 = nn.Linear(in_features=16, out_features=8)
self.mlp_bn4 = nn.BatchNorm1d(num_features=8)
self.fc5 = nn.Linear(in_features=8, out_features=1)
def forward(self, x, weather):
output = self.conv1(x)
output = self.bn1(output)
output = self.relu1(output)
output = self.pool1(output)
# print('pool1 --> {}'.format(output.shape))
output = self.conv2(output)
output = self.bn2(output)
output = self.relu2(output)
output = self.pool2(output)
# print('pool2 --> {}'.format(output.shape))
# output = self.conv3(output)
# output = self.bn3(output)
# output = self.relu3(output)
# output = self.pool3(output)
# print('pool3 --> {}'.format(output.shape))
# output = self.conv4(output)
# output = self.bn4(output)
# output = self.relu4(output)
# output = self.pool4(output)
# print('pool4 --> {}'.format(output.shape))
# trasformo in vettore colonna tramite layer di flatten sia la
# feature map che i dati meteo
output = torch.flatten(output, start_dim=1)
weather = torch.flatten(weather, start_dim=1)
# MLP WEATHER
# weather = self.w1(weather)
# weather = self.w_bn1(weather)
# weather = self.w2(weather)
# weather = self.w_bn2(weather)
# weather = self.w3(weather)
# weather = self.w_bn3(weather)
# weather = self.w4(weather)
# weather = self.w_bn4(weather)
# weather = self.w5(weather)
# MLP FINALE
output = self.bilinear(output, weather)
output = self.bilin_bn(output)
output = self.dropout1(output)
output = self.fc1(output)
output = self.mlp_bn1(output)
output = self.dropout2(output)
output = self.fc2(output)
output = self.mlp_bn2(output)
output = self.fc3(output)
output = self.mlp_bn3(output)
output = self.fc4(output)
output = self.mlp_bn4(output)
output = self.fc5(output)
return output