Sample represent some arbitary data. All datasets in Pythia must return an object of type ``Sample``. Args: init_dict (Dict): Dictionary to init ``Sample`` class with. Usage:: >>> sample = Sample({"text": torch.tensor(2)}) >>> sample.text.zero_() # Cust
| 19 | |
| 20 | |
| 21 | class Sample(OrderedDict): |
| 22 | """Sample represent some arbitary data. All datasets in Pythia must |
| 23 | return an object of type ``Sample``. |
| 24 | |
| 25 | Args: |
| 26 | init_dict (Dict): Dictionary to init ``Sample`` class with. |
| 27 | |
| 28 | Usage:: |
| 29 | |
| 30 | >>> sample = Sample({"text": torch.tensor(2)}) |
| 31 | >>> sample.text.zero_() |
| 32 | # Custom attributes can be added to ``Sample`` after initialization |
| 33 | >>> sample.context = torch.tensor(4) |
| 34 | """ |
| 35 | |
| 36 | def __init__(self, init_dict={}): |
| 37 | super().__init__(init_dict) |
| 38 | |
| 39 | def __setattr__(self, key, value): |
| 40 | self[key] = value |
| 41 | |
| 42 | def __getattr__(self, key): |
| 43 | try: |
| 44 | return self[key] |
| 45 | except KeyError: |
| 46 | raise AttributeError(key) |
| 47 | |
| 48 | def fields(self): |
| 49 | """Get current attributes/fields registered under the sample. |
| 50 | |
| 51 | Returns: |
| 52 | List[str]: Attributes registered under the Sample. |
| 53 | |
| 54 | """ |
| 55 | return list(self.keys()) |
| 56 | |
| 57 | |
| 58 | class SampleList(OrderedDict): |
no outgoing calls