我正在尝试使用Stellargraph库实现GCN模型的小型原型。我已经准备好了我的StellarGraph图形对象,我正在尝试解决一个多类多标签分类问题。这意味着我试图预测不止一列(准确地说是19列),每一列被编码为0或1。
这是我所做的:
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])
我有271045条边&共16354个节点,其中训练节点12265个。我得到的问题是从Keras形状不匹配。它陈述如下。我怀疑这是由于插入多个列作为目标列。我已经尝试过只使用一列(类)模型&
InvalidArgumentError: Incompatible shapes: [1,12265] vs. [1,233035]
[[node LogicalAnd_1 (defined at tmp/ipykernel_52/2745570431.py:7) ]] [Op:__inference_train_function_1405]
值得一提的是,233035 = 12265(列车节点数)乘以19(类数)。你知道这里出了什么问题吗?
我把问题解决了。
这是一个新手的错误,我用1个单位初始化密集分类层,而不是19个(类的数量)。
我只需要将这行修改为:
predictions = layers.Dense(units = 19, activation="sigmoid")(x_out)
祝你今天愉快!