Add bidirectional text RNN learner averaging#80
Open
jddark62 wants to merge 1 commit into
Open
Conversation
There was a problem hiding this comment.
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
BidirectionalTextLearnerwrapper to train two learners and average predictions inget_preds/predict. - Extends
DashTextLearner.create_text_learnerwith a bidirectional AWD_LSTM path that builds forward/backward LM+classifier learners. - Threads
backwardsthroughDashTextDatabunch.create_text_databunchand 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 on lines
+19
to
+20
| def __getattr__(self, attr): | ||
| return getattr(self.forward_learner, attr) |
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 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 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') | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #5
Summary
bidir: true.get_predsand averages probabilities inpredict.backwardsthrough 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.pypython3 -m unittest tests.test_text_bidirectional_learnergit diff --checkNote: I validated the wrapper behavior with fastai stubs because the full legacy fastai/torch stack is not installed in this local environment.