Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
sergeyk committed Mar 2, 2021
1 parent 16dea21 commit cb1a269
Show file tree
Hide file tree
Showing 2 changed files with 193 additions and 338 deletions.
16 changes: 15 additions & 1 deletion lab2/text_recognizer/data/util.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""Base Dataset class."""
from typing import Any, Callable, Sequence, Tuple, Union
from typing import Any, Callable, Dict, Sequence, Tuple, Union
import torch


Expand Down Expand Up @@ -63,3 +63,17 @@ def __getitem__(self, index: int) -> Tuple[Any, Any]:
target = self.target_transform(target)

return datum, target


def convert_strings_to_labels(strings: Sequence[str], mapping: Dict[str, int], length: int) -> torch.Tensor:
"""
Convert sequence of N strings to a (N, length) ndarray, with each string wrapped with <S> and <E> tokens,
and padded with the <P> token.
"""
labels = torch.ones((len(strings), length), dtype=torch.long) * mapping["<P>"]
for i, string in enumerate(strings):
tokens = list(string)
tokens = ["<S>", *tokens, "<E>"]
for ii, token in enumerate(tokens):
labels[i, ii] = mapping[token]
return labels
515 changes: 178 additions & 337 deletions lab5/notebooks/02-look-at-emnist-lines.ipynb

Large diffs are not rendered by default.

0 comments on commit cb1a269

Please sign in to comment.