Skip to content

feat: add MiRU1Cell, MiRU1, MiRU2Cell and MiRU2 implementations - #45

Merged
MartinuzziFrancesco merged 5 commits into
MartinuzziFrancesco:mainfrom
P-r-e-m-i-u-m:feat/miru-cell
Mar 12, 2026
Merged

feat: add MiRU1Cell, MiRU1, MiRU2Cell and MiRU2 implementations#45
MartinuzziFrancesco merged 5 commits into
MartinuzziFrancesco:mainfrom
P-r-e-m-i-u-m:feat/miru-cell

Conversation

@P-r-e-m-i-u-m

Copy link
Copy Markdown
Contributor

Closes #44

Summary

Implements the Minion Gated Unit (MiRU) from:
https://doi.org/10.1016/j.neucom.2026.132847

Changes

  • Added MiRU1Cell and MiRU1 — variant with reset gate r(t)
  • Added MiRU2Cell and MiRU2 — variant with learned θ parameter
  • Registered both in cells/__init__.py and torchrecurrent/__init__.py

Equations

MiRU1:
r(t) = σ(Wr x(t) + br + Ur h(t-1))
h~(t) = tanh(Wh x(t) + bh + Uh(r(t) ⊙ h(t-1)))
h(t) = λ ⊙ h(t-1) + (1-λ) ⊙ h~(t)

MiRU2:
h~(t) = tanh(Wh x(t) + bh + Uh(θ ⊙ h(t-1)))
h(t) = λ ⊙ h(t-1) + (1-λ) ⊙ h~(t)

Signed-off-by: 🄂ʏᴇᴅ 🄰ʙᴅᴜʟ 🄰ᴍᴀ🄝 ✧ <amanbaba9404522@gmail.com>
Comment thread torchrecurrent/cells/miru_cell.py Outdated
Comment on lines +220 to +222
self.lambda_ = nn.Parameter(
torch.full((hidden_size,), 0.5, device=device, dtype=dtype)
)

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

lambda is not a learnable parameter. also call it just lambda

Comment thread torchrecurrent/cells/miru_cell.py Outdated
candidate = self.nonlinearity(ch)

# Update: h(t) = lambda * h + (1 - lambda) * h~
lam = torch.sigmoid(self.lambda_)

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

why is the parameter passed through a nonlinearity?

Comment thread torchrecurrent/cells/miru_cell.py Outdated
Comment on lines +465 to +470
self.lambda_ = nn.Parameter(
torch.full((hidden_size,), 0.5, device=device, dtype=dtype)
)
self.theta = nn.Parameter(
torch.full((hidden_size,), 0.5, device=device, dtype=dtype)
)

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

not learnable parameters, they are hyperparameters of the cell

Comment thread torchrecurrent/cells/miru_cell.py Outdated
inp, state, is_batched = self._preprocess_input_and_state(inp, state)

# Candidate: h~(t) = tanh(W_h x + b_h + U_h (theta * h))
theta = torch.sigmoid(self.theta)

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

theta is not passed through an activation function in the paper definition

Comment thread torchrecurrent/cells/miru_cell.py Outdated
candidate = self.nonlinearity(ch)

# Update: h(t) = lambda * h + (1 - lambda) * h~
lam = torch.sigmoid(self.lambda_)

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

lambda is not passed through an activation function in the paper definition

@MartinuzziFrancesco

Copy link
Copy Markdown
Owner

There are a couple of points to address before merging. also make sure to add the cell and the model to the documentation

Signed-off-by: 🄂ʏᴇᴅ 🄰ʙᴅᴜʟ 🄰ᴍᴀ🄝 ✧ <amanbaba9404522@gmail.com>
Signed-off-by: 🄂ʏᴇᴅ 🄰ʙᴅᴜʟ 🄰ᴍᴀ🄝 ✧ <amanbaba9404522@gmail.com>
@P-r-e-m-i-u-m

Copy link
Copy Markdown
Contributor Author

"Hi @MartinuzziFrancesco, I've addressed all the review comments:

lambda_ and theta are now plain float hyperparameters (not nn.Parameter)
Removed torch.sigmoid() applied to them in forward()
They are used directly in the update equations

