Incompatible shapes in Stellargraph

159 Views Asked by At

I'm trying to implement a small prototype of The GCN Model using the library Stellargraph. I've my StellarGraph graph object ready, I'm trying to solve a multi-class multi-label classification problem. This means I'm trying to predict more than one column (19 exactly) each column is encoded to either 0 or 1.

Here is what I've done:

from sklearn.model_selection import train_test_split
from stellargraph.mapper import FullBatchNodeGenerator

train_subjects, test_subjects = train_test_split(nodelist, test_size = .25)
generator = FullBatchNodeGenerator(graph, method="gcn")
from stellargraph.layer import GCN

train_gen = generator.flow(train_subjects['ID'], train_subjects.drop(['ID'], axis = 1))
gcn = GCN(layer_sizes=[16, 16], activations=["relu", "relu"], generator=generator, dropout=0.5)
from tensorflow.keras import layers, optimizers, losses, metrics, Model

x_inp, x_out = gcn.in_out_tensors()
predictions = layers.Dense(units = 1, activation="sigmoid")(x_out)
from tensorflow.keras.metrics import Precision as Precision
​
model = Model(inputs=x_inp, outputs=predictions)
model.compile(
    optimizer=optimizers.Adam(learning_rate = 0.01),
    loss=losses.categorical_crossentropy,
    metrics= [Precision()])

val_gen = generator.flow(test_subjects['ID'], test_subjects.drop(['ID'], axis = 1))
from tensorflow.keras.callbacks import EarlyStopping

es_callback = EarlyStopping(monitor="val_precision", patience=200, restore_best_weights=True)

history = model.fit(
    train_gen,
    epochs=200,
    validation_data=val_gen,
    verbose=2,
    shuffle=False,  
    callbacks=[es_callback])

I've 271045 edges & 16354 nodes in total including 12265 training nodes. The issue I'm getting is a shape mismatching from Keras. It states as follows. I suspect it's due to inserting multiple columns as target columns. I've tried the model using only one column (class) & it worked perfectly.

InvalidArgumentError:  Incompatible shapes: [1,12265] vs. [1,233035]
     [[node LogicalAnd_1 (defined at tmp/ipykernel_52/2745570431.py:7) ]] [Op:__inference_train_function_1405]

It's worth mentioning that 233035 = 12265 (number of train nodes) times 19 (number of classes). Any Idea on what is going wrong here?

1

There are 1 best solutions below

0
On BEST ANSWER

I figured out the problem.

It was a newbie mistake, I initialized the Dense Classification layer with 1 unit instead of 19 (number of classes).

I just needed to fix that line to:

predictions = layers.Dense(units = 19, activation="sigmoid")(x_out)

Have a nice day!