lenskit.graphs.lightgcn#

LightGCN recommendation.

Classes#

LightGCNConfig

Configuration for LightGCNScorer.

LightGCNScorer

Scorer using LightGCN [].

LightGCNTrainer

Protocol implemented by iterative trainers for models. Models that

LogisticLightGCNTrainer

Protocol implemented by iterative trainers for models. Models that

PairwiseLightGCNTrainer

Protocol implemented by iterative trainers for models. Models that

Module Contents#

class lenskit.graphs.lightgcn.LightGCNConfig#

Bases: lenskit.config.common.EmbeddingSizeMixin, pydantic.BaseModel

Configuration for LightGCNScorer.

Stability:

Experimental

embedding_size: pydantic.PositiveInt = 16#

The dimension of the embedding space (number of latent features). Seems to work best as a power of 2.

layer_count: pydantic.PositiveInt = 2#

The number of layers to use.

layer_blend: pydantic.PositiveFloat | list[pydantic.PositiveFloat] | None = None#

The blending coefficient(s) for layer blending. This is equivalent to alpha in LightGCN.

batch_size: pydantic.PositiveInt = 4096#

The training batch size.

learning_rate: pydantic.PositiveFloat = 0.01#

The learning rate for training.

epochs: pydantic.PositiveInt = 10#

The number of training epochs.

regularization: pydantic.PositiveFloat | None = 0.01#

The regularization strength.

loss: Literal['logistic', 'pairwise'] = 'pairwise'#

The loss to use for model training.

pairwise

BPR pairwise ranking loss, using LightGCN.recommend_loss().

logistic

Logistic link prediction loss, using LightGCN.link_pred_loss().

check_layer_blending()#
Return type:

Self

class lenskit.graphs.lightgcn.LightGCNScorer(config=None, **kwargs)#

Bases: lenskit.training.UsesTrainer, lenskit.pipeline.components.Component[lenskit.data.ItemList]

Scorer using LightGCN [].

Stability:

Experimental

Parameters:
  • config (object | None)

  • kwargs (Any)

config: LightGCNConfig#

The component configuration object. Component classes that support configuration must redefine this attribute with their specific configuration class type, which can be a Python dataclass or a Pydantic model class.

users: lenskit.data.Vocabulary#
items: lenskit.data.Vocabulary#
model: torch_geometric.nn.LightGCN#
to(device)#

Move the model to a different device.

__call__(query, items)#

Generate item scores for a user.

Note that user and items are both user and item IDs, not positions.

Parameters:
Return type:

lenskit.data.ItemList

create_trainer(data, options)#

Create a model trainer to train this model.

class lenskit.graphs.lightgcn.LightGCNTrainer(scorer, data, options)#

Bases: lenskit.training.ModelTrainer

Protocol implemented by iterative trainers for models. Models that implement UsesTrainer will return an object implementing this protocol from their create_trainer() method.

This protocol only defines the core aspects of training a model. Trainers should also implement ParameterContainer to allow training to be checkpointed and resumed.

It is also a good idea for the trainer to be pickleable, but the parameter container interface is the primary mechanism for checkpointing.

Stability:
Full (see Stability Levels).
Parameters:
scorer: LightGCNScorer#
data: lenskit.data.Dataset#
options: lenskit.training.TrainingOptions#
log: structlog.stdlib.BoundLogger#
rng: numpy.random.Generator#
device: str#
model: torch_geometric.nn.LightGCN#
matrix: lenskit.data.MatrixRelationshipSet#
coo: lenskit.data.matrix.COOStructure#
user_base: int#
edges: torch.Tensor#
optimizer: torch.optim.Optimizer#
epochs_trained: int = 0#
train_epoch()#

Perform one epoch of the training process, optionally returning metrics on the training behavior. After each training iteration, the mmodel must be usable.

Return type:

dict[str, float] | None

finalize()#

Finish the training process, cleaning up any unneeded data structures and doing any finalization steps to the model.

The default implementation does nothing.

abstractmethod batch_loss(mb_edges, scores)#
Parameters:
Return type:

torch.Tensor

class lenskit.graphs.lightgcn.LogisticLightGCNTrainer(scorer, data, options)#

Bases: LightGCNTrainer

Protocol implemented by iterative trainers for models. Models that implement UsesTrainer will return an object implementing this protocol from their create_trainer() method.

This protocol only defines the core aspects of training a model. Trainers should also implement ParameterContainer to allow training to be checkpointed and resumed.

It is also a good idea for the trainer to be pickleable, but the parameter container interface is the primary mechanism for checkpointing.

Stability:
Full (see Stability Levels).
Parameters:
batch_loss(mb_edges, scores)#
Parameters:
class lenskit.graphs.lightgcn.PairwiseLightGCNTrainer(scorer, data, options)#

Bases: LightGCNTrainer

Protocol implemented by iterative trainers for models. Models that implement UsesTrainer will return an object implementing this protocol from their create_trainer() method.

This protocol only defines the core aspects of training a model. Trainers should also implement ParameterContainer to allow training to be checkpointed and resumed.

It is also a good idea for the trainer to be pickleable, but the parameter container interface is the primary mechanism for checkpointing.

Stability:
Full (see Stability Levels).
Parameters:
batch_loss(mb_edges, scores)#
Parameters:

Exported Aliases#

class lenskit.graphs.lightgcn.EmbeddingSizeMixin#

Re-exported alias for lenskit.config.common.EmbeddingSizeMixin.

class lenskit.graphs.lightgcn.BatchedRange#

Re-exported alias for lenskit.data.BatchedRange.

class lenskit.graphs.lightgcn.Dataset#

Re-exported alias for lenskit.data.Dataset.

class lenskit.graphs.lightgcn.ItemList#

Re-exported alias for lenskit.data.ItemList.

class lenskit.graphs.lightgcn.MatrixRelationshipSet#

Re-exported alias for lenskit.data.MatrixRelationshipSet.

lenskit.graphs.lightgcn.QueryInput#

Re-exported alias for lenskit.data.QueryInput.

class lenskit.graphs.lightgcn.RecQuery#

Re-exported alias for lenskit.data.RecQuery.

class lenskit.graphs.lightgcn.Vocabulary#

Re-exported alias for lenskit.data.Vocabulary.

class lenskit.graphs.lightgcn.COOStructure#

Re-exported alias for lenskit.data.matrix.COOStructure.

lenskit.graphs.lightgcn.item_progress()#

Re-exported alias for lenskit.logging.progress._dispatch.item_progress().

class lenskit.graphs.lightgcn.Component#

Re-exported alias for lenskit.pipeline.components.Component.

lenskit.graphs.lightgcn.inference_mode()#

Re-exported alias for lenskit.torch.inference_mode().

class lenskit.graphs.lightgcn.ModelTrainer#

Re-exported alias for lenskit.training.ModelTrainer.

class lenskit.graphs.lightgcn.TrainingOptions#

Re-exported alias for lenskit.training.TrainingOptions.

class lenskit.graphs.lightgcn.UsesTrainer#

Re-exported alias for lenskit.training.UsesTrainer.