Exemplo n.º 1
0
 def __init__(self):
     super().__init__()
     self.conv1 = GCSConv(32, activation="relu")
     self.conv2 = GCSConv(32, activation="relu")
     self.conv3 = GCSConv(32, activation="relu")
     self.global_pool = GlobalAvgPool()
     self.dense = Dense(data.n_labels, activation="softmax")
Exemplo n.º 2
0
 def __init__(self, channels, n_layers):
     super().__init__()
     self.conv1 = GINConv(channels, epsilon=0, mlp_hidden=[channels, channels])
     self.convs = []
     for i in range(1, n_layers):
         self.convs.append(
             GINConv(channels, epsilon=0, mlp_hidden=[channels, channels]))
     self.pool = GlobalAvgPool()
     self.dense1 = Dense(channels, activation='relu')
     self.dropout = Dropout(0.5)
     self.dense2 = Dense(n_out, activation='softmax')
Exemplo n.º 3
0
# The inputs will have an arbitrary dimension, while the targets consist of
# batch_size values.
# However, Keras expects the inputs to have the same dimension as the output.
# This is a hack in Tensorflow to bypass the requirements of Keras.
# We use a dynamically initialized tf.Dataset to feed the target values to the
# model at training time.
target_ph = tf.placeholder(tf.float32, shape=(None, 1))
target_data = tf.data.Dataset.from_tensor_slices(target_ph)
target_data = target_data.batch(batch_size)
target_iter = target_data.make_initializable_iterator()
target = target_iter.get_next()

gc1 = GraphConv(64, activation='relu')([X_in, A_in])
gc2 = GraphConv(64, activation='relu')([gc1, A_in])
pool = GlobalAvgPool()([gc2, I_in])
dense1 = Dense(64, activation='relu')(pool)
output = Dense(n_out)(dense1)

# Build model
model = Model(inputs=[X_in, A_in, I_in], outputs=output)
optimizer = Adam(lr=learning_rate)
model.compile(optimizer=optimizer, loss='mse', target_tensors=target)
model.summary()

# Training setup
sess = K.get_session()
batches_train = batch_iterator([A_train, X_train, y_train], batch_size=batch_size, epochs=epochs)
loss = 0
batch_index = 0
batches_in_epoch = np.ceil(len(A_train) / batch_size)
gc2 = GraphConvSkip(n_channels,
                    activation=activ,
                    kernel_regularizer=l2(GNN_l2))([X_1, A_1])
