Ejemplo n.º 1
0
 def __init__(self, nfeat, nhid, nclass, dropout, seed):
     nn.Module.__init__(self)
     np.random.seed(seed)
     torch.manual_seed(seed)
     self.gc1 = GraphConvolution(nfeat, nhid)
     self.gc2 = GraphConvolution(nhid, nclass)
     self.dropout = dropout
Ejemplo n.º 2
0
 def __init__(self, nfeat, nhid, nclass, dropout, seed, depth):
     nn.Module.__init__(self)
     np.random.seed(seed)
     torch.manual_seed(seed)
     self.depth = depth
     self.gc_list = [None] * self.depth
     self.residual_weight_list = [None] * self.depth
     if self.depth == 1:
         self.gc_list[self.depth-1] = GraphConvolution(nfeat, nclass)
         self.residual_weight_list[self.depth-1] = Parameter(torch.FloatTensor(nfeat, nclass))
     else:
         for i in range(self.depth):
             if i == 0:
                 self.gc_list[i] = GraphConvolution(nfeat, nhid)
                 self.residual_weight_list[i] = Parameter(torch.FloatTensor(nfeat, nhid))
             elif i == self.depth - 1:
                 self.gc_list[i] = GraphConvolution(nhid, nclass)
                 self.residual_weight_list[i] = Parameter(torch.FloatTensor(nhid, nclass))
             else:
                 self.gc_list[i] = GraphConvolution(nhid, nhid)
                 self.residual_weight_list[i] = Parameter(torch.FloatTensor(nhid, nhid))
     for i in range(self.depth):
         stdv = 1. / math.sqrt(self.residual_weight_list[i].size(1))
         self.residual_weight_list[i].data.uniform_(-stdv, stdv)
     self.dropout = dropout
Ejemplo n.º 3
0
 def __init__(self, in_feat_dim, nhid, out_feat_dim, dropout):
     super(GCN, self).__init__()
     '''
     ## Inputs:
     - graph_nodes (batch_size, K, in_feat_dim): input features
     - adjacency matrix (batch_size, K, K)
     ## Returns:
     - gcn_enhance_feature (batch_size, K, out_feat_dim)
     '''
     self.gc1 = GraphConvolution(in_feat_dim, nhid)
     self.gc2 = GraphConvolution(nhid, out_feat_dim)
     self.dropout = dropout
Ejemplo n.º 4
0
def GCN(n_classes=7):

    inpt = Input((2708, 1433))  # (b,N,D)
    adj = Input((2708, 2708))  # (b,N,N)

    x = Dropout(0.5)(inpt)
    x = GraphConvolution(16, activation='relu')([x, adj])  # (b,N,F)
    x = Dropout(0.5)(x)
    x = GraphConvolution(n_classes, activation='softmax')([x,
                                                           adj])  # (b,N,cls)

    model = Model([inpt, adj], x)

    return model
