Skip to content

Conversation

@Mustapha-AJEGHRIR
Copy link

Hello there,
I have added batch support for the LSTM_AE. I don't know if this is 100% compatible with the library, but I'm only using the LSTM_AE in one of my projects, so I decided to contribute 😃 .
Thanks

@adrenaline-pr-bot
Copy link

Preparing review...

@adrenaline-pr-bot
Copy link

adrenaline-pr-bot bot commented Dec 21, 2023

PR Analysis

(review updated until commit cf0a261)

  • 🎯 Main theme: Batch Support Addition
  • 📝 PR summary: This PR adds batch support to the LSTM_AE model in the sequitur library. The changes mainly involve modifying the forward methods of the Encoder, Decoder, and LSTM_AE classes to handle batched inputs.
  • 📌 Type of PR: Enhancement
  • 🧪 Relevant tests added: No
  • Focused PR: Yes, the PR is focused as all changes are related to the addition of batch support for LSTM_AE.
  • 🔒 Security concerns: No

PR Feedback

  • 💡 General suggestions: The PR is generally well-structured and the changes are clear. However, it would be beneficial to add tests to verify the new functionality. Additionally, it would be good to ensure that these changes are compatible with the rest of the library, as the contributor mentioned uncertainty about this.

def forward(self, x, seq_len):
x = x.repeat(seq_len, 1).unsqueeze(0)
if len(x.shape) == 1 : # In case the batch dimension is not there
x = x.repeat(seq_len, 1) # Add the sequence dimension by repeating the embedding

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider using the .unsqueeze() function to add an extra dimension to the tensor instead of using .repeat(). This could potentially improve performance as it avoids creating a larger tensor. [medium]


def forward(self, x, seq_len):
x = x.repeat(seq_len, 1).unsqueeze(0)
if len(x.shape) == 1 : # In case the batch dimension is not there

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a good practice to handle tensor shape manipulations in a separate function. This would make the code more readable and maintainable. [medium]


def forward(self, x):
seq_len = x.shape[0]
if len(x.shape) <= 2 : # In case the batch dimension is not there

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of checking the tensor dimensions in every forward pass, consider reshaping the input tensor to always have a batch dimension. This would simplify the forward methods and potentially improve performance. [important]

torch.rand((layer_dims[-1], out_dim), dtype=torch.float),
requires_grad=True
)
self.dense_layer = nn.Linear(layer_dims[-1], out_dim)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The change from using a dense matrix to a linear layer is a good one as it makes the code more readable and leverages PyTorch's built-in functionality. However, ensure that this change doesn't affect the model's performance or results. [medium]

@adrenaline-pr-bot
Copy link

Persistent review updated to latest commit cf0a261

1 similar comment
@adrenaline-pr-bot
Copy link

Persistent review updated to latest commit cf0a261


def forward(self, x):
seq_len = x.shape[0]
if len(x.shape) <= 2 : # In case the batch dimension is not there

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider adding comments to explain the purpose of the conditional statements checking the shape of 'x'. This would make the code more readable and easier to maintain. [medium]

if len(x.shape) == 1 : # In case the batch dimension is not there
x = x.repeat(seq_len, 1) # Add the sequence dimension by repeating the embedding
else :
x = x.unsqueeze(1).repeat(1, seq_len, 1) # Add the sequence dimension by repeating the embedding

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The 'unsqueeze' method is used to add an extra dimension to the tensor. However, this is only done when the shape of 'x' is 1. It would be good to add a comment explaining why this is necessary, to improve code readability. [medium]

def forward(self, x, seq_len):
x = x.repeat(seq_len, 1).unsqueeze(0)
if len(x.shape) == 1 : # In case the batch dimension is not there
x = x.repeat(seq_len, 1) # Add the sequence dimension by repeating the embedding

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The 'repeat' method is used to repeat the tensor along a specified dimension. However, it's not immediately clear why this is necessary. Adding a comment to explain this would improve code readability. [medium]

torch.rand((layer_dims[-1], out_dim), dtype=torch.float),
requires_grad=True
)
self.dense_layer = nn.Linear(layer_dims[-1], out_dim)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The dense layer is created using the 'nn.Linear' method. It would be beneficial to add a comment explaining why this change was made from using a dense matrix to a dense layer. [medium]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant