Skip to content

test: add unit tests for RNN and MultimodalRNN models#936

Open
haoyu-haoyu wants to merge 1 commit intosunlabuiuc:masterfrom
haoyu-haoyu:test/rnn-unit-tests
Open

test: add unit tests for RNN and MultimodalRNN models#936
haoyu-haoyu wants to merge 1 commit intosunlabuiuc:masterfrom
haoyu-haoyu:test/rnn-unit-tests

Conversation

@haoyu-haoyu
Copy link
Copy Markdown
Contributor

Summary

Add tests/core/test_rnn.py with 12 test cases covering the RNN and MultimodalRNN models, which previously had no unit tests.

TestRNN (8 tests)

Test What it verifies
test_model_initialization Correct attributes (embedding_dim, hidden_dim, feature_keys, label_key)
test_model_forward Output dict contains loss, y_prob, y_true, logit with correct shapes
test_model_backward Gradients propagate to at least one parameter
test_model_with_embedding embed=True returns patient embeddings of expected dimension
test_custom_hyperparameters Non-default embedding_dim/hidden_dim work correctly
test_rnn_type_lstm LSTM cell type variant produces valid output
test_rnn_type_vanilla Vanilla RNN cell type variant produces valid output
test_bidirectional Bidirectional RNN with down-projection produces valid output

TestMultimodalRNN (4 tests)

Test What it verifies
test_model_initialization Sequential vs non-sequential feature classification is correct
test_model_forward Mixed modalities (sequence + multi_hot + tensor) forward pass works
test_model_backward Gradient propagation through mixed-modality architecture
test_model_with_embedding Embedding dimension = (n_seq × hidden_dim) + (n_nonseq × embedding_dim)

Pattern

Follows the established test pattern from test_mlp.py and test_tcn.py:

  • Synthetic data via create_sample_dataset()
  • Standard unittest.TestCase structure
  • Tests initialization, forward, backward, embedding extraction, and hyperparameter variants

Ref #425

Add test_rnn.py with 12 test cases covering:

TestRNN (8 tests):
- Model initialization with correct attributes
- Forward pass output structure and shapes
- Backward pass gradient propagation
- Embedding extraction via embed=True
- Custom hyperparameters (embedding_dim, hidden_dim)
- LSTM cell type variant
- Vanilla RNN cell type variant
- Bidirectional RNN variant

TestMultimodalRNN (4 tests):
- Initialization with correct sequential/non-sequential classification
- Forward pass with mixed modalities (sequence + multi_hot + tensor)
- Backward pass gradient propagation
- Embedding extraction with correct mixed-modality dimensions

Follows the established test pattern from test_mlp.py and test_tcn.py
using create_sample_dataset with synthetic data.

Ref sunlabuiuc#425
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