encoders ¶
This file contains encoders classes for encoding various types of data.
Classes:
-
AbstractEncoder
–Abstract class for encoders.
-
HuggingFaceEmbeddingEncoder
–HuggingFace embedding encoder using CLS token from last layer.
-
NumericEncoder
–Encoder for float/int data.
-
NumericRankEncoder
–Encoder for float/int data that encodes the data based on their rank.
-
StrClassificationEncoder
–A string classification encoder that converts lists of strings into numeric labels using scikit-learn.
-
TextAsciiEncoder
–Encoder for text data that encodes the data based on ASCII values.
-
TextOneHotEncoder
–One hot encoder for text data with highly optimized implementation.
AbstractEncoder ¶
Bases: ABC
Abstract class for encoders.
Encoders are classes that encode the raw data into torch.tensors. Different encoders provide different encoding methods. Different encoders may take different types of data as input.
Methods:
-
batch_encode
–encodes a list of data points into a numpy.ndarray
batch_encode abstractmethod
¶
batch_encode(data: ndarray) -> ndarray
Encode a batch of data points.
This is an abstract method, child classes should overwrite it.
Parameters:
-
data
(ndarray
) –a batch of data points
Returns:
-
encoded_data
(ndarray
) –encoded data points
Source code in src/stimulus/data/encoding/encoders.py
29 30 31 32 33 34 35 36 37 38 39 40 41 |
|
HuggingFaceEmbeddingEncoder ¶
HuggingFaceEmbeddingEncoder(
model_repo_name: str,
batch_size: int = 32,
dtype: Optional[dtype[floating]] = None,
layer_index: int = -1,
)
Bases: AbstractEncoder
HuggingFace embedding encoder using CLS token from last layer.
Parameters:
-
model_repo_name
(str
) –HuggingFace model repository name
-
batch_size
(int
, default:32
) –Batch size for processing
-
dtype
(Optional[dtype[floating]]
, default:None
) –Output data type
-
layer_index
(int
, default:-1
) –Which hidden layer to use (-1 for last, 0 for first, etc.)
Methods:
-
batch_encode
–Encode sequences to embeddings using CLS token.
Source code in src/stimulus/data/encoding/encoders.py
486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 |
|
batch_encode ¶
batch_encode(data: ndarray) -> ndarray
Encode sequences to embeddings using CLS token.
Source code in src/stimulus/data/encoding/encoders.py
515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 |
|
NumericEncoder ¶
NumericEncoder(dtype: Optional[dtype[number]] = None)
Bases: AbstractEncoder
Encoder for float/int data.
Attributes:
-
dtype
(dtype
) –The data type of the encoded data. Default = np.dtype(np.float32)
Parameters:
-
dtype
(dtype
, default:None
) –the data type of the encoded data. Default = np.dtype(np.float32)
Methods:
-
batch_encode
–Encodes the data.
Source code in src/stimulus/data/encoding/encoders.py
323 324 325 326 327 328 329 330 331 |
|
batch_encode ¶
batch_encode(data: ndarray) -> ndarray
Encodes the data.
This method takes as input a 1D numpy array of numbers and returns a numpy array.
Parameters:
-
data
(ndarray
) –a 1D numpy array of numbers
Returns:
-
encoded_data
(ndarray
) –the encoded data
Source code in src/stimulus/data/encoding/encoders.py
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 |
|
NumericRankEncoder ¶
Bases: AbstractEncoder
Encoder for float/int data that encodes the data based on their rank.
Attributes:
-
scale
(bool
) –whether to scale the ranks to be between 0 and 1. Default = False
-
dtype
(dtype
) –The data type of the encoded data. Default = np.dtype(np.int16)
Methods:
-
batch_encode
–encodes a list of data points into a numpy.ndarray
Parameters:
-
scale
(bool
, default:False
) –whether to scale the ranks to be between 0 and 1. Default = False
-
dtype
(dtype
, default:None
) –the data type of the encoded data. Default = np.dtype(np.int16)
Source code in src/stimulus/data/encoding/encoders.py
431 432 433 434 435 436 437 438 439 440 441 |
|
batch_encode ¶
batch_encode(data: ndarray) -> ndarray
Encodes the data.
This method takes as input a 1D numpy array of numbers, and returns the ranks of the data points. The ranks are normalized to be between 0 and 1, when scale is set to True.
Parameters:
-
data
(ndarray
) –a 1D numpy array of numeric values
Returns:
-
encoded_data
(ndarray
) –the encoded data
Source code in src/stimulus/data/encoding/encoders.py
443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 |
|
StrClassificationEncoder ¶
Bases: AbstractEncoder
A string classification encoder that converts lists of strings into numeric labels using scikit-learn.
When scale is set to True, the labels are scaled to be between 0 and 1.
Attributes:
-
scale
(bool
) –Whether to scale the labels to be between 0 and 1. Default = False
-
dtype
(dtype
) –The data type of the encoded data. Default = np.dtype(np.int16)
Methods:
-
batch_encode
–encodes a list of data points into a numpy.ndarray
Parameters:
-
scale
(bool
, default:False
) –whether to scale the labels to be between 0 and 1. Default = False
-
dtype
(dtype
, default:None
) –the data type of the encoded data. Default = np.dtype(np.int16)
Source code in src/stimulus/data/encoding/encoders.py
363 364 365 366 367 368 369 370 371 372 373 |
|
batch_encode ¶
batch_encode(data: ndarray) -> ndarray
Encodes the data.
This method takes as input a 1D numpy array of strings, should be mappable to a single output, using LabelEncoder from scikit learn and returning a numpy array. For more info visit : https://scikit-learn.org/stable/modules/generated/sklearn.preprocessing.LabelEncoder.html
Parameters:
-
data
(ndarray
) –a 1D numpy array of strings
Returns:
-
encoded_data
(ndarray
) –the encoded data
Source code in src/stimulus/data/encoding/encoders.py
375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 |
|
TextAsciiEncoder ¶
TextAsciiEncoder(
vocab_size: int = 256,
dtype: Optional[dtype[signedinteger]] = None,
*,
max_len: Optional[int] = None,
trim_strategy: Literal[
"raise", "trim", "slice", "drop"
] = "raise"
)
Bases: AbstractEncoder
Encoder for text data that encodes the data based on ASCII values.
Attributes:
-
vocab_size
(int
) –The size of the vocabulary. Default = 256 (ASCII characters)
-
dtype
(dtype
) –The data type of the encoded data. Default = np.dtype(np.int8)
-
max_len
(Optional[int]
) –the length to pad the sequences to. No padding is done if set to None. Default = None
-
trim_strategy
(Literal['raise', 'trim', 'slice', 'drop']
) –Behavior when a string is longer than max_len. Default = "raise"
Methods:
-
batch_encode
–encodes a list of data points into a numpy.ndarray
Parameters:
-
vocab_size
(int
, default:256
) –the size of the vocabulary. Default = 256 (ASCII characters)
-
dtype
(dtype
, default:None
) –the data type of the encoded data. Default = np.dtype(np.int8)
-
max_len
(Optional[int]
, default:None
) –the length to pad the sequences to. No padding is done if set to None. Default = None
-
trim_strategy
(Literal['raise', 'trim', 'slice', 'drop']
, default:'raise'
) –Behavior when a string is longer than max_len. Default = "raise"
Raises:
-
ValueError
–If an invalid trim strategy is provided.
Source code in src/stimulus/data/encoding/encoders.py
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 |
|
batch_encode ¶
batch_encode(data: ndarray) -> ndarray
Encodes the data.
This method takes as input a 1D numpy array of strings and returns a numpy array.
Parameters:
-
data
(ndarray
) –a 1D numpy array of strings
Returns:
-
encoded_data
(ndarray
) –the encoded data
Raises:
-
TypeError
–If the input data is not a 1D numpy array of strings.
-
ValueError
–If any string in data contains characters with ASCII values greater than vocab_size - 1
Source code in src/stimulus/data/encoding/encoders.py
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 |
|
TextOneHotEncoder ¶
TextOneHotEncoder(
alphabet: str = "acgt",
dtype: Optional[dtype[floating]] = None,
*,
convert_lowercase: bool = False,
force_cpu: bool = True,
padding: bool = False
)
Bases: AbstractEncoder
One hot encoder for text data with highly optimized implementation.
If a character c is not in the alphabet, c will be represented by a vector of zeros. This encoder is optimized for processing large batches of sequences efficiently on GPU.
Parameters:
-
alphabet
(str
, default:'acgt'
) –the alphabet to one hot encode the data with.
-
dtype
(dtype
, default:None
) –the data type of the encoded data. Default = np.dtype(np.float32)
-
convert_lowercase
(bool
, default:False
) –whether to convert sequences to lowercase.
-
force_cpu
(bool
, default:True
) –whether to force the encoder to run on CPU.
-
padding
(bool
, default:False
) –whether to pad sequences of different lengths.
Methods:
-
batch_encode
–Encode all sequences in a batch using fully vectorized operations.
Source code in src/stimulus/data/encoding/encoders.py
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
|
batch_encode ¶
batch_encode(data: ndarray) -> ndarray
Encode all sequences in a batch using fully vectorized operations.
Parameters:
-
data
(ndarray
) –A 1D numpy array of strings (sequences).
Returns:
-
ndarray
–np.ndarray: Array of shape (batch_size, max_seq_length, alphabet_size)
Source code in src/stimulus/data/encoding/encoders.py
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 189 190 191 192 193 194 195 196 197 198 |
|