Skip to content

Add bidirectional text RNN learner averaging#80

Open
jddark62 wants to merge 1 commit into
manikyabard:masterfrom
jddark62:codex/text-bidirectional-rnn-5-v2
Open

Add bidirectional text RNN learner averaging#80
jddark62 wants to merge 1 commit into
manikyabard:masterfrom
jddark62:codex/text-bidirectional-rnn-5-v2

Conversation

@jddark62

@jddark62 jddark62 commented Jun 5, 2026

Copy link
Copy Markdown

Closes #5

Summary

  • Adds a bidirectional text learner path for AWD_LSTM classifiers when the existing config sets bidir: true.
  • Builds separate forward and backward classifier learners, trains/loads separate LM encoders, and returns a wrapper that trains both learners together.
  • Averages forward/backward prediction tensors in get_preds and averages probabilities in predict.
  • Threads backwards through text databunch creation so the backward learner uses reversed text batches.

Validation

  • python3 -m py_compile text/learner.py text/databunch.py tests/test_text_bidirectional_learner.py
  • python3 -m unittest tests.test_text_bidirectional_learner
  • git diff --check

Note: I validated the wrapper behavior with fastai stubs because the full legacy fastai/torch stack is not installed in this local environment.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Adds a bidirectional text learner workflow for AWD_LSTM-based text classifiers when bidir: true is set in the config, by training/using separate forward and backward learners and averaging their prediction outputs.

Changes:

  • Introduces BidirectionalTextLearner wrapper to train two learners and average predictions in get_preds / predict.
  • Extends DashTextLearner.create_text_learner with a bidirectional AWD_LSTM path that builds forward/backward LM+classifier learners.
  • Threads backwards through DashTextDatabunch.create_text_databunch and adds a unit test file using lightweight fastai/torch stubs.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 5 comments.

File Description
text/learner.py Adds bidirectional wrapper + creation path for AWD_LSTM classifiers, including averaging logic.
text/databunch.py Adds backwards plumbing into databunch creation to support reversed batches for backward learner.
tests/test_text_bidirectional_learner.py Adds unit tests (with stubs) validating averaging + dual-training wrapper behavior.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread text/learner.py
Comment on lines +19 to +20
def __getattr__(self, attr):
return getattr(self.forward_learner, attr)
Comment thread text/learner.py
Comment on lines +58 to +62
def get_preds(self, *args, **kwargs):
forward_preds = self.forward_learner.get_preds(*args, **kwargs)
backward_preds = self.backward_learner.get_preds(*args, **kwargs)
averaged_preds = (forward_preds[0] + backward_preds[0]) / 2
return (averaged_preds,) + forward_preds[1:]
Comment thread text/learner.py
Comment on lines +67 to +70
averaged_probs = (forward_prediction[2] + backward_prediction[2]) / 2
class_idx = averaged_probs.argmax()
class_name = self.data.classes[int(class_idx)]
return class_name, class_idx, averaged_probs
Comment thread text/learner.py
Comment on lines +53 to +56
def export(self, file='export.pkl', *args, **kwargs):
self.forward_learner.export('forward_{}'.format(file), *args, **kwargs)
self.backward_learner.export('backward_{}'.format(file), *args, **kwargs)
return self
Comment on lines +77 to +81
@classmethod
def setUpClass(cls):
install_fastai_stubs()
cls.learner_module = importlib.import_module('text.learner')

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.

For bidirectional rnns in text, two learner objects need to be created and then average of the preds has to be taken

2 participants