Model
Hex2Vec model.
This module contains the embedding model from Hex2Vec paper[1].
References
¶
Bases: Model
Hex2Vec embedding model.
This class implements the embedding model from Hex2Vec paper. It is based on a skip-gram model with negative sampling and triplet-loss. The model takes vectors of numbers as input (raw counts of features) per region and outputs dense embeddings.
PARAMETER | DESCRIPTION |
---|---|
layer_sizes |
List of sizes for the model layers. The first element is the input size (number of features), the last element is the output (embedding) size.
TYPE:
|
learning_rate |
Learning rate. Defaults to 0.001.
TYPE:
|
RAISES | DESCRIPTION |
---|---|
ValueError
|
If layer_sizes contains less than 2 elements. |
Source code in srai/embedders/hex2vec/model.py
¶
Get model config.
Source code in srai/embedders/_base.py
¶
Save the model to a directory.
PARAMETER | DESCRIPTION |
---|---|
path |
Path to the directory.
TYPE:
|
¶
classmethod
Load model from a file.
PARAMETER | DESCRIPTION |
---|---|
path |
Path to the file.
TYPE:
|
**kwargs |
Additional kwargs to pass to the model constructor.
TYPE:
|
Source code in srai/embedders/_base.py
¶
Calculate embedding for a region.
PARAMETER | DESCRIPTION |
---|---|
X_anchor |
Region features.
TYPE:
|
¶
Predict the probability of X_anchor being neighbours with X_context.
X_anchor and X_context are assumed to have the same batch size. The probabilities are calculated in pairs, i.e. the first element of X_anchor is compared with the first element of X_context.
PARAMETER | DESCRIPTION |
---|---|
X_anchor |
Anchor regions.
TYPE:
|
X_context |
Context regions.
TYPE:
|
Source code in srai/embedders/hex2vec/model.py
¶
Predict raw unnormalized scores of X_anchor being neighbours with X_context.
X_anchor and X_context are assumed to have the same batch size. The scores are calculated in pairs, i.e. the first element of X_anchor is compared with the first element of X_context. In order to get probabilities, use the sigmoid function.
PARAMETER | DESCRIPTION |
---|---|
X_anchor |
Anchor regions.
TYPE:
|
X_context |
Context regions.
TYPE:
|
Source code in srai/embedders/hex2vec/model.py
¶
Perform one training step.
One batch of data consists of 3 tensors
- X_anchor: Anchor regions.
- X_positive: Positive regions. The regions assumed to be neighbours of the corresponding regions in X_anchor.
- X_negative: Negative regions. The regions assumed to NOT be neighbours of the corresponding regions in X_anchor.
The regions in X_anchor, X_positive and X_negative are first embedded using the encoder.
After that, the dot product of the corresponding embeddings is calculated.
The loss is calculated as a binary cross-entropy between the dot product and the labels.
PARAMETER | DESCRIPTION |
---|---|
batch |
Batch of data.
TYPE:
|
batch_idx |
Batch index.
TYPE:
|
Source code in srai/embedders/hex2vec/model.py
¶
Perform one validation step.
PARAMETER | DESCRIPTION |
---|---|
batch |
Batch of data.
TYPE:
|
batch_idx |
Batch index.
TYPE:
|