Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

change LinearBucketEncoder's Linear layer to column-wise #30

Merged
merged 7 commits into from
Aug 24, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions torch_frame/nn/encoder/stype_encoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ def init_modules(self):
for stats in self.stats_list:
num_categories = len(stats[StatType.COUNT][0])
self.embs.append(Embedding(num_categories, self.out_channels))
self.reset_parameters()
kaidic marked this conversation as resolved.
Show resolved Hide resolved

def forward(self, x: Tensor):
r"""Maps input :obj:`x` from TensorFrame (shape [batch_size, num_cols])
Expand Down Expand Up @@ -105,7 +104,6 @@ def init_modules(self):
num_cols = len(self.stats_list)
self.weight = Parameter(torch.empty(num_cols, self.out_channels))
self.bias = Parameter(torch.empty(num_cols, self.out_channels))
self.reset_parameters()
kaidic marked this conversation as resolved.
Show resolved Hide resolved

def forward(self, x: Tensor):
r"""Maps input :obj:`x` from TensorFrame (shape [batch_size, num_cols])
Expand Down Expand Up @@ -151,10 +149,10 @@ def init_modules(self):
quantiles = [stats[StatType.QUANTILES] for stats in self.stats_list]
self.boundaries = torch.tensor(quantiles)
self.interval = self.boundaries[:, 1:] - self.boundaries[:, :-1] + 1e-9

# Adding a linear layer
input_channels = len(self.boundaries[0]) - 1
self.linear_layer = nn.Linear(input_channels, self.out_channels)
kaidic marked this conversation as resolved.
Show resolved Hide resolved
num_cols = len(self.stats_list)
self.weight = Parameter(
torch.empty(num_cols, self.interval.shape[-1], self.out_channels))
self.bias = Parameter(torch.empty(num_cols, self.out_channels))

def forward(self, x: Tensor):
encoded_values = []
Expand All @@ -177,10 +175,13 @@ def forward(self, x: Tensor):
0) + greater_mask * (1 - one_hot_mask)
encoded_values.append(encoded_value)

# Apply linear layer to the encoded values
# Apply column-wise linear transformation
encoded_values = torch.stack(encoded_values, dim=1).squeeze()
return self.linear_layer(encoded_values)
x_lin = torch.einsum('ijk,jkl->ijl', encoded_values, self.weight)
x = x_lin + self.bias
kaidic marked this conversation as resolved.
Show resolved Hide resolved
return x

def reset_parameters(self):
# Reset learnable parameters of the linear layer
self.linear_layer.reset_parameters()
# Reset learnable parameters of the linear transformation
torch.nn.init.normal_(self.weight, std=0.1)
kaidic marked this conversation as resolved.
Show resolved Hide resolved
torch.nn.init.zeros_(self.bias)
Loading