Ejemplo n.º 5
0
    def __init__(self, shape, conv_channels, linear_channels):
        super(GCN, self).__init__()

        self.conv_channels = conv_channels
        self.linear_channels = linear_channels

        self.A = graph_utils.add_fictional_node_to_A(
            mnist_on_plane.grid_adj_matrix(shape[0], shape[1]))
        self.norm_A = torch.FloatTensor(
            convolutions.kipf_welling_norm(convolutions.degree_matrix(self.A),
                                           self.A))

        self.gc1 = GraphConvolution(conv_channels[0], conv_channels[1])
        self.gc1_bn = nn.BatchNorm1d(conv_channels[1])
        #self.gc1_drop = nn.Dropout(p=0.2)

        self.gc2 = GraphConvolution(conv_channels[1], conv_channels[2])
        self.gc2_bn = nn.BatchNorm1d(conv_channels[2])
        #self.gc2_drop = nn.Dropout(p=0.2)

        self.gc3 = GraphConvolution(conv_channels[2], conv_channels[3])
        self.gc3_bn = nn.BatchNorm1d(conv_channels[3])
        #self.gc3_drop = nn.Dropout(p=0.2)

        self.gc4 = GraphConvolution(conv_channels[3], conv_channels[4])
        self.gc4_bn = nn.BatchNorm1d(conv_channels[4])
        #self.gc4_drop = nn.Dropout(p=0.2)
        '''
        self.gc5 = GraphConvolution(conv_channels[4], conv_channels[5])
        self.gc5_bn = nn.BatchNorm1d(conv_channels[5])
        #self.gc5_drop = nn.Dropout(p=0.2)

        self.gc6 = GraphConvolution(conv_channels[5], conv_channels[6])
        self.gc6_bn = nn.BatchNorm1d(conv_channels[6])
        #self.gc6_drop = nn.Dropout(p=0.2)
        
        self.gc7 = GraphConvolution(conv_channels[6], conv_channels[7])
        self.gc7_bn = nn.BatchNorm1d(conv_channels[7])
        #self.gc7_drop = nn.Dropout(p=0.2)

        self.gc8 = GraphConvolution(conv_channels[7], conv_channels[8])
        self.gc8_bn = nn.BatchNorm1d(conv_channels[8])
        #self.gc8_drop = nn.Dropout(p=0.2)


        self.gc9 = GraphConvolution(conv_channels[8], conv_channels[9])
        self.gc9_bn = nn.BatchNorm1d(conv_channels[9])

        
        self.gc10 = GraphConvolution(conv_channels[9], conv_channels[10])
        self.gc10_bn = nn.BatchNorm1d(conv_channels[10])

        self.gc11 = GraphConvolution(conv_channels[10], conv_channels[11])
        self.gc11_bn = nn.BatchNorm1d(conv_channels[11])

        self.gc12 = GraphConvolution(conv_channels[11], conv_channels[12])
        self.gc12_bn = nn.BatchNorm1d(conv_channels[12])

        self.gc13 = GraphConvolution(conv_channels[12], conv_channels[13])
        self.gc13_bn = nn.BatchNorm1d(conv_channels[13])

        self.gc14 = GraphConvolution(conv_channels[13], conv_channels[14])
        self.gc14_bn = nn.BatchNorm1d(conv_channels[14])

        self.gc15 = GraphConvolution(conv_channels[14], conv_channels[15])
        self.gc15_bn = nn.BatchNorm1d(conv_channels[15])

        self.gc16 = GraphConvolution(conv_channels[15], conv_channels[16])
        self.gc16_bn = nn.BatchNorm1d(conv_channels[16])

        self.gc17 = GraphConvolution(conv_channels[16], conv_channels[17])
        self.gc17_bn = nn.BatchNorm1d(conv_channels[17])

        self.gc18 = GraphConvolution(conv_channels[17], conv_channels[18])
        self.gc18_bn = nn.BatchNorm1d(conv_channels[18])

        self.gc19 = GraphConvolution(conv_channels[18], conv_channels[19])
        self.gc19_bn = nn.BatchNorm1d(conv_channels[19])

        self.gc20 = GraphConvolution(conv_channels[19], conv_channels[20])
        self.gc20_bn = nn.BatchNorm1d(conv_channels[20])

        
        self.gc21 = GraphConvolution(conv_channels[20], conv_channels[21])
        self.gc21_bn = nn.BatchNorm1d(conv_channels[21])

        self.gc22 = GraphConvolution(conv_channels[21], conv_channels[22])
        self.gc22_bn = nn.BatchNorm1d(conv_channels[22])

        self.gc23 = GraphConvolution(conv_channels[22], conv_channels[23])
        self.gc23_bn = nn.BatchNorm1d(conv_channels[23])

        
        self.gc24 = GraphConvolution(conv_channels[23], conv_channels[24])
        self.gc24_bn = nn.BatchNorm1d(conv_channels[24])
        '''

        self.fc1 = nn.Linear(conv_channels[4], linear_channels[0])
        self.fc1_bn = nn.BatchNorm1d(linear_channels[0])
        #self.fc1_drop = nn.Dropout(p=0.5)

        self.fc2 = nn.Linear(linear_channels[0], linear_channels[1])
        self.fc2_bn = nn.BatchNorm1d(linear_channels[1])
        #self.fc2_drop = nn.Dropout(p=0.5)

        self.fc3 = nn.Linear(linear_channels[1], linear_channels[2])
        self.fc3_bn = nn.BatchNorm1d(linear_channels[2])

        self.fc4 = nn.Linear(linear_channels[2], linear_channels[3])
        self.fc4_bn = nn.BatchNorm1d(linear_channels[3])

        self.fc5 = nn.Linear(linear_channels[3], linear_channels[4])
        self.fc5_bn = nn.BatchNorm1d(linear_channels[4])

        self.fc6 = nn.Linear(linear_channels[4], linear_channels[5])
        self.fc6_bn = nn.BatchNorm1d(linear_channels[5])

        self.fc7 = nn.Linear(linear_channels[5], linear_channels[6])
        self.fc7_bn = nn.BatchNorm1d(linear_channels[6])

        self.fc8 = nn.Linear(linear_channels[6], linear_channels[7])
        self.fc8_bn = nn.BatchNorm1d(linear_channels[7])