X_2, A_2, I_2, M_2 = MinCutPool(
    k=int(average_N // 4),
    h=mincut_H,
    activation=activ,
    kernel_regularizer=l2(pool_l2))([gc2, A_1, I_1])

# Block 3
X_3 = GraphConvSkip(n_channels,
                    activation=activ,
                    kernel_regularizer=l2(GNN_l2))([X_2, A_2])

# Output block
avgpool = GlobalAvgPool()([X_3, I_2])
output = Dense(n_out, activation='softmax')(avgpool)

# Build model
model = Model([X_in, A_in, I_in], output)
model.compile(
    optimizer='adam',  # Doesn't matter, won't be used
    loss='categorical_crossentropy',
    target_tensors=[target])
model.summary()

# Training setup
sess = K.get_session()
loss = model.total_loss
acc = K.mean(categorical_accuracy(target, model.output))
opt = tf.train.AdamOptimizer(learning_rate=learning_rate)
Exemplo n.º 5
0
F = X_train[0].shape[-1]  # Dimension of node features
n_out = y_train[0].shape[-1]  # Dimension of the target

################################################################################
# BUILD MODEL
################################################################################
X_in = Input(shape=(F, ), name='X_in')
A_in = Input(shape=(None,), sparse=True)
I_in = Input(shape=(), name='segment_ids_in', dtype=tf.int32)

X_1 = GraphConvSkip(32, activation='relu')([X_in, A_in])
X_1, A_1, I_1 = TopKPool(ratio=0.5)([X_1, A_in, I_in])
X_2 = GraphConvSkip(32, activation='relu')([X_1, A_1])
X_2, A_2, I_2 = TopKPool(ratio=0.5)([X_2, A_1, I_1])
X_3 = GraphConvSkip(32, activation='relu')([X_2, A_2])
X_3 = GlobalAvgPool()([X_3, I_2])
output = Dense(n_out, activation='softmax')(X_3)

# Build model
model = Model(inputs=[X_in, A_in, I_in], outputs=output)
opt = Adam(lr=learning_rate)
loss_fn = CategoricalCrossentropy()
acc_fn = CategoricalAccuracy()


@tf.function(
    input_signature=(tf.TensorSpec((None, F), dtype=tf.float64),
                     tf.SparseTensorSpec((None, None), dtype=tf.float32),
                     tf.TensorSpec((None,), dtype=tf.int32),
                     tf.TensorSpec((None, n_out), dtype=tf.float64)),
    experimental_relax_shapes=True)
A_train, A_test, \
X_train, X_test, \
E_train, E_test, \
y_train, y_test = train_test_split(A, X, E, y, test_size=0.1)

################################################################################
# BUILD MODEL
################################################################################
X_in = Input(shape=(F, ), name='X_in')
A_in = Input(shape=(None, ), name='A_in')
E_in = Input(shape=(None, S), name='E_in')
I_in = Input(shape=(), name='segment_ids_in', dtype=tf.int32)

X_1 = EdgeConditionedConv(32, activation='relu')([X_in, A_in, E_in])
X_2 = EdgeConditionedConv(32, activation='relu')([X_1, A_in, E_in])
X_3 = GlobalAvgPool()([X_2, I_in])
output = Dense(n_out)(X_3)

# Build model
model = Model(inputs=[X_in, A_in, E_in, I_in], outputs=output)
model.compile(
    optimizer='adam',  # Doesn't matter, won't be used
    loss='mse')
model.summary()

# Training setup
opt = tf.keras.optimizers.Adam(learning_rate=learning_rate)
loss_fn = model.loss_functions[0]


@tf.function(experimental_relax_shapes=True)
Exemplo n.º 7
0
es_patience = 5           # Patience fot early stopping

# Train/test split
A_train, A_test, \
X_train, X_test, \
E_train, E_test, \
y_train, y_test = train_test_split(A, X, E, y, test_size=0.1)

# Model definition
X_in = Input(shape=(N, F))
A_in = Input(shape=(N, N))
E_in = Input(shape=(N, N, S))

gc1 = EdgeConditionedConv(32, activation='relu')([X_in, A_in, E_in])
gc2 = EdgeConditionedConv(32, activation='relu')([gc1, A_in, E_in])
pool = GlobalAvgPool()(gc2)
output = Dense(n_out)(pool)

# Build model
model = Model(inputs=[X_in, A_in, E_in], outputs=output)
optimizer = Adam(lr=learning_rate)
model.compile(optimizer=optimizer, loss='mse')
model.summary()

# Train model
model.fit([X_train, A_train, E_train],
          y_train,
          batch_size=batch_size,
          validation_split=0.1,
          epochs=epochs,
          callbacks=[
Exemplo n.º 8
0
def build_gcn_model(trainset, testset, nb_node_features, nb_classes=1, batch_size=32, nb_epochs=100, lr=0.001,
                     save_path=None):
    
    # Create model architecture
    X_in = Input(batch_shape=(None, nb_node_features))
    A_in = Input(batch_shape=(None, None), sparse=True)
    I_in = Input(batch_shape=(None, ), dtype='int64')
    target = Input(tensor=tf.placeholder(tf.float32, shape=(None, nb_classes), name='target'))
    
    gc1 = GraphConv(64, activation='relu')([X_in, A_in])
    gc2 = GraphConv(128, activation='relu')([gc1, A_in])
    pool = GlobalAvgPool()([gc2, I_in])
    dense1 = Dense(128, activation='relu')(pool)
    output = Dense(nb_classes, activation='sigmoid')(dense1)
    
    model = Model(inputs=[X_in, A_in, I_in], outputs=output)
    
    # Compile model
    #optimizer = Adam(lr=lr)    
    opt = tf.train.AdamOptimizer(learning_rate=lr)
    model.compile(optimizer=opt, loss='binary_crossentropy', target_tensors=target, metrics=['accuracy'])
    model.summary()
    loss = model.total_loss
    train_step = opt.minimize(loss)
    
    # Initialize all variables
    sess = K.get_session()
    init_op = tf.global_variables_initializer()
    sess.run(init_op)
    
    # Get train and test data
    [A_train, X_train, y_train] = trainset
    [A_test, X_test, y_test] = testset
    
    SW_KEY = 'dense_2_sample_weights:0' # Keras automatically creates a placeholder for sample weights, which must be fed
    best_accuracy = 0
    for i in range(nb_epochs):
        # Train
        # TODO: compute class weight and use it in loss function
        batches_train = batch_iterator([A_train, X_train, y_train], batch_size=batch_size)
        model_loss = 0
        prediction = []
        for b in batches_train:
            batch = Batch(b[0], b[1])
            X_, A_, I_ = batch.get('XAI')
            y_ = b[2]
            tr_feed_dict = {X_in: X_,
                            A_in: sp_matrix_to_sp_tensor_value(A_),
                            I_in: I_,
                            target: y_,
                            SW_KEY: np.ones((1,))}
            outs = sess.run([train_step, loss, output], feed_dict=tr_feed_dict)
            model_loss += outs[1]
            prediction.append(list(outs[2].flatten()))    
        y_train_predict = (np.concatenate(prediction)[:len(y_train)] > 0.5).astype('uint8')
        train_accuracy = accuracy_score(y_train, y_train_predict)
        train_loss = model_loss / (np.ceil(len(y_train) / batch_size))
        
        # Validation
        batches_val = batch_iterator([A_test, X_test, y_test], batch_size=batch_size)
        model_loss = 0
        prediction = []
        
        for b in batches_val:
            batch = Batch(b[0], b[1])
            X_, A_, I_ = batch.get('XAI')
            y_ = b[2]
            tr_feed_dict = {X_in: X_,
                            A_in: sp_matrix_to_sp_tensor_value(A_),
                            I_in: I_,
                            target: y_,
                            SW_KEY: np.ones((1,))}
            loss_, output_ = sess.run([loss, output], feed_dict=tr_feed_dict)
            model_loss += loss_
            prediction.append(list(output_.flatten()))
        
        y_val_predict = (np.concatenate(prediction)[:len(y_test)] > 0.5).astype('uint8')
        val_accuracy = accuracy_score(y_test, y_val_predict)
        val_loss = model_loss / (np.ceil(len(y_test) / batch_size))
        print('---------------------------------------------')
        print('Epoch {}: train_loss: {}, train_acc: {}, val_loss: {}, val_acc: {}'.format(i+1, train_loss, train_accuracy,
              val_loss, val_accuracy))
        
        if val_accuracy > best_accuracy:
            best_accuracy = val_accuracy
            model.save(save_path)
        
    # Evaluate the model
    model = load_model(save_path)
    batches_val = batch_iterator([A_test, X_test, y_test], batch_size=batch_size)
    prediction = []
    for b in batches_val:
        batch = Batch(b[0], b[1])
        X_, A_, I_ = batch.get('XAI')
        y_ = b[2]
        tr_feed_dict = {X_in: X_,
                        A_in: sp_matrix_to_sp_tensor_value(A_),
                        I_in: I_,
                        target: y_,
                        SW_KEY: np.ones((1,))}
        output_ = sess.run([output], feed_dict=tr_feed_dict)
        prediction.append(list(output_.flatten()))
    
    y_val_predict = (np.concatenate(prediction)[:len(y_test)] > 0.5).astype('uint8')
    with warnings.catch_warnings():
        warnings.simplefilter('ignore')  # disable the warning on f1-score with not all labels
        scores = get_prediction_score(y_val, y_val_predict)
        
    return model, scores