train, val, test = chainer.datasets.get_ptb_words()
train, val, test?def get_ptb_words():
"""Gets the Penn Tree Bank dataset as long word sequences.
[...]
Returns:
tuple of numpy.ndarray: Int32 vectors of word IDs.
"""
train = _retrieve_ptb_words('train.npz', _train_url)
valid = _retrieve_ptb_words('valid.npz', _valid_url)
test = _retrieve_ptb_words('test.npz', _test_url)
return train, valid, test
from typing import Dict
def load_vocab(vocabfile: str) -> Dict[int, str]:
[...]
# typed!
vocab = load_vocab(vocabfile)
If you can’t modify function declaration…
# function of other libraries
def load_vocab(vocabfile):
[...]
from typing import Dict
# typed!
vocab: Dict[int, str] = load_vocab(vocabfile)
from dataclasses import dataclass
from typing import Dict, Optional
@dataclass
class Vocabulary:
padding_token: str
oov_token: str
max_vocab_size: int
pretrained_files: Optional[Dict[str, str]] = None
class Vocabulary:
def __init__(
self,
padding_token: str,
oov_token: str,
max_vocab_size: int,
pretrained_files: Optional[Dict[str, str]] = None,
):
self.padding_token = padding_token
self.oov_token = oov_token
self.max_vocab_size = max_vocab_size
self.pretrained_files = pretrained_files
def __eq__(self, other):
if isinstance(self, other.__class__):
return self.__dict__ == other.__dict__
return False
pip installable?Other languages have great tools
sbtnpm, yarncargodep$ pip freeze > requirements.txt
$ pip install -r requirements.txt
setup.pypip$ poetry init
$ poetry add chainer # equivalent to `pip install chainer`
$ poetry build # make wheels
$ poetry publish # upload to PyPI
# pip installable after this command 🤗