Combines a batch of namedtuples into a single namedtuple of the same type. Args: namedtuples: a list of namedtuples, all of the same type. Returns: a single namedtuple, where each value is the result of stacking all values from the original inputs along the the first dimension.
(namedtuples)
| 647 | |
| 648 | |
| 649 | def batch_namedtuples(namedtuples): |
| 650 | """Combines a batch of namedtuples into a single namedtuple of the same type. |
| 651 | |
| 652 | Args: |
| 653 | namedtuples: a list of namedtuples, all of the same type. |
| 654 | |
| 655 | Returns: |
| 656 | a single namedtuple, where each value is the result of stacking all values |
| 657 | from the original inputs along the the first dimension. |
| 658 | """ |
| 659 | if not namedtuples: |
| 660 | return namedtuples |
| 661 | |
| 662 | nt_type = type(namedtuples[0]) |
| 663 | dicts = [nt._asdict() for nt in namedtuples] |
| 664 | batched_dict = batch_feature_dicts(dicts) |
| 665 | return nt_type(**batched_dict) |
no test coverage detected
searching dependent graphs…