Could you also let me know where to add the documentation? Happy to add it!"

Comment thread torchrecurrent/cells/miru_cell.py Outdated
Default: True
recurrent_bias: If ``False``, the layer does not use recurrent biases.
Default: True
lambda_: Mixing hyperparameter controlling the blend between the

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

just lambda will do

Comment thread torchrecurrent/cells/miru_cell.py Outdated
Default: ``True``.
recurrent_bias: If ``False``, the layer does not use recurrent biases.
Default: ``True``.
lambda_: Mixing hyperparameter controlling the blend between the

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

just lambda

Comment thread torchrecurrent/cells/miru_cell.py Outdated
"hidden_size",
"bias",
"recurrent_bias",
"lambda_",

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

just lambda

Comment thread torchrecurrent/cells/miru_cell.py Outdated
hidden_size: int,
bias: bool = True,
recurrent_bias: bool = True,
lambda_: float = 0.5,

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

lambda

Comment thread torchrecurrent/cells/miru_cell.py Outdated
super(MiRU1Cell, self).__init__(
input_size, hidden_size, bias, recurrent_bias, device=device, dtype=dtype
)
self.lambda_ = lambda_

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

lambda

Comment thread torchrecurrent/cells/miru_cell.py Outdated
candidate = self.nonlinearity(ch)

# Update: h(t) = lambda * h + (1 - lambda) * h~
new_state = self.lambda_ * state + (1.0 - self.lambda_) * candidate

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

lambda

@MartinuzziFrancesco

MartinuzziFrancesco commented Mar 11, 2026

Copy link
Copy Markdown
Owner

Keep the nomenclature consistent, as in have lambda not lambda_. beside that everything else seems correct.

The models should go in the models page here, while docstings for the cells and layers should go in api. please keep in mind that all these lists are alphabetical

@P-r-e-m-i-u-m

Copy link
Copy Markdown
Contributor Author

"Hi @MartinuzziFrancesco, lambda is a reserved keyword in Python and cannot be used as a variable or parameter name. lambda_ is the standard convention (used by PyTorch itself, e.g. lr_lambda). Could we keep lambda_ for this reason?"

@P-r-e-m-i-u-m

Copy link
Copy Markdown
Contributor Author

"Also, could you point me to an existing cell's doc page so I can follow the same format for MiRU?"

Signed-off-by: 🄂ʏᴇᴅ 🄰ʙᴅᴜʟ 🄰ᴍᴀ🄝 ✧ <amanbaba9404522@gmail.com>
@P-r-e-m-i-u-m

Copy link
Copy Markdown
Contributor Author

"Hi @MartinuzziFrancesco, I've added the documentation:

MiRU1 and MiRU2 added to docs/models.rst (alphabetically after MGU)
MiRU1Cell and MiRU2Cell added to docs/api/cells.rst
MiRU1 and MiRU2 added to docs/api/layers.rst

Also regarding lambda_ — since lambda is a reserved keyword in Python, I've kept it as lambda_ following standard convention. Please let me know if you'd prefer a different name!"

@MartinuzziFrancesco

Copy link
Copy Markdown
Owner

Also regarding lambda_ — since lambda is a reserved keyword in Python, I've kept it as lambda_ following standard convention. Please let me know if you'd prefer a different name!"

yeha good point I completely forgot about that. I also think that we could use more expressive names, so following the paper I would use update_coefficient for lambda and reset_coefficient for beta

Signed-off-by: 🄂ʏᴇᴅ 🄰ʙᴅᴜʟ 🄰ᴍᴀ🄝 ✧ <amanbaba9404522@gmail.com>
@P-r-e-m-i-u-m

Copy link
Copy Markdown
Contributor Author

"Hi @MartinuzziFrancesco, I've renamed lambda_ to update_coefficient and theta to reset_coefficient following the paper's naming convention. The math equations in the docstrings still use \lambda and \theta as per the paper."

@MartinuzziFrancesco
MartinuzziFrancesco merged commit 0efbdea into MartinuzziFrancesco:main Mar 12, 2026
20 checks passed
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.

Minion gated unit (MiRUCell/MiRU)

2 participants