Model
GeoVexModel.
This pytorch lightning module implements the GeoVeX hexagonal autoencoder model.
References
¶
Bases: Module
The loss function for the GeoVeX model.
Defined in [1]. Equations (4) and (7).
PARAMETER | DESCRIPTION |
---|---|
R |
The radius of the hexagonal region.
TYPE:
|
Source code in srai/embedders/geovex/model.py
¶
Forward pass of the loss function.
PARAMETER | DESCRIPTION |
---|---|
pi |
The predicted pi tensor.
TYPE:
|
lambda_ |
The predicted lambda tensor.
TYPE:
|
y |
The target tensor.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
float
|
The loss value.
TYPE:
|
Source code in srai/embedders/geovex/model.py
HexagonalConv2d(
in_channels,
out_channels,
kernel_size=3,
stride=2,
padding=0,
bias=True,
groups=1,
)
¶
HexagonalConv2d(
in_channels,
out_channels,
kernel_size=3,
stride=2,
padding=0,
bias=True,
groups=1,
)
Bases: Module
Hexagonal Convolutional Layer.
PARAMETER | DESCRIPTION |
---|---|
in_channels |
The number of input channels.
TYPE:
|
out_channels |
The number of output channels.
TYPE:
|
kernel_size |
The size of the kernel. Defaults to 3.
TYPE:
|
stride |
The stride of the convolution. Defaults to 2.
TYPE:
|
padding |
The padding of the convolution. Defaults to 0.
TYPE:
|
bias |
Whether to use bias. Defaults to True.
TYPE:
|
groups |
The number of groups. Defaults to 1.
TYPE:
|
Source code in srai/embedders/geovex/model.py
¶
Forward pass of the HexagonalConv2d.
PARAMETER | DESCRIPTION |
---|---|
x |
The input tensor.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Tensor
|
torch.Tensor: The output tensor. |
Source code in srai/embedders/geovex/model.py
HexagonalConvTranspose2d(
in_channels,
out_channels,
kernel_size=3,
stride=2,
padding=0,
output_padding=0,
bias=True,
)
¶
HexagonalConvTranspose2d(
in_channels,
out_channels,
kernel_size=3,
stride=2,
padding=0,
output_padding=0,
bias=True,
)
Bases: HexagonalConv2d
Hexagonal Transpose Convolutional Layer.
This is a transpose convolutional layer with a hexagonal kernel.
PARAMETER | DESCRIPTION |
---|---|
in_channels |
The number of input channels.
TYPE:
|
out_channels |
The number of output channels.
TYPE:
|
kernel_size |
The size of the kernel. Defaults to 3.
TYPE:
|
stride |
The stride of the convolution. Defaults to 2.
TYPE:
|
padding |
The padding of the convolution. Defaults to 0.
TYPE:
|
output_padding |
The output padding of the convolution. Defaults to 0.
TYPE:
|
bias |
Whether to use bias. Defaults to True.
TYPE:
|
Source code in srai/embedders/geovex/model.py
¶
Forward pass of the HexagonalConv2d.
PARAMETER | DESCRIPTION |
---|---|
x |
The input tensor.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Tensor
|
torch.Tensor: The output tensor. |
Source code in srai/embedders/geovex/model.py
¶
Bases: Module
Reshape layer.
PARAMETER | DESCRIPTION |
---|---|
shape |
The shape of the output tensor.
TYPE:
|
Source code in srai/embedders/geovex/model.py
¶
Forward pass of the Reshape layer.
PARAMETER | DESCRIPTION |
---|---|
x |
Input tensor.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Tensor
|
torch.Tensor: Reshaped tensor. |
¶
Bases: Module
GeoVeX Zero-Inflated Poisson Layer.
PARAMETER | DESCRIPTION |
---|---|
in_dim |
The input dimension.
TYPE:
|
m |
The height and width of the tensor
TYPE:
|
r |
The radius of the hexagonal region.
TYPE:
|
out_dim |
The output dimension.
TYPE:
|
Source code in srai/embedders/geovex/model.py
¶
Forward pass of the GeoVeXZIP layer.
PARAMETER | DESCRIPTION |
---|---|
x |
The input tensor.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
tuple[Tensor, Tensor]
|
Tuple[torch.Tensor, torch.Tensor]: The predicted pi and lambda tensors. |
Source code in srai/embedders/geovex/model.py
¶
Bases: Model
GeoVeX Model.
This class implements the GeoVeX model. It is based on a convolutional autoencoder with a Zero- Inflated Poisson layer. The model is described in [1]. It takes a 3d tensor as input (counts of features per region) and outputs dense embeddings. The 3d tensor consists of the target region at the center and radius R neighbors around it.
PARAMETER | DESCRIPTION |
---|---|
k_dim |
the number of input channels
TYPE:
|
radius |
the radius of the hexagonal region
TYPE:
|
conv_layers |
The number of convolutional layers. Defaults to 2.
TYPE:
|
emb_size |
The dimension of the inner embedding. Defaults to 32.
TYPE:
|
learning_rate |
The learning rate. Defaults to 1e-5.
TYPE:
|
conv_layer_size |
The size of the initial convolutional layer.
TYPE:
|
Source code in srai/embedders/geovex/model.py
401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 |
|
¶
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
¶
Forward pass of the GeoVeX model.
PARAMETER | DESCRIPTION |
---|---|
x |
The input tensor. The dimensions are (batch_size, k_dim, R * 2 + 1, R * 2 + 1).
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
tuple[Tensor, Tensor]
|
torch.Tensor: The output tensor. |
Source code in srai/embedders/geovex/model.py
¶
Perform a training step. This is called by PyTorch Lightning.
One training step consists of a forward pass, a loss calculation, and a backward pass.
PARAMETER | DESCRIPTION |
---|---|
batch |
The batch of data.
TYPE:
|
batch_idx |
The index of the batch.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Tensor
|
torch.Tensor: The loss value. |
Source code in srai/embedders/geovex/model.py
¶
Perform a validation step. This is called by PyTorch Lightning.
PARAMETER | DESCRIPTION |
---|---|
batch |
The batch of data.
TYPE:
|
batch_idx |
The index of the batch.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Tensor
|
torch.Tensor: The loss value. |
Source code in srai/embedders/geovex/model.py
¶
Configure the optimizers. This is called by PyTorch Lightning.
RETURNS | DESCRIPTION |
---|---|
list[Optimizer]
|
List[torch.optim.Optimizer]: The optimizers. |
Source code in srai/embedders/geovex/model.py
¶
Get the model configuration.
RETURNS | DESCRIPTION |
---|---|
dict[str, Union[int, float]]
|
Dict[str, Union[int, float]]: The model configuration. |
Source code in srai/embedders/geovex/model.py
¶
Calculates the radius of a cube given its coordinates.
Ref https://www.redblobgames.com/grids/hexagons/#distances-axial
PARAMETER | DESCRIPTION |
---|---|
i |
The x-coordinate of the cube.
TYPE:
|
j |
The y-coordinate of the cube.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
int
|
The radius of the cube.
TYPE:
|
Source code in srai/embedders/geovex/model.py
¶
Calculates the maximum distance between two points in a cube.
PARAMETER | DESCRIPTION |
---|---|
a |
The first point.
TYPE:
|
b |
The second point.
TYPE:
|
Source code in srai/embedders/geovex/model.py
¶
Subtracts the coordinates of two 3D points and returns the resulting tuple.
PARAMETER | DESCRIPTION |
---|---|
a |
The first point to subtract from.
TYPE:
|
b |
The second point to subtract.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
tuple[int, int, int]
|
Tuple[int, int, int]: The resulting tuple of subtracted coordinates. |
Source code in srai/embedders/geovex/model.py
¶
Get the shape of the embedding.
PARAMETER | DESCRIPTION |
---|---|
r |
The radius of the hexagonal region.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
int
|
The shape of the embedding.
TYPE:
|
¶
Build the mask functions for the loss function. These functions depend on the radius of the hexagonal region. They weight the loss function to give more importance to the center of the region.
PARAMETER | DESCRIPTION |
---|---|
R |
Radius of the hexagonal region.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
tuple[Callable[[int, int], float], ...]
|
Tuple[callable, callable]: The mask functions. |