简体   繁体   English

Stellargraph 中的不兼容形状

[英]Incompatible shapes in Stellargraph

I'm trying to implement a small prototype of The GCN Model using the library Stellargraph.我正在尝试使用 Stellargraph 库实现 GCN Model 的小型原型。 I've my StellarGraph graph object ready, I'm trying to solve a multi-class multi-label classification problem.我已经准备好我的 StellarGraph 图 object,我正在尝试解决多类多标签分类问题。 This means I'm trying to predict more than one column (19 exactly) each column is encoded to either 0 or 1.这意味着我正在尝试预测不止一列(恰好 19 列),每一列都被编码为 0 或 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.我总共有 271045 个边和 16354 个节点,包括 12265 个训练节点。 The issue I'm getting is a shape mismatching from Keras. It states as follows.我遇到的问题是形状与 Keras 不匹配。它说明如下。 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.我只使用一列(类)尝试了 model,它运行良好。

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).值得一提的是 233035 = 12265(列车节点数)乘以 19(类数)。 Any Idea on what is going wrong here?关于这里出了什么问题的任何想法?

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).这是一个新手错误,我用 1 个单元而不是 19(类数)初始化了密集分类层。

I just needed to fix that line to:我只需要将该行修复为:

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

Have a nice day!祝你今天过得愉快!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM