textcode_encoder
CVE
Bases: Module
Continuous Value Encoder (CVE) module.
Assumes input is a single continuous value, and encodes it as an output_dim size embedding vector.
Source code in meds_torch/input_encoder/textcode_encoder.py
TextCodeEmbedder
Bases: Module, Module, TimeableMixin
Source code in meds_torch/input_encoder/textcode_encoder.py
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 | |
build_code_to_tokens_map()
Builds a mapping from code to tokens
Returns:
| Name | Type | Description |
|---|---|---|
code_to_tokens_map |
A dictionary mapping from code to tokens |
Source code in meds_torch/input_encoder/textcode_encoder.py
TextCodeEncoder
Bases: Module, Module, TimeableMixin
Container module with an encoder, a recurrent or transformer module, and a decoder.
Copied from: https://github.com/pytorch/examples/blob/main/word_language_model/model.py
Source code in meds_torch/input_encoder/textcode_encoder.py
fast_unique_with_inverse(x)
Efficiently computes unique elements and their inverse mapping for a 2D tensor.
The function returns a tuple containing: - unique: tensor of unique values in sorted order - inverse: tensor of same shape as input, where each element is replaced by its index in the unique tensor
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x |
Tensor
|
2D input tensor with values in range [0, 10] |
required |
Returns:
| Name | Type | Description |
|---|---|---|
tuple |
(unique values tensor, inverse mapping tensor) |
Example
x = torch.tensor([[0, 1, 0], … [2, 1, 0]], device=’cpu’) unique, inverse = fast_unique_with_inverse(x) print(unique) tensor([0, 1, 2]) print(inverse) tensor([[0, 1, 0], [2, 1, 0]])
Test with repeated values
x = torch.tensor([[5, 5, 5], … [3, 3, 5]], device=’cpu’) unique, inverse = fast_unique_with_inverse(x) print(unique) tensor([3, 5]) print(inverse) tensor([[1, 1, 1], [0, 0, 1]])
Test with all possible values
x = torch.tensor([[0, 10, 5], … [7, 3, 1]], device=’cpu’) unique, inverse = fast_unique_with_inverse(x) print(unique) tensor([ 0, 1, 3, 5, 7, 10]) print(inverse) tensor([[0, 5, 3], [4, 2, 1]])