liveBook · Manning

Negative sampling

As explained in Graph Neural Networks in Action

A training technique that selects a subset of nonexistent relationships as negative examples instead of evaluating all possible negative examples.

Negative sampling trains link-prediction models with a manageable subset of nonexistent edges instead of evaluating every possible negative relationship. It addresses the class imbalance between existing and nonexistent edges. section 5.2.3

A graph autoencoder training step follows this pipeline:

  • Encode the graph into a latent representation. section 5.2.3
  • Draw new nonexistent edges each epoch with negative_sampling, matching the number of training edge labels. section 5.2.3
  • Concatenate positive and negative edge indices, decode them, and assign labels 1 and 0 respectively. section 5.2.3
  • Compute binary cross-entropy, backpropagate, and update the model; evaluate validation and test edges without resampling. section 5.2.3

PyTorch Geometric training step

Call negative_sampling on train_data.edge_index, use the returned neg_edge_index with the positive edge_label_index, and create matching zero labels before calculating BCEWithLogitsLoss. section 5.2.3

Figure 5.6 Overall steps for training our model for link prediction
figure

Follow the end-to-end link-prediction pipeline from graph encoding through sampled training edges and evaluation. section 5.2.3

How Graph Neural Networks in Action covers it

How deep it goes
Explains per-epoch sampling, edge concatenation, zero-label construction, loss calculation, and evaluation without sampling.
Examples
Uses a PyTorch Geometric training function that samples sparse negative edges and trains for repeated epochs.
What it assumes
Assumes graph edges, encoders, decoders, binary labels, and training loops while demonstrating their interaction.
What you can do after
Apply it via PyTorch Geometric to construct sampled edge-label batches for graph-autoencoder training.
How it connects
Connects graph autoencoders, link prediction, binary cross-entropy, latent representations, and ROC/AUC evaluation.

Also explained in

Compare how 2 books explain negative sampling →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Graph Neural Networks in Action. The section links point to the parts of the book it draws on.