-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaggregate_models.py
More file actions
57 lines (48 loc) · 1.9 KB
/
Copy pathaggregate_models.py
File metadata and controls
57 lines (48 loc) · 1.9 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
import torch
import torch.nn as nn
class LSTMAggregator(nn.Module):
def __init__(self, feature_dim):
super(LSTMAggregator, self).__init__()
self.aggregator = nn.LSTM(feature_dim, feature_dim, batch_first=True)
def forward(self, gene_features: dict, device):
"""
:param device:
:param gene_features: key: node idx, value: set of features
:return:
"""
n_data_feature = []
node_seq = sorted(list(gene_features.keys()))
for i in node_seq:
feature = gene_features[i]
feature = feature.to(device)
r = torch.randperm(feature.size(0))
_, (h, _) = self.aggregator(feature[r].unsqueeze(0))
n_data_feature.append(h.squeeze(0))
n_data_feature = torch.cat(n_data_feature)
return n_data_feature
class MaxAggregator(nn.Module):
def __init__(self, feature_dim):
super(MaxAggregator, self).__init__()
def forward(self, gene_features: dict, device):
n_data_feature = []
node_seq = sorted(list(gene_features.keys()))
for i in node_seq:
feature = gene_features[i]
feature = feature.to(device)
feature = torch.max(feature, dim=0)[0].unsqueeze(0)
n_data_feature.append(feature)
n_data_feature = torch.cat(n_data_feature)
return n_data_feature
class MeanAggregator(nn.Module):
def __init__(self, feature_dim):
super(MeanAggregator, self).__init__()
def forward(self, gene_features: dict, device):
n_data_feature = []
node_seq = sorted(list(gene_features.keys()))
for i in node_seq:
feature = gene_features[i]
feature = feature.to(device)
feature = torch.mean(feature, dim=0).unsqueeze(0)
n_data_feature.append(feature)
n_data_feature = torch.cat(n_data_feature)
return n_data_feature