def __init__(self, in_channels, embedding_channels, conv_ch, dropout, original_dim, num_head): super(NodeDetector_rnn, self).__init__() self.dropout = dropout self.conv_ch = conv_ch self.original_dim = original_dim # Projection self.node_projection = nn.Parameter( torch.Tensor(in_channels, conv_ch).float()) self.embedding_projection = nn.Parameter( torch.Tensor(embedding_channels, conv_ch).float()) # Node aggregation self.node_aggr1 = nn.Conv1d(conv_ch, conv_ch, kernel_size=2, stride=2) self.node_aggr2 = nn.Linear(conv_ch, conv_ch // 2) # Heterogenous Graph Projection self.masked_node_projection = nn.Parameter( torch.Tensor(conv_ch // 2, conv_ch // 2).float()) self.normal_node_projection = nn.Parameter( torch.Tensor(conv_ch // 2, conv_ch // 2).float()) # Graph aggregation self.graph_conv1 = GATv2Conv(conv_ch // 2, conv_ch // 2, num_head, concat=False) self.graph_conv2 = GATv2Conv(conv_ch // 2, conv_ch // 2, num_head, concat=False) # Reconstruction self.reconstruct = nn.Linear(conv_ch // 2, original_dim)
def __init__(self, in_channels, conv_ch, dropout, original_dim, num_head): super(GraphDetector_rnn, self).__init__() self.dropout = dropout self.conv_ch = conv_ch self.original_dim = original_dim # Encoder self.graph_conv1 = GATv2Conv(in_channels, conv_ch, num_head, concat=False) self.graph_conv2 = GATv2Conv(conv_ch, conv_ch // 2, num_head, concat=False) self.rnn1 = nn.GRU(conv_ch // 2, conv_ch // 2, 2) # Reconstruction self.reconstruct1 = GATv2Conv(conv_ch // 2, conv_ch, num_head, concat=False) self.reconstruct2 = nn.Linear(conv_ch, in_channels) self.reconstruct3 = nn.Linear(in_channels, original_dim) # Forecasting self.forecast1 = GATv2Conv(conv_ch // 2, conv_ch, num_head, concat=False) self.forecast2 = nn.Linear(conv_ch, in_channels) self.forecast3 = nn.Linear(in_channels, original_dim)
def __init__(self, input_dim, hidden_dim, output_dim, args): super(GATv2, self).__init__() self.args = args # Use our gat message passing. self.conv1 = GATv2Conv(input_dim, hidden_dim, heads=args['heads']) self.conv2 = GATv2Conv(args['heads'] * hidden_dim, hidden_dim, heads=args['heads']) self.post_mp = torch.nn.Sequential( torch.nn.Linear(args['heads'] * hidden_dim, hidden_dim), torch.nn.Dropout(args['dropout']), torch.nn.Linear(hidden_dim, output_dim) )
def __init__(self, in_channels, conv_ch, dropout=0.5): super(GAE, self).__init__() self.conv1 = GATv2Conv(in_channels, conv_ch) # self.dropout1 = nn.Dropout(dropout) self.conv2 = GATv2Conv(conv_ch, conv_ch * 2) # self.dropout2 = nn.Dropout(dropout) self.edge_decoder_conv = GATv2Conv(conv_ch * 2, conv_ch) # self.dropout3 = nn.Dropout(dropout) self.x_decoder_conv1 = GATv2Conv(conv_ch * 2, conv_ch) # self.dropout4 = nn.Dropout(dropout) self.x_decoder_conv2 = GATv2Conv(conv_ch, in_channels)
def test_gatv2_conv_with_edge_attr(): x = torch.randn(4, 8) edge_index = torch.tensor([[0, 1, 2, 3], [1, 0, 1, 1]]) edge_weight = torch.randn(edge_index.size(1)) edge_attr = torch.randn(edge_index.size(1), 4) conv = GATv2Conv(8, 32, heads=2, edge_dim=1, fill_value=0.5) out = conv(x, edge_index, edge_weight) assert out.size() == (4, 64) conv = GATv2Conv(8, 32, heads=2, edge_dim=1, fill_value='mean') out = conv(x, edge_index, edge_weight) assert out.size() == (4, 64) conv = GATv2Conv(8, 32, heads=2, edge_dim=4, fill_value=0.5) out = conv(x, edge_index, edge_attr) assert out.size() == (4, 64) conv = GATv2Conv(8, 32, heads=2, edge_dim=4, fill_value='mean') out = conv(x, edge_index, edge_attr) assert out.size() == (4, 64)
def __init__(self, hidden_channels, outer_out_channels, inner_out_channels, num_layers, batch_size, num_node_types, num_heads): super().__init__() self.batch_size = batch_size self.hidden_channels = hidden_channels self.heads = num_heads self.convs = torch.nn.ModuleList() for _ in range(num_layers): conv = HeteroConv( { ('x_i', 'inner_edge_i', 'x_i'): GATv2Conv(-1, self.hidden_channels, heads=num_heads), ('x_j', 'inner_edge_j', 'x_j'): GATv2Conv(-1, self.hidden_channels, heads=num_heads), ('x_i', 'outer_edge_ij', 'x_j'): GATv2Conv(-1, self.hidden_channels, heads=num_heads), ('x_j', 'outer_edge_ji', 'x_i'): GATv2Conv(-1, self.hidden_channels, heads=num_heads), ('x_i', 'inner_edge_i', 'x_i'): GATv2Conv(-1, self.hidden_channels, heads=num_heads), ('x_j', 'inner_edge_j', 'x_j'): GATv2Conv(-1, self.hidden_channels, heads=num_heads), }, aggr='sum') self.convs.append(conv) self.lin = Linear(self.hidden_channels, outer_out_channels) self.lin_i = Linear(self.hidden_channels, inner_out_channels) self.lin_j = Linear(self.hidden_channels, inner_out_channels)
def __init__(self, in_channels, out_channels, num_head=4, dropout=0.0): super(d3GraphConv4, self).__init__() self.dropout = dropout self.out_channels = out_channels self.in_channels = in_channels self.graph_conv1 = GATv2Conv(in_channels, out_channels, num_head, concat=False) self.conv1 = nn.TransformerEncoderLayer(out_channels, num_head, dropout=dropout) self.reset_parameters()
def __init__(self, in_channels, out_channels, num_head=4, dropout=0.0): super(d3GraphConv3, self).__init__() self.dropout = dropout self.out_channels = out_channels self.in_channels = in_channels self.graph_conv1 = GATv2Conv(in_channels, out_channels, num_head, concat=False) self.conv1 = nn.Conv1d(out_channels, out_channels, kernel_size=2, padding=1) self.reset_parameters()
def __init__(self, in_channels, conv_ch, dropout, original_dim, num_head, st_module=d3GraphConv2): super(GraphDetector, self).__init__() self.dropout = dropout self.conv_ch = conv_ch # Encoder self.conv1 = st_module(in_channels, conv_ch, num_head, dropout=dropout) self.conv2 = st_module(conv_ch, conv_ch // 2, num_head, dropout=dropout) self.conv3 = st_module(conv_ch // 2, conv_ch // 2, num_head, dropout=dropout) self.conv4 = st_module(conv_ch, conv_ch // 2, num_head, dropout=dropout) # Reconstruction self.reconstruct11 = GATv2Conv(conv_ch // 2, conv_ch // 2, num_head, concat=False) self.reconstruct12 = GATv2Conv(conv_ch // 2, conv_ch // 2, num_head, concat=False) self.reconstruct2 = GATv2Conv(conv_ch, conv_ch, num_head, concat=False) self.reconstruct3 = nn.ConvTranspose1d(conv_ch, in_channels, kernel_size=1) self.reconstruct4 = nn.Linear(in_channels, original_dim) # Forecasting self.forecast11 = GATv2Conv(conv_ch // 2, conv_ch // 2, num_head, concat=False) self.forecast12 = GATv2Conv(conv_ch // 2, conv_ch // 2, num_head, concat=False) self.forecast2 = GATv2Conv(conv_ch, conv_ch, num_head, concat=False) self.forecast3 = nn.ConvTranspose1d(conv_ch, in_channels, kernel_size=1) self.forecast4 = nn.Linear(in_channels, original_dim)
def __init__(self, in_channels, out_channels, num_head=4, dropout=0.5): super(d3GraphConv2, self).__init__() self.dropout = dropout self.out_channels = out_channels self.in_channels = in_channels self.gat = GATv2Conv(in_channels, out_channels, num_head, concat=False) #self.conv = torch.nn.Conv1d(out_channels, out_channels, 3, padding=1) self.t1_weight = Parameter(torch.Tensor(out_channels, out_channels).float()) self.t2_weight = Parameter(torch.Tensor(out_channels, out_channels).float()) self.reset_parameters()
def test_gatv2_conv(): x1 = torch.randn(4, 8) x2 = torch.randn(2, 8) edge_index = torch.tensor([[0, 1, 2, 3], [0, 0, 1, 1]]) row, col = edge_index adj = SparseTensor(row=row, col=col, sparse_sizes=(4, 4)) conv = GATv2Conv(8, 32, heads=2) assert conv.__repr__() == 'GATv2Conv(8, 32, heads=2)' out = conv(x1, edge_index) assert out.size() == (4, 64) assert torch.allclose(conv(x1, edge_index), out) assert torch.allclose(conv(x1, adj.t()), out, atol=1e-6) t = '(Tensor, Tensor, OptTensor, NoneType) -> Tensor' jit = torch.jit.script(conv.jittable(t)) assert torch.allclose(jit(x1, edge_index), out) t = '(Tensor, SparseTensor, OptTensor, NoneType) -> Tensor' jit = torch.jit.script(conv.jittable(t)) assert torch.allclose(jit(x1, adj.t()), out, atol=1e-6) # Test `return_attention_weights`. result = conv(x1, edge_index, return_attention_weights=True) assert torch.allclose(result[0], out) assert result[1][0].size() == (2, 7) assert result[1][1].size() == (7, 2) assert result[1][1].min() >= 0 and result[1][1].max() <= 1 assert conv._alpha is None result = conv(x1, adj.t(), return_attention_weights=True) assert torch.allclose(result[0], out, atol=1e-6) assert result[1].sizes() == [4, 4, 2] and result[1].nnz() == 7 assert conv._alpha is None t = ('(Tensor, Tensor, OptTensor, bool) -> ' 'Tuple[Tensor, Tuple[Tensor, Tensor]]') jit = torch.jit.script(conv.jittable(t)) result = jit(x1, edge_index, return_attention_weights=True) assert torch.allclose(result[0], out) assert result[1][0].size() == (2, 7) assert result[1][1].size() == (7, 2) assert result[1][1].min() >= 0 and result[1][1].max() <= 1 assert conv._alpha is None t = ('(Tensor, SparseTensor, OptTensor, bool) -> ' 'Tuple[Tensor, SparseTensor]') jit = torch.jit.script(conv.jittable(t)) result = jit(x1, adj.t(), return_attention_weights=True) assert torch.allclose(result[0], out, atol=1e-6) assert result[1].sizes() == [4, 4, 2] and result[1].nnz() == 7 assert conv._alpha is None adj = adj.sparse_resize((4, 2)) out1 = conv((x1, x2), edge_index) assert out1.size() == (2, 64) assert torch.allclose(conv((x1, x2), edge_index), out1) assert torch.allclose(conv((x1, x2), adj.t()), out1, atol=1e-6) t = '(OptPairTensor, Tensor, OptTensor, NoneType) -> Tensor' jit = torch.jit.script(conv.jittable(t)) assert torch.allclose(jit((x1, x2), edge_index), out1) t = '(OptPairTensor, SparseTensor, OptTensor, NoneType) -> Tensor' jit = torch.jit.script(conv.jittable(t)) assert torch.allclose(jit((x1, x2), adj.t()), out1, atol=1e-6)