<jupyter_start><jupyter_text>Dataset Preparation<jupyter_code>g1_f = pd.DataFrame(
    {"x": [1, 2, 3, 4], "y": [-0.5, 0.3, 0.15, -0.35]}, index=["a", "b", "c", "d"]
)
g1_f
g1 = pd.DataFrame(
    {"source": ["a", "b", "c", "d", "a"], "target": ["b", "c", "d", "a", "c"]}
)
g1
g_final = sg.StellarDiGraph(g1_f, g1)
print(g_final.info())

g2_f = pd.DataFrame(
    {"x": [10, 20, 30, 40], "y": [-0.25, 0.39, 0.5, -0.5]}, index=["a1", "b1", "c1", "d1"]
)
g2_f
g2 = pd.DataFrame(
    {"source": ["a1", "b1", "c1", "d1", "a1"], "target": ["b1", "c1", "d1", "a1", "c1"]}
)
g2
g_final2 = sg.StellarDiGraph(g2_f, g2)
print(g_final2.info())
g3_f = pd.DataFrame(
    {"x": [15, 25, 35, 52], "y": [-0.15, 0.40, 0.52, -0.15]}, index=["a11", "b11", "c11", "d11"]
)
g3_f
g3 = pd.DataFrame(
    {"source": ["a11", "b11", "c11", "d11", "a11"], "target": ["b11", "c11", "a11", "b11", "d11"]}
)
g3
g_final3 = sg.StellarDiGraph(g3_f, g3)
print(g_final3.info())
g4_f = pd.DataFrame(
    {"x": [115, 125, 315, 152], "y": [-0.915, 0.840, 0.952, -0.715]}, index=["a111", "b111", "c111", "d111"]
)
g4_f
g4 = pd.DataFrame(
    {"source": ["a111", "b111", "c111", "d111", "a111"], "target": ["d111", "b111", "d111", "b111", "a111"]}
)
g4
g_final4 = sg.StellarDiGraph(g4_f, g4)
print(g_final4.info())

#classify graph class

graph_labels = [0,1,0,1]
graph_labels = pd.get_dummies(graph_labels, drop_first=True)

graph_labels
#List of 4 graphs
graphs = [g_final, g_final2,g_final3,g_final4]<jupyter_output><empty_output><jupyter_text>Modelling<jupyter_code>from stellargraph.mapper import PaddedGraphGenerator
from stellargraph.layer import GCNSupervisedGraphClassification
from stellargraph import StellarGraph

from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation
from tensorflow import keras

from sklearn import model_selection

from tensorflow.keras import Model
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.losses import binary_crossentropy
from tensorflow.keras.callbacks import EarlyStopping
import tensorflow as tf

from stellargraph.layer import DeepGraphCNN
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.layers import Dense, Conv1D, MaxPool1D, Dropout, Flatten
print(graphs[0].info())

#Prepare graph generator
#To feed data to the tf.Keras model that will be created later, we need a data generator. 

generator = PaddedGraphGenerator(graphs = graphs)

#First create the base DGCNN model that includes the graph convolutional and SortPooling layers.

k = 35  # the number of rows for the output tensor
layer_sizes = [32, 32, 32, 1]

dgcnn_model = DeepGraphCNN(
    layer_sizes=layer_sizes,
    activations=["tanh", "tanh", "tanh", "tanh"],
    k=k,
    bias=False,
    generator=generator,
)
x_inp, x_out = dgcnn_model.in_out_tensors()
#Next, add the convolutional, max pooling, and dense layers.

x_out = Conv1D(filters=16, kernel_size=sum(layer_sizes), strides=sum(layer_sizes))(x_out)
x_out = MaxPool1D(pool_size=2)(x_out)

x_out = Conv1D(filters=32, kernel_size=5, strides=1)(x_out)

x_out = Flatten()(x_out)

x_out = Dense(units=128, activation="relu")(x_out)
x_out = Dropout(rate=0.5)(x_out)

predictions = Dense(units=1, activation="sigmoid")(x_out)
#Finally, create the Keras model and prepare it for training by specifying the loss and optimisation algorithm.

model = Model(inputs=x_inp, outputs=predictions)

model.compile(
    optimizer=Adam(lr=0.0001), loss=binary_crossentropy, metrics=["acc"],
)<jupyter_output><empty_output><jupyter_text>Train the modelWe can now train the model using the model’s fit method.<jupyter_code>train_graphs, test_graphs = model_selection.train_test_split(
    graph_labels, train_size=0.5, test_size=0.5, stratify=graph_labels,
)
#Create a StellarGraph.PaddedGenerator generator object that prepares the data for training.
#Create data generators suitable for training at tf.keras model by calling the latter generator’s flow method specifying the train and test data.

gen = PaddedGraphGenerator(graphs=graphs)

train_gen = gen.flow(
    list(train_graphs.index -1),
    targets=train_graphs.values,
    batch_size=10,
    symmetric_normalization=False,
)

test_gen = gen.flow(
    list(test_graphs.index -1),
    targets=test_graphs.values,
    batch_size=1,
    symmetric_normalization=False,
)
epochs = 20
history = model.fit(
    train_gen, epochs=epochs, verbose=1, validation_data=test_gen, shuffle=True,
)
sg.utils.plot_history(history)
test_metrics = model.evaluate(test_gen)
print("\nTest Set Metrics:")
for name, val in zip(model.metrics_names, test_metrics):
    print("\t{}: {:0.4f}".format(name, val))<jupyter_output>2/2 [==============================] - 0s 2ms/step - loss: 0.6991 - acc: 0.5000

Test Set Metrics:
	loss: 0.6991
	acc: 0.5000
{'fraction_non_alphanumeric': 0.1610667786644267, 'fraction_numerical': 0.05732885342293154, 'mean_word_length': 5.497953615279672, 'pattern_counts': {'":': 16, '<': 12, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '412121', 'license': 'MIT', 'n_tokens_mistral': 2017, 'n_tokens_neox': 1846, 'n_words': 450, 'path': 'code/Graph_classification_final.ipynb', 'repo_name': 'asifabdullah-git/StellarGraph', 'size': '4762'}