Ejemplo n.º 6
0
    def __init__(self,
                 input_dim,
                 hidden1,
                 output_dim,
                 learning_rate,
                 weight_decay,
                 drop_value=None,
                 sparse_inputs=False,
                 update_type=RMSprop(),
                 clipnorm=0.0):

        self.X = T.matrix('X', dtype='float64')
        self.Y = T.matrix('Y', dtype='float64')
        self.adjacency = T.matrix('adjacency_matrix', dtype='float64')
        self.train_mask = T.vector('train_mask', dtype='float64')

        # Test values for inputs for error-checking
        self.X.tag.test_value = np.random.rand(5, input_dim)
        self.Y.tag.test_value = np.random.rand(5, output_dim)
        self.adjacency.tag.test_value = np.random.rand(5, 5)

        self.update_type = update_type
        self.layers = []
        # Input layer in Theano
        self.layers.append(TemporalInputFeatures(input_dim, self.X))
        # Graph Convolution Layer 1
        self.layers.append(
            GraphConvolution(hidden1, self.adjacency, drop_value=drop_value))
        # Graph Convolution Layer 2
        self.layers.append(
            GraphConvolution(output_dim,
                             self.adjacency,
                             activation_str='linear',
                             drop_value=drop_value))

        L2_sqr = self.layers[0].L2_sqr
        for i in range(1, len(self.layers)):
            self.layers[i].connect(self.layers[i - 1])
            L2_sqr += self.layers[i].L2_sqr

        self.params = []
        for l in self.layers:
            if hasattr(l, 'params'):
                self.params.extend(l.params)

        self.outputs = self.layers[-1].output()
        self.update_type.lr = learning_rate

        # Output of the model from the last layer
        self.Y_pr = nnet.softmax(self.outputs)
        # Marked Softmax CE Loss
        self.cost = self.masked_softmax_cross_entropy(
            self.Y_pr, self.Y, self.train_mask) + T.mean(weight_decay * L2_sqr)
        # Updates taken from the optimizer
        [self.updates,
         self.grads] = self.update_type.get_updates(self.params, self.cost)
        # Training Function
        self.train = theano.function(
            [self.X, self.Y, self.adjacency, self.train_mask],
            [self.cost, self.Y_pr],
            updates=self.updates,
            on_unused_input='ignore')
        self.norm = T.sqrt(sum([T.sum(g**2) for g in self.grads]))
        self.grad_norm = theano.function(
            [self.X, self.Y, self.adjacency, self.train_mask],
            self.norm,
            on_unused_input='ignore')