def build_graph(self): # entry GCN self.entry_conv_first = DenseGCNConv( in_channels=self.in_feature, out_channels=self.hidden_feature, ) self.entry_conv_block = DenseGCNConv( in_channels=self.hidden_feature, out_channels=self.hidden_feature, ) self.entry_conv_last = DenseGCNConv( in_channels=self.hidden_feature, out_channels=self.out_feature, ) self.gcn_hpool_layer = GcnHpoolSubmodel( self.out_feature + self.hidden_feature * 2, self.h_hidden_feature, self.h_out_feature, self.in_node, self.hidden_node, self.out_node #self._hparams ) self.pred_model = torch.nn.Sequential( torch.nn.Linear( 2 * self.hidden_feature + 2 * self.h_hidden_feature + self.h_out_feature + self.out_feature, self.h_hidden_feature), #torch.nn.Linear( self.out_feature, self.h_hidden_feature), torch.nn.ReLU(), torch.nn.Linear(self.h_hidden_feature, self.h_out_feature))
def __init__(self, nfeat, nhid, dropout): super(GCN2, self).__init__() self.gc1 = DenseGCNConv(nfeat, nhid) self.gc2 = DenseGCNConv(nhid, nhid) self.dropout = dropout self.classifier = nn.Linear(nhid * 2, 2)
def __init__(self, num_features, n_classes, num_hidden, num_hidden_layers, dropout, activation, improved=True, bias=True): super(PDenseGCN, self).__init__() # dropout if dropout: self.dropout = nn.Dropout(p=dropout) else: self.dropout = nn.Dropout(p=0.) #activation self.activation = activation # input layer self.conv_input = DenseGCNConv(num_features, num_hidden, improved=improved, bias=bias) # Hidden layers self.layers = nn.ModuleList() for _ in range(num_hidden_layers): self.layers.append( DenseGCNConv(num_hidden, num_hidden, improved=improved, bias=bias)) # output layer self.conv_output = DenseGCNConv(num_hidden, n_classes, improved=improved, bias=bias)
def __init__(self, in_channels, hidden_channels, out_channels): super(GNNBlock, self).__init__() self.conv1 = DenseGCNConv(in_channels, hidden_channels) self.conv2 = DenseGCNConv(hidden_channels, out_channels) self.lin = torch.nn.Linear(hidden_channels + out_channels, out_channels)
def __init__(self, nfeat, nhid, dropout): super(GCN1, self).__init__() self.gc1 = DenseGCNConv(nfeat, nhid) self.gc2 = DenseGCNConv(nhid, nhid) self.dropout = dropout self.classifier = nn.Linear(nhid * 2, 2) self.attention = nn.Linear(nhid, 8) self.node_classifier = nn.Linear(nhid, 2)
def __init__(self, hparams): super(GVAE, self).__init__() self.hparams = hparams if hparams.pool_type == 'stat': # since stat moments x4 self.prepool_dim = hparams.hidden_dim // 4 else: self.prepool_dim = hparams.hidden_dim # encoding layers if hparams.gnn_type == 'gcn': self.gcn_1 = DenseGCNConv(hparams.input_dim, hparams.hidden_dim) self.gcn_2 = DenseGCNConv(hparams.hidden_dim, hparams.hidden_dim) self.gcn_31 = DenseGCNConv(hparams.hidden_dim, self.prepool_dim) self.gcn_32 = DenseGCNConv(hparams.hidden_dim, self.prepool_dim) elif hparams.gnn_type == 'sage': self.gcn_1 = gnn_modules.DenseSAGEConv(hparams.input_dim, hparams.hidden_dim) self.gcn_2 = gnn_modules.DenseSAGEConv(hparams.hidden_dim, hparams.hidden_dim) self.gcn_31 = gnn_modules.DenseSAGEConv(hparams.hidden_dim, self.prepool_dim) self.gcn_32 = gnn_modules.DenseSAGEConv(hparams.hidden_dim, self.prepool_dim) self.fc2 = nn.Linear(hparams.hidden_dim, hparams.bottle_dim) # decoding layers self.fc3 = nn.Linear(hparams.bottle_dim, hparams.node_dim * hparams.input_dim) self.fc4 = nn.Linear(hparams.node_dim * hparams.input_dim, hparams.node_dim * hparams.input_dim) # energy prediction self.regfc1 = nn.Linear(hparams.bottle_dim, 20) self.regfc2 = nn.Linear(20, 1) # diff pool if hparams.pool_type == 'diff': self.gcn_diff = DenseGCNConv(hparams.hidden_dim, 1) if hparams.n_gpus > 0: self.dev_type = 'cuda' if hparams.n_gpus == 0: self.dev_type = 'cpu' self.eps = 1e-5
def __init__(self, dataset, hidden, ratio=0.25): # we only use 1 layer for coarsening super(Coarsening, self).__init__() # self.embed_block1 = GNNBlock(dataset.num_features, hidden, hidden) self.embed_block1 = DenseGCNConv(dataset.num_features, hidden) self.coarse_block1 = CoarsenBlock(hidden, ratio) self.embed_block2 = DenseGCNConv(hidden, dataset.num_features) self.jump = JumpingKnowledge(mode='cat') self.lin1 = Linear(hidden + dataset.num_features, hidden) self.lin2 = Linear(hidden, dataset.num_classes)
def __init__(self, dataset, hidden, num_layers=2, ratio=0.5): super(MultiLayerCoarsening, self).__init__() self.embed_block1 = DenseGCNConv(dataset.num_features, hidden) self.coarse_block1 = CoarsenBlock(hidden, ratio) self.embed_block2 = DenseGCNConv(hidden, dataset.num_features) # self.embed_block2 = GNNBlock(hidden, hidden, dataset.num_features) self.num_layers = num_layers self.jump = JumpingKnowledge(mode='cat') self.lin1 = Linear(hidden + dataset.num_features, hidden) self.lin2 = Linear(hidden, dataset.num_classes)
def __init__(self, nfeat, nhid, dropout): super(GCN, self).__init__() self.gc1 = DenseGCNConv(nfeat, nhid) self.gc2 = DenseGCNConv(nhid, nhid) self.gc3 = DenseGCNConv(nfeat, nhid) self.gc4 = DenseGCNConv(nhid, nhid) self.dropout = dropout self.number_attention = 1 self.classifier = nn.Linear(nhid * self.number_attention, 2) # self.classifier2 = nn.Linear(nhid*self.number_attention, 2) self.node_classifier = nn.Linear(nhid, 2) self.attention = nn.Linear(nhid, self.number_attention)
def __init__(self, in_channels, assign_ratio): super(CoarsenBlock, self).__init__() self.gcn_att = DenseGCNConv(in_channels, 1, bias=True) # self.att = torch.nn.Linear(in_channels, # hidden) self.assign_ratio = assign_ratio
def __init__(self, in_feature, hidden_feature, out_feature, in_node, hidden_node, out_node): super(GcnHpoolSubmodel, self).__init__() #self._hparams = hparams_lib.copy_hparams(hparams) #self.build_graph(in_feature, hidden_feature, out_feature, in_node, hidden_node, out_node) self.reset_parameters() #self._device = torch.device(self._hparams.device) self.pool_tensor = None # # embedding blocks # # self.embed_conv_first = GCNConv( # in_channels=in_feature, # out_channels=hidden_feature, # ) # self.embed_conv_block = GCNConv( # in_channels=hidden_feature, # out_channels=hidden_feature, # ) # self.embed_conv_last = GCNConv( # in_channels=hidden_feature, # out_channels=out_feature, # ) # embedding blocks self.embed_conv_first = DenseGCNConv( in_channels=in_feature, out_channels=hidden_feature, ) self.embed_conv_block = DenseGCNConv( in_channels=hidden_feature, out_channels=hidden_feature, ) self.embed_conv_last = DenseGCNConv( in_channels=hidden_feature, out_channels=out_feature, ) # pooling blocks self.pool_conv_first = DenseGCNConv( in_channels=in_node, out_channels=hidden_node, ) self.pool_conv_block = DenseGCNConv( in_channels=hidden_node, out_channels=hidden_node, ) self.pool_conv_last = DenseGCNConv( in_channels=hidden_node, out_channels=out_node, ) self.pool_linear = torch.nn.Linear(hidden_node * 2 + out_node, out_node)
def test_dense_gcn_conv_with_broadcasting(): batch_size, num_nodes, channels = 8, 3, 16 conv = DenseGCNConv(channels, channels) x = torch.randn(batch_size, num_nodes, channels) adj = torch.Tensor([ [0, 1, 1], [1, 0, 1], [1, 1, 0], ]) assert conv(x, adj).size() == (batch_size, num_nodes, channels) mask = torch.tensor([1, 1, 1], dtype=torch.uint8) assert conv(x, adj, mask).size() == (batch_size, num_nodes, channels)
def __init__(self, input_dim=3, hidden_dim=16, embedding_dim=32, output_dim_id=len(class_to_id), output_dim_p4=4, dropout_rate=0.5, convlayer="sgconv", space_dim=2, nearest=3): super(PFNet5, self).__init__() self.input_dim = input_dim act = nn.LeakyReLU self.inp = nn.Sequential( nn.Linear(input_dim, hidden_dim), act(), nn.Linear(hidden_dim, hidden_dim), act(), nn.Linear(hidden_dim, hidden_dim), act(), nn.Linear(hidden_dim, embedding_dim), act(), ) self.conv = DenseGCNConv(embedding_dim, embedding_dim) self.nn1 = nn.Sequential( nn.Linear(embedding_dim, hidden_dim), act(), nn.Linear(hidden_dim, hidden_dim), act(), nn.Linear(hidden_dim, hidden_dim), act(), nn.Linear(hidden_dim, hidden_dim), act(), nn.Linear(hidden_dim, output_dim_id), ) self.nn2 = nn.Sequential( nn.Linear(embedding_dim + output_dim_id, hidden_dim), act(), nn.Linear(hidden_dim, hidden_dim), act(), nn.Linear(hidden_dim, hidden_dim), act(), nn.Linear(hidden_dim, hidden_dim), act(), nn.Linear(hidden_dim, output_dim_p4), )
def __init__(self, num_nodes, input_dim, output_dim, lstm_hidden_size, lstm_num_layers, batch_size, gnn_hidden_size, lstm_dropout=0, **kwargs): super(GNNLSTM, self).__init__() self.num_nodes = num_nodes self.input_dim = input_dim self.lstm_hidden_size = lstm_hidden_size self.lstm_num_layers = lstm_num_layers self.gnn_hidden_size = gnn_hidden_size self.lstm_dropout = lstm_dropout self.output_dim = output_dim self.batch_size = batch_size if 'target_node' in kwargs.keys(): self.target_node = kwargs['target_node'] else: self.target_node = None # LSTM layers definition self.graph_lstm = GraphLSTM(num_nodes=num_nodes, input_dim=input_dim, hidden_size=lstm_hidden_size, num_layers=lstm_num_layers, batch_size=batch_size, dropout=lstm_dropout) # GNN layers definition self.gcn = DenseGCNConv(in_channels=lstm_num_layers * lstm_hidden_size, out_channels=gnn_hidden_size) # MLP definition self.mlp = nn.Sequential(nn.Linear(gnn_hidden_size, 512), nn.ReLU(), nn.Linear(512, 256), nn.ReLU(), nn.Linear(256, 64), nn.ReLU(), nn.Linear(64, output_dim))
def test_gcn(): x_len, x_dim = 100, 1000 x = np.random.randn(x_len, x_dim) adj = sps.rand(x_len, x_len, density=0.1) edge_index = np.array(adj.nonzero()) gcn = SparseGCN(x_dim, x_dim) print(x[7].mean(), np.linalg.norm(x)) start = time.time() out = gcn.forward(x, edge_index) print(time.time() - start) print(out[7].mean(), np.linalg.norm(out), sps.linalg.norm(gcn.w)) gcn1 = DenseGCNConv(x_dim, x_dim, improved=True, bias=False) adj = adj > 0 out = gcn1(torch.tensor(x, dtype=torch.float), torch.tensor(adj.toarray(), dtype=torch.float)) print(out[0, 7].mean(), out.norm(), gcn1.weight.norm())
def test_dense_gcn_conv(): channels = 16 sparse_conv = GCNConv(channels, channels) dense_conv = DenseGCNConv(channels, channels) assert dense_conv.__repr__() == 'DenseGCNConv(16, 16)' # Ensure same weights and bias. dense_conv.weight = sparse_conv.weight dense_conv.bias = sparse_conv.bias x = torch.randn((5, channels)) edge_index = torch.tensor([[0, 0, 1, 1, 2, 2, 3, 4], [1, 2, 0, 2, 0, 1, 4, 3]]) sparse_out = sparse_conv(x, edge_index) assert sparse_out.size() == (5, channels) x = torch.cat([x, x.new_zeros(1, channels)], dim=0).view(2, 3, channels) adj = torch.Tensor([ [ [0, 1, 1], [1, 0, 1], [1, 1, 0], ], [ [0, 1, 0], [1, 0, 0], [0, 0, 0], ], ]) mask = torch.tensor([[1, 1, 1], [1, 1, 0]], dtype=torch.uint8) dense_out = dense_conv(x, adj, mask) assert dense_out.size() == (2, 3, channels) assert dense_out[1, 2].abs().sum().item() == 0 dense_out = dense_out.view(6, channels)[:-1] assert torch.allclose(sparse_out, dense_out, atol=1e-04)
def __init__(self, in_dim, out_dim, act, p): super(GCN, self).__init__() self.act = act self.drop = nn.Dropout(p=p) if p > 0.0 else nn.Identity() self.gcn = DenseGCNConv(in_dim, out_dim, improved=True)
def __init__(self): super(Net, self).__init__() self.conv1 = DenseGCNConv(dataset.num_node_features, 16) self.conv2 = DenseGCNConv(16, dataset.num_classes)