首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >StellarGraph PaddedGraphGenerator -如何提供特定的培训、验证和测试集

StellarGraph PaddedGraphGenerator -如何提供特定的培训、验证和测试集
EN

Stack Overflow用户
提问于 2022-06-03 21:21:46
回答 1查看 190关注 0票数 1

我试图使用StellarGraph库来训练一个基本的图形神经网络,特别是从本文提供的例子开始。

这个例子很好,但现在我想重复同样的努力,删除N折叠交叉验证,并提供特定的培训、验证和测试集。我试图用以下代码来实现这个目标:

代码语言:javascript
复制
# One hot encoding
graph_training_set_labels_encoded = pd.get_dummies(graphs_training_set_labels, drop_first=True)
graph_validation_set_labels_encoded = pd.get_dummies(graphs_validation_set_labels, drop_first=True)

graphs = graphs_training_set + graphs_validation_set

# Graph generator preparation
generator = PaddedGraphGenerator(graphs=graphs)

train_gen = generator.flow([x for x in range(0, len(graphs_training_set))],
                           targets=graph_training_set_labels_encoded,
                           batch_size=batch_size)

valid_gen = generator.flow([x for x in range(len(graphs_training_set),
                                            len(graphs_training_set) + len(graphs_validation_set))],
                          targets=graph_validation_set_labels_encoded,
                          batch_size=batch_size)

# Stopping criterium
es = EarlyStopping(monitor="val_loss",
                   min_delta=0,
                   patience=20,
                   restore_best_weights=True)

# Model definition
gc_model = GCNSupervisedGraphClassification(layer_sizes=[64, 64],
                                            activations=["relu", "relu"],
                                            generator=generator,
                                            dropout=dropout_value)

x_inp, x_out = gc_model.in_out_tensors()
predictions = Dense(units=32, activation="relu")(x_out)
predictions = Dense(units=16, activation="relu")(predictions)
predictions = Dense(units=1, activation="sigmoid")(predictions)

# Creating Keras model and preparing it for training
model = Model(inputs=x_inp, outputs=predictions)
model.compile(optimizer=Adam(adam_value), loss=binary_crossentropy, metrics=["acc"])

# GNN Training
history = model.fit(train_gen, epochs=num_epochs, validation_data=valid_gen, verbose=0, callbacks=[es])

# Calculate performance on the validation data
test_metrics = model.evaluate(valid_gen, verbose=0)
valid_acc = test_metrics[model.metrics_names.index("acc")]

print(f"Test Accuracy model = {valid_acc}")

其中graphs_training_setgraphs_validation_set是StellarDiGraphs的列表。

我能够运行这段代码,但结果是它提供了NaN。有什么问题吗?

因为这是我第一次使用StellarGraph,尤其是PaddedGraphGenerator。我认为我的错误依赖于这个生成器的使用,但是以不同的方式提供培训集和验证集并没有产生更好的结果。

提前谢谢你。

UPDATE修正了我在代码中的错误,正如here所指出的(感谢george123)。

0

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-07-23 09:19:32

我在StellarGraph documentation for PaddedGraphGeneratorGCN Neural Network Class GCNSupervisedGraphClassification中找到了一个解决方案。此外,我还在StellarGraph Issue Tracker上发现了一个类似的问题,并指出了解决方案。

代码语言:javascript
复制
# Graph generator preparation
generator = PaddedGraphGenerator(graphs=graphs)

train_gen = generator.flow([x for x in range(0, num_graphs_for_training)],
                            targets=training_graphs_labels,
                            batch_size=35)
valid_gen = generator.flow([x for x in range(num_graphs_for_training, num_graphs_for_training + num_graphs_for_validation)],
                            targets=validation_graphs_labels,
                            batch_size=35)

# Stopping criterium
es = EarlyStopping(monitor="val_loss",
                    min_delta=0.001,
                    patience=10,
                    restore_best_weights=True)

# Model definition
gc_model = GCNSupervisedGraphClassification(layer_sizes=[64, 64],
                                            activations=["relu", "relu"],
                                            generator=generator,
                                            dropout=dropout_value)

x_inp, x_out = gc_model.in_out_tensors()
predictions = Dense(units=32, activation="relu")(x_out)
predictions = Dense(units=16, activation="relu")(predictions)
predictions = Dense(units=1, activation="sigmoid")(predictions)

# Let's create the Keras model and prepare it for training
model = Model(inputs=x_inp, outputs=predictions)
model.compile(optimizer=Adam(adam_value), loss=binary_crossentropy, metrics=["acc"])

# GNN Training
history = model.fit(train_gen, epochs=num_epochs, validation_data=valid_gen, verbose=1, callbacks=[es])

# Evaluate performance on the validation data
valid_metrics = model.evaluate(valid_gen, verbose=0)
valid_acc = valid_metrics[model.metrics_names.index("acc")]

# Define test set indices temporary vars
index_begin_test_set = num_graphs_for_training + num_graphs_for_validation
index_end_test_set = index_begin_test_set + num_graphs_for_testing

test_set_indices = [x for x in range(index_begin_test_set, index_end_test_set)]

# Evaluate performance on test set
generator_for_test_set = PaddedGraphGenerator(graphs=graphs)
test_gen = generator_for_test_set.flow(test_set_indices)
result = model.predict(test_gen)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72495273

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档