Loading your learning…
Loading your learning…
Predicting a click often depends on combinations of person, item, and context features. Build a layer that explicitly multiplies these features, with a separate learned mixture for each output coordinate.
import torchfrom torch import nn class CrossNetworkV2(nn.Module): def __init__(self, dim, depth=2): super().__init__() self.layers = nn.ModuleList([nn.Linear(dim, dim) for _ in range(depth)]) def forward(self, x0): # x0: [batch, features], floating-point embeddings and context. x = x0 for layer in self.layers: x = x0 * layer(x) + x return x if __name__ == "__main__": cross = CrossNetworkV2(dim=2, depth=1) with torch.no_grad(): cross.layers[0].weight.copy_(torch.tensor([[1., 2.], [3., 4.]])) cross.layers[0].bias.zero_() x0 = torch.tensor([[2., 3.]]) result = cross(x0) torch.testing.assert_close(result, torch.tensor([[18., 57.]])) print(result)The input x0 has shape [B, D]: B examples with D numeric features each, such as concatenated person and item embeddings plus context. Each Linear(D, D) has a D-by-D weight matrix W and a length-D bias b. Its output coordinate i is the sum of W[i, j] times x[j] over all j, plus b[i]. ModuleList registers every layer so an optimizer can train its parameters.
from torch import nn class CrossNetworkV2(nn.Module): def __init__(self, dim, depth=2): super().__init__() self.layers = nn.ModuleList([nn.Linear(dim, dim) for _ in range(depth)]) def forward(self, x0):