repo
stringlengths
7
55
path
stringlengths
4
223
func_name
stringlengths
1
134
original_string
stringlengths
75
104k
language
stringclasses
1 value
code
stringlengths
75
104k
code_tokens
listlengths
19
28.4k
docstring
stringlengths
1
46.9k
docstring_tokens
listlengths
1
1.97k
sha
stringlengths
40
40
url
stringlengths
87
315
partition
stringclasses
1 value
apache/incubator-mxnet
example/ssd/dataset/concat_db.py
ConcatDB._check_classes
def _check_classes(self): """ check input imdbs, make sure they have same classes """ try: self.classes = self.imdbs[0].classes self.num_classes = len(self.classes) except AttributeError: # fine, if no classes is provided pass if self.num_classes > 0: for db in self.imdbs: assert self.classes == db.classes, "Multiple imdb must have same classes"
python
def _check_classes(self): """ check input imdbs, make sure they have same classes """ try: self.classes = self.imdbs[0].classes self.num_classes = len(self.classes) except AttributeError: # fine, if no classes is provided pass if self.num_classes > 0: for db in self.imdbs: assert self.classes == db.classes, "Multiple imdb must have same classes"
[ "def", "_check_classes", "(", "self", ")", ":", "try", ":", "self", ".", "classes", "=", "self", ".", "imdbs", "[", "0", "]", ".", "classes", "self", ".", "num_classes", "=", "len", "(", "self", ".", "classes", ")", "except", "AttributeError", ":", "# fine, if no classes is provided", "pass", "if", "self", ".", "num_classes", ">", "0", ":", "for", "db", "in", "self", ".", "imdbs", ":", "assert", "self", ".", "classes", "==", "db", ".", "classes", ",", "\"Multiple imdb must have same classes\"" ]
check input imdbs, make sure they have same classes
[ "check", "input", "imdbs", "make", "sure", "they", "have", "same", "classes" ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/example/ssd/dataset/concat_db.py#L40-L53
train
apache/incubator-mxnet
example/ssd/dataset/concat_db.py
ConcatDB._load_image_set_index
def _load_image_set_index(self, shuffle): """ get total number of images, init indices Parameters ---------- shuffle : bool whether to shuffle the initial indices """ self.num_images = 0 for db in self.imdbs: self.num_images += db.num_images indices = list(range(self.num_images)) if shuffle: random.shuffle(indices) return indices
python
def _load_image_set_index(self, shuffle): """ get total number of images, init indices Parameters ---------- shuffle : bool whether to shuffle the initial indices """ self.num_images = 0 for db in self.imdbs: self.num_images += db.num_images indices = list(range(self.num_images)) if shuffle: random.shuffle(indices) return indices
[ "def", "_load_image_set_index", "(", "self", ",", "shuffle", ")", ":", "self", ".", "num_images", "=", "0", "for", "db", "in", "self", ".", "imdbs", ":", "self", ".", "num_images", "+=", "db", ".", "num_images", "indices", "=", "list", "(", "range", "(", "self", ".", "num_images", ")", ")", "if", "shuffle", ":", "random", ".", "shuffle", "(", "indices", ")", "return", "indices" ]
get total number of images, init indices Parameters ---------- shuffle : bool whether to shuffle the initial indices
[ "get", "total", "number", "of", "images", "init", "indices" ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/example/ssd/dataset/concat_db.py#L55-L70
train
apache/incubator-mxnet
example/ssd/dataset/concat_db.py
ConcatDB._locate_index
def _locate_index(self, index): """ given index, find out sub-db and sub-index Parameters ---------- index : int index of a specific image Returns ---------- a tuple (sub-db, sub-index) """ assert index >= 0 and index < self.num_images, "index out of range" pos = self.image_set_index[index] for k, v in enumerate(self.imdbs): if pos >= v.num_images: pos -= v.num_images else: return (k, pos)
python
def _locate_index(self, index): """ given index, find out sub-db and sub-index Parameters ---------- index : int index of a specific image Returns ---------- a tuple (sub-db, sub-index) """ assert index >= 0 and index < self.num_images, "index out of range" pos = self.image_set_index[index] for k, v in enumerate(self.imdbs): if pos >= v.num_images: pos -= v.num_images else: return (k, pos)
[ "def", "_locate_index", "(", "self", ",", "index", ")", ":", "assert", "index", ">=", "0", "and", "index", "<", "self", ".", "num_images", ",", "\"index out of range\"", "pos", "=", "self", ".", "image_set_index", "[", "index", "]", "for", "k", ",", "v", "in", "enumerate", "(", "self", ".", "imdbs", ")", ":", "if", "pos", ">=", "v", ".", "num_images", ":", "pos", "-=", "v", ".", "num_images", "else", ":", "return", "(", "k", ",", "pos", ")" ]
given index, find out sub-db and sub-index Parameters ---------- index : int index of a specific image Returns ---------- a tuple (sub-db, sub-index)
[ "given", "index", "find", "out", "sub", "-", "db", "and", "sub", "-", "index" ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/example/ssd/dataset/concat_db.py#L72-L91
train
apache/incubator-mxnet
example/ssd/dataset/concat_db.py
ConcatDB.image_path_from_index
def image_path_from_index(self, index): """ given image index, find out full path Parameters ---------- index: int index of a specific image Returns ---------- full path of this image """ assert self.image_set_index is not None, "Dataset not initialized" pos = self.image_set_index[index] n_db, n_index = self._locate_index(index) return self.imdbs[n_db].image_path_from_index(n_index)
python
def image_path_from_index(self, index): """ given image index, find out full path Parameters ---------- index: int index of a specific image Returns ---------- full path of this image """ assert self.image_set_index is not None, "Dataset not initialized" pos = self.image_set_index[index] n_db, n_index = self._locate_index(index) return self.imdbs[n_db].image_path_from_index(n_index)
[ "def", "image_path_from_index", "(", "self", ",", "index", ")", ":", "assert", "self", ".", "image_set_index", "is", "not", "None", ",", "\"Dataset not initialized\"", "pos", "=", "self", ".", "image_set_index", "[", "index", "]", "n_db", ",", "n_index", "=", "self", ".", "_locate_index", "(", "index", ")", "return", "self", ".", "imdbs", "[", "n_db", "]", ".", "image_path_from_index", "(", "n_index", ")" ]
given image index, find out full path Parameters ---------- index: int index of a specific image Returns ---------- full path of this image
[ "given", "image", "index", "find", "out", "full", "path" ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/example/ssd/dataset/concat_db.py#L93-L109
train
apache/incubator-mxnet
python/mxnet/callback.py
module_checkpoint
def module_checkpoint(mod, prefix, period=1, save_optimizer_states=False): """Callback to checkpoint Module to prefix every epoch. Parameters ---------- mod : subclass of BaseModule The module to checkpoint. prefix : str The file prefix for this checkpoint. period : int How many epochs to wait before checkpointing. Defaults to 1. save_optimizer_states : bool Indicates whether or not to save optimizer states for continued training. Returns ------- callback : function The callback function that can be passed as iter_end_callback to fit. """ period = int(max(1, period)) # pylint: disable=unused-argument def _callback(iter_no, sym=None, arg=None, aux=None): """The checkpoint function.""" if (iter_no + 1) % period == 0: mod.save_checkpoint(prefix, iter_no + 1, save_optimizer_states) return _callback
python
def module_checkpoint(mod, prefix, period=1, save_optimizer_states=False): """Callback to checkpoint Module to prefix every epoch. Parameters ---------- mod : subclass of BaseModule The module to checkpoint. prefix : str The file prefix for this checkpoint. period : int How many epochs to wait before checkpointing. Defaults to 1. save_optimizer_states : bool Indicates whether or not to save optimizer states for continued training. Returns ------- callback : function The callback function that can be passed as iter_end_callback to fit. """ period = int(max(1, period)) # pylint: disable=unused-argument def _callback(iter_no, sym=None, arg=None, aux=None): """The checkpoint function.""" if (iter_no + 1) % period == 0: mod.save_checkpoint(prefix, iter_no + 1, save_optimizer_states) return _callback
[ "def", "module_checkpoint", "(", "mod", ",", "prefix", ",", "period", "=", "1", ",", "save_optimizer_states", "=", "False", ")", ":", "period", "=", "int", "(", "max", "(", "1", ",", "period", ")", ")", "# pylint: disable=unused-argument", "def", "_callback", "(", "iter_no", ",", "sym", "=", "None", ",", "arg", "=", "None", ",", "aux", "=", "None", ")", ":", "\"\"\"The checkpoint function.\"\"\"", "if", "(", "iter_no", "+", "1", ")", "%", "period", "==", "0", ":", "mod", ".", "save_checkpoint", "(", "prefix", ",", "iter_no", "+", "1", ",", "save_optimizer_states", ")", "return", "_callback" ]
Callback to checkpoint Module to prefix every epoch. Parameters ---------- mod : subclass of BaseModule The module to checkpoint. prefix : str The file prefix for this checkpoint. period : int How many epochs to wait before checkpointing. Defaults to 1. save_optimizer_states : bool Indicates whether or not to save optimizer states for continued training. Returns ------- callback : function The callback function that can be passed as iter_end_callback to fit.
[ "Callback", "to", "checkpoint", "Module", "to", "prefix", "every", "epoch", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/callback.py#L27-L52
train
apache/incubator-mxnet
python/mxnet/callback.py
do_checkpoint
def do_checkpoint(prefix, period=1): """A callback that saves a model checkpoint every few epochs. Each checkpoint is made up of a couple of binary files: a model description file and a parameters (weights and biases) file. The model description file is named `prefix`--symbol.json and the parameters file is named `prefix`-`epoch_number`.params Parameters ---------- prefix : str Prefix for the checkpoint filenames. period : int, optional Interval (number of epochs) between checkpoints. Default `period` is 1. Returns ------- callback : function A callback function that can be passed as `epoch_end_callback` to fit. Example ------- >>> module.fit(iterator, num_epoch=n_epoch, ... epoch_end_callback = mx.callback.do_checkpoint("mymodel", 1)) Start training with [cpu(0)] Epoch[0] Resetting Data Iterator Epoch[0] Time cost=0.100 Saved checkpoint to "mymodel-0001.params" Epoch[1] Resetting Data Iterator Epoch[1] Time cost=0.060 Saved checkpoint to "mymodel-0002.params" """ period = int(max(1, period)) def _callback(iter_no, sym, arg, aux): """The checkpoint function.""" if (iter_no + 1) % period == 0: save_checkpoint(prefix, iter_no + 1, sym, arg, aux) return _callback
python
def do_checkpoint(prefix, period=1): """A callback that saves a model checkpoint every few epochs. Each checkpoint is made up of a couple of binary files: a model description file and a parameters (weights and biases) file. The model description file is named `prefix`--symbol.json and the parameters file is named `prefix`-`epoch_number`.params Parameters ---------- prefix : str Prefix for the checkpoint filenames. period : int, optional Interval (number of epochs) between checkpoints. Default `period` is 1. Returns ------- callback : function A callback function that can be passed as `epoch_end_callback` to fit. Example ------- >>> module.fit(iterator, num_epoch=n_epoch, ... epoch_end_callback = mx.callback.do_checkpoint("mymodel", 1)) Start training with [cpu(0)] Epoch[0] Resetting Data Iterator Epoch[0] Time cost=0.100 Saved checkpoint to "mymodel-0001.params" Epoch[1] Resetting Data Iterator Epoch[1] Time cost=0.060 Saved checkpoint to "mymodel-0002.params" """ period = int(max(1, period)) def _callback(iter_no, sym, arg, aux): """The checkpoint function.""" if (iter_no + 1) % period == 0: save_checkpoint(prefix, iter_no + 1, sym, arg, aux) return _callback
[ "def", "do_checkpoint", "(", "prefix", ",", "period", "=", "1", ")", ":", "period", "=", "int", "(", "max", "(", "1", ",", "period", ")", ")", "def", "_callback", "(", "iter_no", ",", "sym", ",", "arg", ",", "aux", ")", ":", "\"\"\"The checkpoint function.\"\"\"", "if", "(", "iter_no", "+", "1", ")", "%", "period", "==", "0", ":", "save_checkpoint", "(", "prefix", ",", "iter_no", "+", "1", ",", "sym", ",", "arg", ",", "aux", ")", "return", "_callback" ]
A callback that saves a model checkpoint every few epochs. Each checkpoint is made up of a couple of binary files: a model description file and a parameters (weights and biases) file. The model description file is named `prefix`--symbol.json and the parameters file is named `prefix`-`epoch_number`.params Parameters ---------- prefix : str Prefix for the checkpoint filenames. period : int, optional Interval (number of epochs) between checkpoints. Default `period` is 1. Returns ------- callback : function A callback function that can be passed as `epoch_end_callback` to fit. Example ------- >>> module.fit(iterator, num_epoch=n_epoch, ... epoch_end_callback = mx.callback.do_checkpoint("mymodel", 1)) Start training with [cpu(0)] Epoch[0] Resetting Data Iterator Epoch[0] Time cost=0.100 Saved checkpoint to "mymodel-0001.params" Epoch[1] Resetting Data Iterator Epoch[1] Time cost=0.060 Saved checkpoint to "mymodel-0002.params"
[ "A", "callback", "that", "saves", "a", "model", "checkpoint", "every", "few", "epochs", ".", "Each", "checkpoint", "is", "made", "up", "of", "a", "couple", "of", "binary", "files", ":", "a", "model", "description", "file", "and", "a", "parameters", "(", "weights", "and", "biases", ")", "file", ".", "The", "model", "description", "file", "is", "named", "prefix", "--", "symbol", ".", "json", "and", "the", "parameters", "file", "is", "named", "prefix", "-", "epoch_number", ".", "params" ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/callback.py#L55-L90
train
apache/incubator-mxnet
python/mxnet/callback.py
log_train_metric
def log_train_metric(period, auto_reset=False): """Callback to log the training evaluation result every period. Parameters ---------- period : int The number of batch to log the training evaluation metric. auto_reset : bool Reset the metric after each log. Returns ------- callback : function The callback function that can be passed as iter_epoch_callback to fit. """ def _callback(param): """The checkpoint function.""" if param.nbatch % period == 0 and param.eval_metric is not None: name_value = param.eval_metric.get_name_value() for name, value in name_value: logging.info('Iter[%d] Batch[%d] Train-%s=%f', param.epoch, param.nbatch, name, value) if auto_reset: param.eval_metric.reset_local() return _callback
python
def log_train_metric(period, auto_reset=False): """Callback to log the training evaluation result every period. Parameters ---------- period : int The number of batch to log the training evaluation metric. auto_reset : bool Reset the metric after each log. Returns ------- callback : function The callback function that can be passed as iter_epoch_callback to fit. """ def _callback(param): """The checkpoint function.""" if param.nbatch % period == 0 and param.eval_metric is not None: name_value = param.eval_metric.get_name_value() for name, value in name_value: logging.info('Iter[%d] Batch[%d] Train-%s=%f', param.epoch, param.nbatch, name, value) if auto_reset: param.eval_metric.reset_local() return _callback
[ "def", "log_train_metric", "(", "period", ",", "auto_reset", "=", "False", ")", ":", "def", "_callback", "(", "param", ")", ":", "\"\"\"The checkpoint function.\"\"\"", "if", "param", ".", "nbatch", "%", "period", "==", "0", "and", "param", ".", "eval_metric", "is", "not", "None", ":", "name_value", "=", "param", ".", "eval_metric", ".", "get_name_value", "(", ")", "for", "name", ",", "value", "in", "name_value", ":", "logging", ".", "info", "(", "'Iter[%d] Batch[%d] Train-%s=%f'", ",", "param", ".", "epoch", ",", "param", ".", "nbatch", ",", "name", ",", "value", ")", "if", "auto_reset", ":", "param", ".", "eval_metric", ".", "reset_local", "(", ")", "return", "_callback" ]
Callback to log the training evaluation result every period. Parameters ---------- period : int The number of batch to log the training evaluation metric. auto_reset : bool Reset the metric after each log. Returns ------- callback : function The callback function that can be passed as iter_epoch_callback to fit.
[ "Callback", "to", "log", "the", "training", "evaluation", "result", "every", "period", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/callback.py#L93-L117
train
apache/incubator-mxnet
python/mxnet/monitor.py
Monitor.install
def install(self, exe): """install callback to executor. Supports installing to multiple exes. Parameters ---------- exe : mx.executor.Executor The Executor (returned by symbol.bind) to install to. """ exe.set_monitor_callback(self.stat_helper, self.monitor_all) self.exes.append(exe)
python
def install(self, exe): """install callback to executor. Supports installing to multiple exes. Parameters ---------- exe : mx.executor.Executor The Executor (returned by symbol.bind) to install to. """ exe.set_monitor_callback(self.stat_helper, self.monitor_all) self.exes.append(exe)
[ "def", "install", "(", "self", ",", "exe", ")", ":", "exe", ".", "set_monitor_callback", "(", "self", ".", "stat_helper", ",", "self", ".", "monitor_all", ")", "self", ".", "exes", ".", "append", "(", "exe", ")" ]
install callback to executor. Supports installing to multiple exes. Parameters ---------- exe : mx.executor.Executor The Executor (returned by symbol.bind) to install to.
[ "install", "callback", "to", "executor", ".", "Supports", "installing", "to", "multiple", "exes", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/monitor.py#L76-L86
train
apache/incubator-mxnet
python/mxnet/monitor.py
Monitor.tic
def tic(self): """Start collecting stats for current batch. Call before calling forward.""" if self.step % self.interval == 0: for exe in self.exes: for array in exe.arg_arrays: array.wait_to_read() for array in exe.aux_arrays: array.wait_to_read() self.queue = [] self.activated = True self.step += 1
python
def tic(self): """Start collecting stats for current batch. Call before calling forward.""" if self.step % self.interval == 0: for exe in self.exes: for array in exe.arg_arrays: array.wait_to_read() for array in exe.aux_arrays: array.wait_to_read() self.queue = [] self.activated = True self.step += 1
[ "def", "tic", "(", "self", ")", ":", "if", "self", ".", "step", "%", "self", ".", "interval", "==", "0", ":", "for", "exe", "in", "self", ".", "exes", ":", "for", "array", "in", "exe", ".", "arg_arrays", ":", "array", ".", "wait_to_read", "(", ")", "for", "array", "in", "exe", ".", "aux_arrays", ":", "array", ".", "wait_to_read", "(", ")", "self", ".", "queue", "=", "[", "]", "self", ".", "activated", "=", "True", "self", ".", "step", "+=", "1" ]
Start collecting stats for current batch. Call before calling forward.
[ "Start", "collecting", "stats", "for", "current", "batch", ".", "Call", "before", "calling", "forward", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/monitor.py#L88-L99
train
apache/incubator-mxnet
python/mxnet/monitor.py
Monitor.toc
def toc(self): """End collecting for current batch and return results. Call after computation of current batch. Returns ------- res : list of """ if not self.activated: return [] for exe in self.exes: for array in exe.arg_arrays: array.wait_to_read() for array in exe.aux_arrays: array.wait_to_read() for exe in self.exes: for name, array in zip(exe._symbol.list_arguments(), exe.arg_arrays): if self.re_prog.match(name): self.queue.append((self.step, name, self.stat_func(array))) for name, array in zip(exe._symbol.list_auxiliary_states(), exe.aux_arrays): if self.re_prog.match(name): self.queue.append((self.step, name, self.stat_func(array))) self.activated = False res = [] if self.sort: self.queue.sort(key=lambda x: x[1]) for n, k, v_list in self.queue: if isinstance(v_list, NDArray): v_list = [v_list] assert isinstance(v_list, list) s = '' for v in v_list: assert isinstance(v, NDArray) if v.shape == (1,): s += str(v.asscalar()) + '\t' else: s += str(v.asnumpy()) + '\t' res.append((n, k, s)) self.queue = [] return res
python
def toc(self): """End collecting for current batch and return results. Call after computation of current batch. Returns ------- res : list of """ if not self.activated: return [] for exe in self.exes: for array in exe.arg_arrays: array.wait_to_read() for array in exe.aux_arrays: array.wait_to_read() for exe in self.exes: for name, array in zip(exe._symbol.list_arguments(), exe.arg_arrays): if self.re_prog.match(name): self.queue.append((self.step, name, self.stat_func(array))) for name, array in zip(exe._symbol.list_auxiliary_states(), exe.aux_arrays): if self.re_prog.match(name): self.queue.append((self.step, name, self.stat_func(array))) self.activated = False res = [] if self.sort: self.queue.sort(key=lambda x: x[1]) for n, k, v_list in self.queue: if isinstance(v_list, NDArray): v_list = [v_list] assert isinstance(v_list, list) s = '' for v in v_list: assert isinstance(v, NDArray) if v.shape == (1,): s += str(v.asscalar()) + '\t' else: s += str(v.asnumpy()) + '\t' res.append((n, k, s)) self.queue = [] return res
[ "def", "toc", "(", "self", ")", ":", "if", "not", "self", ".", "activated", ":", "return", "[", "]", "for", "exe", "in", "self", ".", "exes", ":", "for", "array", "in", "exe", ".", "arg_arrays", ":", "array", ".", "wait_to_read", "(", ")", "for", "array", "in", "exe", ".", "aux_arrays", ":", "array", ".", "wait_to_read", "(", ")", "for", "exe", "in", "self", ".", "exes", ":", "for", "name", ",", "array", "in", "zip", "(", "exe", ".", "_symbol", ".", "list_arguments", "(", ")", ",", "exe", ".", "arg_arrays", ")", ":", "if", "self", ".", "re_prog", ".", "match", "(", "name", ")", ":", "self", ".", "queue", ".", "append", "(", "(", "self", ".", "step", ",", "name", ",", "self", ".", "stat_func", "(", "array", ")", ")", ")", "for", "name", ",", "array", "in", "zip", "(", "exe", ".", "_symbol", ".", "list_auxiliary_states", "(", ")", ",", "exe", ".", "aux_arrays", ")", ":", "if", "self", ".", "re_prog", ".", "match", "(", "name", ")", ":", "self", ".", "queue", ".", "append", "(", "(", "self", ".", "step", ",", "name", ",", "self", ".", "stat_func", "(", "array", ")", ")", ")", "self", ".", "activated", "=", "False", "res", "=", "[", "]", "if", "self", ".", "sort", ":", "self", ".", "queue", ".", "sort", "(", "key", "=", "lambda", "x", ":", "x", "[", "1", "]", ")", "for", "n", ",", "k", ",", "v_list", "in", "self", ".", "queue", ":", "if", "isinstance", "(", "v_list", ",", "NDArray", ")", ":", "v_list", "=", "[", "v_list", "]", "assert", "isinstance", "(", "v_list", ",", "list", ")", "s", "=", "''", "for", "v", "in", "v_list", ":", "assert", "isinstance", "(", "v", ",", "NDArray", ")", "if", "v", ".", "shape", "==", "(", "1", ",", ")", ":", "s", "+=", "str", "(", "v", ".", "asscalar", "(", ")", ")", "+", "'\\t'", "else", ":", "s", "+=", "str", "(", "v", ".", "asnumpy", "(", ")", ")", "+", "'\\t'", "res", ".", "append", "(", "(", "n", ",", "k", ",", "s", ")", ")", "self", ".", "queue", "=", "[", "]", "return", "res" ]
End collecting for current batch and return results. Call after computation of current batch. Returns ------- res : list of
[ "End", "collecting", "for", "current", "batch", "and", "return", "results", ".", "Call", "after", "computation", "of", "current", "batch", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/monitor.py#L102-L140
train
apache/incubator-mxnet
python/mxnet/monitor.py
Monitor.toc_print
def toc_print(self): """End collecting and print results.""" res = self.toc() for n, k, v in res: logging.info('Batch: {:7d} {:30s} {:s}'.format(n, k, v))
python
def toc_print(self): """End collecting and print results.""" res = self.toc() for n, k, v in res: logging.info('Batch: {:7d} {:30s} {:s}'.format(n, k, v))
[ "def", "toc_print", "(", "self", ")", ":", "res", "=", "self", ".", "toc", "(", ")", "for", "n", ",", "k", ",", "v", "in", "res", ":", "logging", ".", "info", "(", "'Batch: {:7d} {:30s} {:s}'", ".", "format", "(", "n", ",", "k", ",", "v", ")", ")" ]
End collecting and print results.
[ "End", "collecting", "and", "print", "results", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/monitor.py#L142-L146
train
apache/incubator-mxnet
example/rnn/old/bucket_io.py
BucketSentenceIter.make_data_iter_plan
def make_data_iter_plan(self): "make a random data iteration plan" # truncate each bucket into multiple of batch-size bucket_n_batches = [] for i in range(len(self.data)): bucket_n_batches.append(np.floor((self.data[i]) / self.batch_size)) self.data[i] = self.data[i][:int(bucket_n_batches[i]*self.batch_size)] bucket_plan = np.hstack([np.zeros(n, int)+i for i, n in enumerate(bucket_n_batches)]) np.random.shuffle(bucket_plan) bucket_idx_all = [np.random.permutation(len(x)) for x in self.data] self.bucket_plan = bucket_plan self.bucket_idx_all = bucket_idx_all self.bucket_curr_idx = [0 for x in self.data] self.data_buffer = [] self.label_buffer = [] for i_bucket in range(len(self.data)): if not self.model_parallel: data = np.zeros((self.batch_size, self.buckets[i_bucket])) label = np.zeros((self.batch_size, self.buckets[i_bucket])) self.data_buffer.append(data) self.label_buffer.append(label) else: data = np.zeros((self.buckets[i_bucket], self.batch_size)) self.data_buffer.append(data) if self.model_parallel: # Transpose data if model parallel for i in range(len(self.data)): bucket_data = self.data[i] self.data[i] = np.transpose(bucket_data)
python
def make_data_iter_plan(self): "make a random data iteration plan" # truncate each bucket into multiple of batch-size bucket_n_batches = [] for i in range(len(self.data)): bucket_n_batches.append(np.floor((self.data[i]) / self.batch_size)) self.data[i] = self.data[i][:int(bucket_n_batches[i]*self.batch_size)] bucket_plan = np.hstack([np.zeros(n, int)+i for i, n in enumerate(bucket_n_batches)]) np.random.shuffle(bucket_plan) bucket_idx_all = [np.random.permutation(len(x)) for x in self.data] self.bucket_plan = bucket_plan self.bucket_idx_all = bucket_idx_all self.bucket_curr_idx = [0 for x in self.data] self.data_buffer = [] self.label_buffer = [] for i_bucket in range(len(self.data)): if not self.model_parallel: data = np.zeros((self.batch_size, self.buckets[i_bucket])) label = np.zeros((self.batch_size, self.buckets[i_bucket])) self.data_buffer.append(data) self.label_buffer.append(label) else: data = np.zeros((self.buckets[i_bucket], self.batch_size)) self.data_buffer.append(data) if self.model_parallel: # Transpose data if model parallel for i in range(len(self.data)): bucket_data = self.data[i] self.data[i] = np.transpose(bucket_data)
[ "def", "make_data_iter_plan", "(", "self", ")", ":", "# truncate each bucket into multiple of batch-size", "bucket_n_batches", "=", "[", "]", "for", "i", "in", "range", "(", "len", "(", "self", ".", "data", ")", ")", ":", "bucket_n_batches", ".", "append", "(", "np", ".", "floor", "(", "(", "self", ".", "data", "[", "i", "]", ")", "/", "self", ".", "batch_size", ")", ")", "self", ".", "data", "[", "i", "]", "=", "self", ".", "data", "[", "i", "]", "[", ":", "int", "(", "bucket_n_batches", "[", "i", "]", "*", "self", ".", "batch_size", ")", "]", "bucket_plan", "=", "np", ".", "hstack", "(", "[", "np", ".", "zeros", "(", "n", ",", "int", ")", "+", "i", "for", "i", ",", "n", "in", "enumerate", "(", "bucket_n_batches", ")", "]", ")", "np", ".", "random", ".", "shuffle", "(", "bucket_plan", ")", "bucket_idx_all", "=", "[", "np", ".", "random", ".", "permutation", "(", "len", "(", "x", ")", ")", "for", "x", "in", "self", ".", "data", "]", "self", ".", "bucket_plan", "=", "bucket_plan", "self", ".", "bucket_idx_all", "=", "bucket_idx_all", "self", ".", "bucket_curr_idx", "=", "[", "0", "for", "x", "in", "self", ".", "data", "]", "self", ".", "data_buffer", "=", "[", "]", "self", ".", "label_buffer", "=", "[", "]", "for", "i_bucket", "in", "range", "(", "len", "(", "self", ".", "data", ")", ")", ":", "if", "not", "self", ".", "model_parallel", ":", "data", "=", "np", ".", "zeros", "(", "(", "self", ".", "batch_size", ",", "self", ".", "buckets", "[", "i_bucket", "]", ")", ")", "label", "=", "np", ".", "zeros", "(", "(", "self", ".", "batch_size", ",", "self", ".", "buckets", "[", "i_bucket", "]", ")", ")", "self", ".", "data_buffer", ".", "append", "(", "data", ")", "self", ".", "label_buffer", ".", "append", "(", "label", ")", "else", ":", "data", "=", "np", ".", "zeros", "(", "(", "self", ".", "buckets", "[", "i_bucket", "]", ",", "self", ".", "batch_size", ")", ")", "self", ".", "data_buffer", ".", "append", "(", "data", ")", "if", "self", ".", "model_parallel", ":", "# Transpose data if model parallel", "for", "i", "in", "range", "(", "len", "(", "self", ".", "data", ")", ")", ":", "bucket_data", "=", "self", ".", "data", "[", "i", "]", "self", ".", "data", "[", "i", "]", "=", "np", ".", "transpose", "(", "bucket_data", ")" ]
make a random data iteration plan
[ "make", "a", "random", "data", "iteration", "plan" ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/example/rnn/old/bucket_io.py#L200-L233
train
apache/incubator-mxnet
amalgamation/amalgamation.py
expand
def expand(x, pending, stage): """ Expand the pending files in the current stage. Parameters ---------- x: str The file to expand. pending : str The list of pending files to expand. stage: str The current stage for file expansion, used for matching the prefix of files. """ if x in history and x not in ['mshadow/mshadow/expr_scalar-inl.h']: # MULTIPLE includes return if x in pending: #print('loop found: {} in {}'.format(x, pending)) return whtspace = ' ' * expand.treeDepth expand.fileCount += 1 comment = u"//=====[{:3d}] STAGE:{:>4} {}EXPANDING: {} =====\n\n".format(expand.fileCount, stage, whtspace, x) out.write(comment.encode('ascii')) print(comment) with open(x, 'rb') as x_h: for line in x_h.readlines(): uline = line.decode('utf-8') if '#define DMLC_LOG_STACK_TRACE 1' in uline.strip(): # Do not enable stacktrace logging continue if uline.find('#include') < 0: out.write(line) continue if uline.strip().find('#include') > 0: print(uline) continue m = re1.search(uline) if not m: m = re2.search(uline) if m: path = m.groups()[0] else: m = re3.search(uline) if m: path = 'execinfo.h' else: print(uline + ' not found') continue h = path.strip('./') if "../3rdparty/" not in path else path if h.endswith('complex.h') and x.endswith('openblas_config.h'): source = '' elif h.startswith('ps/'): source = '../3rdparty/ps-lite/include/' + h else: source = find_source(h, x, stage) if not source: if (h not in blacklist and h not in sysheaders and 'mkl' not in h and 'nnpack' not in h and 'tensorrt' not in h and not h.endswith('.cuh')): sysheaders.append(h) else: expand.treeDepth += 1 expand(source, pending + [x], stage) expand.treeDepth -= 1 out.write(u"//===== EXPANDED : {} =====\n\n".format(x).encode('ascii')) history.add(x)
python
def expand(x, pending, stage): """ Expand the pending files in the current stage. Parameters ---------- x: str The file to expand. pending : str The list of pending files to expand. stage: str The current stage for file expansion, used for matching the prefix of files. """ if x in history and x not in ['mshadow/mshadow/expr_scalar-inl.h']: # MULTIPLE includes return if x in pending: #print('loop found: {} in {}'.format(x, pending)) return whtspace = ' ' * expand.treeDepth expand.fileCount += 1 comment = u"//=====[{:3d}] STAGE:{:>4} {}EXPANDING: {} =====\n\n".format(expand.fileCount, stage, whtspace, x) out.write(comment.encode('ascii')) print(comment) with open(x, 'rb') as x_h: for line in x_h.readlines(): uline = line.decode('utf-8') if '#define DMLC_LOG_STACK_TRACE 1' in uline.strip(): # Do not enable stacktrace logging continue if uline.find('#include') < 0: out.write(line) continue if uline.strip().find('#include') > 0: print(uline) continue m = re1.search(uline) if not m: m = re2.search(uline) if m: path = m.groups()[0] else: m = re3.search(uline) if m: path = 'execinfo.h' else: print(uline + ' not found') continue h = path.strip('./') if "../3rdparty/" not in path else path if h.endswith('complex.h') and x.endswith('openblas_config.h'): source = '' elif h.startswith('ps/'): source = '../3rdparty/ps-lite/include/' + h else: source = find_source(h, x, stage) if not source: if (h not in blacklist and h not in sysheaders and 'mkl' not in h and 'nnpack' not in h and 'tensorrt' not in h and not h.endswith('.cuh')): sysheaders.append(h) else: expand.treeDepth += 1 expand(source, pending + [x], stage) expand.treeDepth -= 1 out.write(u"//===== EXPANDED : {} =====\n\n".format(x).encode('ascii')) history.add(x)
[ "def", "expand", "(", "x", ",", "pending", ",", "stage", ")", ":", "if", "x", "in", "history", "and", "x", "not", "in", "[", "'mshadow/mshadow/expr_scalar-inl.h'", "]", ":", "# MULTIPLE includes", "return", "if", "x", "in", "pending", ":", "#print('loop found: {} in {}'.format(x, pending))", "return", "whtspace", "=", "' '", "*", "expand", ".", "treeDepth", "expand", ".", "fileCount", "+=", "1", "comment", "=", "u\"//=====[{:3d}] STAGE:{:>4} {}EXPANDING: {} =====\\n\\n\"", ".", "format", "(", "expand", ".", "fileCount", ",", "stage", ",", "whtspace", ",", "x", ")", "out", ".", "write", "(", "comment", ".", "encode", "(", "'ascii'", ")", ")", "print", "(", "comment", ")", "with", "open", "(", "x", ",", "'rb'", ")", "as", "x_h", ":", "for", "line", "in", "x_h", ".", "readlines", "(", ")", ":", "uline", "=", "line", ".", "decode", "(", "'utf-8'", ")", "if", "'#define DMLC_LOG_STACK_TRACE 1'", "in", "uline", ".", "strip", "(", ")", ":", "# Do not enable stacktrace logging", "continue", "if", "uline", ".", "find", "(", "'#include'", ")", "<", "0", ":", "out", ".", "write", "(", "line", ")", "continue", "if", "uline", ".", "strip", "(", ")", ".", "find", "(", "'#include'", ")", ">", "0", ":", "print", "(", "uline", ")", "continue", "m", "=", "re1", ".", "search", "(", "uline", ")", "if", "not", "m", ":", "m", "=", "re2", ".", "search", "(", "uline", ")", "if", "m", ":", "path", "=", "m", ".", "groups", "(", ")", "[", "0", "]", "else", ":", "m", "=", "re3", ".", "search", "(", "uline", ")", "if", "m", ":", "path", "=", "'execinfo.h'", "else", ":", "print", "(", "uline", "+", "' not found'", ")", "continue", "h", "=", "path", ".", "strip", "(", "'./'", ")", "if", "\"../3rdparty/\"", "not", "in", "path", "else", "path", "if", "h", ".", "endswith", "(", "'complex.h'", ")", "and", "x", ".", "endswith", "(", "'openblas_config.h'", ")", ":", "source", "=", "''", "elif", "h", ".", "startswith", "(", "'ps/'", ")", ":", "source", "=", "'../3rdparty/ps-lite/include/'", "+", "h", "else", ":", "source", "=", "find_source", "(", "h", ",", "x", ",", "stage", ")", "if", "not", "source", ":", "if", "(", "h", "not", "in", "blacklist", "and", "h", "not", "in", "sysheaders", "and", "'mkl'", "not", "in", "h", "and", "'nnpack'", "not", "in", "h", "and", "'tensorrt'", "not", "in", "h", "and", "not", "h", ".", "endswith", "(", "'.cuh'", ")", ")", ":", "sysheaders", ".", "append", "(", "h", ")", "else", ":", "expand", ".", "treeDepth", "+=", "1", "expand", "(", "source", ",", "pending", "+", "[", "x", "]", ",", "stage", ")", "expand", ".", "treeDepth", "-=", "1", "out", ".", "write", "(", "u\"//===== EXPANDED : {} =====\\n\\n\"", ".", "format", "(", "x", ")", ".", "encode", "(", "'ascii'", ")", ")", "history", ".", "add", "(", "x", ")" ]
Expand the pending files in the current stage. Parameters ---------- x: str The file to expand. pending : str The list of pending files to expand. stage: str The current stage for file expansion, used for matching the prefix of files.
[ "Expand", "the", "pending", "files", "in", "the", "current", "stage", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/amalgamation/amalgamation.py#L112-L182
train
apache/incubator-mxnet
example/gluon/data.py
get_imagenet_iterator
def get_imagenet_iterator(root, batch_size, num_workers, data_shape=224, dtype='float32'): """Dataset loader with preprocessing.""" train_dir = os.path.join(root, 'train') train_transform, val_transform = get_imagenet_transforms(data_shape, dtype) logging.info("Loading image folder %s, this may take a bit long...", train_dir) train_dataset = ImageFolderDataset(train_dir, transform=train_transform) train_data = DataLoader(train_dataset, batch_size, shuffle=True, last_batch='discard', num_workers=num_workers) val_dir = os.path.join(root, 'val') if not os.path.isdir(os.path.expanduser(os.path.join(root, 'val', 'n01440764'))): user_warning = 'Make sure validation images are stored in one subdir per category, a helper script is available at https://git.io/vNQv1' raise ValueError(user_warning) logging.info("Loading image folder %s, this may take a bit long...", val_dir) val_dataset = ImageFolderDataset(val_dir, transform=val_transform) val_data = DataLoader(val_dataset, batch_size, last_batch='keep', num_workers=num_workers) return DataLoaderIter(train_data, dtype), DataLoaderIter(val_data, dtype)
python
def get_imagenet_iterator(root, batch_size, num_workers, data_shape=224, dtype='float32'): """Dataset loader with preprocessing.""" train_dir = os.path.join(root, 'train') train_transform, val_transform = get_imagenet_transforms(data_shape, dtype) logging.info("Loading image folder %s, this may take a bit long...", train_dir) train_dataset = ImageFolderDataset(train_dir, transform=train_transform) train_data = DataLoader(train_dataset, batch_size, shuffle=True, last_batch='discard', num_workers=num_workers) val_dir = os.path.join(root, 'val') if not os.path.isdir(os.path.expanduser(os.path.join(root, 'val', 'n01440764'))): user_warning = 'Make sure validation images are stored in one subdir per category, a helper script is available at https://git.io/vNQv1' raise ValueError(user_warning) logging.info("Loading image folder %s, this may take a bit long...", val_dir) val_dataset = ImageFolderDataset(val_dir, transform=val_transform) val_data = DataLoader(val_dataset, batch_size, last_batch='keep', num_workers=num_workers) return DataLoaderIter(train_data, dtype), DataLoaderIter(val_data, dtype)
[ "def", "get_imagenet_iterator", "(", "root", ",", "batch_size", ",", "num_workers", ",", "data_shape", "=", "224", ",", "dtype", "=", "'float32'", ")", ":", "train_dir", "=", "os", ".", "path", ".", "join", "(", "root", ",", "'train'", ")", "train_transform", ",", "val_transform", "=", "get_imagenet_transforms", "(", "data_shape", ",", "dtype", ")", "logging", ".", "info", "(", "\"Loading image folder %s, this may take a bit long...\"", ",", "train_dir", ")", "train_dataset", "=", "ImageFolderDataset", "(", "train_dir", ",", "transform", "=", "train_transform", ")", "train_data", "=", "DataLoader", "(", "train_dataset", ",", "batch_size", ",", "shuffle", "=", "True", ",", "last_batch", "=", "'discard'", ",", "num_workers", "=", "num_workers", ")", "val_dir", "=", "os", ".", "path", ".", "join", "(", "root", ",", "'val'", ")", "if", "not", "os", ".", "path", ".", "isdir", "(", "os", ".", "path", ".", "expanduser", "(", "os", ".", "path", ".", "join", "(", "root", ",", "'val'", ",", "'n01440764'", ")", ")", ")", ":", "user_warning", "=", "'Make sure validation images are stored in one subdir per category, a helper script is available at https://git.io/vNQv1'", "raise", "ValueError", "(", "user_warning", ")", "logging", ".", "info", "(", "\"Loading image folder %s, this may take a bit long...\"", ",", "val_dir", ")", "val_dataset", "=", "ImageFolderDataset", "(", "val_dir", ",", "transform", "=", "val_transform", ")", "val_data", "=", "DataLoader", "(", "val_dataset", ",", "batch_size", ",", "last_batch", "=", "'keep'", ",", "num_workers", "=", "num_workers", ")", "return", "DataLoaderIter", "(", "train_data", ",", "dtype", ")", ",", "DataLoaderIter", "(", "val_data", ",", "dtype", ")" ]
Dataset loader with preprocessing.
[ "Dataset", "loader", "with", "preprocessing", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/example/gluon/data.py#L76-L91
train
apache/incubator-mxnet
python/mxnet/contrib/text/embedding.py
create
def create(embedding_name, **kwargs): """Creates an instance of token embedding. Creates a token embedding instance by loading embedding vectors from an externally hosted pre-trained token embedding file, such as those of GloVe and FastText. To get all the valid `embedding_name` and `pretrained_file_name`, use `mxnet.contrib.text.embedding.get_pretrained_file_names()`. Parameters ---------- embedding_name : str The token embedding name (case-insensitive). Returns ------- An instance of `mxnet.contrib.text.glossary._TokenEmbedding`: A token embedding instance that loads embedding vectors from an externally hosted pre-trained token embedding file. """ create_text_embedding = registry.get_create_func(_TokenEmbedding, 'token embedding') return create_text_embedding(embedding_name, **kwargs)
python
def create(embedding_name, **kwargs): """Creates an instance of token embedding. Creates a token embedding instance by loading embedding vectors from an externally hosted pre-trained token embedding file, such as those of GloVe and FastText. To get all the valid `embedding_name` and `pretrained_file_name`, use `mxnet.contrib.text.embedding.get_pretrained_file_names()`. Parameters ---------- embedding_name : str The token embedding name (case-insensitive). Returns ------- An instance of `mxnet.contrib.text.glossary._TokenEmbedding`: A token embedding instance that loads embedding vectors from an externally hosted pre-trained token embedding file. """ create_text_embedding = registry.get_create_func(_TokenEmbedding, 'token embedding') return create_text_embedding(embedding_name, **kwargs)
[ "def", "create", "(", "embedding_name", ",", "*", "*", "kwargs", ")", ":", "create_text_embedding", "=", "registry", ".", "get_create_func", "(", "_TokenEmbedding", ",", "'token embedding'", ")", "return", "create_text_embedding", "(", "embedding_name", ",", "*", "*", "kwargs", ")" ]
Creates an instance of token embedding. Creates a token embedding instance by loading embedding vectors from an externally hosted pre-trained token embedding file, such as those of GloVe and FastText. To get all the valid `embedding_name` and `pretrained_file_name`, use `mxnet.contrib.text.embedding.get_pretrained_file_names()`. Parameters ---------- embedding_name : str The token embedding name (case-insensitive). Returns ------- An instance of `mxnet.contrib.text.glossary._TokenEmbedding`: A token embedding instance that loads embedding vectors from an externally hosted pre-trained token embedding file.
[ "Creates", "an", "instance", "of", "token", "embedding", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/contrib/text/embedding.py#L63-L87
train
apache/incubator-mxnet
python/mxnet/contrib/text/embedding.py
get_pretrained_file_names
def get_pretrained_file_names(embedding_name=None): """Get valid token embedding names and their pre-trained file names. To load token embedding vectors from an externally hosted pre-trained token embedding file, such as those of GloVe and FastText, one should use `mxnet.contrib.text.embedding.create(embedding_name, pretrained_file_name)`. This method returns all the valid names of `pretrained_file_name` for the specified `embedding_name`. If `embedding_name` is set to None, this method returns all the valid names of `embedding_name` with their associated `pretrained_file_name`. Parameters ---------- embedding_name : str or None, default None The pre-trained token embedding name. Returns ------- dict or list: A list of all the valid pre-trained token embedding file names (`pretrained_file_name`) for the specified token embedding name (`embedding_name`). If the text embeding name is set to None, returns a dict mapping each valid token embedding name to a list of valid pre-trained files (`pretrained_file_name`). They can be plugged into `mxnet.contrib.text.embedding.create(embedding_name, pretrained_file_name)`. """ text_embedding_reg = registry.get_registry(_TokenEmbedding) if embedding_name is not None: if embedding_name not in text_embedding_reg: raise KeyError('Cannot find `embedding_name` %s. Use ' '`get_pretrained_file_names(' 'embedding_name=None).keys()` to get all the valid embedding ' 'names.' % embedding_name) return list(text_embedding_reg[embedding_name].pretrained_file_name_sha1.keys()) else: return {embedding_name: list(embedding_cls.pretrained_file_name_sha1.keys()) for embedding_name, embedding_cls in registry.get_registry(_TokenEmbedding).items()}
python
def get_pretrained_file_names(embedding_name=None): """Get valid token embedding names and their pre-trained file names. To load token embedding vectors from an externally hosted pre-trained token embedding file, such as those of GloVe and FastText, one should use `mxnet.contrib.text.embedding.create(embedding_name, pretrained_file_name)`. This method returns all the valid names of `pretrained_file_name` for the specified `embedding_name`. If `embedding_name` is set to None, this method returns all the valid names of `embedding_name` with their associated `pretrained_file_name`. Parameters ---------- embedding_name : str or None, default None The pre-trained token embedding name. Returns ------- dict or list: A list of all the valid pre-trained token embedding file names (`pretrained_file_name`) for the specified token embedding name (`embedding_name`). If the text embeding name is set to None, returns a dict mapping each valid token embedding name to a list of valid pre-trained files (`pretrained_file_name`). They can be plugged into `mxnet.contrib.text.embedding.create(embedding_name, pretrained_file_name)`. """ text_embedding_reg = registry.get_registry(_TokenEmbedding) if embedding_name is not None: if embedding_name not in text_embedding_reg: raise KeyError('Cannot find `embedding_name` %s. Use ' '`get_pretrained_file_names(' 'embedding_name=None).keys()` to get all the valid embedding ' 'names.' % embedding_name) return list(text_embedding_reg[embedding_name].pretrained_file_name_sha1.keys()) else: return {embedding_name: list(embedding_cls.pretrained_file_name_sha1.keys()) for embedding_name, embedding_cls in registry.get_registry(_TokenEmbedding).items()}
[ "def", "get_pretrained_file_names", "(", "embedding_name", "=", "None", ")", ":", "text_embedding_reg", "=", "registry", ".", "get_registry", "(", "_TokenEmbedding", ")", "if", "embedding_name", "is", "not", "None", ":", "if", "embedding_name", "not", "in", "text_embedding_reg", ":", "raise", "KeyError", "(", "'Cannot find `embedding_name` %s. Use '", "'`get_pretrained_file_names('", "'embedding_name=None).keys()` to get all the valid embedding '", "'names.'", "%", "embedding_name", ")", "return", "list", "(", "text_embedding_reg", "[", "embedding_name", "]", ".", "pretrained_file_name_sha1", ".", "keys", "(", ")", ")", "else", ":", "return", "{", "embedding_name", ":", "list", "(", "embedding_cls", ".", "pretrained_file_name_sha1", ".", "keys", "(", ")", ")", "for", "embedding_name", ",", "embedding_cls", "in", "registry", ".", "get_registry", "(", "_TokenEmbedding", ")", ".", "items", "(", ")", "}" ]
Get valid token embedding names and their pre-trained file names. To load token embedding vectors from an externally hosted pre-trained token embedding file, such as those of GloVe and FastText, one should use `mxnet.contrib.text.embedding.create(embedding_name, pretrained_file_name)`. This method returns all the valid names of `pretrained_file_name` for the specified `embedding_name`. If `embedding_name` is set to None, this method returns all the valid names of `embedding_name` with their associated `pretrained_file_name`. Parameters ---------- embedding_name : str or None, default None The pre-trained token embedding name. Returns ------- dict or list: A list of all the valid pre-trained token embedding file names (`pretrained_file_name`) for the specified token embedding name (`embedding_name`). If the text embeding name is set to None, returns a dict mapping each valid token embedding name to a list of valid pre-trained files (`pretrained_file_name`). They can be plugged into `mxnet.contrib.text.embedding.create(embedding_name, pretrained_file_name)`.
[ "Get", "valid", "token", "embedding", "names", "and", "their", "pre", "-", "trained", "file", "names", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/contrib/text/embedding.py#L90-L130
train
apache/incubator-mxnet
python/mxnet/contrib/text/embedding.py
_TokenEmbedding._load_embedding
def _load_embedding(self, pretrained_file_path, elem_delim, init_unknown_vec, encoding='utf8'): """Load embedding vectors from the pre-trained token embedding file. For every unknown token, if its representation `self.unknown_token` is encountered in the pre-trained token embedding file, index 0 of `self.idx_to_vec` maps to the pre-trained token embedding vector loaded from the file; otherwise, index 0 of `self.idx_to_vec` maps to the text embedding vector initialized by `init_unknown_vec`. If a token is encountered multiple times in the pre-trained text embedding file, only the first-encountered token embedding vector will be loaded and the rest will be skipped. """ pretrained_file_path = os.path.expanduser(pretrained_file_path) if not os.path.isfile(pretrained_file_path): raise ValueError('`pretrained_file_path` must be a valid path to ' 'the pre-trained token embedding file.') logging.info('Loading pre-trained token embedding vectors from %s', pretrained_file_path) vec_len = None all_elems = [] tokens = set() loaded_unknown_vec = None line_num = 0 with io.open(pretrained_file_path, 'r', encoding=encoding) as f: for line in f: line_num += 1 elems = line.rstrip().split(elem_delim) assert len(elems) > 1, 'At line %d of the pre-trained text embedding file: the ' \ 'data format of the pre-trained token embedding file %s ' \ 'is unexpected.' % (line_num, pretrained_file_path) token, elems = elems[0], [float(i) for i in elems[1:]] if token == self.unknown_token and loaded_unknown_vec is None: loaded_unknown_vec = elems tokens.add(self.unknown_token) elif token in tokens: warnings.warn('At line %d of the pre-trained token embedding file: the ' 'embedding vector for token %s has been loaded and a duplicate ' 'embedding for the same token is seen and skipped.' % (line_num, token)) elif len(elems) == 1: warnings.warn('At line %d of the pre-trained text embedding file: token %s ' 'with 1-dimensional vector %s is likely a header and is ' 'skipped.' % (line_num, token, elems)) else: if vec_len is None: vec_len = len(elems) # Reserve a vector slot for the unknown token at the very beggining because # the unknown index is 0. all_elems.extend([0] * vec_len) else: assert len(elems) == vec_len, \ 'At line %d of the pre-trained token embedding file: the dimension ' \ 'of token %s is %d but the dimension of previous tokens is %d. ' \ 'Dimensions of all the tokens must be the same.' \ % (line_num, token, len(elems), vec_len) all_elems.extend(elems) self._idx_to_token.append(token) self._token_to_idx[token] = len(self._idx_to_token) - 1 tokens.add(token) self._vec_len = vec_len self._idx_to_vec = nd.array(all_elems).reshape((-1, self.vec_len)) if loaded_unknown_vec is None: self._idx_to_vec[C.UNKNOWN_IDX] = init_unknown_vec(shape=self.vec_len) else: self._idx_to_vec[C.UNKNOWN_IDX] = nd.array(loaded_unknown_vec)
python
def _load_embedding(self, pretrained_file_path, elem_delim, init_unknown_vec, encoding='utf8'): """Load embedding vectors from the pre-trained token embedding file. For every unknown token, if its representation `self.unknown_token` is encountered in the pre-trained token embedding file, index 0 of `self.idx_to_vec` maps to the pre-trained token embedding vector loaded from the file; otherwise, index 0 of `self.idx_to_vec` maps to the text embedding vector initialized by `init_unknown_vec`. If a token is encountered multiple times in the pre-trained text embedding file, only the first-encountered token embedding vector will be loaded and the rest will be skipped. """ pretrained_file_path = os.path.expanduser(pretrained_file_path) if not os.path.isfile(pretrained_file_path): raise ValueError('`pretrained_file_path` must be a valid path to ' 'the pre-trained token embedding file.') logging.info('Loading pre-trained token embedding vectors from %s', pretrained_file_path) vec_len = None all_elems = [] tokens = set() loaded_unknown_vec = None line_num = 0 with io.open(pretrained_file_path, 'r', encoding=encoding) as f: for line in f: line_num += 1 elems = line.rstrip().split(elem_delim) assert len(elems) > 1, 'At line %d of the pre-trained text embedding file: the ' \ 'data format of the pre-trained token embedding file %s ' \ 'is unexpected.' % (line_num, pretrained_file_path) token, elems = elems[0], [float(i) for i in elems[1:]] if token == self.unknown_token and loaded_unknown_vec is None: loaded_unknown_vec = elems tokens.add(self.unknown_token) elif token in tokens: warnings.warn('At line %d of the pre-trained token embedding file: the ' 'embedding vector for token %s has been loaded and a duplicate ' 'embedding for the same token is seen and skipped.' % (line_num, token)) elif len(elems) == 1: warnings.warn('At line %d of the pre-trained text embedding file: token %s ' 'with 1-dimensional vector %s is likely a header and is ' 'skipped.' % (line_num, token, elems)) else: if vec_len is None: vec_len = len(elems) # Reserve a vector slot for the unknown token at the very beggining because # the unknown index is 0. all_elems.extend([0] * vec_len) else: assert len(elems) == vec_len, \ 'At line %d of the pre-trained token embedding file: the dimension ' \ 'of token %s is %d but the dimension of previous tokens is %d. ' \ 'Dimensions of all the tokens must be the same.' \ % (line_num, token, len(elems), vec_len) all_elems.extend(elems) self._idx_to_token.append(token) self._token_to_idx[token] = len(self._idx_to_token) - 1 tokens.add(token) self._vec_len = vec_len self._idx_to_vec = nd.array(all_elems).reshape((-1, self.vec_len)) if loaded_unknown_vec is None: self._idx_to_vec[C.UNKNOWN_IDX] = init_unknown_vec(shape=self.vec_len) else: self._idx_to_vec[C.UNKNOWN_IDX] = nd.array(loaded_unknown_vec)
[ "def", "_load_embedding", "(", "self", ",", "pretrained_file_path", ",", "elem_delim", ",", "init_unknown_vec", ",", "encoding", "=", "'utf8'", ")", ":", "pretrained_file_path", "=", "os", ".", "path", ".", "expanduser", "(", "pretrained_file_path", ")", "if", "not", "os", ".", "path", ".", "isfile", "(", "pretrained_file_path", ")", ":", "raise", "ValueError", "(", "'`pretrained_file_path` must be a valid path to '", "'the pre-trained token embedding file.'", ")", "logging", ".", "info", "(", "'Loading pre-trained token embedding vectors from %s'", ",", "pretrained_file_path", ")", "vec_len", "=", "None", "all_elems", "=", "[", "]", "tokens", "=", "set", "(", ")", "loaded_unknown_vec", "=", "None", "line_num", "=", "0", "with", "io", ".", "open", "(", "pretrained_file_path", ",", "'r'", ",", "encoding", "=", "encoding", ")", "as", "f", ":", "for", "line", "in", "f", ":", "line_num", "+=", "1", "elems", "=", "line", ".", "rstrip", "(", ")", ".", "split", "(", "elem_delim", ")", "assert", "len", "(", "elems", ")", ">", "1", ",", "'At line %d of the pre-trained text embedding file: the '", "'data format of the pre-trained token embedding file %s '", "'is unexpected.'", "%", "(", "line_num", ",", "pretrained_file_path", ")", "token", ",", "elems", "=", "elems", "[", "0", "]", ",", "[", "float", "(", "i", ")", "for", "i", "in", "elems", "[", "1", ":", "]", "]", "if", "token", "==", "self", ".", "unknown_token", "and", "loaded_unknown_vec", "is", "None", ":", "loaded_unknown_vec", "=", "elems", "tokens", ".", "add", "(", "self", ".", "unknown_token", ")", "elif", "token", "in", "tokens", ":", "warnings", ".", "warn", "(", "'At line %d of the pre-trained token embedding file: the '", "'embedding vector for token %s has been loaded and a duplicate '", "'embedding for the same token is seen and skipped.'", "%", "(", "line_num", ",", "token", ")", ")", "elif", "len", "(", "elems", ")", "==", "1", ":", "warnings", ".", "warn", "(", "'At line %d of the pre-trained text embedding file: token %s '", "'with 1-dimensional vector %s is likely a header and is '", "'skipped.'", "%", "(", "line_num", ",", "token", ",", "elems", ")", ")", "else", ":", "if", "vec_len", "is", "None", ":", "vec_len", "=", "len", "(", "elems", ")", "# Reserve a vector slot for the unknown token at the very beggining because", "# the unknown index is 0.", "all_elems", ".", "extend", "(", "[", "0", "]", "*", "vec_len", ")", "else", ":", "assert", "len", "(", "elems", ")", "==", "vec_len", ",", "'At line %d of the pre-trained token embedding file: the dimension '", "'of token %s is %d but the dimension of previous tokens is %d. '", "'Dimensions of all the tokens must be the same.'", "%", "(", "line_num", ",", "token", ",", "len", "(", "elems", ")", ",", "vec_len", ")", "all_elems", ".", "extend", "(", "elems", ")", "self", ".", "_idx_to_token", ".", "append", "(", "token", ")", "self", ".", "_token_to_idx", "[", "token", "]", "=", "len", "(", "self", ".", "_idx_to_token", ")", "-", "1", "tokens", ".", "add", "(", "token", ")", "self", ".", "_vec_len", "=", "vec_len", "self", ".", "_idx_to_vec", "=", "nd", ".", "array", "(", "all_elems", ")", ".", "reshape", "(", "(", "-", "1", ",", "self", ".", "vec_len", ")", ")", "if", "loaded_unknown_vec", "is", "None", ":", "self", ".", "_idx_to_vec", "[", "C", ".", "UNKNOWN_IDX", "]", "=", "init_unknown_vec", "(", "shape", "=", "self", ".", "vec_len", ")", "else", ":", "self", ".", "_idx_to_vec", "[", "C", ".", "UNKNOWN_IDX", "]", "=", "nd", ".", "array", "(", "loaded_unknown_vec", ")" ]
Load embedding vectors from the pre-trained token embedding file. For every unknown token, if its representation `self.unknown_token` is encountered in the pre-trained token embedding file, index 0 of `self.idx_to_vec` maps to the pre-trained token embedding vector loaded from the file; otherwise, index 0 of `self.idx_to_vec` maps to the text embedding vector initialized by `init_unknown_vec`. If a token is encountered multiple times in the pre-trained text embedding file, only the first-encountered token embedding vector will be loaded and the rest will be skipped.
[ "Load", "embedding", "vectors", "from", "the", "pre", "-", "trained", "token", "embedding", "file", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/contrib/text/embedding.py#L232-L303
train
apache/incubator-mxnet
python/mxnet/contrib/text/embedding.py
_TokenEmbedding._set_idx_to_vec_by_embeddings
def _set_idx_to_vec_by_embeddings(self, token_embeddings, vocab_len, vocab_idx_to_token): """Sets the mapping between token indices and token embedding vectors. Parameters ---------- token_embeddings : instance or list `mxnet.contrib.text.embedding._TokenEmbedding` One or multiple pre-trained token embeddings to load. If it is a list of multiple embeddings, these embedding vectors will be concatenated for each token. vocab_len : int Length of vocabulary whose tokens are indexed in the token embedding. vocab_idx_to_token: list of str A list of indexed tokens in the vocabulary. These tokens are indexed in the token embedding. """ new_vec_len = sum(embed.vec_len for embed in token_embeddings) new_idx_to_vec = nd.zeros(shape=(vocab_len, new_vec_len)) col_start = 0 # Concatenate all the embedding vectors in token_embeddings. for embed in token_embeddings: col_end = col_start + embed.vec_len # Cancatenate vectors of the unknown token. new_idx_to_vec[0, col_start:col_end] = embed.idx_to_vec[0] new_idx_to_vec[1:, col_start:col_end] = embed.get_vecs_by_tokens(vocab_idx_to_token[1:]) col_start = col_end self._vec_len = new_vec_len self._idx_to_vec = new_idx_to_vec
python
def _set_idx_to_vec_by_embeddings(self, token_embeddings, vocab_len, vocab_idx_to_token): """Sets the mapping between token indices and token embedding vectors. Parameters ---------- token_embeddings : instance or list `mxnet.contrib.text.embedding._TokenEmbedding` One or multiple pre-trained token embeddings to load. If it is a list of multiple embeddings, these embedding vectors will be concatenated for each token. vocab_len : int Length of vocabulary whose tokens are indexed in the token embedding. vocab_idx_to_token: list of str A list of indexed tokens in the vocabulary. These tokens are indexed in the token embedding. """ new_vec_len = sum(embed.vec_len for embed in token_embeddings) new_idx_to_vec = nd.zeros(shape=(vocab_len, new_vec_len)) col_start = 0 # Concatenate all the embedding vectors in token_embeddings. for embed in token_embeddings: col_end = col_start + embed.vec_len # Cancatenate vectors of the unknown token. new_idx_to_vec[0, col_start:col_end] = embed.idx_to_vec[0] new_idx_to_vec[1:, col_start:col_end] = embed.get_vecs_by_tokens(vocab_idx_to_token[1:]) col_start = col_end self._vec_len = new_vec_len self._idx_to_vec = new_idx_to_vec
[ "def", "_set_idx_to_vec_by_embeddings", "(", "self", ",", "token_embeddings", ",", "vocab_len", ",", "vocab_idx_to_token", ")", ":", "new_vec_len", "=", "sum", "(", "embed", ".", "vec_len", "for", "embed", "in", "token_embeddings", ")", "new_idx_to_vec", "=", "nd", ".", "zeros", "(", "shape", "=", "(", "vocab_len", ",", "new_vec_len", ")", ")", "col_start", "=", "0", "# Concatenate all the embedding vectors in token_embeddings.", "for", "embed", "in", "token_embeddings", ":", "col_end", "=", "col_start", "+", "embed", ".", "vec_len", "# Cancatenate vectors of the unknown token.", "new_idx_to_vec", "[", "0", ",", "col_start", ":", "col_end", "]", "=", "embed", ".", "idx_to_vec", "[", "0", "]", "new_idx_to_vec", "[", "1", ":", ",", "col_start", ":", "col_end", "]", "=", "embed", ".", "get_vecs_by_tokens", "(", "vocab_idx_to_token", "[", "1", ":", "]", ")", "col_start", "=", "col_end", "self", ".", "_vec_len", "=", "new_vec_len", "self", ".", "_idx_to_vec", "=", "new_idx_to_vec" ]
Sets the mapping between token indices and token embedding vectors. Parameters ---------- token_embeddings : instance or list `mxnet.contrib.text.embedding._TokenEmbedding` One or multiple pre-trained token embeddings to load. If it is a list of multiple embeddings, these embedding vectors will be concatenated for each token. vocab_len : int Length of vocabulary whose tokens are indexed in the token embedding. vocab_idx_to_token: list of str A list of indexed tokens in the vocabulary. These tokens are indexed in the token embedding.
[ "Sets", "the", "mapping", "between", "token", "indices", "and", "token", "embedding", "vectors", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/contrib/text/embedding.py#L314-L343
train
apache/incubator-mxnet
python/mxnet/contrib/text/embedding.py
_TokenEmbedding.get_vecs_by_tokens
def get_vecs_by_tokens(self, tokens, lower_case_backup=False): """Look up embedding vectors of tokens. Parameters ---------- tokens : str or list of strs A token or a list of tokens. lower_case_backup : bool, default False If False, each token in the original case will be looked up; if True, each token in the original case will be looked up first, if not found in the keys of the property `token_to_idx`, the token in the lower case will be looked up. Returns ------- mxnet.ndarray.NDArray: The embedding vector(s) of the token(s). According to numpy conventions, if `tokens` is a string, returns a 1-D NDArray of shape `self.vec_len`; if `tokens` is a list of strings, returns a 2-D NDArray of shape=(len(tokens), self.vec_len). """ to_reduce = False if not isinstance(tokens, list): tokens = [tokens] to_reduce = True if not lower_case_backup: indices = [self.token_to_idx.get(token, C.UNKNOWN_IDX) for token in tokens] else: indices = [self.token_to_idx[token] if token in self.token_to_idx else self.token_to_idx.get(token.lower(), C.UNKNOWN_IDX) for token in tokens] vecs = nd.Embedding(nd.array(indices), self.idx_to_vec, self.idx_to_vec.shape[0], self.idx_to_vec.shape[1]) return vecs[0] if to_reduce else vecs
python
def get_vecs_by_tokens(self, tokens, lower_case_backup=False): """Look up embedding vectors of tokens. Parameters ---------- tokens : str or list of strs A token or a list of tokens. lower_case_backup : bool, default False If False, each token in the original case will be looked up; if True, each token in the original case will be looked up first, if not found in the keys of the property `token_to_idx`, the token in the lower case will be looked up. Returns ------- mxnet.ndarray.NDArray: The embedding vector(s) of the token(s). According to numpy conventions, if `tokens` is a string, returns a 1-D NDArray of shape `self.vec_len`; if `tokens` is a list of strings, returns a 2-D NDArray of shape=(len(tokens), self.vec_len). """ to_reduce = False if not isinstance(tokens, list): tokens = [tokens] to_reduce = True if not lower_case_backup: indices = [self.token_to_idx.get(token, C.UNKNOWN_IDX) for token in tokens] else: indices = [self.token_to_idx[token] if token in self.token_to_idx else self.token_to_idx.get(token.lower(), C.UNKNOWN_IDX) for token in tokens] vecs = nd.Embedding(nd.array(indices), self.idx_to_vec, self.idx_to_vec.shape[0], self.idx_to_vec.shape[1]) return vecs[0] if to_reduce else vecs
[ "def", "get_vecs_by_tokens", "(", "self", ",", "tokens", ",", "lower_case_backup", "=", "False", ")", ":", "to_reduce", "=", "False", "if", "not", "isinstance", "(", "tokens", ",", "list", ")", ":", "tokens", "=", "[", "tokens", "]", "to_reduce", "=", "True", "if", "not", "lower_case_backup", ":", "indices", "=", "[", "self", ".", "token_to_idx", ".", "get", "(", "token", ",", "C", ".", "UNKNOWN_IDX", ")", "for", "token", "in", "tokens", "]", "else", ":", "indices", "=", "[", "self", ".", "token_to_idx", "[", "token", "]", "if", "token", "in", "self", ".", "token_to_idx", "else", "self", ".", "token_to_idx", ".", "get", "(", "token", ".", "lower", "(", ")", ",", "C", ".", "UNKNOWN_IDX", ")", "for", "token", "in", "tokens", "]", "vecs", "=", "nd", ".", "Embedding", "(", "nd", ".", "array", "(", "indices", ")", ",", "self", ".", "idx_to_vec", ",", "self", ".", "idx_to_vec", ".", "shape", "[", "0", "]", ",", "self", ".", "idx_to_vec", ".", "shape", "[", "1", "]", ")", "return", "vecs", "[", "0", "]", "if", "to_reduce", "else", "vecs" ]
Look up embedding vectors of tokens. Parameters ---------- tokens : str or list of strs A token or a list of tokens. lower_case_backup : bool, default False If False, each token in the original case will be looked up; if True, each token in the original case will be looked up first, if not found in the keys of the property `token_to_idx`, the token in the lower case will be looked up. Returns ------- mxnet.ndarray.NDArray: The embedding vector(s) of the token(s). According to numpy conventions, if `tokens` is a string, returns a 1-D NDArray of shape `self.vec_len`; if `tokens` is a list of strings, returns a 2-D NDArray of shape=(len(tokens), self.vec_len).
[ "Look", "up", "embedding", "vectors", "of", "tokens", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/contrib/text/embedding.py#L366-L403
train
apache/incubator-mxnet
python/mxnet/contrib/text/embedding.py
_TokenEmbedding.update_token_vectors
def update_token_vectors(self, tokens, new_vectors): """Updates embedding vectors for tokens. Parameters ---------- tokens : str or a list of strs A token or a list of tokens whose embedding vector are to be updated. new_vectors : mxnet.ndarray.NDArray An NDArray to be assigned to the embedding vectors of `tokens`. Its length must be equal to the number of `tokens` and its width must be equal to the dimension of embeddings of the glossary. If `tokens` is a singleton, it must be 1-D or 2-D. If `tokens` is a list of multiple strings, it must be 2-D. """ assert self.idx_to_vec is not None, 'The property `idx_to_vec` has not been properly set.' if not isinstance(tokens, list) or len(tokens) == 1: assert isinstance(new_vectors, nd.NDArray) and len(new_vectors.shape) in [1, 2], \ '`new_vectors` must be a 1-D or 2-D NDArray if `tokens` is a singleton.' if not isinstance(tokens, list): tokens = [tokens] if len(new_vectors.shape) == 1: new_vectors = new_vectors.expand_dims(0) else: assert isinstance(new_vectors, nd.NDArray) and len(new_vectors.shape) == 2, \ '`new_vectors` must be a 2-D NDArray if `tokens` is a list of multiple strings.' assert new_vectors.shape == (len(tokens), self.vec_len), \ 'The length of new_vectors must be equal to the number of tokens and the width of' \ 'new_vectors must be equal to the dimension of embeddings of the glossary.' indices = [] for token in tokens: if token in self.token_to_idx: indices.append(self.token_to_idx[token]) else: raise ValueError('Token %s is unknown. To update the embedding vector for an ' 'unknown token, please specify it explicitly as the ' '`unknown_token` %s in `tokens`. This is to avoid unintended ' 'updates.' % (token, self.idx_to_token[C.UNKNOWN_IDX])) self._idx_to_vec[nd.array(indices)] = new_vectors
python
def update_token_vectors(self, tokens, new_vectors): """Updates embedding vectors for tokens. Parameters ---------- tokens : str or a list of strs A token or a list of tokens whose embedding vector are to be updated. new_vectors : mxnet.ndarray.NDArray An NDArray to be assigned to the embedding vectors of `tokens`. Its length must be equal to the number of `tokens` and its width must be equal to the dimension of embeddings of the glossary. If `tokens` is a singleton, it must be 1-D or 2-D. If `tokens` is a list of multiple strings, it must be 2-D. """ assert self.idx_to_vec is not None, 'The property `idx_to_vec` has not been properly set.' if not isinstance(tokens, list) or len(tokens) == 1: assert isinstance(new_vectors, nd.NDArray) and len(new_vectors.shape) in [1, 2], \ '`new_vectors` must be a 1-D or 2-D NDArray if `tokens` is a singleton.' if not isinstance(tokens, list): tokens = [tokens] if len(new_vectors.shape) == 1: new_vectors = new_vectors.expand_dims(0) else: assert isinstance(new_vectors, nd.NDArray) and len(new_vectors.shape) == 2, \ '`new_vectors` must be a 2-D NDArray if `tokens` is a list of multiple strings.' assert new_vectors.shape == (len(tokens), self.vec_len), \ 'The length of new_vectors must be equal to the number of tokens and the width of' \ 'new_vectors must be equal to the dimension of embeddings of the glossary.' indices = [] for token in tokens: if token in self.token_to_idx: indices.append(self.token_to_idx[token]) else: raise ValueError('Token %s is unknown. To update the embedding vector for an ' 'unknown token, please specify it explicitly as the ' '`unknown_token` %s in `tokens`. This is to avoid unintended ' 'updates.' % (token, self.idx_to_token[C.UNKNOWN_IDX])) self._idx_to_vec[nd.array(indices)] = new_vectors
[ "def", "update_token_vectors", "(", "self", ",", "tokens", ",", "new_vectors", ")", ":", "assert", "self", ".", "idx_to_vec", "is", "not", "None", ",", "'The property `idx_to_vec` has not been properly set.'", "if", "not", "isinstance", "(", "tokens", ",", "list", ")", "or", "len", "(", "tokens", ")", "==", "1", ":", "assert", "isinstance", "(", "new_vectors", ",", "nd", ".", "NDArray", ")", "and", "len", "(", "new_vectors", ".", "shape", ")", "in", "[", "1", ",", "2", "]", ",", "'`new_vectors` must be a 1-D or 2-D NDArray if `tokens` is a singleton.'", "if", "not", "isinstance", "(", "tokens", ",", "list", ")", ":", "tokens", "=", "[", "tokens", "]", "if", "len", "(", "new_vectors", ".", "shape", ")", "==", "1", ":", "new_vectors", "=", "new_vectors", ".", "expand_dims", "(", "0", ")", "else", ":", "assert", "isinstance", "(", "new_vectors", ",", "nd", ".", "NDArray", ")", "and", "len", "(", "new_vectors", ".", "shape", ")", "==", "2", ",", "'`new_vectors` must be a 2-D NDArray if `tokens` is a list of multiple strings.'", "assert", "new_vectors", ".", "shape", "==", "(", "len", "(", "tokens", ")", ",", "self", ".", "vec_len", ")", ",", "'The length of new_vectors must be equal to the number of tokens and the width of'", "'new_vectors must be equal to the dimension of embeddings of the glossary.'", "indices", "=", "[", "]", "for", "token", "in", "tokens", ":", "if", "token", "in", "self", ".", "token_to_idx", ":", "indices", ".", "append", "(", "self", ".", "token_to_idx", "[", "token", "]", ")", "else", ":", "raise", "ValueError", "(", "'Token %s is unknown. To update the embedding vector for an '", "'unknown token, please specify it explicitly as the '", "'`unknown_token` %s in `tokens`. This is to avoid unintended '", "'updates.'", "%", "(", "token", ",", "self", ".", "idx_to_token", "[", "C", ".", "UNKNOWN_IDX", "]", ")", ")", "self", ".", "_idx_to_vec", "[", "nd", ".", "array", "(", "indices", ")", "]", "=", "new_vectors" ]
Updates embedding vectors for tokens. Parameters ---------- tokens : str or a list of strs A token or a list of tokens whose embedding vector are to be updated. new_vectors : mxnet.ndarray.NDArray An NDArray to be assigned to the embedding vectors of `tokens`. Its length must be equal to the number of `tokens` and its width must be equal to the dimension of embeddings of the glossary. If `tokens` is a singleton, it must be 1-D or 2-D. If `tokens` is a list of multiple strings, it must be 2-D.
[ "Updates", "embedding", "vectors", "for", "tokens", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/contrib/text/embedding.py#L405-L447
train
apache/incubator-mxnet
python/mxnet/contrib/text/embedding.py
_TokenEmbedding._check_pretrained_file_names
def _check_pretrained_file_names(cls, pretrained_file_name): """Checks if a pre-trained token embedding file name is valid. Parameters ---------- pretrained_file_name : str The pre-trained token embedding file. """ embedding_name = cls.__name__.lower() if pretrained_file_name not in cls.pretrained_file_name_sha1: raise KeyError('Cannot find pretrained file %s for token embedding %s. Valid ' 'pretrained files for embedding %s: %s' % (pretrained_file_name, embedding_name, embedding_name, ', '.join(cls.pretrained_file_name_sha1.keys())))
python
def _check_pretrained_file_names(cls, pretrained_file_name): """Checks if a pre-trained token embedding file name is valid. Parameters ---------- pretrained_file_name : str The pre-trained token embedding file. """ embedding_name = cls.__name__.lower() if pretrained_file_name not in cls.pretrained_file_name_sha1: raise KeyError('Cannot find pretrained file %s for token embedding %s. Valid ' 'pretrained files for embedding %s: %s' % (pretrained_file_name, embedding_name, embedding_name, ', '.join(cls.pretrained_file_name_sha1.keys())))
[ "def", "_check_pretrained_file_names", "(", "cls", ",", "pretrained_file_name", ")", ":", "embedding_name", "=", "cls", ".", "__name__", ".", "lower", "(", ")", "if", "pretrained_file_name", "not", "in", "cls", ".", "pretrained_file_name_sha1", ":", "raise", "KeyError", "(", "'Cannot find pretrained file %s for token embedding %s. Valid '", "'pretrained files for embedding %s: %s'", "%", "(", "pretrained_file_name", ",", "embedding_name", ",", "embedding_name", ",", "', '", ".", "join", "(", "cls", ".", "pretrained_file_name_sha1", ".", "keys", "(", ")", ")", ")", ")" ]
Checks if a pre-trained token embedding file name is valid. Parameters ---------- pretrained_file_name : str The pre-trained token embedding file.
[ "Checks", "if", "a", "pre", "-", "trained", "token", "embedding", "file", "name", "is", "valid", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/contrib/text/embedding.py#L450-L465
train
apache/incubator-mxnet
example/bayesian-methods/algos.py
calc_grad
def calc_grad(exe, exe_grads, params, X, Y, label_name=None, outgrad_f=None): """Calculate gradient""" exe.copy_params_from(params) exe.arg_dict['data'][:] = X if outgrad_f is None: exe.arg_dict[label_name][:] = Y exe.forward(is_train=True) exe.backward() else: exe.forward(is_train=True) exe.backward(outgrad_f(exe.outpus, Y)) for k, v in exe_grads.items(): v.wait_to_read()
python
def calc_grad(exe, exe_grads, params, X, Y, label_name=None, outgrad_f=None): """Calculate gradient""" exe.copy_params_from(params) exe.arg_dict['data'][:] = X if outgrad_f is None: exe.arg_dict[label_name][:] = Y exe.forward(is_train=True) exe.backward() else: exe.forward(is_train=True) exe.backward(outgrad_f(exe.outpus, Y)) for k, v in exe_grads.items(): v.wait_to_read()
[ "def", "calc_grad", "(", "exe", ",", "exe_grads", ",", "params", ",", "X", ",", "Y", ",", "label_name", "=", "None", ",", "outgrad_f", "=", "None", ")", ":", "exe", ".", "copy_params_from", "(", "params", ")", "exe", ".", "arg_dict", "[", "'data'", "]", "[", ":", "]", "=", "X", "if", "outgrad_f", "is", "None", ":", "exe", ".", "arg_dict", "[", "label_name", "]", "[", ":", "]", "=", "Y", "exe", ".", "forward", "(", "is_train", "=", "True", ")", "exe", ".", "backward", "(", ")", "else", ":", "exe", ".", "forward", "(", "is_train", "=", "True", ")", "exe", ".", "backward", "(", "outgrad_f", "(", "exe", ".", "outpus", ",", "Y", ")", ")", "for", "k", ",", "v", "in", "exe_grads", ".", "items", "(", ")", ":", "v", ".", "wait_to_read", "(", ")" ]
Calculate gradient
[ "Calculate", "gradient" ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/example/bayesian-methods/algos.py#L37-L49
train
apache/incubator-mxnet
example/bayesian-methods/algos.py
step_HMC
def step_HMC(exe, exe_params, exe_grads, label_key, noise_precision, prior_precision, L=10, eps=1E-6): """Generate the implementation of step HMC""" init_params = {k: v.copyto(v.context) for k, v in exe_params.items()} end_params = {k: v.copyto(v.context) for k, v in exe_params.items()} init_momentums = {k: mx.random.normal(0, 1, v.shape) for k, v in init_params.items()} end_momentums = {k: v.copyto(v.context) for k, v in init_momentums.items()} init_potential = calc_potential(exe, init_params, label_key, noise_precision, prior_precision) # 0. Calculate Initial Energy and Kinetic init_kinetic = sum([nd.sum(nd.square(momentum)) / 2.0 for momentum in init_momentums.values()]).asscalar() # 1. Make a half step for momentum at the beginning exe.copy_params_from(end_params) exe.forward(is_train=True) exe.backward() for k, v in exe_grads.items(): v.wait_to_read() for k, momentum in end_momentums.items(): momentum[:] = momentum - (eps / 2) * exe_grads[k] # 2. Alternate full steps for position and momentum for i in range(L): # 2.1 Full step for position for k, param in exe_params.items(): param[:] = param + eps * end_momentums[k] # 2.2 Full step for the momentum, except at the end of trajectory we perform a half step exe.forward(is_train=True) exe.backward() for v in exe_grads.values(): v.wait_to_read() if i != L - 1: for k, momentum in end_momentums.items(): momentum[:] = momentum - eps * exe_grads[k] else: for k, momentum in end_momentums.items(): # We should reverse the sign of the momentum at the end momentum[:] = -(momentum - eps / 2.0 * exe_grads[k]) copy_param(exe, end_params) # 3. Calculate acceptance ratio and accept/reject the move end_potential = calc_potential(exe, end_params, label_key, noise_precision, prior_precision) end_kinetic = sum([nd.sum(nd.square(momentum)) / 2.0 for momentum in end_momentums.values()]).asscalar() # print init_potential, init_kinetic, end_potential, end_kinetic r = numpy.random.rand(1) if r < numpy.exp(-(end_potential + end_kinetic) + (init_potential + init_kinetic)): exe.copy_params_from(end_params) return end_params, 1 else: exe.copy_params_from(init_params) return init_params, 0
python
def step_HMC(exe, exe_params, exe_grads, label_key, noise_precision, prior_precision, L=10, eps=1E-6): """Generate the implementation of step HMC""" init_params = {k: v.copyto(v.context) for k, v in exe_params.items()} end_params = {k: v.copyto(v.context) for k, v in exe_params.items()} init_momentums = {k: mx.random.normal(0, 1, v.shape) for k, v in init_params.items()} end_momentums = {k: v.copyto(v.context) for k, v in init_momentums.items()} init_potential = calc_potential(exe, init_params, label_key, noise_precision, prior_precision) # 0. Calculate Initial Energy and Kinetic init_kinetic = sum([nd.sum(nd.square(momentum)) / 2.0 for momentum in init_momentums.values()]).asscalar() # 1. Make a half step for momentum at the beginning exe.copy_params_from(end_params) exe.forward(is_train=True) exe.backward() for k, v in exe_grads.items(): v.wait_to_read() for k, momentum in end_momentums.items(): momentum[:] = momentum - (eps / 2) * exe_grads[k] # 2. Alternate full steps for position and momentum for i in range(L): # 2.1 Full step for position for k, param in exe_params.items(): param[:] = param + eps * end_momentums[k] # 2.2 Full step for the momentum, except at the end of trajectory we perform a half step exe.forward(is_train=True) exe.backward() for v in exe_grads.values(): v.wait_to_read() if i != L - 1: for k, momentum in end_momentums.items(): momentum[:] = momentum - eps * exe_grads[k] else: for k, momentum in end_momentums.items(): # We should reverse the sign of the momentum at the end momentum[:] = -(momentum - eps / 2.0 * exe_grads[k]) copy_param(exe, end_params) # 3. Calculate acceptance ratio and accept/reject the move end_potential = calc_potential(exe, end_params, label_key, noise_precision, prior_precision) end_kinetic = sum([nd.sum(nd.square(momentum)) / 2.0 for momentum in end_momentums.values()]).asscalar() # print init_potential, init_kinetic, end_potential, end_kinetic r = numpy.random.rand(1) if r < numpy.exp(-(end_potential + end_kinetic) + (init_potential + init_kinetic)): exe.copy_params_from(end_params) return end_params, 1 else: exe.copy_params_from(init_params) return init_params, 0
[ "def", "step_HMC", "(", "exe", ",", "exe_params", ",", "exe_grads", ",", "label_key", ",", "noise_precision", ",", "prior_precision", ",", "L", "=", "10", ",", "eps", "=", "1E-6", ")", ":", "init_params", "=", "{", "k", ":", "v", ".", "copyto", "(", "v", ".", "context", ")", "for", "k", ",", "v", "in", "exe_params", ".", "items", "(", ")", "}", "end_params", "=", "{", "k", ":", "v", ".", "copyto", "(", "v", ".", "context", ")", "for", "k", ",", "v", "in", "exe_params", ".", "items", "(", ")", "}", "init_momentums", "=", "{", "k", ":", "mx", ".", "random", ".", "normal", "(", "0", ",", "1", ",", "v", ".", "shape", ")", "for", "k", ",", "v", "in", "init_params", ".", "items", "(", ")", "}", "end_momentums", "=", "{", "k", ":", "v", ".", "copyto", "(", "v", ".", "context", ")", "for", "k", ",", "v", "in", "init_momentums", ".", "items", "(", ")", "}", "init_potential", "=", "calc_potential", "(", "exe", ",", "init_params", ",", "label_key", ",", "noise_precision", ",", "prior_precision", ")", "# 0. Calculate Initial Energy and Kinetic", "init_kinetic", "=", "sum", "(", "[", "nd", ".", "sum", "(", "nd", ".", "square", "(", "momentum", ")", ")", "/", "2.0", "for", "momentum", "in", "init_momentums", ".", "values", "(", ")", "]", ")", ".", "asscalar", "(", ")", "# 1. Make a half step for momentum at the beginning", "exe", ".", "copy_params_from", "(", "end_params", ")", "exe", ".", "forward", "(", "is_train", "=", "True", ")", "exe", ".", "backward", "(", ")", "for", "k", ",", "v", "in", "exe_grads", ".", "items", "(", ")", ":", "v", ".", "wait_to_read", "(", ")", "for", "k", ",", "momentum", "in", "end_momentums", ".", "items", "(", ")", ":", "momentum", "[", ":", "]", "=", "momentum", "-", "(", "eps", "/", "2", ")", "*", "exe_grads", "[", "k", "]", "# 2. Alternate full steps for position and momentum", "for", "i", "in", "range", "(", "L", ")", ":", "# 2.1 Full step for position", "for", "k", ",", "param", "in", "exe_params", ".", "items", "(", ")", ":", "param", "[", ":", "]", "=", "param", "+", "eps", "*", "end_momentums", "[", "k", "]", "# 2.2 Full step for the momentum, except at the end of trajectory we perform a half step", "exe", ".", "forward", "(", "is_train", "=", "True", ")", "exe", ".", "backward", "(", ")", "for", "v", "in", "exe_grads", ".", "values", "(", ")", ":", "v", ".", "wait_to_read", "(", ")", "if", "i", "!=", "L", "-", "1", ":", "for", "k", ",", "momentum", "in", "end_momentums", ".", "items", "(", ")", ":", "momentum", "[", ":", "]", "=", "momentum", "-", "eps", "*", "exe_grads", "[", "k", "]", "else", ":", "for", "k", ",", "momentum", "in", "end_momentums", ".", "items", "(", ")", ":", "# We should reverse the sign of the momentum at the end", "momentum", "[", ":", "]", "=", "-", "(", "momentum", "-", "eps", "/", "2.0", "*", "exe_grads", "[", "k", "]", ")", "copy_param", "(", "exe", ",", "end_params", ")", "# 3. Calculate acceptance ratio and accept/reject the move", "end_potential", "=", "calc_potential", "(", "exe", ",", "end_params", ",", "label_key", ",", "noise_precision", ",", "prior_precision", ")", "end_kinetic", "=", "sum", "(", "[", "nd", ".", "sum", "(", "nd", ".", "square", "(", "momentum", ")", ")", "/", "2.0", "for", "momentum", "in", "end_momentums", ".", "values", "(", ")", "]", ")", ".", "asscalar", "(", ")", "# print init_potential, init_kinetic, end_potential, end_kinetic", "r", "=", "numpy", ".", "random", ".", "rand", "(", "1", ")", "if", "r", "<", "numpy", ".", "exp", "(", "-", "(", "end_potential", "+", "end_kinetic", ")", "+", "(", "init_potential", "+", "init_kinetic", ")", ")", ":", "exe", ".", "copy_params_from", "(", "end_params", ")", "return", "end_params", ",", "1", "else", ":", "exe", ".", "copy_params_from", "(", "init_params", ")", "return", "init_params", ",", "0" ]
Generate the implementation of step HMC
[ "Generate", "the", "implementation", "of", "step", "HMC" ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/example/bayesian-methods/algos.py#L52-L100
train
apache/incubator-mxnet
example/bayesian-methods/algos.py
HMC
def HMC(sym, data_inputs, X, Y, X_test, Y_test, sample_num, initializer=None, noise_precision=1 / 9.0, prior_precision=0.1, learning_rate=1E-6, L=10, dev=mx.gpu()): """Generate the implementation of HMC""" label_key = list(set(data_inputs.keys()) - set(['data']))[0] exe, exe_params, exe_grads, _ = get_executor(sym, dev, data_inputs, initializer) exe.arg_dict['data'][:] = X exe.arg_dict[label_key][:] = Y sample_pool = [] accept_num = 0 start = time.time() for i in range(sample_num): sample_params, is_accept = step_HMC(exe, exe_params, exe_grads, label_key, noise_precision, prior_precision, L, learning_rate) accept_num += is_accept if (i + 1) % 10 == 0: sample_pool.append(sample_params) if (i + 1) % 100000 == 0: end = time.time() print("Current Iter Num: %d" % (i + 1), "Time Spent: %f" % (end - start), "MSE:", sample_test_regression(exe, X=X_test, Y=Y_test, sample_pool=sample_pool, minibatch_size=Y.shape[0], save_path='regression_HMC.txt')) start = time.time() exe.copy_params_from(sample_params) print('accept ratio', accept_num / float(sample_num)) return sample_pool
python
def HMC(sym, data_inputs, X, Y, X_test, Y_test, sample_num, initializer=None, noise_precision=1 / 9.0, prior_precision=0.1, learning_rate=1E-6, L=10, dev=mx.gpu()): """Generate the implementation of HMC""" label_key = list(set(data_inputs.keys()) - set(['data']))[0] exe, exe_params, exe_grads, _ = get_executor(sym, dev, data_inputs, initializer) exe.arg_dict['data'][:] = X exe.arg_dict[label_key][:] = Y sample_pool = [] accept_num = 0 start = time.time() for i in range(sample_num): sample_params, is_accept = step_HMC(exe, exe_params, exe_grads, label_key, noise_precision, prior_precision, L, learning_rate) accept_num += is_accept if (i + 1) % 10 == 0: sample_pool.append(sample_params) if (i + 1) % 100000 == 0: end = time.time() print("Current Iter Num: %d" % (i + 1), "Time Spent: %f" % (end - start), "MSE:", sample_test_regression(exe, X=X_test, Y=Y_test, sample_pool=sample_pool, minibatch_size=Y.shape[0], save_path='regression_HMC.txt')) start = time.time() exe.copy_params_from(sample_params) print('accept ratio', accept_num / float(sample_num)) return sample_pool
[ "def", "HMC", "(", "sym", ",", "data_inputs", ",", "X", ",", "Y", ",", "X_test", ",", "Y_test", ",", "sample_num", ",", "initializer", "=", "None", ",", "noise_precision", "=", "1", "/", "9.0", ",", "prior_precision", "=", "0.1", ",", "learning_rate", "=", "1E-6", ",", "L", "=", "10", ",", "dev", "=", "mx", ".", "gpu", "(", ")", ")", ":", "label_key", "=", "list", "(", "set", "(", "data_inputs", ".", "keys", "(", ")", ")", "-", "set", "(", "[", "'data'", "]", ")", ")", "[", "0", "]", "exe", ",", "exe_params", ",", "exe_grads", ",", "_", "=", "get_executor", "(", "sym", ",", "dev", ",", "data_inputs", ",", "initializer", ")", "exe", ".", "arg_dict", "[", "'data'", "]", "[", ":", "]", "=", "X", "exe", ".", "arg_dict", "[", "label_key", "]", "[", ":", "]", "=", "Y", "sample_pool", "=", "[", "]", "accept_num", "=", "0", "start", "=", "time", ".", "time", "(", ")", "for", "i", "in", "range", "(", "sample_num", ")", ":", "sample_params", ",", "is_accept", "=", "step_HMC", "(", "exe", ",", "exe_params", ",", "exe_grads", ",", "label_key", ",", "noise_precision", ",", "prior_precision", ",", "L", ",", "learning_rate", ")", "accept_num", "+=", "is_accept", "if", "(", "i", "+", "1", ")", "%", "10", "==", "0", ":", "sample_pool", ".", "append", "(", "sample_params", ")", "if", "(", "i", "+", "1", ")", "%", "100000", "==", "0", ":", "end", "=", "time", ".", "time", "(", ")", "print", "(", "\"Current Iter Num: %d\"", "%", "(", "i", "+", "1", ")", ",", "\"Time Spent: %f\"", "%", "(", "end", "-", "start", ")", ",", "\"MSE:\"", ",", "sample_test_regression", "(", "exe", ",", "X", "=", "X_test", ",", "Y", "=", "Y_test", ",", "sample_pool", "=", "sample_pool", ",", "minibatch_size", "=", "Y", ".", "shape", "[", "0", "]", ",", "save_path", "=", "'regression_HMC.txt'", ")", ")", "start", "=", "time", ".", "time", "(", ")", "exe", ".", "copy_params_from", "(", "sample_params", ")", "print", "(", "'accept ratio'", ",", "accept_num", "/", "float", "(", "sample_num", ")", ")", "return", "sample_pool" ]
Generate the implementation of HMC
[ "Generate", "the", "implementation", "of", "HMC" ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/example/bayesian-methods/algos.py#L103-L130
train
apache/incubator-mxnet
example/bayesian-methods/algos.py
SGD
def SGD(sym, data_inputs, X, Y, X_test, Y_test, total_iter_num, lr=None, lr_scheduler=None, prior_precision=1, out_grad_f=None, initializer=None, minibatch_size=100, dev=mx.gpu()): """Generate the implementation of SGD""" if out_grad_f is None: label_key = list(set(data_inputs.keys()) - set(['data']))[0] exe, params, params_grad, _ = get_executor(sym, dev, data_inputs, initializer) optimizer = mx.optimizer.create('sgd', learning_rate=lr, rescale_grad=X.shape[0] / minibatch_size, lr_scheduler=lr_scheduler, wd=prior_precision) updater = mx.optimizer.get_updater(optimizer) start = time.time() for i in range(total_iter_num): indices = numpy.random.randint(X.shape[0], size=minibatch_size) X_batch = X[indices] Y_batch = Y[indices] exe.arg_dict['data'][:] = X_batch if out_grad_f is None: exe.arg_dict[label_key][:] = Y_batch exe.forward(is_train=True) exe.backward() else: exe.forward(is_train=True) exe.backward(out_grad_f(exe.outputs, nd.array(Y_batch, ctx=dev))) for k in params: updater(k, params_grad[k], params[k]) if (i + 1) % 500 == 0: end = time.time() print("Current Iter Num: %d" % (i + 1), "Time Spent: %f" % (end - start)) sample_test_acc(exe, X=X_test, Y=Y_test, label_num=10, minibatch_size=100) start = time.time() return exe, params, params_grad
python
def SGD(sym, data_inputs, X, Y, X_test, Y_test, total_iter_num, lr=None, lr_scheduler=None, prior_precision=1, out_grad_f=None, initializer=None, minibatch_size=100, dev=mx.gpu()): """Generate the implementation of SGD""" if out_grad_f is None: label_key = list(set(data_inputs.keys()) - set(['data']))[0] exe, params, params_grad, _ = get_executor(sym, dev, data_inputs, initializer) optimizer = mx.optimizer.create('sgd', learning_rate=lr, rescale_grad=X.shape[0] / minibatch_size, lr_scheduler=lr_scheduler, wd=prior_precision) updater = mx.optimizer.get_updater(optimizer) start = time.time() for i in range(total_iter_num): indices = numpy.random.randint(X.shape[0], size=minibatch_size) X_batch = X[indices] Y_batch = Y[indices] exe.arg_dict['data'][:] = X_batch if out_grad_f is None: exe.arg_dict[label_key][:] = Y_batch exe.forward(is_train=True) exe.backward() else: exe.forward(is_train=True) exe.backward(out_grad_f(exe.outputs, nd.array(Y_batch, ctx=dev))) for k in params: updater(k, params_grad[k], params[k]) if (i + 1) % 500 == 0: end = time.time() print("Current Iter Num: %d" % (i + 1), "Time Spent: %f" % (end - start)) sample_test_acc(exe, X=X_test, Y=Y_test, label_num=10, minibatch_size=100) start = time.time() return exe, params, params_grad
[ "def", "SGD", "(", "sym", ",", "data_inputs", ",", "X", ",", "Y", ",", "X_test", ",", "Y_test", ",", "total_iter_num", ",", "lr", "=", "None", ",", "lr_scheduler", "=", "None", ",", "prior_precision", "=", "1", ",", "out_grad_f", "=", "None", ",", "initializer", "=", "None", ",", "minibatch_size", "=", "100", ",", "dev", "=", "mx", ".", "gpu", "(", ")", ")", ":", "if", "out_grad_f", "is", "None", ":", "label_key", "=", "list", "(", "set", "(", "data_inputs", ".", "keys", "(", ")", ")", "-", "set", "(", "[", "'data'", "]", ")", ")", "[", "0", "]", "exe", ",", "params", ",", "params_grad", ",", "_", "=", "get_executor", "(", "sym", ",", "dev", ",", "data_inputs", ",", "initializer", ")", "optimizer", "=", "mx", ".", "optimizer", ".", "create", "(", "'sgd'", ",", "learning_rate", "=", "lr", ",", "rescale_grad", "=", "X", ".", "shape", "[", "0", "]", "/", "minibatch_size", ",", "lr_scheduler", "=", "lr_scheduler", ",", "wd", "=", "prior_precision", ")", "updater", "=", "mx", ".", "optimizer", ".", "get_updater", "(", "optimizer", ")", "start", "=", "time", ".", "time", "(", ")", "for", "i", "in", "range", "(", "total_iter_num", ")", ":", "indices", "=", "numpy", ".", "random", ".", "randint", "(", "X", ".", "shape", "[", "0", "]", ",", "size", "=", "minibatch_size", ")", "X_batch", "=", "X", "[", "indices", "]", "Y_batch", "=", "Y", "[", "indices", "]", "exe", ".", "arg_dict", "[", "'data'", "]", "[", ":", "]", "=", "X_batch", "if", "out_grad_f", "is", "None", ":", "exe", ".", "arg_dict", "[", "label_key", "]", "[", ":", "]", "=", "Y_batch", "exe", ".", "forward", "(", "is_train", "=", "True", ")", "exe", ".", "backward", "(", ")", "else", ":", "exe", ".", "forward", "(", "is_train", "=", "True", ")", "exe", ".", "backward", "(", "out_grad_f", "(", "exe", ".", "outputs", ",", "nd", ".", "array", "(", "Y_batch", ",", "ctx", "=", "dev", ")", ")", ")", "for", "k", "in", "params", ":", "updater", "(", "k", ",", "params_grad", "[", "k", "]", ",", "params", "[", "k", "]", ")", "if", "(", "i", "+", "1", ")", "%", "500", "==", "0", ":", "end", "=", "time", ".", "time", "(", ")", "print", "(", "\"Current Iter Num: %d\"", "%", "(", "i", "+", "1", ")", ",", "\"Time Spent: %f\"", "%", "(", "end", "-", "start", ")", ")", "sample_test_acc", "(", "exe", ",", "X", "=", "X_test", ",", "Y", "=", "Y_test", ",", "label_num", "=", "10", ",", "minibatch_size", "=", "100", ")", "start", "=", "time", ".", "time", "(", ")", "return", "exe", ",", "params", ",", "params_grad" ]
Generate the implementation of SGD
[ "Generate", "the", "implementation", "of", "SGD" ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/example/bayesian-methods/algos.py#L133-L168
train
apache/incubator-mxnet
example/bayesian-methods/algos.py
SGLD
def SGLD(sym, X, Y, X_test, Y_test, total_iter_num, data_inputs=None, learning_rate=None, lr_scheduler=None, prior_precision=1, out_grad_f=None, initializer=None, minibatch_size=100, thin_interval=100, burn_in_iter_num=1000, task='classification', dev=mx.gpu()): """Generate the implementation of SGLD""" if out_grad_f is None: label_key = list(set(data_inputs.keys()) - set(['data']))[0] exe, params, params_grad, _ = get_executor(sym, dev, data_inputs, initializer) optimizer = mx.optimizer.create('sgld', learning_rate=learning_rate, rescale_grad=X.shape[0] / minibatch_size, lr_scheduler=lr_scheduler, wd=prior_precision) updater = mx.optimizer.get_updater(optimizer) sample_pool = [] start = time.time() for i in range(total_iter_num): indices = numpy.random.randint(X.shape[0], size=minibatch_size) X_batch = X[indices] Y_batch = Y[indices] exe.arg_dict['data'][:] = X_batch if out_grad_f is None: exe.arg_dict[label_key][:] = Y_batch exe.forward(is_train=True) exe.backward() else: exe.forward(is_train=True) exe.backward(out_grad_f(exe.outputs, nd.array(Y_batch, ctx=dev))) for k in params: updater(k, params_grad[k], params[k]) if i < burn_in_iter_num: continue else: if (i - burn_in_iter_num) % thin_interval == 0: if optimizer.lr_scheduler is not None: lr = optimizer.lr_scheduler(optimizer.num_update) else: lr = learning_rate sample_pool.append([lr, copy_param(exe)]) if (i + 1) % 100000 == 0: end = time.time() if task == 'classification': print("Current Iter Num: %d" % (i + 1), "Time Spent: %f" % (end - start)) test_correct, test_total, test_acc = \ sample_test_acc(exe, sample_pool=sample_pool, X=X_test, Y=Y_test, label_num=10, minibatch_size=minibatch_size) print("Test %d/%d=%f" % (test_correct, test_total, test_acc)) else: print("Current Iter Num: %d" % (i + 1), "Time Spent: %f" % (end - start), "MSE:", sample_test_regression(exe=exe, sample_pool=sample_pool, X=X_test, Y=Y_test, minibatch_size=minibatch_size, save_path='regression_SGLD.txt')) start = time.time() return exe, sample_pool
python
def SGLD(sym, X, Y, X_test, Y_test, total_iter_num, data_inputs=None, learning_rate=None, lr_scheduler=None, prior_precision=1, out_grad_f=None, initializer=None, minibatch_size=100, thin_interval=100, burn_in_iter_num=1000, task='classification', dev=mx.gpu()): """Generate the implementation of SGLD""" if out_grad_f is None: label_key = list(set(data_inputs.keys()) - set(['data']))[0] exe, params, params_grad, _ = get_executor(sym, dev, data_inputs, initializer) optimizer = mx.optimizer.create('sgld', learning_rate=learning_rate, rescale_grad=X.shape[0] / minibatch_size, lr_scheduler=lr_scheduler, wd=prior_precision) updater = mx.optimizer.get_updater(optimizer) sample_pool = [] start = time.time() for i in range(total_iter_num): indices = numpy.random.randint(X.shape[0], size=minibatch_size) X_batch = X[indices] Y_batch = Y[indices] exe.arg_dict['data'][:] = X_batch if out_grad_f is None: exe.arg_dict[label_key][:] = Y_batch exe.forward(is_train=True) exe.backward() else: exe.forward(is_train=True) exe.backward(out_grad_f(exe.outputs, nd.array(Y_batch, ctx=dev))) for k in params: updater(k, params_grad[k], params[k]) if i < burn_in_iter_num: continue else: if (i - burn_in_iter_num) % thin_interval == 0: if optimizer.lr_scheduler is not None: lr = optimizer.lr_scheduler(optimizer.num_update) else: lr = learning_rate sample_pool.append([lr, copy_param(exe)]) if (i + 1) % 100000 == 0: end = time.time() if task == 'classification': print("Current Iter Num: %d" % (i + 1), "Time Spent: %f" % (end - start)) test_correct, test_total, test_acc = \ sample_test_acc(exe, sample_pool=sample_pool, X=X_test, Y=Y_test, label_num=10, minibatch_size=minibatch_size) print("Test %d/%d=%f" % (test_correct, test_total, test_acc)) else: print("Current Iter Num: %d" % (i + 1), "Time Spent: %f" % (end - start), "MSE:", sample_test_regression(exe=exe, sample_pool=sample_pool, X=X_test, Y=Y_test, minibatch_size=minibatch_size, save_path='regression_SGLD.txt')) start = time.time() return exe, sample_pool
[ "def", "SGLD", "(", "sym", ",", "X", ",", "Y", ",", "X_test", ",", "Y_test", ",", "total_iter_num", ",", "data_inputs", "=", "None", ",", "learning_rate", "=", "None", ",", "lr_scheduler", "=", "None", ",", "prior_precision", "=", "1", ",", "out_grad_f", "=", "None", ",", "initializer", "=", "None", ",", "minibatch_size", "=", "100", ",", "thin_interval", "=", "100", ",", "burn_in_iter_num", "=", "1000", ",", "task", "=", "'classification'", ",", "dev", "=", "mx", ".", "gpu", "(", ")", ")", ":", "if", "out_grad_f", "is", "None", ":", "label_key", "=", "list", "(", "set", "(", "data_inputs", ".", "keys", "(", ")", ")", "-", "set", "(", "[", "'data'", "]", ")", ")", "[", "0", "]", "exe", ",", "params", ",", "params_grad", ",", "_", "=", "get_executor", "(", "sym", ",", "dev", ",", "data_inputs", ",", "initializer", ")", "optimizer", "=", "mx", ".", "optimizer", ".", "create", "(", "'sgld'", ",", "learning_rate", "=", "learning_rate", ",", "rescale_grad", "=", "X", ".", "shape", "[", "0", "]", "/", "minibatch_size", ",", "lr_scheduler", "=", "lr_scheduler", ",", "wd", "=", "prior_precision", ")", "updater", "=", "mx", ".", "optimizer", ".", "get_updater", "(", "optimizer", ")", "sample_pool", "=", "[", "]", "start", "=", "time", ".", "time", "(", ")", "for", "i", "in", "range", "(", "total_iter_num", ")", ":", "indices", "=", "numpy", ".", "random", ".", "randint", "(", "X", ".", "shape", "[", "0", "]", ",", "size", "=", "minibatch_size", ")", "X_batch", "=", "X", "[", "indices", "]", "Y_batch", "=", "Y", "[", "indices", "]", "exe", ".", "arg_dict", "[", "'data'", "]", "[", ":", "]", "=", "X_batch", "if", "out_grad_f", "is", "None", ":", "exe", ".", "arg_dict", "[", "label_key", "]", "[", ":", "]", "=", "Y_batch", "exe", ".", "forward", "(", "is_train", "=", "True", ")", "exe", ".", "backward", "(", ")", "else", ":", "exe", ".", "forward", "(", "is_train", "=", "True", ")", "exe", ".", "backward", "(", "out_grad_f", "(", "exe", ".", "outputs", ",", "nd", ".", "array", "(", "Y_batch", ",", "ctx", "=", "dev", ")", ")", ")", "for", "k", "in", "params", ":", "updater", "(", "k", ",", "params_grad", "[", "k", "]", ",", "params", "[", "k", "]", ")", "if", "i", "<", "burn_in_iter_num", ":", "continue", "else", ":", "if", "(", "i", "-", "burn_in_iter_num", ")", "%", "thin_interval", "==", "0", ":", "if", "optimizer", ".", "lr_scheduler", "is", "not", "None", ":", "lr", "=", "optimizer", ".", "lr_scheduler", "(", "optimizer", ".", "num_update", ")", "else", ":", "lr", "=", "learning_rate", "sample_pool", ".", "append", "(", "[", "lr", ",", "copy_param", "(", "exe", ")", "]", ")", "if", "(", "i", "+", "1", ")", "%", "100000", "==", "0", ":", "end", "=", "time", ".", "time", "(", ")", "if", "task", "==", "'classification'", ":", "print", "(", "\"Current Iter Num: %d\"", "%", "(", "i", "+", "1", ")", ",", "\"Time Spent: %f\"", "%", "(", "end", "-", "start", ")", ")", "test_correct", ",", "test_total", ",", "test_acc", "=", "sample_test_acc", "(", "exe", ",", "sample_pool", "=", "sample_pool", ",", "X", "=", "X_test", ",", "Y", "=", "Y_test", ",", "label_num", "=", "10", ",", "minibatch_size", "=", "minibatch_size", ")", "print", "(", "\"Test %d/%d=%f\"", "%", "(", "test_correct", ",", "test_total", ",", "test_acc", ")", ")", "else", ":", "print", "(", "\"Current Iter Num: %d\"", "%", "(", "i", "+", "1", ")", ",", "\"Time Spent: %f\"", "%", "(", "end", "-", "start", ")", ",", "\"MSE:\"", ",", "sample_test_regression", "(", "exe", "=", "exe", ",", "sample_pool", "=", "sample_pool", ",", "X", "=", "X_test", ",", "Y", "=", "Y_test", ",", "minibatch_size", "=", "minibatch_size", ",", "save_path", "=", "'regression_SGLD.txt'", ")", ")", "start", "=", "time", ".", "time", "(", ")", "return", "exe", ",", "sample_pool" ]
Generate the implementation of SGLD
[ "Generate", "the", "implementation", "of", "SGLD" ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/example/bayesian-methods/algos.py#L171-L228
train
apache/incubator-mxnet
example/bayesian-methods/algos.py
DistilledSGLD
def DistilledSGLD(teacher_sym, student_sym, teacher_data_inputs, student_data_inputs, X, Y, X_test, Y_test, total_iter_num, teacher_learning_rate, student_learning_rate, teacher_lr_scheduler=None, student_lr_scheduler=None, student_optimizing_algorithm='sgd', teacher_grad_f=None, student_grad_f=None, teacher_prior_precision=1, student_prior_precision=0.001, perturb_deviation=0.001, student_initializer=None, teacher_initializer=None, minibatch_size=100, task='classification', dev=mx.gpu()): """Generate the implementation of DistilledSGLD""" teacher_exe, teacher_params, teacher_params_grad, _ = \ get_executor(teacher_sym, dev, teacher_data_inputs, teacher_initializer) student_exe, student_params, student_params_grad, _ = \ get_executor(student_sym, dev, student_data_inputs, student_initializer) if teacher_grad_f is None: teacher_label_key = list(set(teacher_data_inputs.keys()) - set(['data']))[0] if student_grad_f is None: student_label_key = list(set(student_data_inputs.keys()) - set(['data']))[0] teacher_optimizer = mx.optimizer.create('sgld', learning_rate=teacher_learning_rate, rescale_grad=X.shape[0] / float(minibatch_size), lr_scheduler=teacher_lr_scheduler, wd=teacher_prior_precision) student_optimizer = mx.optimizer.create(student_optimizing_algorithm, learning_rate=student_learning_rate, rescale_grad=1.0 / float(minibatch_size), lr_scheduler=student_lr_scheduler, wd=student_prior_precision) teacher_updater = mx.optimizer.get_updater(teacher_optimizer) student_updater = mx.optimizer.get_updater(student_optimizer) start = time.time() for i in range(total_iter_num): # 1.1 Draw random minibatch indices = numpy.random.randint(X.shape[0], size=minibatch_size) X_batch = X[indices] Y_batch = Y[indices] # 1.2 Update teacher teacher_exe.arg_dict['data'][:] = X_batch if teacher_grad_f is None: teacher_exe.arg_dict[teacher_label_key][:] = Y_batch teacher_exe.forward(is_train=True) teacher_exe.backward() else: teacher_exe.forward(is_train=True) teacher_exe.backward( teacher_grad_f(teacher_exe.outputs, nd.array(Y_batch, ctx=dev))) for k in teacher_params: teacher_updater(k, teacher_params_grad[k], teacher_params[k]) # 2.1 Draw random minibatch and do random perturbation if task == 'classification': indices = numpy.random.randint(X.shape[0], size=minibatch_size) X_student_batch = X[indices] + numpy.random.normal(0, perturb_deviation, X_batch.shape).astype('float32') else: X_student_batch = mx.random.uniform(-6, 6, X_batch.shape, mx.cpu()) # 2.2 Get teacher predictions teacher_exe.arg_dict['data'][:] = X_student_batch teacher_exe.forward(is_train=False) teacher_pred = teacher_exe.outputs[0] teacher_pred.wait_to_read() # 2.3 Update student student_exe.arg_dict['data'][:] = X_student_batch if student_grad_f is None: student_exe.arg_dict[student_label_key][:] = teacher_pred student_exe.forward(is_train=True) student_exe.backward() else: student_exe.forward(is_train=True) student_exe.backward(student_grad_f(student_exe.outputs, teacher_pred)) for k in student_params: student_updater(k, student_params_grad[k], student_params[k]) if (i + 1) % 2000 == 0: end = time.time() if task == 'classification': print("Current Iter Num: %d" % (i + 1), "Time Spent: %f" % (end - start)) test_correct, test_total, test_acc = \ sample_test_acc(student_exe, X=X_test, Y=Y_test, label_num=10, minibatch_size=minibatch_size) train_correct, train_total, train_acc = \ sample_test_acc(student_exe, X=X, Y=Y, label_num=10, minibatch_size=minibatch_size) teacher_test_correct, teacher_test_total, teacher_test_acc = \ sample_test_acc(teacher_exe, X=X_test, Y=Y_test, label_num=10, minibatch_size=minibatch_size) teacher_train_correct, teacher_train_total, teacher_train_acc = \ sample_test_acc(teacher_exe, X=X, Y=Y, label_num=10, minibatch_size=minibatch_size) print("Student: Test ACC %d/%d=%f, Train ACC %d/%d=%f" % (test_correct, test_total, test_acc, train_correct, train_total, train_acc)) print("Teacher: Test ACC %d/%d=%f, Train ACC %d/%d=%f" \ % (teacher_test_correct, teacher_test_total, teacher_test_acc, teacher_train_correct, teacher_train_total, teacher_train_acc)) else: print("Current Iter Num: %d" % (i + 1), "Time Spent: %f" % (end - start), "MSE:", sample_test_regression(exe=student_exe, X=X_test, Y=Y_test, minibatch_size=minibatch_size, save_path='regression_DSGLD.txt')) start = time.time() return student_exe, student_params, student_params_grad
python
def DistilledSGLD(teacher_sym, student_sym, teacher_data_inputs, student_data_inputs, X, Y, X_test, Y_test, total_iter_num, teacher_learning_rate, student_learning_rate, teacher_lr_scheduler=None, student_lr_scheduler=None, student_optimizing_algorithm='sgd', teacher_grad_f=None, student_grad_f=None, teacher_prior_precision=1, student_prior_precision=0.001, perturb_deviation=0.001, student_initializer=None, teacher_initializer=None, minibatch_size=100, task='classification', dev=mx.gpu()): """Generate the implementation of DistilledSGLD""" teacher_exe, teacher_params, teacher_params_grad, _ = \ get_executor(teacher_sym, dev, teacher_data_inputs, teacher_initializer) student_exe, student_params, student_params_grad, _ = \ get_executor(student_sym, dev, student_data_inputs, student_initializer) if teacher_grad_f is None: teacher_label_key = list(set(teacher_data_inputs.keys()) - set(['data']))[0] if student_grad_f is None: student_label_key = list(set(student_data_inputs.keys()) - set(['data']))[0] teacher_optimizer = mx.optimizer.create('sgld', learning_rate=teacher_learning_rate, rescale_grad=X.shape[0] / float(minibatch_size), lr_scheduler=teacher_lr_scheduler, wd=teacher_prior_precision) student_optimizer = mx.optimizer.create(student_optimizing_algorithm, learning_rate=student_learning_rate, rescale_grad=1.0 / float(minibatch_size), lr_scheduler=student_lr_scheduler, wd=student_prior_precision) teacher_updater = mx.optimizer.get_updater(teacher_optimizer) student_updater = mx.optimizer.get_updater(student_optimizer) start = time.time() for i in range(total_iter_num): # 1.1 Draw random minibatch indices = numpy.random.randint(X.shape[0], size=minibatch_size) X_batch = X[indices] Y_batch = Y[indices] # 1.2 Update teacher teacher_exe.arg_dict['data'][:] = X_batch if teacher_grad_f is None: teacher_exe.arg_dict[teacher_label_key][:] = Y_batch teacher_exe.forward(is_train=True) teacher_exe.backward() else: teacher_exe.forward(is_train=True) teacher_exe.backward( teacher_grad_f(teacher_exe.outputs, nd.array(Y_batch, ctx=dev))) for k in teacher_params: teacher_updater(k, teacher_params_grad[k], teacher_params[k]) # 2.1 Draw random minibatch and do random perturbation if task == 'classification': indices = numpy.random.randint(X.shape[0], size=minibatch_size) X_student_batch = X[indices] + numpy.random.normal(0, perturb_deviation, X_batch.shape).astype('float32') else: X_student_batch = mx.random.uniform(-6, 6, X_batch.shape, mx.cpu()) # 2.2 Get teacher predictions teacher_exe.arg_dict['data'][:] = X_student_batch teacher_exe.forward(is_train=False) teacher_pred = teacher_exe.outputs[0] teacher_pred.wait_to_read() # 2.3 Update student student_exe.arg_dict['data'][:] = X_student_batch if student_grad_f is None: student_exe.arg_dict[student_label_key][:] = teacher_pred student_exe.forward(is_train=True) student_exe.backward() else: student_exe.forward(is_train=True) student_exe.backward(student_grad_f(student_exe.outputs, teacher_pred)) for k in student_params: student_updater(k, student_params_grad[k], student_params[k]) if (i + 1) % 2000 == 0: end = time.time() if task == 'classification': print("Current Iter Num: %d" % (i + 1), "Time Spent: %f" % (end - start)) test_correct, test_total, test_acc = \ sample_test_acc(student_exe, X=X_test, Y=Y_test, label_num=10, minibatch_size=minibatch_size) train_correct, train_total, train_acc = \ sample_test_acc(student_exe, X=X, Y=Y, label_num=10, minibatch_size=minibatch_size) teacher_test_correct, teacher_test_total, teacher_test_acc = \ sample_test_acc(teacher_exe, X=X_test, Y=Y_test, label_num=10, minibatch_size=minibatch_size) teacher_train_correct, teacher_train_total, teacher_train_acc = \ sample_test_acc(teacher_exe, X=X, Y=Y, label_num=10, minibatch_size=minibatch_size) print("Student: Test ACC %d/%d=%f, Train ACC %d/%d=%f" % (test_correct, test_total, test_acc, train_correct, train_total, train_acc)) print("Teacher: Test ACC %d/%d=%f, Train ACC %d/%d=%f" \ % (teacher_test_correct, teacher_test_total, teacher_test_acc, teacher_train_correct, teacher_train_total, teacher_train_acc)) else: print("Current Iter Num: %d" % (i + 1), "Time Spent: %f" % (end - start), "MSE:", sample_test_regression(exe=student_exe, X=X_test, Y=Y_test, minibatch_size=minibatch_size, save_path='regression_DSGLD.txt')) start = time.time() return student_exe, student_params, student_params_grad
[ "def", "DistilledSGLD", "(", "teacher_sym", ",", "student_sym", ",", "teacher_data_inputs", ",", "student_data_inputs", ",", "X", ",", "Y", ",", "X_test", ",", "Y_test", ",", "total_iter_num", ",", "teacher_learning_rate", ",", "student_learning_rate", ",", "teacher_lr_scheduler", "=", "None", ",", "student_lr_scheduler", "=", "None", ",", "student_optimizing_algorithm", "=", "'sgd'", ",", "teacher_grad_f", "=", "None", ",", "student_grad_f", "=", "None", ",", "teacher_prior_precision", "=", "1", ",", "student_prior_precision", "=", "0.001", ",", "perturb_deviation", "=", "0.001", ",", "student_initializer", "=", "None", ",", "teacher_initializer", "=", "None", ",", "minibatch_size", "=", "100", ",", "task", "=", "'classification'", ",", "dev", "=", "mx", ".", "gpu", "(", ")", ")", ":", "teacher_exe", ",", "teacher_params", ",", "teacher_params_grad", ",", "_", "=", "get_executor", "(", "teacher_sym", ",", "dev", ",", "teacher_data_inputs", ",", "teacher_initializer", ")", "student_exe", ",", "student_params", ",", "student_params_grad", ",", "_", "=", "get_executor", "(", "student_sym", ",", "dev", ",", "student_data_inputs", ",", "student_initializer", ")", "if", "teacher_grad_f", "is", "None", ":", "teacher_label_key", "=", "list", "(", "set", "(", "teacher_data_inputs", ".", "keys", "(", ")", ")", "-", "set", "(", "[", "'data'", "]", ")", ")", "[", "0", "]", "if", "student_grad_f", "is", "None", ":", "student_label_key", "=", "list", "(", "set", "(", "student_data_inputs", ".", "keys", "(", ")", ")", "-", "set", "(", "[", "'data'", "]", ")", ")", "[", "0", "]", "teacher_optimizer", "=", "mx", ".", "optimizer", ".", "create", "(", "'sgld'", ",", "learning_rate", "=", "teacher_learning_rate", ",", "rescale_grad", "=", "X", ".", "shape", "[", "0", "]", "/", "float", "(", "minibatch_size", ")", ",", "lr_scheduler", "=", "teacher_lr_scheduler", ",", "wd", "=", "teacher_prior_precision", ")", "student_optimizer", "=", "mx", ".", "optimizer", ".", "create", "(", "student_optimizing_algorithm", ",", "learning_rate", "=", "student_learning_rate", ",", "rescale_grad", "=", "1.0", "/", "float", "(", "minibatch_size", ")", ",", "lr_scheduler", "=", "student_lr_scheduler", ",", "wd", "=", "student_prior_precision", ")", "teacher_updater", "=", "mx", ".", "optimizer", ".", "get_updater", "(", "teacher_optimizer", ")", "student_updater", "=", "mx", ".", "optimizer", ".", "get_updater", "(", "student_optimizer", ")", "start", "=", "time", ".", "time", "(", ")", "for", "i", "in", "range", "(", "total_iter_num", ")", ":", "# 1.1 Draw random minibatch", "indices", "=", "numpy", ".", "random", ".", "randint", "(", "X", ".", "shape", "[", "0", "]", ",", "size", "=", "minibatch_size", ")", "X_batch", "=", "X", "[", "indices", "]", "Y_batch", "=", "Y", "[", "indices", "]", "# 1.2 Update teacher", "teacher_exe", ".", "arg_dict", "[", "'data'", "]", "[", ":", "]", "=", "X_batch", "if", "teacher_grad_f", "is", "None", ":", "teacher_exe", ".", "arg_dict", "[", "teacher_label_key", "]", "[", ":", "]", "=", "Y_batch", "teacher_exe", ".", "forward", "(", "is_train", "=", "True", ")", "teacher_exe", ".", "backward", "(", ")", "else", ":", "teacher_exe", ".", "forward", "(", "is_train", "=", "True", ")", "teacher_exe", ".", "backward", "(", "teacher_grad_f", "(", "teacher_exe", ".", "outputs", ",", "nd", ".", "array", "(", "Y_batch", ",", "ctx", "=", "dev", ")", ")", ")", "for", "k", "in", "teacher_params", ":", "teacher_updater", "(", "k", ",", "teacher_params_grad", "[", "k", "]", ",", "teacher_params", "[", "k", "]", ")", "# 2.1 Draw random minibatch and do random perturbation", "if", "task", "==", "'classification'", ":", "indices", "=", "numpy", ".", "random", ".", "randint", "(", "X", ".", "shape", "[", "0", "]", ",", "size", "=", "minibatch_size", ")", "X_student_batch", "=", "X", "[", "indices", "]", "+", "numpy", ".", "random", ".", "normal", "(", "0", ",", "perturb_deviation", ",", "X_batch", ".", "shape", ")", ".", "astype", "(", "'float32'", ")", "else", ":", "X_student_batch", "=", "mx", ".", "random", ".", "uniform", "(", "-", "6", ",", "6", ",", "X_batch", ".", "shape", ",", "mx", ".", "cpu", "(", ")", ")", "# 2.2 Get teacher predictions", "teacher_exe", ".", "arg_dict", "[", "'data'", "]", "[", ":", "]", "=", "X_student_batch", "teacher_exe", ".", "forward", "(", "is_train", "=", "False", ")", "teacher_pred", "=", "teacher_exe", ".", "outputs", "[", "0", "]", "teacher_pred", ".", "wait_to_read", "(", ")", "# 2.3 Update student", "student_exe", ".", "arg_dict", "[", "'data'", "]", "[", ":", "]", "=", "X_student_batch", "if", "student_grad_f", "is", "None", ":", "student_exe", ".", "arg_dict", "[", "student_label_key", "]", "[", ":", "]", "=", "teacher_pred", "student_exe", ".", "forward", "(", "is_train", "=", "True", ")", "student_exe", ".", "backward", "(", ")", "else", ":", "student_exe", ".", "forward", "(", "is_train", "=", "True", ")", "student_exe", ".", "backward", "(", "student_grad_f", "(", "student_exe", ".", "outputs", ",", "teacher_pred", ")", ")", "for", "k", "in", "student_params", ":", "student_updater", "(", "k", ",", "student_params_grad", "[", "k", "]", ",", "student_params", "[", "k", "]", ")", "if", "(", "i", "+", "1", ")", "%", "2000", "==", "0", ":", "end", "=", "time", ".", "time", "(", ")", "if", "task", "==", "'classification'", ":", "print", "(", "\"Current Iter Num: %d\"", "%", "(", "i", "+", "1", ")", ",", "\"Time Spent: %f\"", "%", "(", "end", "-", "start", ")", ")", "test_correct", ",", "test_total", ",", "test_acc", "=", "sample_test_acc", "(", "student_exe", ",", "X", "=", "X_test", ",", "Y", "=", "Y_test", ",", "label_num", "=", "10", ",", "minibatch_size", "=", "minibatch_size", ")", "train_correct", ",", "train_total", ",", "train_acc", "=", "sample_test_acc", "(", "student_exe", ",", "X", "=", "X", ",", "Y", "=", "Y", ",", "label_num", "=", "10", ",", "minibatch_size", "=", "minibatch_size", ")", "teacher_test_correct", ",", "teacher_test_total", ",", "teacher_test_acc", "=", "sample_test_acc", "(", "teacher_exe", ",", "X", "=", "X_test", ",", "Y", "=", "Y_test", ",", "label_num", "=", "10", ",", "minibatch_size", "=", "minibatch_size", ")", "teacher_train_correct", ",", "teacher_train_total", ",", "teacher_train_acc", "=", "sample_test_acc", "(", "teacher_exe", ",", "X", "=", "X", ",", "Y", "=", "Y", ",", "label_num", "=", "10", ",", "minibatch_size", "=", "minibatch_size", ")", "print", "(", "\"Student: Test ACC %d/%d=%f, Train ACC %d/%d=%f\"", "%", "(", "test_correct", ",", "test_total", ",", "test_acc", ",", "train_correct", ",", "train_total", ",", "train_acc", ")", ")", "print", "(", "\"Teacher: Test ACC %d/%d=%f, Train ACC %d/%d=%f\"", "%", "(", "teacher_test_correct", ",", "teacher_test_total", ",", "teacher_test_acc", ",", "teacher_train_correct", ",", "teacher_train_total", ",", "teacher_train_acc", ")", ")", "else", ":", "print", "(", "\"Current Iter Num: %d\"", "%", "(", "i", "+", "1", ")", ",", "\"Time Spent: %f\"", "%", "(", "end", "-", "start", ")", ",", "\"MSE:\"", ",", "sample_test_regression", "(", "exe", "=", "student_exe", ",", "X", "=", "X_test", ",", "Y", "=", "Y_test", ",", "minibatch_size", "=", "minibatch_size", ",", "save_path", "=", "'regression_DSGLD.txt'", ")", ")", "start", "=", "time", ".", "time", "(", ")", "return", "student_exe", ",", "student_params", ",", "student_params_grad" ]
Generate the implementation of DistilledSGLD
[ "Generate", "the", "implementation", "of", "DistilledSGLD" ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/example/bayesian-methods/algos.py#L231-L343
train
apache/incubator-mxnet
ci/build.py
get_platforms
def get_platforms(path: str = get_dockerfiles_path()) -> List[str]: """Get a list of architectures given our dockerfiles""" dockerfiles = glob.glob(os.path.join(path, "Dockerfile.*")) dockerfiles = list(filter(lambda x: x[-1] != '~', dockerfiles)) files = list(map(lambda x: re.sub(r"Dockerfile.(.*)", r"\1", x), dockerfiles)) platforms = list(map(lambda x: os.path.split(x)[1], sorted(files))) return platforms
python
def get_platforms(path: str = get_dockerfiles_path()) -> List[str]: """Get a list of architectures given our dockerfiles""" dockerfiles = glob.glob(os.path.join(path, "Dockerfile.*")) dockerfiles = list(filter(lambda x: x[-1] != '~', dockerfiles)) files = list(map(lambda x: re.sub(r"Dockerfile.(.*)", r"\1", x), dockerfiles)) platforms = list(map(lambda x: os.path.split(x)[1], sorted(files))) return platforms
[ "def", "get_platforms", "(", "path", ":", "str", "=", "get_dockerfiles_path", "(", ")", ")", "->", "List", "[", "str", "]", ":", "dockerfiles", "=", "glob", ".", "glob", "(", "os", ".", "path", ".", "join", "(", "path", ",", "\"Dockerfile.*\"", ")", ")", "dockerfiles", "=", "list", "(", "filter", "(", "lambda", "x", ":", "x", "[", "-", "1", "]", "!=", "'~'", ",", "dockerfiles", ")", ")", "files", "=", "list", "(", "map", "(", "lambda", "x", ":", "re", ".", "sub", "(", "r\"Dockerfile.(.*)\"", ",", "r\"\\1\"", ",", "x", ")", ",", "dockerfiles", ")", ")", "platforms", "=", "list", "(", "map", "(", "lambda", "x", ":", "os", ".", "path", ".", "split", "(", "x", ")", "[", "1", "]", ",", "sorted", "(", "files", ")", ")", ")", "return", "platforms" ]
Get a list of architectures given our dockerfiles
[ "Get", "a", "list", "of", "architectures", "given", "our", "dockerfiles" ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/ci/build.py#L93-L99
train
apache/incubator-mxnet
ci/build.py
get_docker_tag
def get_docker_tag(platform: str, registry: str) -> str: """:return: docker tag to be used for the container""" platform = platform if any(x in platform for x in ['build.', 'publish.']) else 'build.{}'.format(platform) if not registry: registry = "mxnet_local" return "{0}/{1}".format(registry, platform)
python
def get_docker_tag(platform: str, registry: str) -> str: """:return: docker tag to be used for the container""" platform = platform if any(x in platform for x in ['build.', 'publish.']) else 'build.{}'.format(platform) if not registry: registry = "mxnet_local" return "{0}/{1}".format(registry, platform)
[ "def", "get_docker_tag", "(", "platform", ":", "str", ",", "registry", ":", "str", ")", "->", "str", ":", "platform", "=", "platform", "if", "any", "(", "x", "in", "platform", "for", "x", "in", "[", "'build.'", ",", "'publish.'", "]", ")", "else", "'build.{}'", ".", "format", "(", "platform", ")", "if", "not", "registry", ":", "registry", "=", "\"mxnet_local\"", "return", "\"{0}/{1}\"", ".", "format", "(", "registry", ",", "platform", ")" ]
:return: docker tag to be used for the container
[ ":", "return", ":", "docker", "tag", "to", "be", "used", "for", "the", "container" ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/ci/build.py#L102-L107
train
apache/incubator-mxnet
ci/build.py
build_docker
def build_docker(platform: str, docker_binary: str, registry: str, num_retries: int, no_cache: bool) -> str: """ Build a container for the given platform :param platform: Platform :param docker_binary: docker binary to use (docker/nvidia-docker) :param registry: Dockerhub registry name :param num_retries: Number of retries to build the docker image :param no_cache: pass no-cache to docker to rebuild the images :return: Id of the top level image """ tag = get_docker_tag(platform=platform, registry=registry) logging.info("Building docker container tagged '%s' with %s", tag, docker_binary) # # We add a user with the same group as the executing non-root user so files created in the # container match permissions of the local user. Same for the group. # # These variables are used in the docker files to create user and group with these ids. # see: docker/install/ubuntu_adduser.sh # # cache-from is needed so we use the cached images tagged from the remote via # docker pull see: docker_cache.load_docker_cache # # This also prevents using local layers for caching: https://github.com/moby/moby/issues/33002 # So to use local caching, we should omit the cache-from by using --no-dockerhub-cache argument to this # script. # # This doesn't work with multi head docker files. # cmd = [docker_binary, "build", "-f", get_dockerfile(platform), "--build-arg", "USER_ID={}".format(os.getuid()), "--build-arg", "GROUP_ID={}".format(os.getgid())] if no_cache: cmd.append("--no-cache") elif registry: cmd.extend(["--cache-from", tag]) cmd.extend(["-t", tag, get_dockerfiles_path()]) @retry(subprocess.CalledProcessError, tries=num_retries) def run_cmd(): logging.info("Running command: '%s'", ' '.join(cmd)) check_call(cmd) run_cmd() # Get image id by reading the tag. It's guaranteed (except race condition) that the tag exists. Otherwise, the # check_call would have failed image_id = _get_local_image_id(docker_binary=docker_binary, docker_tag=tag) if not image_id: raise FileNotFoundError('Unable to find docker image id matching with {}'.format(tag)) return image_id
python
def build_docker(platform: str, docker_binary: str, registry: str, num_retries: int, no_cache: bool) -> str: """ Build a container for the given platform :param platform: Platform :param docker_binary: docker binary to use (docker/nvidia-docker) :param registry: Dockerhub registry name :param num_retries: Number of retries to build the docker image :param no_cache: pass no-cache to docker to rebuild the images :return: Id of the top level image """ tag = get_docker_tag(platform=platform, registry=registry) logging.info("Building docker container tagged '%s' with %s", tag, docker_binary) # # We add a user with the same group as the executing non-root user so files created in the # container match permissions of the local user. Same for the group. # # These variables are used in the docker files to create user and group with these ids. # see: docker/install/ubuntu_adduser.sh # # cache-from is needed so we use the cached images tagged from the remote via # docker pull see: docker_cache.load_docker_cache # # This also prevents using local layers for caching: https://github.com/moby/moby/issues/33002 # So to use local caching, we should omit the cache-from by using --no-dockerhub-cache argument to this # script. # # This doesn't work with multi head docker files. # cmd = [docker_binary, "build", "-f", get_dockerfile(platform), "--build-arg", "USER_ID={}".format(os.getuid()), "--build-arg", "GROUP_ID={}".format(os.getgid())] if no_cache: cmd.append("--no-cache") elif registry: cmd.extend(["--cache-from", tag]) cmd.extend(["-t", tag, get_dockerfiles_path()]) @retry(subprocess.CalledProcessError, tries=num_retries) def run_cmd(): logging.info("Running command: '%s'", ' '.join(cmd)) check_call(cmd) run_cmd() # Get image id by reading the tag. It's guaranteed (except race condition) that the tag exists. Otherwise, the # check_call would have failed image_id = _get_local_image_id(docker_binary=docker_binary, docker_tag=tag) if not image_id: raise FileNotFoundError('Unable to find docker image id matching with {}'.format(tag)) return image_id
[ "def", "build_docker", "(", "platform", ":", "str", ",", "docker_binary", ":", "str", ",", "registry", ":", "str", ",", "num_retries", ":", "int", ",", "no_cache", ":", "bool", ")", "->", "str", ":", "tag", "=", "get_docker_tag", "(", "platform", "=", "platform", ",", "registry", "=", "registry", ")", "logging", ".", "info", "(", "\"Building docker container tagged '%s' with %s\"", ",", "tag", ",", "docker_binary", ")", "#", "# We add a user with the same group as the executing non-root user so files created in the", "# container match permissions of the local user. Same for the group.", "#", "# These variables are used in the docker files to create user and group with these ids.", "# see: docker/install/ubuntu_adduser.sh", "#", "# cache-from is needed so we use the cached images tagged from the remote via", "# docker pull see: docker_cache.load_docker_cache", "#", "# This also prevents using local layers for caching: https://github.com/moby/moby/issues/33002", "# So to use local caching, we should omit the cache-from by using --no-dockerhub-cache argument to this", "# script.", "#", "# This doesn't work with multi head docker files.", "#", "cmd", "=", "[", "docker_binary", ",", "\"build\"", ",", "\"-f\"", ",", "get_dockerfile", "(", "platform", ")", ",", "\"--build-arg\"", ",", "\"USER_ID={}\"", ".", "format", "(", "os", ".", "getuid", "(", ")", ")", ",", "\"--build-arg\"", ",", "\"GROUP_ID={}\"", ".", "format", "(", "os", ".", "getgid", "(", ")", ")", "]", "if", "no_cache", ":", "cmd", ".", "append", "(", "\"--no-cache\"", ")", "elif", "registry", ":", "cmd", ".", "extend", "(", "[", "\"--cache-from\"", ",", "tag", "]", ")", "cmd", ".", "extend", "(", "[", "\"-t\"", ",", "tag", ",", "get_dockerfiles_path", "(", ")", "]", ")", "@", "retry", "(", "subprocess", ".", "CalledProcessError", ",", "tries", "=", "num_retries", ")", "def", "run_cmd", "(", ")", ":", "logging", ".", "info", "(", "\"Running command: '%s'\"", ",", "' '", ".", "join", "(", "cmd", ")", ")", "check_call", "(", "cmd", ")", "run_cmd", "(", ")", "# Get image id by reading the tag. It's guaranteed (except race condition) that the tag exists. Otherwise, the", "# check_call would have failed", "image_id", "=", "_get_local_image_id", "(", "docker_binary", "=", "docker_binary", ",", "docker_tag", "=", "tag", ")", "if", "not", "image_id", ":", "raise", "FileNotFoundError", "(", "'Unable to find docker image id matching with {}'", ".", "format", "(", "tag", ")", ")", "return", "image_id" ]
Build a container for the given platform :param platform: Platform :param docker_binary: docker binary to use (docker/nvidia-docker) :param registry: Dockerhub registry name :param num_retries: Number of retries to build the docker image :param no_cache: pass no-cache to docker to rebuild the images :return: Id of the top level image
[ "Build", "a", "container", "for", "the", "given", "platform", ":", "param", "platform", ":", "Platform", ":", "param", "docker_binary", ":", "docker", "binary", "to", "use", "(", "docker", "/", "nvidia", "-", "docker", ")", ":", "param", "registry", ":", "Dockerhub", "registry", "name", ":", "param", "num_retries", ":", "Number", "of", "retries", "to", "build", "the", "docker", "image", ":", "param", "no_cache", ":", "pass", "no", "-", "cache", "to", "docker", "to", "rebuild", "the", "images", ":", "return", ":", "Id", "of", "the", "top", "level", "image" ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/ci/build.py#L119-L168
train
apache/incubator-mxnet
ci/build.py
_get_local_image_id
def _get_local_image_id(docker_binary, docker_tag): """ Get the image id of the local docker layer with the passed tag :param docker_tag: docker tag :return: Image id as string or None if tag does not exist """ cmd = [docker_binary, "images", "-q", docker_tag] image_id_b = check_output(cmd) image_id = image_id_b.decode('utf-8').strip() if not image_id: raise RuntimeError('Unable to find docker image id matching with tag {}'.format(docker_tag)) return image_id
python
def _get_local_image_id(docker_binary, docker_tag): """ Get the image id of the local docker layer with the passed tag :param docker_tag: docker tag :return: Image id as string or None if tag does not exist """ cmd = [docker_binary, "images", "-q", docker_tag] image_id_b = check_output(cmd) image_id = image_id_b.decode('utf-8').strip() if not image_id: raise RuntimeError('Unable to find docker image id matching with tag {}'.format(docker_tag)) return image_id
[ "def", "_get_local_image_id", "(", "docker_binary", ",", "docker_tag", ")", ":", "cmd", "=", "[", "docker_binary", ",", "\"images\"", ",", "\"-q\"", ",", "docker_tag", "]", "image_id_b", "=", "check_output", "(", "cmd", ")", "image_id", "=", "image_id_b", ".", "decode", "(", "'utf-8'", ")", ".", "strip", "(", ")", "if", "not", "image_id", ":", "raise", "RuntimeError", "(", "'Unable to find docker image id matching with tag {}'", ".", "format", "(", "docker_tag", ")", ")", "return", "image_id" ]
Get the image id of the local docker layer with the passed tag :param docker_tag: docker tag :return: Image id as string or None if tag does not exist
[ "Get", "the", "image", "id", "of", "the", "local", "docker", "layer", "with", "the", "passed", "tag", ":", "param", "docker_tag", ":", "docker", "tag", ":", "return", ":", "Image", "id", "as", "string", "or", "None", "if", "tag", "does", "not", "exist" ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/ci/build.py#L171-L182
train
apache/incubator-mxnet
ci/build.py
default_ccache_dir
def default_ccache_dir() -> str: """:return: ccache directory for the current platform""" # Share ccache across containers if 'CCACHE_DIR' in os.environ: ccache_dir = os.path.realpath(os.environ['CCACHE_DIR']) try: os.makedirs(ccache_dir, exist_ok=True) return ccache_dir except PermissionError: logging.info('Unable to make dirs at %s, falling back to local temp dir', ccache_dir) # In osx tmpdir is not mountable by default import platform if platform.system() == 'Darwin': ccache_dir = "/tmp/_mxnet_ccache" os.makedirs(ccache_dir, exist_ok=True) return ccache_dir return os.path.join(os.path.expanduser("~"), ".ccache")
python
def default_ccache_dir() -> str: """:return: ccache directory for the current platform""" # Share ccache across containers if 'CCACHE_DIR' in os.environ: ccache_dir = os.path.realpath(os.environ['CCACHE_DIR']) try: os.makedirs(ccache_dir, exist_ok=True) return ccache_dir except PermissionError: logging.info('Unable to make dirs at %s, falling back to local temp dir', ccache_dir) # In osx tmpdir is not mountable by default import platform if platform.system() == 'Darwin': ccache_dir = "/tmp/_mxnet_ccache" os.makedirs(ccache_dir, exist_ok=True) return ccache_dir return os.path.join(os.path.expanduser("~"), ".ccache")
[ "def", "default_ccache_dir", "(", ")", "->", "str", ":", "# Share ccache across containers", "if", "'CCACHE_DIR'", "in", "os", ".", "environ", ":", "ccache_dir", "=", "os", ".", "path", ".", "realpath", "(", "os", ".", "environ", "[", "'CCACHE_DIR'", "]", ")", "try", ":", "os", ".", "makedirs", "(", "ccache_dir", ",", "exist_ok", "=", "True", ")", "return", "ccache_dir", "except", "PermissionError", ":", "logging", ".", "info", "(", "'Unable to make dirs at %s, falling back to local temp dir'", ",", "ccache_dir", ")", "# In osx tmpdir is not mountable by default", "import", "platform", "if", "platform", ".", "system", "(", ")", "==", "'Darwin'", ":", "ccache_dir", "=", "\"/tmp/_mxnet_ccache\"", "os", ".", "makedirs", "(", "ccache_dir", ",", "exist_ok", "=", "True", ")", "return", "ccache_dir", "return", "os", ".", "path", ".", "join", "(", "os", ".", "path", ".", "expanduser", "(", "\"~\"", ")", ",", "\".ccache\"", ")" ]
:return: ccache directory for the current platform
[ ":", "return", ":", "ccache", "directory", "for", "the", "current", "platform" ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/ci/build.py#L189-L205
train
apache/incubator-mxnet
ci/build.py
container_run
def container_run(platform: str, nvidia_runtime: bool, docker_registry: str, shared_memory_size: str, local_ccache_dir: str, command: List[str], cleanup: Cleanup, environment: Dict[str, str], dry_run: bool = False) -> int: """Run command in a container""" container_wait_s = 600 # # Environment setup # environment.update({ 'CCACHE_MAXSIZE': '500G', 'CCACHE_TEMPDIR': '/tmp/ccache', # temp dir should be local and not shared 'CCACHE_DIR': '/work/ccache', # this path is inside the container as /work/ccache is # mounted 'CCACHE_LOGFILE': '/tmp/ccache.log', # a container-scoped log, useful for ccache # verification. }) # These variables are passed to the container to the process tree killer can find runaway # process inside the container # https://wiki.jenkins.io/display/JENKINS/ProcessTreeKiller # https://github.com/jenkinsci/jenkins/blob/578d6bacb33a5e99f149de504c80275796f0b231/core/src/main/java/hudson/model/Run.java#L2393 # jenkins_env_vars = ['BUILD_NUMBER', 'BUILD_ID', 'BUILD_TAG'] environment.update({k: os.environ[k] for k in jenkins_env_vars if k in os.environ}) environment.update({k: os.environ[k] for k in ['CCACHE_MAXSIZE'] if k in os.environ}) tag = get_docker_tag(platform=platform, registry=docker_registry) mx_root = get_mxnet_root() local_build_folder = buildir() # We need to create it first, otherwise it will be created by the docker daemon with root only permissions os.makedirs(local_build_folder, exist_ok=True) os.makedirs(local_ccache_dir, exist_ok=True) logging.info("Using ccache directory: %s", local_ccache_dir) docker_client = docker.from_env() # Equivalent command docker_cmd_list = [ get_docker_binary(nvidia_runtime), 'run', "--cap-add", "SYS_PTRACE", # Required by ASAN '--rm', '--shm-size={}'.format(shared_memory_size), # mount mxnet root '-v', "{}:/work/mxnet".format(mx_root), # mount mxnet/build for storing build '-v', "{}:/work/build".format(local_build_folder), '-v', "{}:/work/ccache".format(local_ccache_dir), '-u', '{}:{}'.format(os.getuid(), os.getgid()), '-e', 'CCACHE_MAXSIZE={}'.format(environment['CCACHE_MAXSIZE']), # temp dir should be local and not shared '-e', 'CCACHE_TEMPDIR={}'.format(environment['CCACHE_TEMPDIR']), # this path is inside the container as /work/ccache is mounted '-e', "CCACHE_DIR={}".format(environment['CCACHE_DIR']), # a container-scoped log, useful for ccache verification. '-e', "CCACHE_LOGFILE={}".format(environment['CCACHE_LOGFILE']), '-ti', tag] docker_cmd_list.extend(command) docker_cmd = ' \\\n\t'.join(docker_cmd_list) logging.info("Running %s in container %s", command, tag) logging.info("Executing the equivalent of:\n%s\n", docker_cmd) # return code of the command inside docker ret = 0 if not dry_run: ############################# # signal.pthread_sigmask(signal.SIG_BLOCK, {signal.SIGINT, signal.SIGTERM}) # noinspection PyShadowingNames runtime = None if nvidia_runtime: # noinspection PyShadowingNames # runc is default (docker info | grep -i runtime) runtime = 'nvidia' container = docker_client.containers.run( tag, runtime=runtime, detach=True, command=command, shm_size=shared_memory_size, user='{}:{}'.format(os.getuid(), os.getgid()), cap_add='SYS_PTRACE', volumes={ mx_root: {'bind': '/work/mxnet', 'mode': 'rw'}, local_build_folder: {'bind': '/work/build', 'mode': 'rw'}, local_ccache_dir: {'bind': '/work/ccache', 'mode': 'rw'}, }, environment=environment) try: logging.info("Started container: %s", trim_container_id(container.id)) # Race condition: # If the previous call is interrupted then it's possible that the container is not cleaned up # We avoid by masking the signals temporarily cleanup.add_container(container) signal.pthread_sigmask(signal.SIG_UNBLOCK, {signal.SIGINT, signal.SIGTERM}) # ############################# stream = container.logs(stream=True, stdout=True, stderr=True) sys.stdout.flush() for chunk in stream: sys.stdout.buffer.write(chunk) sys.stdout.buffer.flush() sys.stdout.flush() stream.close() try: logging.info("Waiting for status of container %s for %d s.", trim_container_id(container.id), container_wait_s) wait_result = container.wait(timeout=container_wait_s) logging.info("Container exit status: %s", wait_result) ret = wait_result.get('StatusCode', 200) if ret != 0: logging.error("Container exited with an error 😞") else: logging.info("Container exited with success 👍") except Exception as e: logging.exception(e) ret = 150 # Stop try: logging.info("Stopping container: %s", trim_container_id(container.id)) container.stop() except Exception as e: logging.exception(e) ret = 151 # Remove try: logging.info("Removing container: %s", trim_container_id(container.id)) container.remove() except Exception as e: logging.exception(e) ret = 152 cleanup.remove_container(container) containers = docker_client.containers.list() if containers: logging.info("Other running containers: %s", [trim_container_id(x.id) for x in containers]) except docker.errors.NotFound as e: logging.info("Container was stopped before cleanup started: %s", e) return ret
python
def container_run(platform: str, nvidia_runtime: bool, docker_registry: str, shared_memory_size: str, local_ccache_dir: str, command: List[str], cleanup: Cleanup, environment: Dict[str, str], dry_run: bool = False) -> int: """Run command in a container""" container_wait_s = 600 # # Environment setup # environment.update({ 'CCACHE_MAXSIZE': '500G', 'CCACHE_TEMPDIR': '/tmp/ccache', # temp dir should be local and not shared 'CCACHE_DIR': '/work/ccache', # this path is inside the container as /work/ccache is # mounted 'CCACHE_LOGFILE': '/tmp/ccache.log', # a container-scoped log, useful for ccache # verification. }) # These variables are passed to the container to the process tree killer can find runaway # process inside the container # https://wiki.jenkins.io/display/JENKINS/ProcessTreeKiller # https://github.com/jenkinsci/jenkins/blob/578d6bacb33a5e99f149de504c80275796f0b231/core/src/main/java/hudson/model/Run.java#L2393 # jenkins_env_vars = ['BUILD_NUMBER', 'BUILD_ID', 'BUILD_TAG'] environment.update({k: os.environ[k] for k in jenkins_env_vars if k in os.environ}) environment.update({k: os.environ[k] for k in ['CCACHE_MAXSIZE'] if k in os.environ}) tag = get_docker_tag(platform=platform, registry=docker_registry) mx_root = get_mxnet_root() local_build_folder = buildir() # We need to create it first, otherwise it will be created by the docker daemon with root only permissions os.makedirs(local_build_folder, exist_ok=True) os.makedirs(local_ccache_dir, exist_ok=True) logging.info("Using ccache directory: %s", local_ccache_dir) docker_client = docker.from_env() # Equivalent command docker_cmd_list = [ get_docker_binary(nvidia_runtime), 'run', "--cap-add", "SYS_PTRACE", # Required by ASAN '--rm', '--shm-size={}'.format(shared_memory_size), # mount mxnet root '-v', "{}:/work/mxnet".format(mx_root), # mount mxnet/build for storing build '-v', "{}:/work/build".format(local_build_folder), '-v', "{}:/work/ccache".format(local_ccache_dir), '-u', '{}:{}'.format(os.getuid(), os.getgid()), '-e', 'CCACHE_MAXSIZE={}'.format(environment['CCACHE_MAXSIZE']), # temp dir should be local and not shared '-e', 'CCACHE_TEMPDIR={}'.format(environment['CCACHE_TEMPDIR']), # this path is inside the container as /work/ccache is mounted '-e', "CCACHE_DIR={}".format(environment['CCACHE_DIR']), # a container-scoped log, useful for ccache verification. '-e', "CCACHE_LOGFILE={}".format(environment['CCACHE_LOGFILE']), '-ti', tag] docker_cmd_list.extend(command) docker_cmd = ' \\\n\t'.join(docker_cmd_list) logging.info("Running %s in container %s", command, tag) logging.info("Executing the equivalent of:\n%s\n", docker_cmd) # return code of the command inside docker ret = 0 if not dry_run: ############################# # signal.pthread_sigmask(signal.SIG_BLOCK, {signal.SIGINT, signal.SIGTERM}) # noinspection PyShadowingNames runtime = None if nvidia_runtime: # noinspection PyShadowingNames # runc is default (docker info | grep -i runtime) runtime = 'nvidia' container = docker_client.containers.run( tag, runtime=runtime, detach=True, command=command, shm_size=shared_memory_size, user='{}:{}'.format(os.getuid(), os.getgid()), cap_add='SYS_PTRACE', volumes={ mx_root: {'bind': '/work/mxnet', 'mode': 'rw'}, local_build_folder: {'bind': '/work/build', 'mode': 'rw'}, local_ccache_dir: {'bind': '/work/ccache', 'mode': 'rw'}, }, environment=environment) try: logging.info("Started container: %s", trim_container_id(container.id)) # Race condition: # If the previous call is interrupted then it's possible that the container is not cleaned up # We avoid by masking the signals temporarily cleanup.add_container(container) signal.pthread_sigmask(signal.SIG_UNBLOCK, {signal.SIGINT, signal.SIGTERM}) # ############################# stream = container.logs(stream=True, stdout=True, stderr=True) sys.stdout.flush() for chunk in stream: sys.stdout.buffer.write(chunk) sys.stdout.buffer.flush() sys.stdout.flush() stream.close() try: logging.info("Waiting for status of container %s for %d s.", trim_container_id(container.id), container_wait_s) wait_result = container.wait(timeout=container_wait_s) logging.info("Container exit status: %s", wait_result) ret = wait_result.get('StatusCode', 200) if ret != 0: logging.error("Container exited with an error 😞") else: logging.info("Container exited with success 👍") except Exception as e: logging.exception(e) ret = 150 # Stop try: logging.info("Stopping container: %s", trim_container_id(container.id)) container.stop() except Exception as e: logging.exception(e) ret = 151 # Remove try: logging.info("Removing container: %s", trim_container_id(container.id)) container.remove() except Exception as e: logging.exception(e) ret = 152 cleanup.remove_container(container) containers = docker_client.containers.list() if containers: logging.info("Other running containers: %s", [trim_container_id(x.id) for x in containers]) except docker.errors.NotFound as e: logging.info("Container was stopped before cleanup started: %s", e) return ret
[ "def", "container_run", "(", "platform", ":", "str", ",", "nvidia_runtime", ":", "bool", ",", "docker_registry", ":", "str", ",", "shared_memory_size", ":", "str", ",", "local_ccache_dir", ":", "str", ",", "command", ":", "List", "[", "str", "]", ",", "cleanup", ":", "Cleanup", ",", "environment", ":", "Dict", "[", "str", ",", "str", "]", ",", "dry_run", ":", "bool", "=", "False", ")", "->", "int", ":", "container_wait_s", "=", "600", "#", "# Environment setup", "#", "environment", ".", "update", "(", "{", "'CCACHE_MAXSIZE'", ":", "'500G'", ",", "'CCACHE_TEMPDIR'", ":", "'/tmp/ccache'", ",", "# temp dir should be local and not shared", "'CCACHE_DIR'", ":", "'/work/ccache'", ",", "# this path is inside the container as /work/ccache is", "# mounted", "'CCACHE_LOGFILE'", ":", "'/tmp/ccache.log'", ",", "# a container-scoped log, useful for ccache", "# verification.", "}", ")", "# These variables are passed to the container to the process tree killer can find runaway", "# process inside the container", "# https://wiki.jenkins.io/display/JENKINS/ProcessTreeKiller", "# https://github.com/jenkinsci/jenkins/blob/578d6bacb33a5e99f149de504c80275796f0b231/core/src/main/java/hudson/model/Run.java#L2393", "#", "jenkins_env_vars", "=", "[", "'BUILD_NUMBER'", ",", "'BUILD_ID'", ",", "'BUILD_TAG'", "]", "environment", ".", "update", "(", "{", "k", ":", "os", ".", "environ", "[", "k", "]", "for", "k", "in", "jenkins_env_vars", "if", "k", "in", "os", ".", "environ", "}", ")", "environment", ".", "update", "(", "{", "k", ":", "os", ".", "environ", "[", "k", "]", "for", "k", "in", "[", "'CCACHE_MAXSIZE'", "]", "if", "k", "in", "os", ".", "environ", "}", ")", "tag", "=", "get_docker_tag", "(", "platform", "=", "platform", ",", "registry", "=", "docker_registry", ")", "mx_root", "=", "get_mxnet_root", "(", ")", "local_build_folder", "=", "buildir", "(", ")", "# We need to create it first, otherwise it will be created by the docker daemon with root only permissions", "os", ".", "makedirs", "(", "local_build_folder", ",", "exist_ok", "=", "True", ")", "os", ".", "makedirs", "(", "local_ccache_dir", ",", "exist_ok", "=", "True", ")", "logging", ".", "info", "(", "\"Using ccache directory: %s\"", ",", "local_ccache_dir", ")", "docker_client", "=", "docker", ".", "from_env", "(", ")", "# Equivalent command", "docker_cmd_list", "=", "[", "get_docker_binary", "(", "nvidia_runtime", ")", ",", "'run'", ",", "\"--cap-add\"", ",", "\"SYS_PTRACE\"", ",", "# Required by ASAN", "'--rm'", ",", "'--shm-size={}'", ".", "format", "(", "shared_memory_size", ")", ",", "# mount mxnet root", "'-v'", ",", "\"{}:/work/mxnet\"", ".", "format", "(", "mx_root", ")", ",", "# mount mxnet/build for storing build", "'-v'", ",", "\"{}:/work/build\"", ".", "format", "(", "local_build_folder", ")", ",", "'-v'", ",", "\"{}:/work/ccache\"", ".", "format", "(", "local_ccache_dir", ")", ",", "'-u'", ",", "'{}:{}'", ".", "format", "(", "os", ".", "getuid", "(", ")", ",", "os", ".", "getgid", "(", ")", ")", ",", "'-e'", ",", "'CCACHE_MAXSIZE={}'", ".", "format", "(", "environment", "[", "'CCACHE_MAXSIZE'", "]", ")", ",", "# temp dir should be local and not shared", "'-e'", ",", "'CCACHE_TEMPDIR={}'", ".", "format", "(", "environment", "[", "'CCACHE_TEMPDIR'", "]", ")", ",", "# this path is inside the container as /work/ccache is mounted", "'-e'", ",", "\"CCACHE_DIR={}\"", ".", "format", "(", "environment", "[", "'CCACHE_DIR'", "]", ")", ",", "# a container-scoped log, useful for ccache verification.", "'-e'", ",", "\"CCACHE_LOGFILE={}\"", ".", "format", "(", "environment", "[", "'CCACHE_LOGFILE'", "]", ")", ",", "'-ti'", ",", "tag", "]", "docker_cmd_list", ".", "extend", "(", "command", ")", "docker_cmd", "=", "' \\\\\\n\\t'", ".", "join", "(", "docker_cmd_list", ")", "logging", ".", "info", "(", "\"Running %s in container %s\"", ",", "command", ",", "tag", ")", "logging", ".", "info", "(", "\"Executing the equivalent of:\\n%s\\n\"", ",", "docker_cmd", ")", "# return code of the command inside docker", "ret", "=", "0", "if", "not", "dry_run", ":", "#############################", "#", "signal", ".", "pthread_sigmask", "(", "signal", ".", "SIG_BLOCK", ",", "{", "signal", ".", "SIGINT", ",", "signal", ".", "SIGTERM", "}", ")", "# noinspection PyShadowingNames", "runtime", "=", "None", "if", "nvidia_runtime", ":", "# noinspection PyShadowingNames", "# runc is default (docker info | grep -i runtime)", "runtime", "=", "'nvidia'", "container", "=", "docker_client", ".", "containers", ".", "run", "(", "tag", ",", "runtime", "=", "runtime", ",", "detach", "=", "True", ",", "command", "=", "command", ",", "shm_size", "=", "shared_memory_size", ",", "user", "=", "'{}:{}'", ".", "format", "(", "os", ".", "getuid", "(", ")", ",", "os", ".", "getgid", "(", ")", ")", ",", "cap_add", "=", "'SYS_PTRACE'", ",", "volumes", "=", "{", "mx_root", ":", "{", "'bind'", ":", "'/work/mxnet'", ",", "'mode'", ":", "'rw'", "}", ",", "local_build_folder", ":", "{", "'bind'", ":", "'/work/build'", ",", "'mode'", ":", "'rw'", "}", ",", "local_ccache_dir", ":", "{", "'bind'", ":", "'/work/ccache'", ",", "'mode'", ":", "'rw'", "}", ",", "}", ",", "environment", "=", "environment", ")", "try", ":", "logging", ".", "info", "(", "\"Started container: %s\"", ",", "trim_container_id", "(", "container", ".", "id", ")", ")", "# Race condition:", "# If the previous call is interrupted then it's possible that the container is not cleaned up", "# We avoid by masking the signals temporarily", "cleanup", ".", "add_container", "(", "container", ")", "signal", ".", "pthread_sigmask", "(", "signal", ".", "SIG_UNBLOCK", ",", "{", "signal", ".", "SIGINT", ",", "signal", ".", "SIGTERM", "}", ")", "#", "#############################", "stream", "=", "container", ".", "logs", "(", "stream", "=", "True", ",", "stdout", "=", "True", ",", "stderr", "=", "True", ")", "sys", ".", "stdout", ".", "flush", "(", ")", "for", "chunk", "in", "stream", ":", "sys", ".", "stdout", ".", "buffer", ".", "write", "(", "chunk", ")", "sys", ".", "stdout", ".", "buffer", ".", "flush", "(", ")", "sys", ".", "stdout", ".", "flush", "(", ")", "stream", ".", "close", "(", ")", "try", ":", "logging", ".", "info", "(", "\"Waiting for status of container %s for %d s.\"", ",", "trim_container_id", "(", "container", ".", "id", ")", ",", "container_wait_s", ")", "wait_result", "=", "container", ".", "wait", "(", "timeout", "=", "container_wait_s", ")", "logging", ".", "info", "(", "\"Container exit status: %s\"", ",", "wait_result", ")", "ret", "=", "wait_result", ".", "get", "(", "'StatusCode'", ",", "200", ")", "if", "ret", "!=", "0", ":", "logging", ".", "error", "(", "\"Container exited with an error 😞\")", "", "else", ":", "logging", ".", "info", "(", "\"Container exited with success 👍\")", "", "except", "Exception", "as", "e", ":", "logging", ".", "exception", "(", "e", ")", "ret", "=", "150", "# Stop", "try", ":", "logging", ".", "info", "(", "\"Stopping container: %s\"", ",", "trim_container_id", "(", "container", ".", "id", ")", ")", "container", ".", "stop", "(", ")", "except", "Exception", "as", "e", ":", "logging", ".", "exception", "(", "e", ")", "ret", "=", "151", "# Remove", "try", ":", "logging", ".", "info", "(", "\"Removing container: %s\"", ",", "trim_container_id", "(", "container", ".", "id", ")", ")", "container", ".", "remove", "(", ")", "except", "Exception", "as", "e", ":", "logging", ".", "exception", "(", "e", ")", "ret", "=", "152", "cleanup", ".", "remove_container", "(", "container", ")", "containers", "=", "docker_client", ".", "containers", ".", "list", "(", ")", "if", "containers", ":", "logging", ".", "info", "(", "\"Other running containers: %s\"", ",", "[", "trim_container_id", "(", "x", ".", "id", ")", "for", "x", "in", "containers", "]", ")", "except", "docker", ".", "errors", ".", "NotFound", "as", "e", ":", "logging", ".", "info", "(", "\"Container was stopped before cleanup started: %s\"", ",", "e", ")", "return", "ret" ]
Run command in a container
[ "Run", "command", "in", "a", "container" ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/ci/build.py#L213-L361
train
apache/incubator-mxnet
ci/build.py
load_docker_cache
def load_docker_cache(tag, docker_registry) -> None: """Imports tagged container from the given docker registry""" if docker_registry: # noinspection PyBroadException try: import docker_cache logging.info('Docker cache download is enabled from registry %s', docker_registry) docker_cache.load_docker_cache(registry=docker_registry, docker_tag=tag) except Exception: logging.exception('Unable to retrieve Docker cache. Continue without...') else: logging.info('Distributed docker cache disabled')
python
def load_docker_cache(tag, docker_registry) -> None: """Imports tagged container from the given docker registry""" if docker_registry: # noinspection PyBroadException try: import docker_cache logging.info('Docker cache download is enabled from registry %s', docker_registry) docker_cache.load_docker_cache(registry=docker_registry, docker_tag=tag) except Exception: logging.exception('Unable to retrieve Docker cache. Continue without...') else: logging.info('Distributed docker cache disabled')
[ "def", "load_docker_cache", "(", "tag", ",", "docker_registry", ")", "->", "None", ":", "if", "docker_registry", ":", "# noinspection PyBroadException", "try", ":", "import", "docker_cache", "logging", ".", "info", "(", "'Docker cache download is enabled from registry %s'", ",", "docker_registry", ")", "docker_cache", ".", "load_docker_cache", "(", "registry", "=", "docker_registry", ",", "docker_tag", "=", "tag", ")", "except", "Exception", ":", "logging", ".", "exception", "(", "'Unable to retrieve Docker cache. Continue without...'", ")", "else", ":", "logging", ".", "info", "(", "'Distributed docker cache disabled'", ")" ]
Imports tagged container from the given docker registry
[ "Imports", "tagged", "container", "from", "the", "given", "docker", "registry" ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/ci/build.py#L368-L379
train
apache/incubator-mxnet
python/mxnet/module/executor_group.py
_load_general
def _load_general(data, targets, major_axis): """Load a list of arrays into a list of arrays specified by slices.""" for d_src, d_targets, axis in zip(data, targets, major_axis): # pylint: disable=too-many-nested-blocks if isinstance(d_targets, nd.NDArray): d_src.copyto(d_targets) elif isinstance(d_src, (list, tuple)): for src, dst in zip(d_src, d_targets): src.copyto(dst) else: for slice_idx, d_dst in d_targets: if axis >= 0: # copy slice shape = d_src.shape do_crop = (slice_idx.start != 0 or shape[axis] != slice_idx.stop) # pylint: disable=no-member,protected-access if do_crop: if axis == 0: d_src[slice_idx.start:slice_idx.stop].copyto(d_dst) else: if d_src.context == d_dst.context: nd.slice_axis(d_src, axis=axis, begin=slice_idx.start, end=slice_idx.stop, out=d_dst) else: # on different device, crop and then do cross device copy d_dst_copy = nd.slice_axis(d_src, axis=axis, begin=slice_idx.start, end=slice_idx.stop) d_dst_copy.copyto(d_dst) else: d_src.copyto(d_dst) # pylint: enable=no-member,protected-access else: d_src.copyto(d_dst)
python
def _load_general(data, targets, major_axis): """Load a list of arrays into a list of arrays specified by slices.""" for d_src, d_targets, axis in zip(data, targets, major_axis): # pylint: disable=too-many-nested-blocks if isinstance(d_targets, nd.NDArray): d_src.copyto(d_targets) elif isinstance(d_src, (list, tuple)): for src, dst in zip(d_src, d_targets): src.copyto(dst) else: for slice_idx, d_dst in d_targets: if axis >= 0: # copy slice shape = d_src.shape do_crop = (slice_idx.start != 0 or shape[axis] != slice_idx.stop) # pylint: disable=no-member,protected-access if do_crop: if axis == 0: d_src[slice_idx.start:slice_idx.stop].copyto(d_dst) else: if d_src.context == d_dst.context: nd.slice_axis(d_src, axis=axis, begin=slice_idx.start, end=slice_idx.stop, out=d_dst) else: # on different device, crop and then do cross device copy d_dst_copy = nd.slice_axis(d_src, axis=axis, begin=slice_idx.start, end=slice_idx.stop) d_dst_copy.copyto(d_dst) else: d_src.copyto(d_dst) # pylint: enable=no-member,protected-access else: d_src.copyto(d_dst)
[ "def", "_load_general", "(", "data", ",", "targets", ",", "major_axis", ")", ":", "for", "d_src", ",", "d_targets", ",", "axis", "in", "zip", "(", "data", ",", "targets", ",", "major_axis", ")", ":", "# pylint: disable=too-many-nested-blocks", "if", "isinstance", "(", "d_targets", ",", "nd", ".", "NDArray", ")", ":", "d_src", ".", "copyto", "(", "d_targets", ")", "elif", "isinstance", "(", "d_src", ",", "(", "list", ",", "tuple", ")", ")", ":", "for", "src", ",", "dst", "in", "zip", "(", "d_src", ",", "d_targets", ")", ":", "src", ".", "copyto", "(", "dst", ")", "else", ":", "for", "slice_idx", ",", "d_dst", "in", "d_targets", ":", "if", "axis", ">=", "0", ":", "# copy slice", "shape", "=", "d_src", ".", "shape", "do_crop", "=", "(", "slice_idx", ".", "start", "!=", "0", "or", "shape", "[", "axis", "]", "!=", "slice_idx", ".", "stop", ")", "# pylint: disable=no-member,protected-access", "if", "do_crop", ":", "if", "axis", "==", "0", ":", "d_src", "[", "slice_idx", ".", "start", ":", "slice_idx", ".", "stop", "]", ".", "copyto", "(", "d_dst", ")", "else", ":", "if", "d_src", ".", "context", "==", "d_dst", ".", "context", ":", "nd", ".", "slice_axis", "(", "d_src", ",", "axis", "=", "axis", ",", "begin", "=", "slice_idx", ".", "start", ",", "end", "=", "slice_idx", ".", "stop", ",", "out", "=", "d_dst", ")", "else", ":", "# on different device, crop and then do cross device copy", "d_dst_copy", "=", "nd", ".", "slice_axis", "(", "d_src", ",", "axis", "=", "axis", ",", "begin", "=", "slice_idx", ".", "start", ",", "end", "=", "slice_idx", ".", "stop", ")", "d_dst_copy", ".", "copyto", "(", "d_dst", ")", "else", ":", "d_src", ".", "copyto", "(", "d_dst", ")", "# pylint: enable=no-member,protected-access", "else", ":", "d_src", ".", "copyto", "(", "d_dst", ")" ]
Load a list of arrays into a list of arrays specified by slices.
[ "Load", "a", "list", "of", "arrays", "into", "a", "list", "of", "arrays", "specified", "by", "slices", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/module/executor_group.py#L31-L62
train
apache/incubator-mxnet
python/mxnet/module/executor_group.py
_load_data
def _load_data(batch, targets, major_axis): """Load data into sliced arrays.""" if isinstance(batch, list): new_batch = [] for i in range(len(targets)): new_batch.append([b.data[i] for b in batch]) new_targets = [[dst for _, dst in d_target] for d_target in targets] _load_general(new_batch, new_targets, major_axis) else: _load_general(batch.data, targets, major_axis)
python
def _load_data(batch, targets, major_axis): """Load data into sliced arrays.""" if isinstance(batch, list): new_batch = [] for i in range(len(targets)): new_batch.append([b.data[i] for b in batch]) new_targets = [[dst for _, dst in d_target] for d_target in targets] _load_general(new_batch, new_targets, major_axis) else: _load_general(batch.data, targets, major_axis)
[ "def", "_load_data", "(", "batch", ",", "targets", ",", "major_axis", ")", ":", "if", "isinstance", "(", "batch", ",", "list", ")", ":", "new_batch", "=", "[", "]", "for", "i", "in", "range", "(", "len", "(", "targets", ")", ")", ":", "new_batch", ".", "append", "(", "[", "b", ".", "data", "[", "i", "]", "for", "b", "in", "batch", "]", ")", "new_targets", "=", "[", "[", "dst", "for", "_", ",", "dst", "in", "d_target", "]", "for", "d_target", "in", "targets", "]", "_load_general", "(", "new_batch", ",", "new_targets", ",", "major_axis", ")", "else", ":", "_load_general", "(", "batch", ".", "data", ",", "targets", ",", "major_axis", ")" ]
Load data into sliced arrays.
[ "Load", "data", "into", "sliced", "arrays", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/module/executor_group.py#L65-L74
train
apache/incubator-mxnet
python/mxnet/module/executor_group.py
_merge_multi_context
def _merge_multi_context(outputs, major_axis): """Merge outputs that lives on multiple context into one, so that they look like living on one context. """ rets = [] for tensors, axis in zip(outputs, major_axis): if axis >= 0: # pylint: disable=no-member,protected-access if len(tensors) == 1: rets.append(tensors[0]) else: # Concatenate if necessary rets.append(nd.concat(*[tensor.as_in_context(tensors[0].context) for tensor in tensors], dim=axis)) # pylint: enable=no-member,protected-access else: # negative axis means the there is no batch_size axis, and all the # results should be the same on each device. We simply take the # first one, without checking they are actually the same rets.append(tensors[0]) return rets
python
def _merge_multi_context(outputs, major_axis): """Merge outputs that lives on multiple context into one, so that they look like living on one context. """ rets = [] for tensors, axis in zip(outputs, major_axis): if axis >= 0: # pylint: disable=no-member,protected-access if len(tensors) == 1: rets.append(tensors[0]) else: # Concatenate if necessary rets.append(nd.concat(*[tensor.as_in_context(tensors[0].context) for tensor in tensors], dim=axis)) # pylint: enable=no-member,protected-access else: # negative axis means the there is no batch_size axis, and all the # results should be the same on each device. We simply take the # first one, without checking they are actually the same rets.append(tensors[0]) return rets
[ "def", "_merge_multi_context", "(", "outputs", ",", "major_axis", ")", ":", "rets", "=", "[", "]", "for", "tensors", ",", "axis", "in", "zip", "(", "outputs", ",", "major_axis", ")", ":", "if", "axis", ">=", "0", ":", "# pylint: disable=no-member,protected-access", "if", "len", "(", "tensors", ")", "==", "1", ":", "rets", ".", "append", "(", "tensors", "[", "0", "]", ")", "else", ":", "# Concatenate if necessary", "rets", ".", "append", "(", "nd", ".", "concat", "(", "*", "[", "tensor", ".", "as_in_context", "(", "tensors", "[", "0", "]", ".", "context", ")", "for", "tensor", "in", "tensors", "]", ",", "dim", "=", "axis", ")", ")", "# pylint: enable=no-member,protected-access", "else", ":", "# negative axis means the there is no batch_size axis, and all the", "# results should be the same on each device. We simply take the", "# first one, without checking they are actually the same", "rets", ".", "append", "(", "tensors", "[", "0", "]", ")", "return", "rets" ]
Merge outputs that lives on multiple context into one, so that they look like living on one context.
[ "Merge", "outputs", "that", "lives", "on", "multiple", "context", "into", "one", "so", "that", "they", "look", "like", "living", "on", "one", "context", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/module/executor_group.py#L89-L110
train
apache/incubator-mxnet
python/mxnet/module/executor_group.py
_prepare_group2ctxs
def _prepare_group2ctxs(group2ctxs, ctx_len): """Prepare the group2contexts, will duplicate the context if some ctx_group map to only one context. """ if group2ctxs is None: return [None] * ctx_len elif isinstance(group2ctxs, list): assert(len(group2ctxs) == ctx_len), "length of group2ctxs\ should be %d" % ctx_len return group2ctxs elif isinstance(group2ctxs, dict): ret = [{} for i in range(ctx_len)] for k, v in group2ctxs.items(): ctxs = None if isinstance(v, ctx.Context): ctxs = [v] * ctx_len else: if len(v) == 1: ctxs = v * ctx_len else: assert(len(v) == ctx_len), "length of group2ctxs[%s]\ should be %d or 1" % (k, ctx_len) ctxs = v for i in range(ctx_len): ret[i][k] = ctxs[i] return ret else: assert(False), "group2ctxs should be list of dict of str to context,\ or dict of str to context or list of context" return False
python
def _prepare_group2ctxs(group2ctxs, ctx_len): """Prepare the group2contexts, will duplicate the context if some ctx_group map to only one context. """ if group2ctxs is None: return [None] * ctx_len elif isinstance(group2ctxs, list): assert(len(group2ctxs) == ctx_len), "length of group2ctxs\ should be %d" % ctx_len return group2ctxs elif isinstance(group2ctxs, dict): ret = [{} for i in range(ctx_len)] for k, v in group2ctxs.items(): ctxs = None if isinstance(v, ctx.Context): ctxs = [v] * ctx_len else: if len(v) == 1: ctxs = v * ctx_len else: assert(len(v) == ctx_len), "length of group2ctxs[%s]\ should be %d or 1" % (k, ctx_len) ctxs = v for i in range(ctx_len): ret[i][k] = ctxs[i] return ret else: assert(False), "group2ctxs should be list of dict of str to context,\ or dict of str to context or list of context" return False
[ "def", "_prepare_group2ctxs", "(", "group2ctxs", ",", "ctx_len", ")", ":", "if", "group2ctxs", "is", "None", ":", "return", "[", "None", "]", "*", "ctx_len", "elif", "isinstance", "(", "group2ctxs", ",", "list", ")", ":", "assert", "(", "len", "(", "group2ctxs", ")", "==", "ctx_len", ")", ",", "\"length of group2ctxs\\\n should be %d\"", "%", "ctx_len", "return", "group2ctxs", "elif", "isinstance", "(", "group2ctxs", ",", "dict", ")", ":", "ret", "=", "[", "{", "}", "for", "i", "in", "range", "(", "ctx_len", ")", "]", "for", "k", ",", "v", "in", "group2ctxs", ".", "items", "(", ")", ":", "ctxs", "=", "None", "if", "isinstance", "(", "v", ",", "ctx", ".", "Context", ")", ":", "ctxs", "=", "[", "v", "]", "*", "ctx_len", "else", ":", "if", "len", "(", "v", ")", "==", "1", ":", "ctxs", "=", "v", "*", "ctx_len", "else", ":", "assert", "(", "len", "(", "v", ")", "==", "ctx_len", ")", ",", "\"length of group2ctxs[%s]\\\n should be %d or 1\"", "%", "(", "k", ",", "ctx_len", ")", "ctxs", "=", "v", "for", "i", "in", "range", "(", "ctx_len", ")", ":", "ret", "[", "i", "]", "[", "k", "]", "=", "ctxs", "[", "i", "]", "return", "ret", "else", ":", "assert", "(", "False", ")", ",", "\"group2ctxs should be list of dict of str to context,\\\n or dict of str to context or list of context\"", "return", "False" ]
Prepare the group2contexts, will duplicate the context if some ctx_group map to only one context.
[ "Prepare", "the", "group2contexts", "will", "duplicate", "the", "context", "if", "some", "ctx_group", "map", "to", "only", "one", "context", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/module/executor_group.py#L112-L141
train
apache/incubator-mxnet
python/mxnet/module/executor_group.py
DataParallelExecutorGroup.decide_slices
def decide_slices(self, data_shapes): """Decide the slices for each context according to the workload. Parameters ---------- data_shapes : list list of (name, shape) specifying the shapes for the input data or label. """ assert len(data_shapes) > 0 major_axis = [DataDesc.get_batch_axis(x.layout) for x in data_shapes] for (name, shape), axis in zip(data_shapes, major_axis): if axis == -1: continue batch_size = shape[axis] if self.batch_size is not None: assert batch_size == self.batch_size, ("all data must have the same batch size: " + ("batch_size = %d, but " % self.batch_size) + ("%s has shape %s" % (name, shape))) else: self.batch_size = batch_size self.slices = _split_input_slice(self.batch_size, self.workload) return major_axis
python
def decide_slices(self, data_shapes): """Decide the slices for each context according to the workload. Parameters ---------- data_shapes : list list of (name, shape) specifying the shapes for the input data or label. """ assert len(data_shapes) > 0 major_axis = [DataDesc.get_batch_axis(x.layout) for x in data_shapes] for (name, shape), axis in zip(data_shapes, major_axis): if axis == -1: continue batch_size = shape[axis] if self.batch_size is not None: assert batch_size == self.batch_size, ("all data must have the same batch size: " + ("batch_size = %d, but " % self.batch_size) + ("%s has shape %s" % (name, shape))) else: self.batch_size = batch_size self.slices = _split_input_slice(self.batch_size, self.workload) return major_axis
[ "def", "decide_slices", "(", "self", ",", "data_shapes", ")", ":", "assert", "len", "(", "data_shapes", ")", ">", "0", "major_axis", "=", "[", "DataDesc", ".", "get_batch_axis", "(", "x", ".", "layout", ")", "for", "x", "in", "data_shapes", "]", "for", "(", "name", ",", "shape", ")", ",", "axis", "in", "zip", "(", "data_shapes", ",", "major_axis", ")", ":", "if", "axis", "==", "-", "1", ":", "continue", "batch_size", "=", "shape", "[", "axis", "]", "if", "self", ".", "batch_size", "is", "not", "None", ":", "assert", "batch_size", "==", "self", ".", "batch_size", ",", "(", "\"all data must have the same batch size: \"", "+", "(", "\"batch_size = %d, but \"", "%", "self", ".", "batch_size", ")", "+", "(", "\"%s has shape %s\"", "%", "(", "name", ",", "shape", ")", ")", ")", "else", ":", "self", ".", "batch_size", "=", "batch_size", "self", ".", "slices", "=", "_split_input_slice", "(", "self", ".", "batch_size", ",", "self", ".", "workload", ")", "return", "major_axis" ]
Decide the slices for each context according to the workload. Parameters ---------- data_shapes : list list of (name, shape) specifying the shapes for the input data or label.
[ "Decide", "the", "slices", "for", "each", "context", "according", "to", "the", "workload", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/module/executor_group.py#L281-L305
train
apache/incubator-mxnet
python/mxnet/module/executor_group.py
DataParallelExecutorGroup._collect_arrays
def _collect_arrays(self): """Collect internal arrays from executors.""" # convenient data structures self.data_arrays = [[(self.slices[i], e.arg_dict[name]) for i, e in enumerate(self.execs)] for name, _ in self.data_shapes] self.state_arrays = [[e.arg_dict[name] for e in self.execs] for name in self.state_names] if self.label_shapes is not None: self.label_arrays = [[(self.slices[i], e.arg_dict[name]) for i, e in enumerate(self.execs)] for name, _ in self.label_shapes] else: self.label_arrays = None self.param_arrays = [[exec_.arg_arrays[i] for exec_ in self.execs] for i, name in enumerate(self.arg_names) if name in self.param_names] if self.for_training: self.grad_arrays = [[exec_.grad_arrays[i] for exec_ in self.execs] for i, name in enumerate(self.arg_names) if name in self.param_names] else: self.grad_arrays = None data_names = [x[0] for x in self.data_shapes] if self.inputs_need_grad: self.input_grad_arrays = [[exec_.grad_arrays[self.arg_names.index(name)] for exec_ in self.execs] for name in data_names if name in self.arg_names] else: self.input_grad_arrays = None self.aux_arrays = [[exec_.aux_arrays[i] for exec_ in self.execs] for i in range(len(self.aux_names))]
python
def _collect_arrays(self): """Collect internal arrays from executors.""" # convenient data structures self.data_arrays = [[(self.slices[i], e.arg_dict[name]) for i, e in enumerate(self.execs)] for name, _ in self.data_shapes] self.state_arrays = [[e.arg_dict[name] for e in self.execs] for name in self.state_names] if self.label_shapes is not None: self.label_arrays = [[(self.slices[i], e.arg_dict[name]) for i, e in enumerate(self.execs)] for name, _ in self.label_shapes] else: self.label_arrays = None self.param_arrays = [[exec_.arg_arrays[i] for exec_ in self.execs] for i, name in enumerate(self.arg_names) if name in self.param_names] if self.for_training: self.grad_arrays = [[exec_.grad_arrays[i] for exec_ in self.execs] for i, name in enumerate(self.arg_names) if name in self.param_names] else: self.grad_arrays = None data_names = [x[0] for x in self.data_shapes] if self.inputs_need_grad: self.input_grad_arrays = [[exec_.grad_arrays[self.arg_names.index(name)] for exec_ in self.execs] for name in data_names if name in self.arg_names] else: self.input_grad_arrays = None self.aux_arrays = [[exec_.aux_arrays[i] for exec_ in self.execs] for i in range(len(self.aux_names))]
[ "def", "_collect_arrays", "(", "self", ")", ":", "# convenient data structures", "self", ".", "data_arrays", "=", "[", "[", "(", "self", ".", "slices", "[", "i", "]", ",", "e", ".", "arg_dict", "[", "name", "]", ")", "for", "i", ",", "e", "in", "enumerate", "(", "self", ".", "execs", ")", "]", "for", "name", ",", "_", "in", "self", ".", "data_shapes", "]", "self", ".", "state_arrays", "=", "[", "[", "e", ".", "arg_dict", "[", "name", "]", "for", "e", "in", "self", ".", "execs", "]", "for", "name", "in", "self", ".", "state_names", "]", "if", "self", ".", "label_shapes", "is", "not", "None", ":", "self", ".", "label_arrays", "=", "[", "[", "(", "self", ".", "slices", "[", "i", "]", ",", "e", ".", "arg_dict", "[", "name", "]", ")", "for", "i", ",", "e", "in", "enumerate", "(", "self", ".", "execs", ")", "]", "for", "name", ",", "_", "in", "self", ".", "label_shapes", "]", "else", ":", "self", ".", "label_arrays", "=", "None", "self", ".", "param_arrays", "=", "[", "[", "exec_", ".", "arg_arrays", "[", "i", "]", "for", "exec_", "in", "self", ".", "execs", "]", "for", "i", ",", "name", "in", "enumerate", "(", "self", ".", "arg_names", ")", "if", "name", "in", "self", ".", "param_names", "]", "if", "self", ".", "for_training", ":", "self", ".", "grad_arrays", "=", "[", "[", "exec_", ".", "grad_arrays", "[", "i", "]", "for", "exec_", "in", "self", ".", "execs", "]", "for", "i", ",", "name", "in", "enumerate", "(", "self", ".", "arg_names", ")", "if", "name", "in", "self", ".", "param_names", "]", "else", ":", "self", ".", "grad_arrays", "=", "None", "data_names", "=", "[", "x", "[", "0", "]", "for", "x", "in", "self", ".", "data_shapes", "]", "if", "self", ".", "inputs_need_grad", ":", "self", ".", "input_grad_arrays", "=", "[", "[", "exec_", ".", "grad_arrays", "[", "self", ".", "arg_names", ".", "index", "(", "name", ")", "]", "for", "exec_", "in", "self", ".", "execs", "]", "for", "name", "in", "data_names", "if", "name", "in", "self", ".", "arg_names", "]", "else", ":", "self", ".", "input_grad_arrays", "=", "None", "self", ".", "aux_arrays", "=", "[", "[", "exec_", ".", "aux_arrays", "[", "i", "]", "for", "exec_", "in", "self", ".", "execs", "]", "for", "i", "in", "range", "(", "len", "(", "self", ".", "aux_names", ")", ")", "]" ]
Collect internal arrays from executors.
[ "Collect", "internal", "arrays", "from", "executors", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/module/executor_group.py#L307-L342
train
apache/incubator-mxnet
python/mxnet/module/executor_group.py
DataParallelExecutorGroup.bind_exec
def bind_exec(self, data_shapes, label_shapes, shared_group=None, reshape=False): """Bind executors on their respective devices. Parameters ---------- data_shapes : list label_shapes : list shared_group : DataParallelExecutorGroup reshape : bool """ assert reshape or not self.execs self.batch_size = None # calculate workload and bind executors self.data_layouts = self.decide_slices(data_shapes) if label_shapes is not None: # call it to make sure labels has the same batch size as data self.label_layouts = self.decide_slices(label_shapes) for i in range(len(self.contexts)): data_shapes_i = self._sliced_shape(data_shapes, i, self.data_layouts) if label_shapes is not None: label_shapes_i = self._sliced_shape(label_shapes, i, self.label_layouts) else: label_shapes_i = [] if reshape: self.execs[i] = self._default_execs[i].reshape( allow_up_sizing=True, **dict(data_shapes_i + label_shapes_i)) else: self.execs.append(self._bind_ith_exec(i, data_shapes_i, label_shapes_i, shared_group)) self.data_shapes = data_shapes self.label_shapes = label_shapes self.data_names = [i.name for i in self.data_shapes] if label_shapes is not None: self.label_names = [i.name for i in self.label_shapes] self._collect_arrays()
python
def bind_exec(self, data_shapes, label_shapes, shared_group=None, reshape=False): """Bind executors on their respective devices. Parameters ---------- data_shapes : list label_shapes : list shared_group : DataParallelExecutorGroup reshape : bool """ assert reshape or not self.execs self.batch_size = None # calculate workload and bind executors self.data_layouts = self.decide_slices(data_shapes) if label_shapes is not None: # call it to make sure labels has the same batch size as data self.label_layouts = self.decide_slices(label_shapes) for i in range(len(self.contexts)): data_shapes_i = self._sliced_shape(data_shapes, i, self.data_layouts) if label_shapes is not None: label_shapes_i = self._sliced_shape(label_shapes, i, self.label_layouts) else: label_shapes_i = [] if reshape: self.execs[i] = self._default_execs[i].reshape( allow_up_sizing=True, **dict(data_shapes_i + label_shapes_i)) else: self.execs.append(self._bind_ith_exec(i, data_shapes_i, label_shapes_i, shared_group)) self.data_shapes = data_shapes self.label_shapes = label_shapes self.data_names = [i.name for i in self.data_shapes] if label_shapes is not None: self.label_names = [i.name for i in self.label_shapes] self._collect_arrays()
[ "def", "bind_exec", "(", "self", ",", "data_shapes", ",", "label_shapes", ",", "shared_group", "=", "None", ",", "reshape", "=", "False", ")", ":", "assert", "reshape", "or", "not", "self", ".", "execs", "self", ".", "batch_size", "=", "None", "# calculate workload and bind executors", "self", ".", "data_layouts", "=", "self", ".", "decide_slices", "(", "data_shapes", ")", "if", "label_shapes", "is", "not", "None", ":", "# call it to make sure labels has the same batch size as data", "self", ".", "label_layouts", "=", "self", ".", "decide_slices", "(", "label_shapes", ")", "for", "i", "in", "range", "(", "len", "(", "self", ".", "contexts", ")", ")", ":", "data_shapes_i", "=", "self", ".", "_sliced_shape", "(", "data_shapes", ",", "i", ",", "self", ".", "data_layouts", ")", "if", "label_shapes", "is", "not", "None", ":", "label_shapes_i", "=", "self", ".", "_sliced_shape", "(", "label_shapes", ",", "i", ",", "self", ".", "label_layouts", ")", "else", ":", "label_shapes_i", "=", "[", "]", "if", "reshape", ":", "self", ".", "execs", "[", "i", "]", "=", "self", ".", "_default_execs", "[", "i", "]", ".", "reshape", "(", "allow_up_sizing", "=", "True", ",", "*", "*", "dict", "(", "data_shapes_i", "+", "label_shapes_i", ")", ")", "else", ":", "self", ".", "execs", ".", "append", "(", "self", ".", "_bind_ith_exec", "(", "i", ",", "data_shapes_i", ",", "label_shapes_i", ",", "shared_group", ")", ")", "self", ".", "data_shapes", "=", "data_shapes", "self", ".", "label_shapes", "=", "label_shapes", "self", ".", "data_names", "=", "[", "i", ".", "name", "for", "i", "in", "self", ".", "data_shapes", "]", "if", "label_shapes", "is", "not", "None", ":", "self", ".", "label_names", "=", "[", "i", ".", "name", "for", "i", "in", "self", ".", "label_shapes", "]", "self", ".", "_collect_arrays", "(", ")" ]
Bind executors on their respective devices. Parameters ---------- data_shapes : list label_shapes : list shared_group : DataParallelExecutorGroup reshape : bool
[ "Bind", "executors", "on", "their", "respective", "devices", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/module/executor_group.py#L344-L382
train
apache/incubator-mxnet
python/mxnet/module/executor_group.py
DataParallelExecutorGroup.reshape
def reshape(self, data_shapes, label_shapes): """Reshape executors. Parameters ---------- data_shapes : list label_shapes : list """ if data_shapes == self.data_shapes and label_shapes == self.label_shapes: return if self._default_execs is None: self._default_execs = [i for i in self.execs] self.bind_exec(data_shapes, label_shapes, reshape=True)
python
def reshape(self, data_shapes, label_shapes): """Reshape executors. Parameters ---------- data_shapes : list label_shapes : list """ if data_shapes == self.data_shapes and label_shapes == self.label_shapes: return if self._default_execs is None: self._default_execs = [i for i in self.execs] self.bind_exec(data_shapes, label_shapes, reshape=True)
[ "def", "reshape", "(", "self", ",", "data_shapes", ",", "label_shapes", ")", ":", "if", "data_shapes", "==", "self", ".", "data_shapes", "and", "label_shapes", "==", "self", ".", "label_shapes", ":", "return", "if", "self", ".", "_default_execs", "is", "None", ":", "self", ".", "_default_execs", "=", "[", "i", "for", "i", "in", "self", ".", "execs", "]", "self", ".", "bind_exec", "(", "data_shapes", ",", "label_shapes", ",", "reshape", "=", "True", ")" ]
Reshape executors. Parameters ---------- data_shapes : list label_shapes : list
[ "Reshape", "executors", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/module/executor_group.py#L384-L396
train
apache/incubator-mxnet
python/mxnet/module/executor_group.py
DataParallelExecutorGroup.set_params
def set_params(self, arg_params, aux_params, allow_extra=False): """Assign, i.e. copy parameters to all the executors. Parameters ---------- arg_params : dict A dictionary of name to `NDArray` parameter mapping. aux_params : dict A dictionary of name to `NDArray` auxiliary variable mapping. allow_extra : boolean, optional Whether allow extra parameters that are not needed by symbol. If this is True, no error will be thrown when arg_params or aux_params contain extra parameters that is not needed by the executor. """ for exec_ in self.execs: exec_.copy_params_from(arg_params, aux_params, allow_extra_params=allow_extra)
python
def set_params(self, arg_params, aux_params, allow_extra=False): """Assign, i.e. copy parameters to all the executors. Parameters ---------- arg_params : dict A dictionary of name to `NDArray` parameter mapping. aux_params : dict A dictionary of name to `NDArray` auxiliary variable mapping. allow_extra : boolean, optional Whether allow extra parameters that are not needed by symbol. If this is True, no error will be thrown when arg_params or aux_params contain extra parameters that is not needed by the executor. """ for exec_ in self.execs: exec_.copy_params_from(arg_params, aux_params, allow_extra_params=allow_extra)
[ "def", "set_params", "(", "self", ",", "arg_params", ",", "aux_params", ",", "allow_extra", "=", "False", ")", ":", "for", "exec_", "in", "self", ".", "execs", ":", "exec_", ".", "copy_params_from", "(", "arg_params", ",", "aux_params", ",", "allow_extra_params", "=", "allow_extra", ")" ]
Assign, i.e. copy parameters to all the executors. Parameters ---------- arg_params : dict A dictionary of name to `NDArray` parameter mapping. aux_params : dict A dictionary of name to `NDArray` auxiliary variable mapping. allow_extra : boolean, optional Whether allow extra parameters that are not needed by symbol. If this is True, no error will be thrown when arg_params or aux_params contain extra parameters that is not needed by the executor.
[ "Assign", "i", ".", "e", ".", "copy", "parameters", "to", "all", "the", "executors", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/module/executor_group.py#L398-L413
train
apache/incubator-mxnet
python/mxnet/module/executor_group.py
DataParallelExecutorGroup.get_params
def get_params(self, arg_params, aux_params): """ Copy data from each executor to `arg_params` and `aux_params`. Parameters ---------- arg_params : list of NDArray Target parameter arrays. aux_params : list of NDArray Target aux arrays. Notes ----- - This function will inplace update the NDArrays in arg_params and aux_params. """ for name, block in zip(self.param_names, self.param_arrays): weight = sum(w.copyto(ctx.cpu()) for w in block) / len(block) weight.astype(arg_params[name].dtype).copyto(arg_params[name]) for name, block in zip(self.aux_names, self.aux_arrays): weight = sum(w.copyto(ctx.cpu()) for w in block) / len(block) weight.astype(aux_params[name].dtype).copyto(aux_params[name])
python
def get_params(self, arg_params, aux_params): """ Copy data from each executor to `arg_params` and `aux_params`. Parameters ---------- arg_params : list of NDArray Target parameter arrays. aux_params : list of NDArray Target aux arrays. Notes ----- - This function will inplace update the NDArrays in arg_params and aux_params. """ for name, block in zip(self.param_names, self.param_arrays): weight = sum(w.copyto(ctx.cpu()) for w in block) / len(block) weight.astype(arg_params[name].dtype).copyto(arg_params[name]) for name, block in zip(self.aux_names, self.aux_arrays): weight = sum(w.copyto(ctx.cpu()) for w in block) / len(block) weight.astype(aux_params[name].dtype).copyto(aux_params[name])
[ "def", "get_params", "(", "self", ",", "arg_params", ",", "aux_params", ")", ":", "for", "name", ",", "block", "in", "zip", "(", "self", ".", "param_names", ",", "self", ".", "param_arrays", ")", ":", "weight", "=", "sum", "(", "w", ".", "copyto", "(", "ctx", ".", "cpu", "(", ")", ")", "for", "w", "in", "block", ")", "/", "len", "(", "block", ")", "weight", ".", "astype", "(", "arg_params", "[", "name", "]", ".", "dtype", ")", ".", "copyto", "(", "arg_params", "[", "name", "]", ")", "for", "name", ",", "block", "in", "zip", "(", "self", ".", "aux_names", ",", "self", ".", "aux_arrays", ")", ":", "weight", "=", "sum", "(", "w", ".", "copyto", "(", "ctx", ".", "cpu", "(", ")", ")", "for", "w", "in", "block", ")", "/", "len", "(", "block", ")", "weight", ".", "astype", "(", "aux_params", "[", "name", "]", ".", "dtype", ")", ".", "copyto", "(", "aux_params", "[", "name", "]", ")" ]
Copy data from each executor to `arg_params` and `aux_params`. Parameters ---------- arg_params : list of NDArray Target parameter arrays. aux_params : list of NDArray Target aux arrays. Notes ----- - This function will inplace update the NDArrays in arg_params and aux_params.
[ "Copy", "data", "from", "each", "executor", "to", "arg_params", "and", "aux_params", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/module/executor_group.py#L415-L434
train
apache/incubator-mxnet
python/mxnet/module/executor_group.py
DataParallelExecutorGroup.forward
def forward(self, data_batch, is_train=None): """Split `data_batch` according to workload and run forward on each devices. Parameters ---------- data_batch : DataBatch Or could be any object implementing similar interface. is_train : bool The hint for the backend, indicating whether we are during training phase. Default is `None`, then the value `self.for_training` will be used. Returns ------- """ _load_data(data_batch, self.data_arrays, self.data_layouts) if is_train is None: is_train = self.for_training if isinstance(data_batch, list): if self.label_arrays is not None and data_batch is not None and data_batch[0].label: _load_label(data_batch, self.label_arrays, self.label_layouts) else: if self.label_arrays is not None and data_batch.label: _load_label(data_batch, self.label_arrays, self.label_layouts) for exec_ in self.execs: exec_.forward(is_train=is_train)
python
def forward(self, data_batch, is_train=None): """Split `data_batch` according to workload and run forward on each devices. Parameters ---------- data_batch : DataBatch Or could be any object implementing similar interface. is_train : bool The hint for the backend, indicating whether we are during training phase. Default is `None`, then the value `self.for_training` will be used. Returns ------- """ _load_data(data_batch, self.data_arrays, self.data_layouts) if is_train is None: is_train = self.for_training if isinstance(data_batch, list): if self.label_arrays is not None and data_batch is not None and data_batch[0].label: _load_label(data_batch, self.label_arrays, self.label_layouts) else: if self.label_arrays is not None and data_batch.label: _load_label(data_batch, self.label_arrays, self.label_layouts) for exec_ in self.execs: exec_.forward(is_train=is_train)
[ "def", "forward", "(", "self", ",", "data_batch", ",", "is_train", "=", "None", ")", ":", "_load_data", "(", "data_batch", ",", "self", ".", "data_arrays", ",", "self", ".", "data_layouts", ")", "if", "is_train", "is", "None", ":", "is_train", "=", "self", ".", "for_training", "if", "isinstance", "(", "data_batch", ",", "list", ")", ":", "if", "self", ".", "label_arrays", "is", "not", "None", "and", "data_batch", "is", "not", "None", "and", "data_batch", "[", "0", "]", ".", "label", ":", "_load_label", "(", "data_batch", ",", "self", ".", "label_arrays", ",", "self", ".", "label_layouts", ")", "else", ":", "if", "self", ".", "label_arrays", "is", "not", "None", "and", "data_batch", ".", "label", ":", "_load_label", "(", "data_batch", ",", "self", ".", "label_arrays", ",", "self", ".", "label_layouts", ")", "for", "exec_", "in", "self", ".", "execs", ":", "exec_", ".", "forward", "(", "is_train", "=", "is_train", ")" ]
Split `data_batch` according to workload and run forward on each devices. Parameters ---------- data_batch : DataBatch Or could be any object implementing similar interface. is_train : bool The hint for the backend, indicating whether we are during training phase. Default is `None`, then the value `self.for_training` will be used. Returns -------
[ "Split", "data_batch", "according", "to", "workload", "and", "run", "forward", "on", "each", "devices", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/module/executor_group.py#L436-L462
train
apache/incubator-mxnet
python/mxnet/module/executor_group.py
DataParallelExecutorGroup.get_output_shapes
def get_output_shapes(self): """Get the shapes of the outputs.""" outputs = self.execs[0].outputs shapes = [out.shape for out in outputs] concat_shapes = [] for key, the_shape, axis in zip(self.symbol.list_outputs(), shapes, self.output_layouts): the_shape = list(the_shape) if axis >= 0: the_shape[axis] = self.batch_size concat_shapes.append((key, tuple(the_shape))) return concat_shapes
python
def get_output_shapes(self): """Get the shapes of the outputs.""" outputs = self.execs[0].outputs shapes = [out.shape for out in outputs] concat_shapes = [] for key, the_shape, axis in zip(self.symbol.list_outputs(), shapes, self.output_layouts): the_shape = list(the_shape) if axis >= 0: the_shape[axis] = self.batch_size concat_shapes.append((key, tuple(the_shape))) return concat_shapes
[ "def", "get_output_shapes", "(", "self", ")", ":", "outputs", "=", "self", ".", "execs", "[", "0", "]", ".", "outputs", "shapes", "=", "[", "out", ".", "shape", "for", "out", "in", "outputs", "]", "concat_shapes", "=", "[", "]", "for", "key", ",", "the_shape", ",", "axis", "in", "zip", "(", "self", ".", "symbol", ".", "list_outputs", "(", ")", ",", "shapes", ",", "self", ".", "output_layouts", ")", ":", "the_shape", "=", "list", "(", "the_shape", ")", "if", "axis", ">=", "0", ":", "the_shape", "[", "axis", "]", "=", "self", ".", "batch_size", "concat_shapes", ".", "append", "(", "(", "key", ",", "tuple", "(", "the_shape", ")", ")", ")", "return", "concat_shapes" ]
Get the shapes of the outputs.
[ "Get", "the", "shapes", "of", "the", "outputs", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/module/executor_group.py#L464-L475
train
apache/incubator-mxnet
python/mxnet/module/executor_group.py
DataParallelExecutorGroup.get_outputs
def get_outputs(self, merge_multi_context=True, begin=0, end=None): """Get outputs of the previous forward computation. If begin or end is specified, return [begin, end)-th outputs, otherwise return all outputs. Parameters ---------- merge_multi_context : bool Default is `True`. In the case when data-parallelism is used, the outputs will be collected from multiple devices. A `True` value indicate that we should merge the collected results so that they look like from a single executor. begin : int starting index of returned outputs in all outputs end : int or None ending index (excluded) of returned outputs. Returns ------- If `merge_multi_context` is ``True``, it is like ``[out1, out2]``. Otherwise, it is like ``[[out1_dev1, out1_dev2], [out2_dev1, out2_dev2]]``. All the output elements are `NDArray`. """ if end is None: end = self.num_outputs outputs = [[exec_.outputs[i] for exec_ in self.execs] for i in range(begin, end)] if merge_multi_context: outputs = _merge_multi_context(outputs, self.output_layouts) return outputs
python
def get_outputs(self, merge_multi_context=True, begin=0, end=None): """Get outputs of the previous forward computation. If begin or end is specified, return [begin, end)-th outputs, otherwise return all outputs. Parameters ---------- merge_multi_context : bool Default is `True`. In the case when data-parallelism is used, the outputs will be collected from multiple devices. A `True` value indicate that we should merge the collected results so that they look like from a single executor. begin : int starting index of returned outputs in all outputs end : int or None ending index (excluded) of returned outputs. Returns ------- If `merge_multi_context` is ``True``, it is like ``[out1, out2]``. Otherwise, it is like ``[[out1_dev1, out1_dev2], [out2_dev1, out2_dev2]]``. All the output elements are `NDArray`. """ if end is None: end = self.num_outputs outputs = [[exec_.outputs[i] for exec_ in self.execs] for i in range(begin, end)] if merge_multi_context: outputs = _merge_multi_context(outputs, self.output_layouts) return outputs
[ "def", "get_outputs", "(", "self", ",", "merge_multi_context", "=", "True", ",", "begin", "=", "0", ",", "end", "=", "None", ")", ":", "if", "end", "is", "None", ":", "end", "=", "self", ".", "num_outputs", "outputs", "=", "[", "[", "exec_", ".", "outputs", "[", "i", "]", "for", "exec_", "in", "self", ".", "execs", "]", "for", "i", "in", "range", "(", "begin", ",", "end", ")", "]", "if", "merge_multi_context", ":", "outputs", "=", "_merge_multi_context", "(", "outputs", ",", "self", ".", "output_layouts", ")", "return", "outputs" ]
Get outputs of the previous forward computation. If begin or end is specified, return [begin, end)-th outputs, otherwise return all outputs. Parameters ---------- merge_multi_context : bool Default is `True`. In the case when data-parallelism is used, the outputs will be collected from multiple devices. A `True` value indicate that we should merge the collected results so that they look like from a single executor. begin : int starting index of returned outputs in all outputs end : int or None ending index (excluded) of returned outputs. Returns ------- If `merge_multi_context` is ``True``, it is like ``[out1, out2]``. Otherwise, it is like ``[[out1_dev1, out1_dev2], [out2_dev1, out2_dev2]]``. All the output elements are `NDArray`.
[ "Get", "outputs", "of", "the", "previous", "forward", "computation", ".", "If", "begin", "or", "end", "is", "specified", "return", "[", "begin", "end", ")", "-", "th", "outputs", "otherwise", "return", "all", "outputs", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/module/executor_group.py#L477-L506
train
apache/incubator-mxnet
python/mxnet/module/executor_group.py
DataParallelExecutorGroup.set_states
def set_states(self, states=None, value=None): """Set value for states. Only one of states & value can be specified. Parameters ---------- states : list of list of NDArrays source states arrays formatted like [[state1_dev1, state1_dev2], [state2_dev1, state2_dev2]]. value : number a single scalar value for all state arrays. """ if states is not None: assert value is None, "Only one of states & value can be specified." _load_general(states, self.state_arrays, (0,)*len(states)) else: assert value is not None, "At least one of states & value must be specified." assert states is None, "Only one of states & value can be specified." for d_dst in self.state_arrays: for dst in d_dst: dst[:] = value
python
def set_states(self, states=None, value=None): """Set value for states. Only one of states & value can be specified. Parameters ---------- states : list of list of NDArrays source states arrays formatted like [[state1_dev1, state1_dev2], [state2_dev1, state2_dev2]]. value : number a single scalar value for all state arrays. """ if states is not None: assert value is None, "Only one of states & value can be specified." _load_general(states, self.state_arrays, (0,)*len(states)) else: assert value is not None, "At least one of states & value must be specified." assert states is None, "Only one of states & value can be specified." for d_dst in self.state_arrays: for dst in d_dst: dst[:] = value
[ "def", "set_states", "(", "self", ",", "states", "=", "None", ",", "value", "=", "None", ")", ":", "if", "states", "is", "not", "None", ":", "assert", "value", "is", "None", ",", "\"Only one of states & value can be specified.\"", "_load_general", "(", "states", ",", "self", ".", "state_arrays", ",", "(", "0", ",", ")", "*", "len", "(", "states", ")", ")", "else", ":", "assert", "value", "is", "not", "None", ",", "\"At least one of states & value must be specified.\"", "assert", "states", "is", "None", ",", "\"Only one of states & value can be specified.\"", "for", "d_dst", "in", "self", ".", "state_arrays", ":", "for", "dst", "in", "d_dst", ":", "dst", "[", ":", "]", "=", "value" ]
Set value for states. Only one of states & value can be specified. Parameters ---------- states : list of list of NDArrays source states arrays formatted like [[state1_dev1, state1_dev2], [state2_dev1, state2_dev2]]. value : number a single scalar value for all state arrays.
[ "Set", "value", "for", "states", ".", "Only", "one", "of", "states", "&", "value", "can", "be", "specified", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/module/executor_group.py#L529-L548
train
apache/incubator-mxnet
python/mxnet/module/executor_group.py
DataParallelExecutorGroup.get_input_grads
def get_input_grads(self, merge_multi_context=True): """Get the gradients with respect to the inputs of the module. Parameters ---------- merge_multi_context : bool Defaults to ``True``. In the case when data-parallelism is used, the outputs will be collected from multiple devices. A `True` value indicate that we should merge the collected results so that they look like from a single executor. Returns ------- If `merge_multi_context` is ``True``, it is like ``[grad1, grad2]``. Otherwise, it is like ``[[grad1_dev1, grad1_dev2], [grad2_dev1, grad2_dev2]]``. All the output elements are `NDArray`. """ assert self.inputs_need_grad if merge_multi_context: return _merge_multi_context(self.input_grad_arrays, self.data_layouts) return self.input_grad_arrays
python
def get_input_grads(self, merge_multi_context=True): """Get the gradients with respect to the inputs of the module. Parameters ---------- merge_multi_context : bool Defaults to ``True``. In the case when data-parallelism is used, the outputs will be collected from multiple devices. A `True` value indicate that we should merge the collected results so that they look like from a single executor. Returns ------- If `merge_multi_context` is ``True``, it is like ``[grad1, grad2]``. Otherwise, it is like ``[[grad1_dev1, grad1_dev2], [grad2_dev1, grad2_dev2]]``. All the output elements are `NDArray`. """ assert self.inputs_need_grad if merge_multi_context: return _merge_multi_context(self.input_grad_arrays, self.data_layouts) return self.input_grad_arrays
[ "def", "get_input_grads", "(", "self", ",", "merge_multi_context", "=", "True", ")", ":", "assert", "self", ".", "inputs_need_grad", "if", "merge_multi_context", ":", "return", "_merge_multi_context", "(", "self", ".", "input_grad_arrays", ",", "self", ".", "data_layouts", ")", "return", "self", ".", "input_grad_arrays" ]
Get the gradients with respect to the inputs of the module. Parameters ---------- merge_multi_context : bool Defaults to ``True``. In the case when data-parallelism is used, the outputs will be collected from multiple devices. A `True` value indicate that we should merge the collected results so that they look like from a single executor. Returns ------- If `merge_multi_context` is ``True``, it is like ``[grad1, grad2]``. Otherwise, it is like ``[[grad1_dev1, grad1_dev2], [grad2_dev1, grad2_dev2]]``. All the output elements are `NDArray`.
[ "Get", "the", "gradients", "with", "respect", "to", "the", "inputs", "of", "the", "module", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/module/executor_group.py#L550-L570
train
apache/incubator-mxnet
python/mxnet/module/executor_group.py
DataParallelExecutorGroup.backward
def backward(self, out_grads=None): """Run backward on all devices. A backward should be called after a call to the forward function. Backward cannot be called unless ``self.for_training`` is ``True``. Parameters ---------- out_grads : NDArray or list of NDArray, optional Gradient on the outputs to be propagated back. This parameter is only needed when bind is called on outputs that are not a loss function. """ assert self.for_training, 're-bind with for_training=True to run backward' if out_grads is None: out_grads = [] for i, (exec_, islice) in enumerate(zip(self.execs, self.slices)): out_grads_slice = [] for grad, axis in zip(out_grads, self.output_layouts): if axis >= 0: # pylint: disable=no-member og_my_slice = nd.slice_axis(grad, axis=axis, begin=islice.start, end=islice.stop) out_grads_slice.append(og_my_slice.as_in_context(self.contexts[i])) # pylint: enable=no-member else: out_grads_slice.append(grad.copyto(self.contexts[i])) exec_.backward(out_grads=out_grads_slice)
python
def backward(self, out_grads=None): """Run backward on all devices. A backward should be called after a call to the forward function. Backward cannot be called unless ``self.for_training`` is ``True``. Parameters ---------- out_grads : NDArray or list of NDArray, optional Gradient on the outputs to be propagated back. This parameter is only needed when bind is called on outputs that are not a loss function. """ assert self.for_training, 're-bind with for_training=True to run backward' if out_grads is None: out_grads = [] for i, (exec_, islice) in enumerate(zip(self.execs, self.slices)): out_grads_slice = [] for grad, axis in zip(out_grads, self.output_layouts): if axis >= 0: # pylint: disable=no-member og_my_slice = nd.slice_axis(grad, axis=axis, begin=islice.start, end=islice.stop) out_grads_slice.append(og_my_slice.as_in_context(self.contexts[i])) # pylint: enable=no-member else: out_grads_slice.append(grad.copyto(self.contexts[i])) exec_.backward(out_grads=out_grads_slice)
[ "def", "backward", "(", "self", ",", "out_grads", "=", "None", ")", ":", "assert", "self", ".", "for_training", ",", "'re-bind with for_training=True to run backward'", "if", "out_grads", "is", "None", ":", "out_grads", "=", "[", "]", "for", "i", ",", "(", "exec_", ",", "islice", ")", "in", "enumerate", "(", "zip", "(", "self", ".", "execs", ",", "self", ".", "slices", ")", ")", ":", "out_grads_slice", "=", "[", "]", "for", "grad", ",", "axis", "in", "zip", "(", "out_grads", ",", "self", ".", "output_layouts", ")", ":", "if", "axis", ">=", "0", ":", "# pylint: disable=no-member", "og_my_slice", "=", "nd", ".", "slice_axis", "(", "grad", ",", "axis", "=", "axis", ",", "begin", "=", "islice", ".", "start", ",", "end", "=", "islice", ".", "stop", ")", "out_grads_slice", ".", "append", "(", "og_my_slice", ".", "as_in_context", "(", "self", ".", "contexts", "[", "i", "]", ")", ")", "# pylint: enable=no-member", "else", ":", "out_grads_slice", ".", "append", "(", "grad", ".", "copyto", "(", "self", ".", "contexts", "[", "i", "]", ")", ")", "exec_", ".", "backward", "(", "out_grads", "=", "out_grads_slice", ")" ]
Run backward on all devices. A backward should be called after a call to the forward function. Backward cannot be called unless ``self.for_training`` is ``True``. Parameters ---------- out_grads : NDArray or list of NDArray, optional Gradient on the outputs to be propagated back. This parameter is only needed when bind is called on outputs that are not a loss function.
[ "Run", "backward", "on", "all", "devices", ".", "A", "backward", "should", "be", "called", "after", "a", "call", "to", "the", "forward", "function", ".", "Backward", "cannot", "be", "called", "unless", "self", ".", "for_training", "is", "True", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/module/executor_group.py#L572-L599
train
apache/incubator-mxnet
python/mxnet/module/executor_group.py
DataParallelExecutorGroup.update_metric
def update_metric(self, eval_metric, labels, pre_sliced): """Accumulate the performance according to `eval_metric` on all devices by comparing outputs from [begin, end) to labels. By default use all outputs. Parameters ---------- eval_metric : EvalMetric The metric used for evaluation. labels : list of NDArray Typically comes from `label` of a `DataBatch`. pre_sliced : bool Whether labels are already sliced. begin : int Starting index of used outputs. end : int or None Ending index of used outputs. """ for current_exec, (texec, islice) in enumerate(zip(self.execs, self.slices)): if not pre_sliced: labels_slice = [] for label, axis in zip(labels, self.label_layouts): if axis == 0: # slicing NDArray along axis 0 can avoid copying labels_slice.append(label[islice]) elif axis > 0: # pylint: disable=no-member label_my_slice = nd.slice_axis(label, axis=axis, begin=islice.start, end=islice.stop).as_in_context(label.context) # pylint: enable=no-member labels_slice.append(label_my_slice) else: labels_slice.append(label) else: labels_slice = labels[current_exec] labels_ = OrderedDict(zip(self.label_names, labels_slice)) preds = OrderedDict(zip(self.output_names, texec.outputs)) eval_metric.update_dict(labels_, preds)
python
def update_metric(self, eval_metric, labels, pre_sliced): """Accumulate the performance according to `eval_metric` on all devices by comparing outputs from [begin, end) to labels. By default use all outputs. Parameters ---------- eval_metric : EvalMetric The metric used for evaluation. labels : list of NDArray Typically comes from `label` of a `DataBatch`. pre_sliced : bool Whether labels are already sliced. begin : int Starting index of used outputs. end : int or None Ending index of used outputs. """ for current_exec, (texec, islice) in enumerate(zip(self.execs, self.slices)): if not pre_sliced: labels_slice = [] for label, axis in zip(labels, self.label_layouts): if axis == 0: # slicing NDArray along axis 0 can avoid copying labels_slice.append(label[islice]) elif axis > 0: # pylint: disable=no-member label_my_slice = nd.slice_axis(label, axis=axis, begin=islice.start, end=islice.stop).as_in_context(label.context) # pylint: enable=no-member labels_slice.append(label_my_slice) else: labels_slice.append(label) else: labels_slice = labels[current_exec] labels_ = OrderedDict(zip(self.label_names, labels_slice)) preds = OrderedDict(zip(self.output_names, texec.outputs)) eval_metric.update_dict(labels_, preds)
[ "def", "update_metric", "(", "self", ",", "eval_metric", ",", "labels", ",", "pre_sliced", ")", ":", "for", "current_exec", ",", "(", "texec", ",", "islice", ")", "in", "enumerate", "(", "zip", "(", "self", ".", "execs", ",", "self", ".", "slices", ")", ")", ":", "if", "not", "pre_sliced", ":", "labels_slice", "=", "[", "]", "for", "label", ",", "axis", "in", "zip", "(", "labels", ",", "self", ".", "label_layouts", ")", ":", "if", "axis", "==", "0", ":", "# slicing NDArray along axis 0 can avoid copying", "labels_slice", ".", "append", "(", "label", "[", "islice", "]", ")", "elif", "axis", ">", "0", ":", "# pylint: disable=no-member", "label_my_slice", "=", "nd", ".", "slice_axis", "(", "label", ",", "axis", "=", "axis", ",", "begin", "=", "islice", ".", "start", ",", "end", "=", "islice", ".", "stop", ")", ".", "as_in_context", "(", "label", ".", "context", ")", "# pylint: enable=no-member", "labels_slice", ".", "append", "(", "label_my_slice", ")", "else", ":", "labels_slice", ".", "append", "(", "label", ")", "else", ":", "labels_slice", "=", "labels", "[", "current_exec", "]", "labels_", "=", "OrderedDict", "(", "zip", "(", "self", ".", "label_names", ",", "labels_slice", ")", ")", "preds", "=", "OrderedDict", "(", "zip", "(", "self", ".", "output_names", ",", "texec", ".", "outputs", ")", ")", "eval_metric", ".", "update_dict", "(", "labels_", ",", "preds", ")" ]
Accumulate the performance according to `eval_metric` on all devices by comparing outputs from [begin, end) to labels. By default use all outputs. Parameters ---------- eval_metric : EvalMetric The metric used for evaluation. labels : list of NDArray Typically comes from `label` of a `DataBatch`. pre_sliced : bool Whether labels are already sliced. begin : int Starting index of used outputs. end : int or None Ending index of used outputs.
[ "Accumulate", "the", "performance", "according", "to", "eval_metric", "on", "all", "devices", "by", "comparing", "outputs", "from", "[", "begin", "end", ")", "to", "labels", ".", "By", "default", "use", "all", "outputs", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/module/executor_group.py#L601-L639
train
apache/incubator-mxnet
python/mxnet/module/executor_group.py
DataParallelExecutorGroup._bind_ith_exec
def _bind_ith_exec(self, i, data_shapes, label_shapes, shared_group): """Internal utility function to bind the i-th executor. This function utilizes simple_bind python interface. """ shared_exec = None if shared_group is None else shared_group.execs[i] context = self.contexts[i] shared_data_arrays = self.shared_data_arrays[i] input_shapes = dict(data_shapes) if label_shapes is not None: input_shapes.update(dict(label_shapes)) input_types = {x.name: x.dtype for x in data_shapes} if label_shapes is not None: input_types.update({x.name: x.dtype for x in label_shapes}) group2ctx = self.group2ctxs[i] executor = self.symbol.simple_bind(ctx=context, grad_req=self.grad_req, type_dict=input_types, shared_arg_names=self.param_names, shared_exec=shared_exec, group2ctx=group2ctx, shared_buffer=shared_data_arrays, **input_shapes) self._total_exec_bytes += int(executor.debug_str().split('\n')[-3].split()[1]) return executor
python
def _bind_ith_exec(self, i, data_shapes, label_shapes, shared_group): """Internal utility function to bind the i-th executor. This function utilizes simple_bind python interface. """ shared_exec = None if shared_group is None else shared_group.execs[i] context = self.contexts[i] shared_data_arrays = self.shared_data_arrays[i] input_shapes = dict(data_shapes) if label_shapes is not None: input_shapes.update(dict(label_shapes)) input_types = {x.name: x.dtype for x in data_shapes} if label_shapes is not None: input_types.update({x.name: x.dtype for x in label_shapes}) group2ctx = self.group2ctxs[i] executor = self.symbol.simple_bind(ctx=context, grad_req=self.grad_req, type_dict=input_types, shared_arg_names=self.param_names, shared_exec=shared_exec, group2ctx=group2ctx, shared_buffer=shared_data_arrays, **input_shapes) self._total_exec_bytes += int(executor.debug_str().split('\n')[-3].split()[1]) return executor
[ "def", "_bind_ith_exec", "(", "self", ",", "i", ",", "data_shapes", ",", "label_shapes", ",", "shared_group", ")", ":", "shared_exec", "=", "None", "if", "shared_group", "is", "None", "else", "shared_group", ".", "execs", "[", "i", "]", "context", "=", "self", ".", "contexts", "[", "i", "]", "shared_data_arrays", "=", "self", ".", "shared_data_arrays", "[", "i", "]", "input_shapes", "=", "dict", "(", "data_shapes", ")", "if", "label_shapes", "is", "not", "None", ":", "input_shapes", ".", "update", "(", "dict", "(", "label_shapes", ")", ")", "input_types", "=", "{", "x", ".", "name", ":", "x", ".", "dtype", "for", "x", "in", "data_shapes", "}", "if", "label_shapes", "is", "not", "None", ":", "input_types", ".", "update", "(", "{", "x", ".", "name", ":", "x", ".", "dtype", "for", "x", "in", "label_shapes", "}", ")", "group2ctx", "=", "self", ".", "group2ctxs", "[", "i", "]", "executor", "=", "self", ".", "symbol", ".", "simple_bind", "(", "ctx", "=", "context", ",", "grad_req", "=", "self", ".", "grad_req", ",", "type_dict", "=", "input_types", ",", "shared_arg_names", "=", "self", ".", "param_names", ",", "shared_exec", "=", "shared_exec", ",", "group2ctx", "=", "group2ctx", ",", "shared_buffer", "=", "shared_data_arrays", ",", "*", "*", "input_shapes", ")", "self", ".", "_total_exec_bytes", "+=", "int", "(", "executor", ".", "debug_str", "(", ")", ".", "split", "(", "'\\n'", ")", "[", "-", "3", "]", ".", "split", "(", ")", "[", "1", "]", ")", "return", "executor" ]
Internal utility function to bind the i-th executor. This function utilizes simple_bind python interface.
[ "Internal", "utility", "function", "to", "bind", "the", "i", "-", "th", "executor", ".", "This", "function", "utilizes", "simple_bind", "python", "interface", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/module/executor_group.py#L641-L664
train
apache/incubator-mxnet
python/mxnet/module/executor_group.py
DataParallelExecutorGroup._sliced_shape
def _sliced_shape(self, shapes, i, major_axis): """Get the sliced shapes for the i-th executor. Parameters ---------- shapes : list of (str, tuple) The original (name, shape) pairs. i : int Which executor we are dealing with. """ sliced_shapes = [] for desc, axis in zip(shapes, major_axis): shape = list(desc.shape) if axis >= 0: shape[axis] = self.slices[i].stop - self.slices[i].start sliced_shapes.append(DataDesc(desc.name, tuple(shape), desc.dtype, desc.layout)) return sliced_shapes
python
def _sliced_shape(self, shapes, i, major_axis): """Get the sliced shapes for the i-th executor. Parameters ---------- shapes : list of (str, tuple) The original (name, shape) pairs. i : int Which executor we are dealing with. """ sliced_shapes = [] for desc, axis in zip(shapes, major_axis): shape = list(desc.shape) if axis >= 0: shape[axis] = self.slices[i].stop - self.slices[i].start sliced_shapes.append(DataDesc(desc.name, tuple(shape), desc.dtype, desc.layout)) return sliced_shapes
[ "def", "_sliced_shape", "(", "self", ",", "shapes", ",", "i", ",", "major_axis", ")", ":", "sliced_shapes", "=", "[", "]", "for", "desc", ",", "axis", "in", "zip", "(", "shapes", ",", "major_axis", ")", ":", "shape", "=", "list", "(", "desc", ".", "shape", ")", "if", "axis", ">=", "0", ":", "shape", "[", "axis", "]", "=", "self", ".", "slices", "[", "i", "]", ".", "stop", "-", "self", ".", "slices", "[", "i", "]", ".", "start", "sliced_shapes", ".", "append", "(", "DataDesc", "(", "desc", ".", "name", ",", "tuple", "(", "shape", ")", ",", "desc", ".", "dtype", ",", "desc", ".", "layout", ")", ")", "return", "sliced_shapes" ]
Get the sliced shapes for the i-th executor. Parameters ---------- shapes : list of (str, tuple) The original (name, shape) pairs. i : int Which executor we are dealing with.
[ "Get", "the", "sliced", "shapes", "for", "the", "i", "-", "th", "executor", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/module/executor_group.py#L666-L682
train
apache/incubator-mxnet
example/ssd/train.py
parse_class_names
def parse_class_names(args): """ parse # classes and class_names if applicable """ num_class = args.num_class if len(args.class_names) > 0: if os.path.isfile(args.class_names): # try to open it to read class names with open(args.class_names, 'r') as f: class_names = [l.strip() for l in f.readlines()] else: class_names = [c.strip() for c in args.class_names.split(',')] assert len(class_names) == num_class, str(len(class_names)) for name in class_names: assert len(name) > 0 else: class_names = None return class_names
python
def parse_class_names(args): """ parse # classes and class_names if applicable """ num_class = args.num_class if len(args.class_names) > 0: if os.path.isfile(args.class_names): # try to open it to read class names with open(args.class_names, 'r') as f: class_names = [l.strip() for l in f.readlines()] else: class_names = [c.strip() for c in args.class_names.split(',')] assert len(class_names) == num_class, str(len(class_names)) for name in class_names: assert len(name) > 0 else: class_names = None return class_names
[ "def", "parse_class_names", "(", "args", ")", ":", "num_class", "=", "args", ".", "num_class", "if", "len", "(", "args", ".", "class_names", ")", ">", "0", ":", "if", "os", ".", "path", ".", "isfile", "(", "args", ".", "class_names", ")", ":", "# try to open it to read class names", "with", "open", "(", "args", ".", "class_names", ",", "'r'", ")", "as", "f", ":", "class_names", "=", "[", "l", ".", "strip", "(", ")", "for", "l", "in", "f", ".", "readlines", "(", ")", "]", "else", ":", "class_names", "=", "[", "c", ".", "strip", "(", ")", "for", "c", "in", "args", ".", "class_names", ".", "split", "(", "','", ")", "]", "assert", "len", "(", "class_names", ")", "==", "num_class", ",", "str", "(", "len", "(", "class_names", ")", ")", "for", "name", "in", "class_names", ":", "assert", "len", "(", "name", ")", ">", "0", "else", ":", "class_names", "=", "None", "return", "class_names" ]
parse # classes and class_names if applicable
[ "parse", "#", "classes", "and", "class_names", "if", "applicable" ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/example/ssd/train.py#L111-L126
train
apache/incubator-mxnet
python/mxnet/io/utils.py
_has_instance
def _has_instance(data, dtype): """Return True if ``data`` has instance of ``dtype``. This function is called after _init_data. ``data`` is a list of (str, NDArray)""" for item in data: _, arr = item if isinstance(arr, dtype): return True return False
python
def _has_instance(data, dtype): """Return True if ``data`` has instance of ``dtype``. This function is called after _init_data. ``data`` is a list of (str, NDArray)""" for item in data: _, arr = item if isinstance(arr, dtype): return True return False
[ "def", "_has_instance", "(", "data", ",", "dtype", ")", ":", "for", "item", "in", "data", ":", "_", ",", "arr", "=", "item", "if", "isinstance", "(", "arr", ",", "dtype", ")", ":", "return", "True", "return", "False" ]
Return True if ``data`` has instance of ``dtype``. This function is called after _init_data. ``data`` is a list of (str, NDArray)
[ "Return", "True", "if", "data", "has", "instance", "of", "dtype", ".", "This", "function", "is", "called", "after", "_init_data", ".", "data", "is", "a", "list", "of", "(", "str", "NDArray", ")" ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/io/utils.py#L63-L71
train
apache/incubator-mxnet
python/mxnet/io/utils.py
_getdata_by_idx
def _getdata_by_idx(data, idx): """Shuffle the data.""" shuffle_data = [] for k, v in data: if (isinstance(v, h5py.Dataset) if h5py else False): shuffle_data.append((k, v)) elif isinstance(v, CSRNDArray): shuffle_data.append((k, sparse_array(v.asscipy()[idx], v.context))) else: shuffle_data.append((k, array(v.asnumpy()[idx], v.context))) return shuffle_data
python
def _getdata_by_idx(data, idx): """Shuffle the data.""" shuffle_data = [] for k, v in data: if (isinstance(v, h5py.Dataset) if h5py else False): shuffle_data.append((k, v)) elif isinstance(v, CSRNDArray): shuffle_data.append((k, sparse_array(v.asscipy()[idx], v.context))) else: shuffle_data.append((k, array(v.asnumpy()[idx], v.context))) return shuffle_data
[ "def", "_getdata_by_idx", "(", "data", ",", "idx", ")", ":", "shuffle_data", "=", "[", "]", "for", "k", ",", "v", "in", "data", ":", "if", "(", "isinstance", "(", "v", ",", "h5py", ".", "Dataset", ")", "if", "h5py", "else", "False", ")", ":", "shuffle_data", ".", "append", "(", "(", "k", ",", "v", ")", ")", "elif", "isinstance", "(", "v", ",", "CSRNDArray", ")", ":", "shuffle_data", ".", "append", "(", "(", "k", ",", "sparse_array", "(", "v", ".", "asscipy", "(", ")", "[", "idx", "]", ",", "v", ".", "context", ")", ")", ")", "else", ":", "shuffle_data", ".", "append", "(", "(", "k", ",", "array", "(", "v", ".", "asnumpy", "(", ")", "[", "idx", "]", ",", "v", ".", "context", ")", ")", ")", "return", "shuffle_data" ]
Shuffle the data.
[ "Shuffle", "the", "data", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/io/utils.py#L74-L86
train
apache/incubator-mxnet
python/mxnet/gluon/model_zoo/vision/mobilenet.py
get_mobilenet
def get_mobilenet(multiplier, pretrained=False, ctx=cpu(), root=os.path.join(base.data_dir(), 'models'), **kwargs): r"""MobileNet model from the `"MobileNets: Efficient Convolutional Neural Networks for Mobile Vision Applications" <https://arxiv.org/abs/1704.04861>`_ paper. Parameters ---------- multiplier : float The width multiplier for controling the model size. Only multipliers that are no less than 0.25 are supported. The actual number of channels is equal to the original channel size multiplied by this multiplier. pretrained : bool, default False Whether to load the pretrained weights for model. ctx : Context, default CPU The context in which to load the pretrained weights. root : str, default $MXNET_HOME/models Location for keeping the model parameters. """ net = MobileNet(multiplier, **kwargs) if pretrained: from ..model_store import get_model_file version_suffix = '{0:.2f}'.format(multiplier) if version_suffix in ('1.00', '0.50'): version_suffix = version_suffix[:-1] net.load_parameters( get_model_file('mobilenet%s' % version_suffix, root=root), ctx=ctx) return net
python
def get_mobilenet(multiplier, pretrained=False, ctx=cpu(), root=os.path.join(base.data_dir(), 'models'), **kwargs): r"""MobileNet model from the `"MobileNets: Efficient Convolutional Neural Networks for Mobile Vision Applications" <https://arxiv.org/abs/1704.04861>`_ paper. Parameters ---------- multiplier : float The width multiplier for controling the model size. Only multipliers that are no less than 0.25 are supported. The actual number of channels is equal to the original channel size multiplied by this multiplier. pretrained : bool, default False Whether to load the pretrained weights for model. ctx : Context, default CPU The context in which to load the pretrained weights. root : str, default $MXNET_HOME/models Location for keeping the model parameters. """ net = MobileNet(multiplier, **kwargs) if pretrained: from ..model_store import get_model_file version_suffix = '{0:.2f}'.format(multiplier) if version_suffix in ('1.00', '0.50'): version_suffix = version_suffix[:-1] net.load_parameters( get_model_file('mobilenet%s' % version_suffix, root=root), ctx=ctx) return net
[ "def", "get_mobilenet", "(", "multiplier", ",", "pretrained", "=", "False", ",", "ctx", "=", "cpu", "(", ")", ",", "root", "=", "os", ".", "path", ".", "join", "(", "base", ".", "data_dir", "(", ")", ",", "'models'", ")", ",", "*", "*", "kwargs", ")", ":", "net", "=", "MobileNet", "(", "multiplier", ",", "*", "*", "kwargs", ")", "if", "pretrained", ":", "from", ".", ".", "model_store", "import", "get_model_file", "version_suffix", "=", "'{0:.2f}'", ".", "format", "(", "multiplier", ")", "if", "version_suffix", "in", "(", "'1.00'", ",", "'0.50'", ")", ":", "version_suffix", "=", "version_suffix", "[", ":", "-", "1", "]", "net", ".", "load_parameters", "(", "get_model_file", "(", "'mobilenet%s'", "%", "version_suffix", ",", "root", "=", "root", ")", ",", "ctx", "=", "ctx", ")", "return", "net" ]
r"""MobileNet model from the `"MobileNets: Efficient Convolutional Neural Networks for Mobile Vision Applications" <https://arxiv.org/abs/1704.04861>`_ paper. Parameters ---------- multiplier : float The width multiplier for controling the model size. Only multipliers that are no less than 0.25 are supported. The actual number of channels is equal to the original channel size multiplied by this multiplier. pretrained : bool, default False Whether to load the pretrained weights for model. ctx : Context, default CPU The context in which to load the pretrained weights. root : str, default $MXNET_HOME/models Location for keeping the model parameters.
[ "r", "MobileNet", "model", "from", "the", "MobileNets", ":", "Efficient", "Convolutional", "Neural", "Networks", "for", "Mobile", "Vision", "Applications", "<https", ":", "//", "arxiv", ".", "org", "/", "abs", "/", "1704", ".", "04861", ">", "_", "paper", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/gluon/model_zoo/vision/mobilenet.py#L191-L219
train
apache/incubator-mxnet
python/mxnet/name.py
NameManager.get
def get(self, name, hint): """Get the canonical name for a symbol. This is the default implementation. If the user specifies a name, the user-specified name will be used. When user does not specify a name, we automatically generate a name based on the hint string. Parameters ---------- name : str or None The name specified by the user. hint : str A hint string, which can be used to generate name. Returns ------- full_name : str A canonical name for the symbol. """ if name: return name if hint not in self._counter: self._counter[hint] = 0 name = '%s%d' % (hint, self._counter[hint]) self._counter[hint] += 1 return name
python
def get(self, name, hint): """Get the canonical name for a symbol. This is the default implementation. If the user specifies a name, the user-specified name will be used. When user does not specify a name, we automatically generate a name based on the hint string. Parameters ---------- name : str or None The name specified by the user. hint : str A hint string, which can be used to generate name. Returns ------- full_name : str A canonical name for the symbol. """ if name: return name if hint not in self._counter: self._counter[hint] = 0 name = '%s%d' % (hint, self._counter[hint]) self._counter[hint] += 1 return name
[ "def", "get", "(", "self", ",", "name", ",", "hint", ")", ":", "if", "name", ":", "return", "name", "if", "hint", "not", "in", "self", ".", "_counter", ":", "self", ".", "_counter", "[", "hint", "]", "=", "0", "name", "=", "'%s%d'", "%", "(", "hint", ",", "self", ".", "_counter", "[", "hint", "]", ")", "self", ".", "_counter", "[", "hint", "]", "+=", "1", "return", "name" ]
Get the canonical name for a symbol. This is the default implementation. If the user specifies a name, the user-specified name will be used. When user does not specify a name, we automatically generate a name based on the hint string. Parameters ---------- name : str or None The name specified by the user. hint : str A hint string, which can be used to generate name. Returns ------- full_name : str A canonical name for the symbol.
[ "Get", "the", "canonical", "name", "for", "a", "symbol", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/name.py#L36-L65
train
apache/incubator-mxnet
example/rnn/large_word_lm/sampler.py
LogUniformSampler.draw
def draw(self, true_classes): """Draw samples from log uniform distribution and returns sampled candidates, expected count for true classes and sampled classes.""" range_max = self.range_max num_sampled = self.num_sampled ctx = true_classes.context log_range = math.log(range_max + 1) num_tries = 0 true_classes = true_classes.reshape((-1,)) sampled_classes, num_tries = self.sampler.sample_unique(num_sampled) true_cls = true_classes.as_in_context(ctx).astype('float64') prob_true = ((true_cls + 2.0) / (true_cls + 1.0)).log() / log_range count_true = self._prob_helper(num_tries, num_sampled, prob_true) sampled_classes = ndarray.array(sampled_classes, ctx=ctx, dtype='int64') sampled_cls_fp64 = sampled_classes.astype('float64') prob_sampled = ((sampled_cls_fp64 + 2.0) / (sampled_cls_fp64 + 1.0)).log() / log_range count_sampled = self._prob_helper(num_tries, num_sampled, prob_sampled) return [sampled_classes, count_true, count_sampled]
python
def draw(self, true_classes): """Draw samples from log uniform distribution and returns sampled candidates, expected count for true classes and sampled classes.""" range_max = self.range_max num_sampled = self.num_sampled ctx = true_classes.context log_range = math.log(range_max + 1) num_tries = 0 true_classes = true_classes.reshape((-1,)) sampled_classes, num_tries = self.sampler.sample_unique(num_sampled) true_cls = true_classes.as_in_context(ctx).astype('float64') prob_true = ((true_cls + 2.0) / (true_cls + 1.0)).log() / log_range count_true = self._prob_helper(num_tries, num_sampled, prob_true) sampled_classes = ndarray.array(sampled_classes, ctx=ctx, dtype='int64') sampled_cls_fp64 = sampled_classes.astype('float64') prob_sampled = ((sampled_cls_fp64 + 2.0) / (sampled_cls_fp64 + 1.0)).log() / log_range count_sampled = self._prob_helper(num_tries, num_sampled, prob_sampled) return [sampled_classes, count_true, count_sampled]
[ "def", "draw", "(", "self", ",", "true_classes", ")", ":", "range_max", "=", "self", ".", "range_max", "num_sampled", "=", "self", ".", "num_sampled", "ctx", "=", "true_classes", ".", "context", "log_range", "=", "math", ".", "log", "(", "range_max", "+", "1", ")", "num_tries", "=", "0", "true_classes", "=", "true_classes", ".", "reshape", "(", "(", "-", "1", ",", ")", ")", "sampled_classes", ",", "num_tries", "=", "self", ".", "sampler", ".", "sample_unique", "(", "num_sampled", ")", "true_cls", "=", "true_classes", ".", "as_in_context", "(", "ctx", ")", ".", "astype", "(", "'float64'", ")", "prob_true", "=", "(", "(", "true_cls", "+", "2.0", ")", "/", "(", "true_cls", "+", "1.0", ")", ")", ".", "log", "(", ")", "/", "log_range", "count_true", "=", "self", ".", "_prob_helper", "(", "num_tries", ",", "num_sampled", ",", "prob_true", ")", "sampled_classes", "=", "ndarray", ".", "array", "(", "sampled_classes", ",", "ctx", "=", "ctx", ",", "dtype", "=", "'int64'", ")", "sampled_cls_fp64", "=", "sampled_classes", ".", "astype", "(", "'float64'", ")", "prob_sampled", "=", "(", "(", "sampled_cls_fp64", "+", "2.0", ")", "/", "(", "sampled_cls_fp64", "+", "1.0", ")", ")", ".", "log", "(", ")", "/", "log_range", "count_sampled", "=", "self", ".", "_prob_helper", "(", "num_tries", ",", "num_sampled", ",", "prob_sampled", ")", "return", "[", "sampled_classes", ",", "count_true", ",", "count_sampled", "]" ]
Draw samples from log uniform distribution and returns sampled candidates, expected count for true classes and sampled classes.
[ "Draw", "samples", "from", "log", "uniform", "distribution", "and", "returns", "sampled", "candidates", "expected", "count", "for", "true", "classes", "and", "sampled", "classes", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/example/rnn/large_word_lm/sampler.py#L36-L55
train
apache/incubator-mxnet
example/gluon/dc_gan/inception_score.py
get_inception_score
def get_inception_score(images, splits=10): """ Inception_score function. The images will be divided into 'splits' parts, and calculate each inception_score separately, then return the mean and std of inception_scores of these parts. :param images: Images(num x c x w x h) that needs to calculate inception_score. :param splits: :return: mean and std of inception_score """ assert (images.shape[1] == 3) # load inception model if inception_model is None: _init_inception() # resize images to adapt inception model(inceptionV3) if images.shape[2] != 299: images = resize(images, 299, 299) preds = [] bs = 4 n_batches = int(math.ceil(float(images.shape[0])/float(bs))) # to get the predictions/picture of inception model for i in range(n_batches): sys.stdout.write(".") sys.stdout.flush() inps = images[(i * bs):min((i + 1) * bs, len(images))] # inps size. bs x 3 x 299 x 299 pred = nd.softmax(inception_model(inps)) # pred size. bs x 1000 preds.append(pred.asnumpy()) # list to array preds = np.concatenate(preds, 0) scores = [] # to calculate the inception_score each split. for i in range(splits): # extract per split image pred part = preds[(i * preds.shape[0] // splits):((i + 1) * preds.shape[0] // splits), :] kl = part * (np.log(part) - np.log(np.expand_dims(np.mean(part, 0), 0))) kl = np.mean(np.sum(kl, 1)) scores.append(np.exp(kl)) return np.mean(scores), np.std(scores)
python
def get_inception_score(images, splits=10): """ Inception_score function. The images will be divided into 'splits' parts, and calculate each inception_score separately, then return the mean and std of inception_scores of these parts. :param images: Images(num x c x w x h) that needs to calculate inception_score. :param splits: :return: mean and std of inception_score """ assert (images.shape[1] == 3) # load inception model if inception_model is None: _init_inception() # resize images to adapt inception model(inceptionV3) if images.shape[2] != 299: images = resize(images, 299, 299) preds = [] bs = 4 n_batches = int(math.ceil(float(images.shape[0])/float(bs))) # to get the predictions/picture of inception model for i in range(n_batches): sys.stdout.write(".") sys.stdout.flush() inps = images[(i * bs):min((i + 1) * bs, len(images))] # inps size. bs x 3 x 299 x 299 pred = nd.softmax(inception_model(inps)) # pred size. bs x 1000 preds.append(pred.asnumpy()) # list to array preds = np.concatenate(preds, 0) scores = [] # to calculate the inception_score each split. for i in range(splits): # extract per split image pred part = preds[(i * preds.shape[0] // splits):((i + 1) * preds.shape[0] // splits), :] kl = part * (np.log(part) - np.log(np.expand_dims(np.mean(part, 0), 0))) kl = np.mean(np.sum(kl, 1)) scores.append(np.exp(kl)) return np.mean(scores), np.std(scores)
[ "def", "get_inception_score", "(", "images", ",", "splits", "=", "10", ")", ":", "assert", "(", "images", ".", "shape", "[", "1", "]", "==", "3", ")", "# load inception model", "if", "inception_model", "is", "None", ":", "_init_inception", "(", ")", "# resize images to adapt inception model(inceptionV3)", "if", "images", ".", "shape", "[", "2", "]", "!=", "299", ":", "images", "=", "resize", "(", "images", ",", "299", ",", "299", ")", "preds", "=", "[", "]", "bs", "=", "4", "n_batches", "=", "int", "(", "math", ".", "ceil", "(", "float", "(", "images", ".", "shape", "[", "0", "]", ")", "/", "float", "(", "bs", ")", ")", ")", "# to get the predictions/picture of inception model", "for", "i", "in", "range", "(", "n_batches", ")", ":", "sys", ".", "stdout", ".", "write", "(", "\".\"", ")", "sys", ".", "stdout", ".", "flush", "(", ")", "inps", "=", "images", "[", "(", "i", "*", "bs", ")", ":", "min", "(", "(", "i", "+", "1", ")", "*", "bs", ",", "len", "(", "images", ")", ")", "]", "# inps size. bs x 3 x 299 x 299", "pred", "=", "nd", ".", "softmax", "(", "inception_model", "(", "inps", ")", ")", "# pred size. bs x 1000", "preds", ".", "append", "(", "pred", ".", "asnumpy", "(", ")", ")", "# list to array", "preds", "=", "np", ".", "concatenate", "(", "preds", ",", "0", ")", "scores", "=", "[", "]", "# to calculate the inception_score each split.", "for", "i", "in", "range", "(", "splits", ")", ":", "# extract per split image pred", "part", "=", "preds", "[", "(", "i", "*", "preds", ".", "shape", "[", "0", "]", "//", "splits", ")", ":", "(", "(", "i", "+", "1", ")", "*", "preds", ".", "shape", "[", "0", "]", "//", "splits", ")", ",", ":", "]", "kl", "=", "part", "*", "(", "np", ".", "log", "(", "part", ")", "-", "np", ".", "log", "(", "np", ".", "expand_dims", "(", "np", ".", "mean", "(", "part", ",", "0", ")", ",", "0", ")", ")", ")", "kl", "=", "np", ".", "mean", "(", "np", ".", "sum", "(", "kl", ",", "1", ")", ")", "scores", ".", "append", "(", "np", ".", "exp", "(", "kl", ")", ")", "return", "np", ".", "mean", "(", "scores", ")", ",", "np", ".", "std", "(", "scores", ")" ]
Inception_score function. The images will be divided into 'splits' parts, and calculate each inception_score separately, then return the mean and std of inception_scores of these parts. :param images: Images(num x c x w x h) that needs to calculate inception_score. :param splits: :return: mean and std of inception_score
[ "Inception_score", "function", ".", "The", "images", "will", "be", "divided", "into", "splits", "parts", "and", "calculate", "each", "inception_score", "separately", "then", "return", "the", "mean", "and", "std", "of", "inception_scores", "of", "these", "parts", ".", ":", "param", "images", ":", "Images", "(", "num", "x", "c", "x", "w", "x", "h", ")", "that", "needs", "to", "calculate", "inception_score", ".", ":", "param", "splits", ":", ":", "return", ":", "mean", "and", "std", "of", "inception_score" ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/example/gluon/dc_gan/inception_score.py#L31-L76
train
apache/incubator-mxnet
example/rcnn/symnet/model.py
load_param
def load_param(params, ctx=None): """same as mx.model.load_checkpoint, but do not load symnet and will convert context""" if ctx is None: ctx = mx.cpu() save_dict = mx.nd.load(params) arg_params = {} aux_params = {} for k, v in save_dict.items(): tp, name = k.split(':', 1) if tp == 'arg': arg_params[name] = v.as_in_context(ctx) if tp == 'aux': aux_params[name] = v.as_in_context(ctx) return arg_params, aux_params
python
def load_param(params, ctx=None): """same as mx.model.load_checkpoint, but do not load symnet and will convert context""" if ctx is None: ctx = mx.cpu() save_dict = mx.nd.load(params) arg_params = {} aux_params = {} for k, v in save_dict.items(): tp, name = k.split(':', 1) if tp == 'arg': arg_params[name] = v.as_in_context(ctx) if tp == 'aux': aux_params[name] = v.as_in_context(ctx) return arg_params, aux_params
[ "def", "load_param", "(", "params", ",", "ctx", "=", "None", ")", ":", "if", "ctx", "is", "None", ":", "ctx", "=", "mx", ".", "cpu", "(", ")", "save_dict", "=", "mx", ".", "nd", ".", "load", "(", "params", ")", "arg_params", "=", "{", "}", "aux_params", "=", "{", "}", "for", "k", ",", "v", "in", "save_dict", ".", "items", "(", ")", ":", "tp", ",", "name", "=", "k", ".", "split", "(", "':'", ",", "1", ")", "if", "tp", "==", "'arg'", ":", "arg_params", "[", "name", "]", "=", "v", ".", "as_in_context", "(", "ctx", ")", "if", "tp", "==", "'aux'", ":", "aux_params", "[", "name", "]", "=", "v", ".", "as_in_context", "(", "ctx", ")", "return", "arg_params", ",", "aux_params" ]
same as mx.model.load_checkpoint, but do not load symnet and will convert context
[ "same", "as", "mx", ".", "model", ".", "load_checkpoint", "but", "do", "not", "load", "symnet", "and", "will", "convert", "context" ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/example/rcnn/symnet/model.py#L21-L34
train
apache/incubator-mxnet
python/mxnet/rnn/rnn.py
rnn_unroll
def rnn_unroll(cell, length, inputs=None, begin_state=None, input_prefix='', layout='NTC'): """Deprecated. Please use cell.unroll instead""" warnings.warn('rnn_unroll is deprecated. Please call cell.unroll directly.') return cell.unroll(length=length, inputs=inputs, begin_state=begin_state, input_prefix=input_prefix, layout=layout)
python
def rnn_unroll(cell, length, inputs=None, begin_state=None, input_prefix='', layout='NTC'): """Deprecated. Please use cell.unroll instead""" warnings.warn('rnn_unroll is deprecated. Please call cell.unroll directly.') return cell.unroll(length=length, inputs=inputs, begin_state=begin_state, input_prefix=input_prefix, layout=layout)
[ "def", "rnn_unroll", "(", "cell", ",", "length", ",", "inputs", "=", "None", ",", "begin_state", "=", "None", ",", "input_prefix", "=", "''", ",", "layout", "=", "'NTC'", ")", ":", "warnings", ".", "warn", "(", "'rnn_unroll is deprecated. Please call cell.unroll directly.'", ")", "return", "cell", ".", "unroll", "(", "length", "=", "length", ",", "inputs", "=", "inputs", ",", "begin_state", "=", "begin_state", ",", "input_prefix", "=", "input_prefix", ",", "layout", "=", "layout", ")" ]
Deprecated. Please use cell.unroll instead
[ "Deprecated", ".", "Please", "use", "cell", ".", "unroll", "instead" ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/rnn/rnn.py#L26-L30
train
apache/incubator-mxnet
python/mxnet/rnn/rnn.py
save_rnn_checkpoint
def save_rnn_checkpoint(cells, prefix, epoch, symbol, arg_params, aux_params): """Save checkpoint for model using RNN cells. Unpacks weight before saving. Parameters ---------- cells : mxnet.rnn.RNNCell or list of RNNCells The RNN cells used by this symbol. prefix : str Prefix of model name. epoch : int The epoch number of the model. symbol : Symbol The input symbol arg_params : dict of str to NDArray Model parameter, dict of name to NDArray of net's weights. aux_params : dict of str to NDArray Model parameter, dict of name to NDArray of net's auxiliary states. Notes ----- - ``prefix-symbol.json`` will be saved for symbol. - ``prefix-epoch.params`` will be saved for parameters. """ if isinstance(cells, BaseRNNCell): cells = [cells] for cell in cells: arg_params = cell.unpack_weights(arg_params) save_checkpoint(prefix, epoch, symbol, arg_params, aux_params)
python
def save_rnn_checkpoint(cells, prefix, epoch, symbol, arg_params, aux_params): """Save checkpoint for model using RNN cells. Unpacks weight before saving. Parameters ---------- cells : mxnet.rnn.RNNCell or list of RNNCells The RNN cells used by this symbol. prefix : str Prefix of model name. epoch : int The epoch number of the model. symbol : Symbol The input symbol arg_params : dict of str to NDArray Model parameter, dict of name to NDArray of net's weights. aux_params : dict of str to NDArray Model parameter, dict of name to NDArray of net's auxiliary states. Notes ----- - ``prefix-symbol.json`` will be saved for symbol. - ``prefix-epoch.params`` will be saved for parameters. """ if isinstance(cells, BaseRNNCell): cells = [cells] for cell in cells: arg_params = cell.unpack_weights(arg_params) save_checkpoint(prefix, epoch, symbol, arg_params, aux_params)
[ "def", "save_rnn_checkpoint", "(", "cells", ",", "prefix", ",", "epoch", ",", "symbol", ",", "arg_params", ",", "aux_params", ")", ":", "if", "isinstance", "(", "cells", ",", "BaseRNNCell", ")", ":", "cells", "=", "[", "cells", "]", "for", "cell", "in", "cells", ":", "arg_params", "=", "cell", ".", "unpack_weights", "(", "arg_params", ")", "save_checkpoint", "(", "prefix", ",", "epoch", ",", "symbol", ",", "arg_params", ",", "aux_params", ")" ]
Save checkpoint for model using RNN cells. Unpacks weight before saving. Parameters ---------- cells : mxnet.rnn.RNNCell or list of RNNCells The RNN cells used by this symbol. prefix : str Prefix of model name. epoch : int The epoch number of the model. symbol : Symbol The input symbol arg_params : dict of str to NDArray Model parameter, dict of name to NDArray of net's weights. aux_params : dict of str to NDArray Model parameter, dict of name to NDArray of net's auxiliary states. Notes ----- - ``prefix-symbol.json`` will be saved for symbol. - ``prefix-epoch.params`` will be saved for parameters.
[ "Save", "checkpoint", "for", "model", "using", "RNN", "cells", ".", "Unpacks", "weight", "before", "saving", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/rnn/rnn.py#L32-L60
train
apache/incubator-mxnet
python/mxnet/rnn/rnn.py
load_rnn_checkpoint
def load_rnn_checkpoint(cells, prefix, epoch): """Load model checkpoint from file. Pack weights after loading. Parameters ---------- cells : mxnet.rnn.RNNCell or list of RNNCells The RNN cells used by this symbol. prefix : str Prefix of model name. epoch : int Epoch number of model we would like to load. Returns ------- symbol : Symbol The symbol configuration of computation network. arg_params : dict of str to NDArray Model parameter, dict of name to NDArray of net's weights. aux_params : dict of str to NDArray Model parameter, dict of name to NDArray of net's auxiliary states. Notes ----- - symbol will be loaded from ``prefix-symbol.json``. - parameters will be loaded from ``prefix-epoch.params``. """ sym, arg, aux = load_checkpoint(prefix, epoch) if isinstance(cells, BaseRNNCell): cells = [cells] for cell in cells: arg = cell.pack_weights(arg) return sym, arg, aux
python
def load_rnn_checkpoint(cells, prefix, epoch): """Load model checkpoint from file. Pack weights after loading. Parameters ---------- cells : mxnet.rnn.RNNCell or list of RNNCells The RNN cells used by this symbol. prefix : str Prefix of model name. epoch : int Epoch number of model we would like to load. Returns ------- symbol : Symbol The symbol configuration of computation network. arg_params : dict of str to NDArray Model parameter, dict of name to NDArray of net's weights. aux_params : dict of str to NDArray Model parameter, dict of name to NDArray of net's auxiliary states. Notes ----- - symbol will be loaded from ``prefix-symbol.json``. - parameters will be loaded from ``prefix-epoch.params``. """ sym, arg, aux = load_checkpoint(prefix, epoch) if isinstance(cells, BaseRNNCell): cells = [cells] for cell in cells: arg = cell.pack_weights(arg) return sym, arg, aux
[ "def", "load_rnn_checkpoint", "(", "cells", ",", "prefix", ",", "epoch", ")", ":", "sym", ",", "arg", ",", "aux", "=", "load_checkpoint", "(", "prefix", ",", "epoch", ")", "if", "isinstance", "(", "cells", ",", "BaseRNNCell", ")", ":", "cells", "=", "[", "cells", "]", "for", "cell", "in", "cells", ":", "arg", "=", "cell", ".", "pack_weights", "(", "arg", ")", "return", "sym", ",", "arg", ",", "aux" ]
Load model checkpoint from file. Pack weights after loading. Parameters ---------- cells : mxnet.rnn.RNNCell or list of RNNCells The RNN cells used by this symbol. prefix : str Prefix of model name. epoch : int Epoch number of model we would like to load. Returns ------- symbol : Symbol The symbol configuration of computation network. arg_params : dict of str to NDArray Model parameter, dict of name to NDArray of net's weights. aux_params : dict of str to NDArray Model parameter, dict of name to NDArray of net's auxiliary states. Notes ----- - symbol will be loaded from ``prefix-symbol.json``. - parameters will be loaded from ``prefix-epoch.params``.
[ "Load", "model", "checkpoint", "from", "file", ".", "Pack", "weights", "after", "loading", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/rnn/rnn.py#L62-L95
train
apache/incubator-mxnet
python/mxnet/rnn/rnn.py
do_rnn_checkpoint
def do_rnn_checkpoint(cells, prefix, period=1): """Make a callback to checkpoint Module to prefix every epoch. unpacks weights used by cells before saving. Parameters ---------- cells : mxnet.rnn.RNNCell or list of RNNCells The RNN cells used by this symbol. prefix : str The file prefix to checkpoint to period : int How many epochs to wait before checkpointing. Default is 1. Returns ------- callback : function The callback function that can be passed as iter_end_callback to fit. """ period = int(max(1, period)) # pylint: disable=unused-argument def _callback(iter_no, sym=None, arg=None, aux=None): """The checkpoint function.""" if (iter_no + 1) % period == 0: save_rnn_checkpoint(cells, prefix, iter_no+1, sym, arg, aux) return _callback
python
def do_rnn_checkpoint(cells, prefix, period=1): """Make a callback to checkpoint Module to prefix every epoch. unpacks weights used by cells before saving. Parameters ---------- cells : mxnet.rnn.RNNCell or list of RNNCells The RNN cells used by this symbol. prefix : str The file prefix to checkpoint to period : int How many epochs to wait before checkpointing. Default is 1. Returns ------- callback : function The callback function that can be passed as iter_end_callback to fit. """ period = int(max(1, period)) # pylint: disable=unused-argument def _callback(iter_no, sym=None, arg=None, aux=None): """The checkpoint function.""" if (iter_no + 1) % period == 0: save_rnn_checkpoint(cells, prefix, iter_no+1, sym, arg, aux) return _callback
[ "def", "do_rnn_checkpoint", "(", "cells", ",", "prefix", ",", "period", "=", "1", ")", ":", "period", "=", "int", "(", "max", "(", "1", ",", "period", ")", ")", "# pylint: disable=unused-argument", "def", "_callback", "(", "iter_no", ",", "sym", "=", "None", ",", "arg", "=", "None", ",", "aux", "=", "None", ")", ":", "\"\"\"The checkpoint function.\"\"\"", "if", "(", "iter_no", "+", "1", ")", "%", "period", "==", "0", ":", "save_rnn_checkpoint", "(", "cells", ",", "prefix", ",", "iter_no", "+", "1", ",", "sym", ",", "arg", ",", "aux", ")", "return", "_callback" ]
Make a callback to checkpoint Module to prefix every epoch. unpacks weights used by cells before saving. Parameters ---------- cells : mxnet.rnn.RNNCell or list of RNNCells The RNN cells used by this symbol. prefix : str The file prefix to checkpoint to period : int How many epochs to wait before checkpointing. Default is 1. Returns ------- callback : function The callback function that can be passed as iter_end_callback to fit.
[ "Make", "a", "callback", "to", "checkpoint", "Module", "to", "prefix", "every", "epoch", ".", "unpacks", "weights", "used", "by", "cells", "before", "saving", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/rnn/rnn.py#L97-L121
train
apache/incubator-mxnet
python/mxnet/gluon/nn/basic_layers.py
Sequential.hybridize
def hybridize(self, active=True, **kwargs): """Activates or deactivates `HybridBlock` s recursively. Has no effect on non-hybrid children. Parameters ---------- active : bool, default True Whether to turn hybrid on or off. **kwargs : string Additional flags for hybridized operator. """ if self._children and all(isinstance(c, HybridBlock) for c in self._children.values()): warnings.warn( "All children of this Sequential layer '%s' are HybridBlocks. Consider " "using HybridSequential for the best performance."%self.prefix, stacklevel=2) super(Sequential, self).hybridize(active, **kwargs)
python
def hybridize(self, active=True, **kwargs): """Activates or deactivates `HybridBlock` s recursively. Has no effect on non-hybrid children. Parameters ---------- active : bool, default True Whether to turn hybrid on or off. **kwargs : string Additional flags for hybridized operator. """ if self._children and all(isinstance(c, HybridBlock) for c in self._children.values()): warnings.warn( "All children of this Sequential layer '%s' are HybridBlocks. Consider " "using HybridSequential for the best performance."%self.prefix, stacklevel=2) super(Sequential, self).hybridize(active, **kwargs)
[ "def", "hybridize", "(", "self", ",", "active", "=", "True", ",", "*", "*", "kwargs", ")", ":", "if", "self", ".", "_children", "and", "all", "(", "isinstance", "(", "c", ",", "HybridBlock", ")", "for", "c", "in", "self", ".", "_children", ".", "values", "(", ")", ")", ":", "warnings", ".", "warn", "(", "\"All children of this Sequential layer '%s' are HybridBlocks. Consider \"", "\"using HybridSequential for the best performance.\"", "%", "self", ".", "prefix", ",", "stacklevel", "=", "2", ")", "super", "(", "Sequential", ",", "self", ")", ".", "hybridize", "(", "active", ",", "*", "*", "kwargs", ")" ]
Activates or deactivates `HybridBlock` s recursively. Has no effect on non-hybrid children. Parameters ---------- active : bool, default True Whether to turn hybrid on or off. **kwargs : string Additional flags for hybridized operator.
[ "Activates", "or", "deactivates", "HybridBlock", "s", "recursively", ".", "Has", "no", "effect", "on", "non", "-", "hybrid", "children", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/gluon/nn/basic_layers.py#L77-L92
train
apache/incubator-mxnet
example/ctc/lstm_ocr_infer.py
read_img
def read_img(path): """ Reads image specified by path into numpy.ndarray""" img = cv2.resize(cv2.imread(path, 0), (80, 30)).astype(np.float32) / 255 img = np.expand_dims(img.transpose(1, 0), 0) return img
python
def read_img(path): """ Reads image specified by path into numpy.ndarray""" img = cv2.resize(cv2.imread(path, 0), (80, 30)).astype(np.float32) / 255 img = np.expand_dims(img.transpose(1, 0), 0) return img
[ "def", "read_img", "(", "path", ")", ":", "img", "=", "cv2", ".", "resize", "(", "cv2", ".", "imread", "(", "path", ",", "0", ")", ",", "(", "80", ",", "30", ")", ")", ".", "astype", "(", "np", ".", "float32", ")", "/", "255", "img", "=", "np", ".", "expand_dims", "(", "img", ".", "transpose", "(", "1", ",", "0", ")", ",", "0", ")", "return", "img" ]
Reads image specified by path into numpy.ndarray
[ "Reads", "image", "specified", "by", "path", "into", "numpy", ".", "ndarray" ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/example/ctc/lstm_ocr_infer.py#L32-L36
train
apache/incubator-mxnet
example/ctc/lstm_ocr_infer.py
lstm_init_states
def lstm_init_states(batch_size): """ Returns a tuple of names and zero arrays for LSTM init states""" hp = Hyperparams() init_shapes = lstm.init_states(batch_size=batch_size, num_lstm_layer=hp.num_lstm_layer, num_hidden=hp.num_hidden) init_names = [s[0] for s in init_shapes] init_arrays = [mx.nd.zeros(x[1]) for x in init_shapes] return init_names, init_arrays
python
def lstm_init_states(batch_size): """ Returns a tuple of names and zero arrays for LSTM init states""" hp = Hyperparams() init_shapes = lstm.init_states(batch_size=batch_size, num_lstm_layer=hp.num_lstm_layer, num_hidden=hp.num_hidden) init_names = [s[0] for s in init_shapes] init_arrays = [mx.nd.zeros(x[1]) for x in init_shapes] return init_names, init_arrays
[ "def", "lstm_init_states", "(", "batch_size", ")", ":", "hp", "=", "Hyperparams", "(", ")", "init_shapes", "=", "lstm", ".", "init_states", "(", "batch_size", "=", "batch_size", ",", "num_lstm_layer", "=", "hp", ".", "num_lstm_layer", ",", "num_hidden", "=", "hp", ".", "num_hidden", ")", "init_names", "=", "[", "s", "[", "0", "]", "for", "s", "in", "init_shapes", "]", "init_arrays", "=", "[", "mx", ".", "nd", ".", "zeros", "(", "x", "[", "1", "]", ")", "for", "x", "in", "init_shapes", "]", "return", "init_names", ",", "init_arrays" ]
Returns a tuple of names and zero arrays for LSTM init states
[ "Returns", "a", "tuple", "of", "names", "and", "zero", "arrays", "for", "LSTM", "init", "states" ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/example/ctc/lstm_ocr_infer.py#L39-L45
train
apache/incubator-mxnet
example/ctc/lstm_ocr_infer.py
load_module
def load_module(prefix, epoch, data_names, data_shapes): """Loads the model from checkpoint specified by prefix and epoch, binds it to an executor, and sets its parameters and returns a mx.mod.Module """ sym, arg_params, aux_params = mx.model.load_checkpoint(prefix, epoch) # We don't need CTC loss for prediction, just a simple softmax will suffice. # We get the output of the layer just before the loss layer ('pred_fc') and add softmax on top pred_fc = sym.get_internals()['pred_fc_output'] sym = mx.sym.softmax(data=pred_fc) mod = mx.mod.Module(symbol=sym, context=mx.cpu(), data_names=data_names, label_names=None) mod.bind(for_training=False, data_shapes=data_shapes) mod.set_params(arg_params, aux_params, allow_missing=False) return mod
python
def load_module(prefix, epoch, data_names, data_shapes): """Loads the model from checkpoint specified by prefix and epoch, binds it to an executor, and sets its parameters and returns a mx.mod.Module """ sym, arg_params, aux_params = mx.model.load_checkpoint(prefix, epoch) # We don't need CTC loss for prediction, just a simple softmax will suffice. # We get the output of the layer just before the loss layer ('pred_fc') and add softmax on top pred_fc = sym.get_internals()['pred_fc_output'] sym = mx.sym.softmax(data=pred_fc) mod = mx.mod.Module(symbol=sym, context=mx.cpu(), data_names=data_names, label_names=None) mod.bind(for_training=False, data_shapes=data_shapes) mod.set_params(arg_params, aux_params, allow_missing=False) return mod
[ "def", "load_module", "(", "prefix", ",", "epoch", ",", "data_names", ",", "data_shapes", ")", ":", "sym", ",", "arg_params", ",", "aux_params", "=", "mx", ".", "model", ".", "load_checkpoint", "(", "prefix", ",", "epoch", ")", "# We don't need CTC loss for prediction, just a simple softmax will suffice.", "# We get the output of the layer just before the loss layer ('pred_fc') and add softmax on top", "pred_fc", "=", "sym", ".", "get_internals", "(", ")", "[", "'pred_fc_output'", "]", "sym", "=", "mx", ".", "sym", ".", "softmax", "(", "data", "=", "pred_fc", ")", "mod", "=", "mx", ".", "mod", ".", "Module", "(", "symbol", "=", "sym", ",", "context", "=", "mx", ".", "cpu", "(", ")", ",", "data_names", "=", "data_names", ",", "label_names", "=", "None", ")", "mod", ".", "bind", "(", "for_training", "=", "False", ",", "data_shapes", "=", "data_shapes", ")", "mod", ".", "set_params", "(", "arg_params", ",", "aux_params", ",", "allow_missing", "=", "False", ")", "return", "mod" ]
Loads the model from checkpoint specified by prefix and epoch, binds it to an executor, and sets its parameters and returns a mx.mod.Module
[ "Loads", "the", "model", "from", "checkpoint", "specified", "by", "prefix", "and", "epoch", "binds", "it", "to", "an", "executor", "and", "sets", "its", "parameters", "and", "returns", "a", "mx", ".", "mod", ".", "Module" ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/example/ctc/lstm_ocr_infer.py#L48-L62
train
apache/incubator-mxnet
example/ctc/lstm_ocr_infer.py
main
def main(): """Program entry point""" parser = argparse.ArgumentParser() parser.add_argument("path", help="Path to the CAPTCHA image file") parser.add_argument("--prefix", help="Checkpoint prefix [Default 'ocr']", default='ocr') parser.add_argument("--epoch", help="Checkpoint epoch [Default 100]", type=int, default=100) args = parser.parse_args() init_state_names, init_state_arrays = lstm_init_states(batch_size=1) img = read_img(args.path) sample = SimpleBatch( data_names=['data'] + init_state_names, data=[mx.nd.array(img)] + init_state_arrays) mod = load_module(args.prefix, args.epoch, sample.data_names, sample.provide_data) mod.forward(sample) prob = mod.get_outputs()[0].asnumpy() prediction = CtcMetrics.ctc_label(np.argmax(prob, axis=-1).tolist()) # Predictions are 1 to 10 for digits 0 to 9 respectively (prediction 0 means no-digit) prediction = [p - 1 for p in prediction] print("Digits:", prediction)
python
def main(): """Program entry point""" parser = argparse.ArgumentParser() parser.add_argument("path", help="Path to the CAPTCHA image file") parser.add_argument("--prefix", help="Checkpoint prefix [Default 'ocr']", default='ocr') parser.add_argument("--epoch", help="Checkpoint epoch [Default 100]", type=int, default=100) args = parser.parse_args() init_state_names, init_state_arrays = lstm_init_states(batch_size=1) img = read_img(args.path) sample = SimpleBatch( data_names=['data'] + init_state_names, data=[mx.nd.array(img)] + init_state_arrays) mod = load_module(args.prefix, args.epoch, sample.data_names, sample.provide_data) mod.forward(sample) prob = mod.get_outputs()[0].asnumpy() prediction = CtcMetrics.ctc_label(np.argmax(prob, axis=-1).tolist()) # Predictions are 1 to 10 for digits 0 to 9 respectively (prediction 0 means no-digit) prediction = [p - 1 for p in prediction] print("Digits:", prediction)
[ "def", "main", "(", ")", ":", "parser", "=", "argparse", ".", "ArgumentParser", "(", ")", "parser", ".", "add_argument", "(", "\"path\"", ",", "help", "=", "\"Path to the CAPTCHA image file\"", ")", "parser", ".", "add_argument", "(", "\"--prefix\"", ",", "help", "=", "\"Checkpoint prefix [Default 'ocr']\"", ",", "default", "=", "'ocr'", ")", "parser", ".", "add_argument", "(", "\"--epoch\"", ",", "help", "=", "\"Checkpoint epoch [Default 100]\"", ",", "type", "=", "int", ",", "default", "=", "100", ")", "args", "=", "parser", ".", "parse_args", "(", ")", "init_state_names", ",", "init_state_arrays", "=", "lstm_init_states", "(", "batch_size", "=", "1", ")", "img", "=", "read_img", "(", "args", ".", "path", ")", "sample", "=", "SimpleBatch", "(", "data_names", "=", "[", "'data'", "]", "+", "init_state_names", ",", "data", "=", "[", "mx", ".", "nd", ".", "array", "(", "img", ")", "]", "+", "init_state_arrays", ")", "mod", "=", "load_module", "(", "args", ".", "prefix", ",", "args", ".", "epoch", ",", "sample", ".", "data_names", ",", "sample", ".", "provide_data", ")", "mod", ".", "forward", "(", "sample", ")", "prob", "=", "mod", ".", "get_outputs", "(", ")", "[", "0", "]", ".", "asnumpy", "(", ")", "prediction", "=", "CtcMetrics", ".", "ctc_label", "(", "np", ".", "argmax", "(", "prob", ",", "axis", "=", "-", "1", ")", ".", "tolist", "(", ")", ")", "# Predictions are 1 to 10 for digits 0 to 9 respectively (prediction 0 means no-digit)", "prediction", "=", "[", "p", "-", "1", "for", "p", "in", "prediction", "]", "print", "(", "\"Digits:\"", ",", "prediction", ")" ]
Program entry point
[ "Program", "entry", "point" ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/example/ctc/lstm_ocr_infer.py#L65-L88
train
apache/incubator-mxnet
example/rcnn/symdata/bbox.py
bbox_flip
def bbox_flip(bbox, width, flip_x=False): """ invalid value in bbox_transform if this wrong (no overlap), note index 0 and 2 also note need to save before assignment :param bbox: [n][x1, y1, x2, y2] :param width: cv2 (height, width, channel) :param flip_x: will flip x1 and x2 :return: flipped box """ if flip_x: xmax = width - bbox[:, 0] xmin = width - bbox[:, 2] bbox[:, 0] = xmin bbox[:, 2] = xmax return bbox
python
def bbox_flip(bbox, width, flip_x=False): """ invalid value in bbox_transform if this wrong (no overlap), note index 0 and 2 also note need to save before assignment :param bbox: [n][x1, y1, x2, y2] :param width: cv2 (height, width, channel) :param flip_x: will flip x1 and x2 :return: flipped box """ if flip_x: xmax = width - bbox[:, 0] xmin = width - bbox[:, 2] bbox[:, 0] = xmin bbox[:, 2] = xmax return bbox
[ "def", "bbox_flip", "(", "bbox", ",", "width", ",", "flip_x", "=", "False", ")", ":", "if", "flip_x", ":", "xmax", "=", "width", "-", "bbox", "[", ":", ",", "0", "]", "xmin", "=", "width", "-", "bbox", "[", ":", ",", "2", "]", "bbox", "[", ":", ",", "0", "]", "=", "xmin", "bbox", "[", ":", ",", "2", "]", "=", "xmax", "return", "bbox" ]
invalid value in bbox_transform if this wrong (no overlap), note index 0 and 2 also note need to save before assignment :param bbox: [n][x1, y1, x2, y2] :param width: cv2 (height, width, channel) :param flip_x: will flip x1 and x2 :return: flipped box
[ "invalid", "value", "in", "bbox_transform", "if", "this", "wrong", "(", "no", "overlap", ")", "note", "index", "0", "and", "2", "also", "note", "need", "to", "save", "before", "assignment", ":", "param", "bbox", ":", "[", "n", "]", "[", "x1", "y1", "x2", "y2", "]", ":", "param", "width", ":", "cv2", "(", "height", "width", "channel", ")", ":", "param", "flip_x", ":", "will", "flip", "x1", "and", "x2", ":", "return", ":", "flipped", "box" ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/example/rcnn/symdata/bbox.py#L21-L35
train
apache/incubator-mxnet
example/rcnn/symdata/bbox.py
bbox_overlaps
def bbox_overlaps(boxes, query_boxes): """ determine overlaps between boxes and query_boxes :param boxes: n * 4 bounding boxes :param query_boxes: k * 4 bounding boxes :return: overlaps: n * k overlaps """ n_ = boxes.shape[0] k_ = query_boxes.shape[0] overlaps = np.zeros((n_, k_), dtype=np.float) for k in range(k_): query_box_area = (query_boxes[k, 2] - query_boxes[k, 0] + 1) * (query_boxes[k, 3] - query_boxes[k, 1] + 1) for n in range(n_): iw = min(boxes[n, 2], query_boxes[k, 2]) - max(boxes[n, 0], query_boxes[k, 0]) + 1 if iw > 0: ih = min(boxes[n, 3], query_boxes[k, 3]) - max(boxes[n, 1], query_boxes[k, 1]) + 1 if ih > 0: box_area = (boxes[n, 2] - boxes[n, 0] + 1) * (boxes[n, 3] - boxes[n, 1] + 1) all_area = float(box_area + query_box_area - iw * ih) overlaps[n, k] = iw * ih / all_area return overlaps
python
def bbox_overlaps(boxes, query_boxes): """ determine overlaps between boxes and query_boxes :param boxes: n * 4 bounding boxes :param query_boxes: k * 4 bounding boxes :return: overlaps: n * k overlaps """ n_ = boxes.shape[0] k_ = query_boxes.shape[0] overlaps = np.zeros((n_, k_), dtype=np.float) for k in range(k_): query_box_area = (query_boxes[k, 2] - query_boxes[k, 0] + 1) * (query_boxes[k, 3] - query_boxes[k, 1] + 1) for n in range(n_): iw = min(boxes[n, 2], query_boxes[k, 2]) - max(boxes[n, 0], query_boxes[k, 0]) + 1 if iw > 0: ih = min(boxes[n, 3], query_boxes[k, 3]) - max(boxes[n, 1], query_boxes[k, 1]) + 1 if ih > 0: box_area = (boxes[n, 2] - boxes[n, 0] + 1) * (boxes[n, 3] - boxes[n, 1] + 1) all_area = float(box_area + query_box_area - iw * ih) overlaps[n, k] = iw * ih / all_area return overlaps
[ "def", "bbox_overlaps", "(", "boxes", ",", "query_boxes", ")", ":", "n_", "=", "boxes", ".", "shape", "[", "0", "]", "k_", "=", "query_boxes", ".", "shape", "[", "0", "]", "overlaps", "=", "np", ".", "zeros", "(", "(", "n_", ",", "k_", ")", ",", "dtype", "=", "np", ".", "float", ")", "for", "k", "in", "range", "(", "k_", ")", ":", "query_box_area", "=", "(", "query_boxes", "[", "k", ",", "2", "]", "-", "query_boxes", "[", "k", ",", "0", "]", "+", "1", ")", "*", "(", "query_boxes", "[", "k", ",", "3", "]", "-", "query_boxes", "[", "k", ",", "1", "]", "+", "1", ")", "for", "n", "in", "range", "(", "n_", ")", ":", "iw", "=", "min", "(", "boxes", "[", "n", ",", "2", "]", ",", "query_boxes", "[", "k", ",", "2", "]", ")", "-", "max", "(", "boxes", "[", "n", ",", "0", "]", ",", "query_boxes", "[", "k", ",", "0", "]", ")", "+", "1", "if", "iw", ">", "0", ":", "ih", "=", "min", "(", "boxes", "[", "n", ",", "3", "]", ",", "query_boxes", "[", "k", ",", "3", "]", ")", "-", "max", "(", "boxes", "[", "n", ",", "1", "]", ",", "query_boxes", "[", "k", ",", "1", "]", ")", "+", "1", "if", "ih", ">", "0", ":", "box_area", "=", "(", "boxes", "[", "n", ",", "2", "]", "-", "boxes", "[", "n", ",", "0", "]", "+", "1", ")", "*", "(", "boxes", "[", "n", ",", "3", "]", "-", "boxes", "[", "n", ",", "1", "]", "+", "1", ")", "all_area", "=", "float", "(", "box_area", "+", "query_box_area", "-", "iw", "*", "ih", ")", "overlaps", "[", "n", ",", "k", "]", "=", "iw", "*", "ih", "/", "all_area", "return", "overlaps" ]
determine overlaps between boxes and query_boxes :param boxes: n * 4 bounding boxes :param query_boxes: k * 4 bounding boxes :return: overlaps: n * k overlaps
[ "determine", "overlaps", "between", "boxes", "and", "query_boxes", ":", "param", "boxes", ":", "n", "*", "4", "bounding", "boxes", ":", "param", "query_boxes", ":", "k", "*", "4", "bounding", "boxes", ":", "return", ":", "overlaps", ":", "n", "*", "k", "overlaps" ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/example/rcnn/symdata/bbox.py#L38-L58
train
apache/incubator-mxnet
example/rcnn/symdata/bbox.py
clip_boxes
def clip_boxes(boxes, im_shape): """ Clip boxes to image boundaries. :param boxes: [N, 4* num_classes] :param im_shape: tuple of 2 :return: [N, 4* num_classes] """ # x1 >= 0 boxes[:, 0::4] = np.maximum(np.minimum(boxes[:, 0::4], im_shape[1] - 1), 0) # y1 >= 0 boxes[:, 1::4] = np.maximum(np.minimum(boxes[:, 1::4], im_shape[0] - 1), 0) # x2 < im_shape[1] boxes[:, 2::4] = np.maximum(np.minimum(boxes[:, 2::4], im_shape[1] - 1), 0) # y2 < im_shape[0] boxes[:, 3::4] = np.maximum(np.minimum(boxes[:, 3::4], im_shape[0] - 1), 0) return boxes
python
def clip_boxes(boxes, im_shape): """ Clip boxes to image boundaries. :param boxes: [N, 4* num_classes] :param im_shape: tuple of 2 :return: [N, 4* num_classes] """ # x1 >= 0 boxes[:, 0::4] = np.maximum(np.minimum(boxes[:, 0::4], im_shape[1] - 1), 0) # y1 >= 0 boxes[:, 1::4] = np.maximum(np.minimum(boxes[:, 1::4], im_shape[0] - 1), 0) # x2 < im_shape[1] boxes[:, 2::4] = np.maximum(np.minimum(boxes[:, 2::4], im_shape[1] - 1), 0) # y2 < im_shape[0] boxes[:, 3::4] = np.maximum(np.minimum(boxes[:, 3::4], im_shape[0] - 1), 0) return boxes
[ "def", "clip_boxes", "(", "boxes", ",", "im_shape", ")", ":", "# x1 >= 0", "boxes", "[", ":", ",", "0", ":", ":", "4", "]", "=", "np", ".", "maximum", "(", "np", ".", "minimum", "(", "boxes", "[", ":", ",", "0", ":", ":", "4", "]", ",", "im_shape", "[", "1", "]", "-", "1", ")", ",", "0", ")", "# y1 >= 0", "boxes", "[", ":", ",", "1", ":", ":", "4", "]", "=", "np", ".", "maximum", "(", "np", ".", "minimum", "(", "boxes", "[", ":", ",", "1", ":", ":", "4", "]", ",", "im_shape", "[", "0", "]", "-", "1", ")", ",", "0", ")", "# x2 < im_shape[1]", "boxes", "[", ":", ",", "2", ":", ":", "4", "]", "=", "np", ".", "maximum", "(", "np", ".", "minimum", "(", "boxes", "[", ":", ",", "2", ":", ":", "4", "]", ",", "im_shape", "[", "1", "]", "-", "1", ")", ",", "0", ")", "# y2 < im_shape[0]", "boxes", "[", ":", ",", "3", ":", ":", "4", "]", "=", "np", ".", "maximum", "(", "np", ".", "minimum", "(", "boxes", "[", ":", ",", "3", ":", ":", "4", "]", ",", "im_shape", "[", "0", "]", "-", "1", ")", ",", "0", ")", "return", "boxes" ]
Clip boxes to image boundaries. :param boxes: [N, 4* num_classes] :param im_shape: tuple of 2 :return: [N, 4* num_classes]
[ "Clip", "boxes", "to", "image", "boundaries", ".", ":", "param", "boxes", ":", "[", "N", "4", "*", "num_classes", "]", ":", "param", "im_shape", ":", "tuple", "of", "2", ":", "return", ":", "[", "N", "4", "*", "num_classes", "]" ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/example/rcnn/symdata/bbox.py#L61-L76
train
apache/incubator-mxnet
example/rcnn/symdata/bbox.py
bbox_transform
def bbox_transform(ex_rois, gt_rois, box_stds): """ compute bounding box regression targets from ex_rois to gt_rois :param ex_rois: [N, 4] :param gt_rois: [N, 4] :return: [N, 4] """ assert ex_rois.shape[0] == gt_rois.shape[0], 'inconsistent rois number' ex_widths = ex_rois[:, 2] - ex_rois[:, 0] + 1.0 ex_heights = ex_rois[:, 3] - ex_rois[:, 1] + 1.0 ex_ctr_x = ex_rois[:, 0] + 0.5 * (ex_widths - 1.0) ex_ctr_y = ex_rois[:, 1] + 0.5 * (ex_heights - 1.0) gt_widths = gt_rois[:, 2] - gt_rois[:, 0] + 1.0 gt_heights = gt_rois[:, 3] - gt_rois[:, 1] + 1.0 gt_ctr_x = gt_rois[:, 0] + 0.5 * (gt_widths - 1.0) gt_ctr_y = gt_rois[:, 1] + 0.5 * (gt_heights - 1.0) targets_dx = (gt_ctr_x - ex_ctr_x) / (ex_widths + 1e-14) / box_stds[0] targets_dy = (gt_ctr_y - ex_ctr_y) / (ex_heights + 1e-14) / box_stds[1] targets_dw = np.log(gt_widths / ex_widths) / box_stds[2] targets_dh = np.log(gt_heights / ex_heights) / box_stds[3] targets = np.vstack((targets_dx, targets_dy, targets_dw, targets_dh)).transpose() return targets
python
def bbox_transform(ex_rois, gt_rois, box_stds): """ compute bounding box regression targets from ex_rois to gt_rois :param ex_rois: [N, 4] :param gt_rois: [N, 4] :return: [N, 4] """ assert ex_rois.shape[0] == gt_rois.shape[0], 'inconsistent rois number' ex_widths = ex_rois[:, 2] - ex_rois[:, 0] + 1.0 ex_heights = ex_rois[:, 3] - ex_rois[:, 1] + 1.0 ex_ctr_x = ex_rois[:, 0] + 0.5 * (ex_widths - 1.0) ex_ctr_y = ex_rois[:, 1] + 0.5 * (ex_heights - 1.0) gt_widths = gt_rois[:, 2] - gt_rois[:, 0] + 1.0 gt_heights = gt_rois[:, 3] - gt_rois[:, 1] + 1.0 gt_ctr_x = gt_rois[:, 0] + 0.5 * (gt_widths - 1.0) gt_ctr_y = gt_rois[:, 1] + 0.5 * (gt_heights - 1.0) targets_dx = (gt_ctr_x - ex_ctr_x) / (ex_widths + 1e-14) / box_stds[0] targets_dy = (gt_ctr_y - ex_ctr_y) / (ex_heights + 1e-14) / box_stds[1] targets_dw = np.log(gt_widths / ex_widths) / box_stds[2] targets_dh = np.log(gt_heights / ex_heights) / box_stds[3] targets = np.vstack((targets_dx, targets_dy, targets_dw, targets_dh)).transpose() return targets
[ "def", "bbox_transform", "(", "ex_rois", ",", "gt_rois", ",", "box_stds", ")", ":", "assert", "ex_rois", ".", "shape", "[", "0", "]", "==", "gt_rois", ".", "shape", "[", "0", "]", ",", "'inconsistent rois number'", "ex_widths", "=", "ex_rois", "[", ":", ",", "2", "]", "-", "ex_rois", "[", ":", ",", "0", "]", "+", "1.0", "ex_heights", "=", "ex_rois", "[", ":", ",", "3", "]", "-", "ex_rois", "[", ":", ",", "1", "]", "+", "1.0", "ex_ctr_x", "=", "ex_rois", "[", ":", ",", "0", "]", "+", "0.5", "*", "(", "ex_widths", "-", "1.0", ")", "ex_ctr_y", "=", "ex_rois", "[", ":", ",", "1", "]", "+", "0.5", "*", "(", "ex_heights", "-", "1.0", ")", "gt_widths", "=", "gt_rois", "[", ":", ",", "2", "]", "-", "gt_rois", "[", ":", ",", "0", "]", "+", "1.0", "gt_heights", "=", "gt_rois", "[", ":", ",", "3", "]", "-", "gt_rois", "[", ":", ",", "1", "]", "+", "1.0", "gt_ctr_x", "=", "gt_rois", "[", ":", ",", "0", "]", "+", "0.5", "*", "(", "gt_widths", "-", "1.0", ")", "gt_ctr_y", "=", "gt_rois", "[", ":", ",", "1", "]", "+", "0.5", "*", "(", "gt_heights", "-", "1.0", ")", "targets_dx", "=", "(", "gt_ctr_x", "-", "ex_ctr_x", ")", "/", "(", "ex_widths", "+", "1e-14", ")", "/", "box_stds", "[", "0", "]", "targets_dy", "=", "(", "gt_ctr_y", "-", "ex_ctr_y", ")", "/", "(", "ex_heights", "+", "1e-14", ")", "/", "box_stds", "[", "1", "]", "targets_dw", "=", "np", ".", "log", "(", "gt_widths", "/", "ex_widths", ")", "/", "box_stds", "[", "2", "]", "targets_dh", "=", "np", ".", "log", "(", "gt_heights", "/", "ex_heights", ")", "/", "box_stds", "[", "3", "]", "targets", "=", "np", ".", "vstack", "(", "(", "targets_dx", ",", "targets_dy", ",", "targets_dw", ",", "targets_dh", ")", ")", ".", "transpose", "(", ")", "return", "targets" ]
compute bounding box regression targets from ex_rois to gt_rois :param ex_rois: [N, 4] :param gt_rois: [N, 4] :return: [N, 4]
[ "compute", "bounding", "box", "regression", "targets", "from", "ex_rois", "to", "gt_rois", ":", "param", "ex_rois", ":", "[", "N", "4", "]", ":", "param", "gt_rois", ":", "[", "N", "4", "]", ":", "return", ":", "[", "N", "4", "]" ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/example/rcnn/symdata/bbox.py#L79-L104
train
apache/incubator-mxnet
example/rcnn/symdata/bbox.py
bbox_pred
def bbox_pred(boxes, box_deltas, box_stds): """ Transform the set of class-agnostic boxes into class-specific boxes by applying the predicted offsets (box_deltas) :param boxes: !important [N 4] :param box_deltas: [N, 4 * num_classes] :return: [N 4 * num_classes] """ if boxes.shape[0] == 0: return np.zeros((0, box_deltas.shape[1])) widths = boxes[:, 2] - boxes[:, 0] + 1.0 heights = boxes[:, 3] - boxes[:, 1] + 1.0 ctr_x = boxes[:, 0] + 0.5 * (widths - 1.0) ctr_y = boxes[:, 1] + 0.5 * (heights - 1.0) dx = box_deltas[:, 0::4] * box_stds[0] dy = box_deltas[:, 1::4] * box_stds[1] dw = box_deltas[:, 2::4] * box_stds[2] dh = box_deltas[:, 3::4] * box_stds[3] pred_ctr_x = dx * widths[:, np.newaxis] + ctr_x[:, np.newaxis] pred_ctr_y = dy * heights[:, np.newaxis] + ctr_y[:, np.newaxis] pred_w = np.exp(dw) * widths[:, np.newaxis] pred_h = np.exp(dh) * heights[:, np.newaxis] pred_boxes = np.zeros(box_deltas.shape) # x1 pred_boxes[:, 0::4] = pred_ctr_x - 0.5 * (pred_w - 1.0) # y1 pred_boxes[:, 1::4] = pred_ctr_y - 0.5 * (pred_h - 1.0) # x2 pred_boxes[:, 2::4] = pred_ctr_x + 0.5 * (pred_w - 1.0) # y2 pred_boxes[:, 3::4] = pred_ctr_y + 0.5 * (pred_h - 1.0) return pred_boxes
python
def bbox_pred(boxes, box_deltas, box_stds): """ Transform the set of class-agnostic boxes into class-specific boxes by applying the predicted offsets (box_deltas) :param boxes: !important [N 4] :param box_deltas: [N, 4 * num_classes] :return: [N 4 * num_classes] """ if boxes.shape[0] == 0: return np.zeros((0, box_deltas.shape[1])) widths = boxes[:, 2] - boxes[:, 0] + 1.0 heights = boxes[:, 3] - boxes[:, 1] + 1.0 ctr_x = boxes[:, 0] + 0.5 * (widths - 1.0) ctr_y = boxes[:, 1] + 0.5 * (heights - 1.0) dx = box_deltas[:, 0::4] * box_stds[0] dy = box_deltas[:, 1::4] * box_stds[1] dw = box_deltas[:, 2::4] * box_stds[2] dh = box_deltas[:, 3::4] * box_stds[3] pred_ctr_x = dx * widths[:, np.newaxis] + ctr_x[:, np.newaxis] pred_ctr_y = dy * heights[:, np.newaxis] + ctr_y[:, np.newaxis] pred_w = np.exp(dw) * widths[:, np.newaxis] pred_h = np.exp(dh) * heights[:, np.newaxis] pred_boxes = np.zeros(box_deltas.shape) # x1 pred_boxes[:, 0::4] = pred_ctr_x - 0.5 * (pred_w - 1.0) # y1 pred_boxes[:, 1::4] = pred_ctr_y - 0.5 * (pred_h - 1.0) # x2 pred_boxes[:, 2::4] = pred_ctr_x + 0.5 * (pred_w - 1.0) # y2 pred_boxes[:, 3::4] = pred_ctr_y + 0.5 * (pred_h - 1.0) return pred_boxes
[ "def", "bbox_pred", "(", "boxes", ",", "box_deltas", ",", "box_stds", ")", ":", "if", "boxes", ".", "shape", "[", "0", "]", "==", "0", ":", "return", "np", ".", "zeros", "(", "(", "0", ",", "box_deltas", ".", "shape", "[", "1", "]", ")", ")", "widths", "=", "boxes", "[", ":", ",", "2", "]", "-", "boxes", "[", ":", ",", "0", "]", "+", "1.0", "heights", "=", "boxes", "[", ":", ",", "3", "]", "-", "boxes", "[", ":", ",", "1", "]", "+", "1.0", "ctr_x", "=", "boxes", "[", ":", ",", "0", "]", "+", "0.5", "*", "(", "widths", "-", "1.0", ")", "ctr_y", "=", "boxes", "[", ":", ",", "1", "]", "+", "0.5", "*", "(", "heights", "-", "1.0", ")", "dx", "=", "box_deltas", "[", ":", ",", "0", ":", ":", "4", "]", "*", "box_stds", "[", "0", "]", "dy", "=", "box_deltas", "[", ":", ",", "1", ":", ":", "4", "]", "*", "box_stds", "[", "1", "]", "dw", "=", "box_deltas", "[", ":", ",", "2", ":", ":", "4", "]", "*", "box_stds", "[", "2", "]", "dh", "=", "box_deltas", "[", ":", ",", "3", ":", ":", "4", "]", "*", "box_stds", "[", "3", "]", "pred_ctr_x", "=", "dx", "*", "widths", "[", ":", ",", "np", ".", "newaxis", "]", "+", "ctr_x", "[", ":", ",", "np", ".", "newaxis", "]", "pred_ctr_y", "=", "dy", "*", "heights", "[", ":", ",", "np", ".", "newaxis", "]", "+", "ctr_y", "[", ":", ",", "np", ".", "newaxis", "]", "pred_w", "=", "np", ".", "exp", "(", "dw", ")", "*", "widths", "[", ":", ",", "np", ".", "newaxis", "]", "pred_h", "=", "np", ".", "exp", "(", "dh", ")", "*", "heights", "[", ":", ",", "np", ".", "newaxis", "]", "pred_boxes", "=", "np", ".", "zeros", "(", "box_deltas", ".", "shape", ")", "# x1", "pred_boxes", "[", ":", ",", "0", ":", ":", "4", "]", "=", "pred_ctr_x", "-", "0.5", "*", "(", "pred_w", "-", "1.0", ")", "# y1", "pred_boxes", "[", ":", ",", "1", ":", ":", "4", "]", "=", "pred_ctr_y", "-", "0.5", "*", "(", "pred_h", "-", "1.0", ")", "# x2", "pred_boxes", "[", ":", ",", "2", ":", ":", "4", "]", "=", "pred_ctr_x", "+", "0.5", "*", "(", "pred_w", "-", "1.0", ")", "# y2", "pred_boxes", "[", ":", ",", "3", ":", ":", "4", "]", "=", "pred_ctr_y", "+", "0.5", "*", "(", "pred_h", "-", "1.0", ")", "return", "pred_boxes" ]
Transform the set of class-agnostic boxes into class-specific boxes by applying the predicted offsets (box_deltas) :param boxes: !important [N 4] :param box_deltas: [N, 4 * num_classes] :return: [N 4 * num_classes]
[ "Transform", "the", "set", "of", "class", "-", "agnostic", "boxes", "into", "class", "-", "specific", "boxes", "by", "applying", "the", "predicted", "offsets", "(", "box_deltas", ")", ":", "param", "boxes", ":", "!important", "[", "N", "4", "]", ":", "param", "box_deltas", ":", "[", "N", "4", "*", "num_classes", "]", ":", "return", ":", "[", "N", "4", "*", "num_classes", "]" ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/example/rcnn/symdata/bbox.py#L107-L143
train
apache/incubator-mxnet
example/rcnn/symdata/bbox.py
nms
def nms(dets, thresh): """ greedily select boxes with high confidence and overlap with current maximum <= thresh rule out overlap >= thresh :param dets: [[x1, y1, x2, y2 score]] :param thresh: retain overlap < thresh :return: indexes to keep """ x1 = dets[:, 0] y1 = dets[:, 1] x2 = dets[:, 2] y2 = dets[:, 3] scores = dets[:, 4] areas = (x2 - x1 + 1) * (y2 - y1 + 1) order = scores.argsort()[::-1] keep = [] while order.size > 0: i = order[0] keep.append(i) xx1 = np.maximum(x1[i], x1[order[1:]]) yy1 = np.maximum(y1[i], y1[order[1:]]) xx2 = np.minimum(x2[i], x2[order[1:]]) yy2 = np.minimum(y2[i], y2[order[1:]]) w = np.maximum(0.0, xx2 - xx1 + 1) h = np.maximum(0.0, yy2 - yy1 + 1) inter = w * h ovr = inter / (areas[i] + areas[order[1:]] - inter) inds = np.where(ovr <= thresh)[0] order = order[inds + 1] return keep
python
def nms(dets, thresh): """ greedily select boxes with high confidence and overlap with current maximum <= thresh rule out overlap >= thresh :param dets: [[x1, y1, x2, y2 score]] :param thresh: retain overlap < thresh :return: indexes to keep """ x1 = dets[:, 0] y1 = dets[:, 1] x2 = dets[:, 2] y2 = dets[:, 3] scores = dets[:, 4] areas = (x2 - x1 + 1) * (y2 - y1 + 1) order = scores.argsort()[::-1] keep = [] while order.size > 0: i = order[0] keep.append(i) xx1 = np.maximum(x1[i], x1[order[1:]]) yy1 = np.maximum(y1[i], y1[order[1:]]) xx2 = np.minimum(x2[i], x2[order[1:]]) yy2 = np.minimum(y2[i], y2[order[1:]]) w = np.maximum(0.0, xx2 - xx1 + 1) h = np.maximum(0.0, yy2 - yy1 + 1) inter = w * h ovr = inter / (areas[i] + areas[order[1:]] - inter) inds = np.where(ovr <= thresh)[0] order = order[inds + 1] return keep
[ "def", "nms", "(", "dets", ",", "thresh", ")", ":", "x1", "=", "dets", "[", ":", ",", "0", "]", "y1", "=", "dets", "[", ":", ",", "1", "]", "x2", "=", "dets", "[", ":", ",", "2", "]", "y2", "=", "dets", "[", ":", ",", "3", "]", "scores", "=", "dets", "[", ":", ",", "4", "]", "areas", "=", "(", "x2", "-", "x1", "+", "1", ")", "*", "(", "y2", "-", "y1", "+", "1", ")", "order", "=", "scores", ".", "argsort", "(", ")", "[", ":", ":", "-", "1", "]", "keep", "=", "[", "]", "while", "order", ".", "size", ">", "0", ":", "i", "=", "order", "[", "0", "]", "keep", ".", "append", "(", "i", ")", "xx1", "=", "np", ".", "maximum", "(", "x1", "[", "i", "]", ",", "x1", "[", "order", "[", "1", ":", "]", "]", ")", "yy1", "=", "np", ".", "maximum", "(", "y1", "[", "i", "]", ",", "y1", "[", "order", "[", "1", ":", "]", "]", ")", "xx2", "=", "np", ".", "minimum", "(", "x2", "[", "i", "]", ",", "x2", "[", "order", "[", "1", ":", "]", "]", ")", "yy2", "=", "np", ".", "minimum", "(", "y2", "[", "i", "]", ",", "y2", "[", "order", "[", "1", ":", "]", "]", ")", "w", "=", "np", ".", "maximum", "(", "0.0", ",", "xx2", "-", "xx1", "+", "1", ")", "h", "=", "np", ".", "maximum", "(", "0.0", ",", "yy2", "-", "yy1", "+", "1", ")", "inter", "=", "w", "*", "h", "ovr", "=", "inter", "/", "(", "areas", "[", "i", "]", "+", "areas", "[", "order", "[", "1", ":", "]", "]", "-", "inter", ")", "inds", "=", "np", ".", "where", "(", "ovr", "<=", "thresh", ")", "[", "0", "]", "order", "=", "order", "[", "inds", "+", "1", "]", "return", "keep" ]
greedily select boxes with high confidence and overlap with current maximum <= thresh rule out overlap >= thresh :param dets: [[x1, y1, x2, y2 score]] :param thresh: retain overlap < thresh :return: indexes to keep
[ "greedily", "select", "boxes", "with", "high", "confidence", "and", "overlap", "with", "current", "maximum", "<", "=", "thresh", "rule", "out", "overlap", ">", "=", "thresh", ":", "param", "dets", ":", "[[", "x1", "y1", "x2", "y2", "score", "]]", ":", "param", "thresh", ":", "retain", "overlap", "<", "thresh", ":", "return", ":", "indexes", "to", "keep" ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/example/rcnn/symdata/bbox.py#L146-L180
train
apache/incubator-mxnet
example/rcnn/symdata/bbox.py
im_detect
def im_detect(rois, scores, bbox_deltas, im_info, bbox_stds, nms_thresh, conf_thresh): """rois (nroi, 4), scores (nrois, nclasses), bbox_deltas (nrois, 4 * nclasses), im_info (3)""" rois = rois.asnumpy() scores = scores.asnumpy() bbox_deltas = bbox_deltas.asnumpy() im_info = im_info.asnumpy() height, width, scale = im_info # post processing pred_boxes = bbox_pred(rois, bbox_deltas, bbox_stds) pred_boxes = clip_boxes(pred_boxes, (height, width)) # we used scaled image & roi to train, so it is necessary to transform them back pred_boxes = pred_boxes / scale # convert to per class detection results det = [] for j in range(1, scores.shape[-1]): indexes = np.where(scores[:, j] > conf_thresh)[0] cls_scores = scores[indexes, j, np.newaxis] cls_boxes = pred_boxes[indexes, j * 4:(j + 1) * 4] cls_dets = np.hstack((cls_boxes, cls_scores)) keep = nms(cls_dets, thresh=nms_thresh) cls_id = np.ones_like(cls_scores) * j det.append(np.hstack((cls_id, cls_scores, cls_boxes))[keep, :]) # assemble all classes det = np.concatenate(det, axis=0) return det
python
def im_detect(rois, scores, bbox_deltas, im_info, bbox_stds, nms_thresh, conf_thresh): """rois (nroi, 4), scores (nrois, nclasses), bbox_deltas (nrois, 4 * nclasses), im_info (3)""" rois = rois.asnumpy() scores = scores.asnumpy() bbox_deltas = bbox_deltas.asnumpy() im_info = im_info.asnumpy() height, width, scale = im_info # post processing pred_boxes = bbox_pred(rois, bbox_deltas, bbox_stds) pred_boxes = clip_boxes(pred_boxes, (height, width)) # we used scaled image & roi to train, so it is necessary to transform them back pred_boxes = pred_boxes / scale # convert to per class detection results det = [] for j in range(1, scores.shape[-1]): indexes = np.where(scores[:, j] > conf_thresh)[0] cls_scores = scores[indexes, j, np.newaxis] cls_boxes = pred_boxes[indexes, j * 4:(j + 1) * 4] cls_dets = np.hstack((cls_boxes, cls_scores)) keep = nms(cls_dets, thresh=nms_thresh) cls_id = np.ones_like(cls_scores) * j det.append(np.hstack((cls_id, cls_scores, cls_boxes))[keep, :]) # assemble all classes det = np.concatenate(det, axis=0) return det
[ "def", "im_detect", "(", "rois", ",", "scores", ",", "bbox_deltas", ",", "im_info", ",", "bbox_stds", ",", "nms_thresh", ",", "conf_thresh", ")", ":", "rois", "=", "rois", ".", "asnumpy", "(", ")", "scores", "=", "scores", ".", "asnumpy", "(", ")", "bbox_deltas", "=", "bbox_deltas", ".", "asnumpy", "(", ")", "im_info", "=", "im_info", ".", "asnumpy", "(", ")", "height", ",", "width", ",", "scale", "=", "im_info", "# post processing", "pred_boxes", "=", "bbox_pred", "(", "rois", ",", "bbox_deltas", ",", "bbox_stds", ")", "pred_boxes", "=", "clip_boxes", "(", "pred_boxes", ",", "(", "height", ",", "width", ")", ")", "# we used scaled image & roi to train, so it is necessary to transform them back", "pred_boxes", "=", "pred_boxes", "/", "scale", "# convert to per class detection results", "det", "=", "[", "]", "for", "j", "in", "range", "(", "1", ",", "scores", ".", "shape", "[", "-", "1", "]", ")", ":", "indexes", "=", "np", ".", "where", "(", "scores", "[", ":", ",", "j", "]", ">", "conf_thresh", ")", "[", "0", "]", "cls_scores", "=", "scores", "[", "indexes", ",", "j", ",", "np", ".", "newaxis", "]", "cls_boxes", "=", "pred_boxes", "[", "indexes", ",", "j", "*", "4", ":", "(", "j", "+", "1", ")", "*", "4", "]", "cls_dets", "=", "np", ".", "hstack", "(", "(", "cls_boxes", ",", "cls_scores", ")", ")", "keep", "=", "nms", "(", "cls_dets", ",", "thresh", "=", "nms_thresh", ")", "cls_id", "=", "np", ".", "ones_like", "(", "cls_scores", ")", "*", "j", "det", ".", "append", "(", "np", ".", "hstack", "(", "(", "cls_id", ",", "cls_scores", ",", "cls_boxes", ")", ")", "[", "keep", ",", ":", "]", ")", "# assemble all classes", "det", "=", "np", ".", "concatenate", "(", "det", ",", "axis", "=", "0", ")", "return", "det" ]
rois (nroi, 4), scores (nrois, nclasses), bbox_deltas (nrois, 4 * nclasses), im_info (3)
[ "rois", "(", "nroi", "4", ")", "scores", "(", "nrois", "nclasses", ")", "bbox_deltas", "(", "nrois", "4", "*", "nclasses", ")", "im_info", "(", "3", ")" ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/example/rcnn/symdata/bbox.py#L183-L214
train
apache/incubator-mxnet
amalgamation/python/mxnet_predict.py
c_str
def c_str(string): """"Convert a python string to C string.""" if not isinstance(string, str): string = string.decode('ascii') return ctypes.c_char_p(string.encode('utf-8'))
python
def c_str(string): """"Convert a python string to C string.""" if not isinstance(string, str): string = string.decode('ascii') return ctypes.c_char_p(string.encode('utf-8'))
[ "def", "c_str", "(", "string", ")", ":", "if", "not", "isinstance", "(", "string", ",", "str", ")", ":", "string", "=", "string", ".", "decode", "(", "'ascii'", ")", "return", "ctypes", ".", "c_char_p", "(", "string", ".", "encode", "(", "'utf-8'", ")", ")" ]
Convert a python string to C string.
[ "Convert", "a", "python", "string", "to", "C", "string", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/amalgamation/python/mxnet_predict.py#L40-L44
train
apache/incubator-mxnet
amalgamation/python/mxnet_predict.py
_find_lib_path
def _find_lib_path(): """Find mxnet library.""" curr_path = os.path.dirname(os.path.abspath(os.path.expanduser(__file__))) amalgamation_lib_path = os.path.join(curr_path, '../../lib/libmxnet_predict.so') if os.path.exists(amalgamation_lib_path) and os.path.isfile(amalgamation_lib_path): lib_path = [amalgamation_lib_path] return lib_path else: logging.info('Cannot find libmxnet_predict.so. Will search for MXNet library using libinfo.py then.') try: from mxnet.libinfo import find_lib_path lib_path = find_lib_path() return lib_path except ImportError: libinfo_path = os.path.join(curr_path, '../../python/mxnet/libinfo.py') if os.path.exists(libinfo_path) and os.path.isfile(libinfo_path): libinfo = {'__file__': libinfo_path} exec(compile(open(libinfo_path, "rb").read(), libinfo_path, 'exec'), libinfo, libinfo) lib_path = libinfo['find_lib_path']() return lib_path else: raise RuntimeError('Cannot find libinfo.py at %s.' % libinfo_path)
python
def _find_lib_path(): """Find mxnet library.""" curr_path = os.path.dirname(os.path.abspath(os.path.expanduser(__file__))) amalgamation_lib_path = os.path.join(curr_path, '../../lib/libmxnet_predict.so') if os.path.exists(amalgamation_lib_path) and os.path.isfile(amalgamation_lib_path): lib_path = [amalgamation_lib_path] return lib_path else: logging.info('Cannot find libmxnet_predict.so. Will search for MXNet library using libinfo.py then.') try: from mxnet.libinfo import find_lib_path lib_path = find_lib_path() return lib_path except ImportError: libinfo_path = os.path.join(curr_path, '../../python/mxnet/libinfo.py') if os.path.exists(libinfo_path) and os.path.isfile(libinfo_path): libinfo = {'__file__': libinfo_path} exec(compile(open(libinfo_path, "rb").read(), libinfo_path, 'exec'), libinfo, libinfo) lib_path = libinfo['find_lib_path']() return lib_path else: raise RuntimeError('Cannot find libinfo.py at %s.' % libinfo_path)
[ "def", "_find_lib_path", "(", ")", ":", "curr_path", "=", "os", ".", "path", ".", "dirname", "(", "os", ".", "path", ".", "abspath", "(", "os", ".", "path", ".", "expanduser", "(", "__file__", ")", ")", ")", "amalgamation_lib_path", "=", "os", ".", "path", ".", "join", "(", "curr_path", ",", "'../../lib/libmxnet_predict.so'", ")", "if", "os", ".", "path", ".", "exists", "(", "amalgamation_lib_path", ")", "and", "os", ".", "path", ".", "isfile", "(", "amalgamation_lib_path", ")", ":", "lib_path", "=", "[", "amalgamation_lib_path", "]", "return", "lib_path", "else", ":", "logging", ".", "info", "(", "'Cannot find libmxnet_predict.so. Will search for MXNet library using libinfo.py then.'", ")", "try", ":", "from", "mxnet", ".", "libinfo", "import", "find_lib_path", "lib_path", "=", "find_lib_path", "(", ")", "return", "lib_path", "except", "ImportError", ":", "libinfo_path", "=", "os", ".", "path", ".", "join", "(", "curr_path", ",", "'../../python/mxnet/libinfo.py'", ")", "if", "os", ".", "path", ".", "exists", "(", "libinfo_path", ")", "and", "os", ".", "path", ".", "isfile", "(", "libinfo_path", ")", ":", "libinfo", "=", "{", "'__file__'", ":", "libinfo_path", "}", "exec", "(", "compile", "(", "open", "(", "libinfo_path", ",", "\"rb\"", ")", ".", "read", "(", ")", ",", "libinfo_path", ",", "'exec'", ")", ",", "libinfo", ",", "libinfo", ")", "lib_path", "=", "libinfo", "[", "'find_lib_path'", "]", "(", ")", "return", "lib_path", "else", ":", "raise", "RuntimeError", "(", "'Cannot find libinfo.py at %s.'", "%", "libinfo_path", ")" ]
Find mxnet library.
[ "Find", "mxnet", "library", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/amalgamation/python/mxnet_predict.py#L52-L73
train
apache/incubator-mxnet
amalgamation/python/mxnet_predict.py
_load_lib
def _load_lib(): """Load libary by searching possible path.""" lib_path = _find_lib_path() lib = ctypes.cdll.LoadLibrary(lib_path[0]) # DMatrix functions lib.MXGetLastError.restype = ctypes.c_char_p return lib
python
def _load_lib(): """Load libary by searching possible path.""" lib_path = _find_lib_path() lib = ctypes.cdll.LoadLibrary(lib_path[0]) # DMatrix functions lib.MXGetLastError.restype = ctypes.c_char_p return lib
[ "def", "_load_lib", "(", ")", ":", "lib_path", "=", "_find_lib_path", "(", ")", "lib", "=", "ctypes", ".", "cdll", ".", "LoadLibrary", "(", "lib_path", "[", "0", "]", ")", "# DMatrix functions", "lib", ".", "MXGetLastError", ".", "restype", "=", "ctypes", ".", "c_char_p", "return", "lib" ]
Load libary by searching possible path.
[ "Load", "libary", "by", "searching", "possible", "path", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/amalgamation/python/mxnet_predict.py#L76-L82
train
apache/incubator-mxnet
amalgamation/python/mxnet_predict.py
load_ndarray_file
def load_ndarray_file(nd_bytes): """Load ndarray file and return as list of numpy array. Parameters ---------- nd_bytes : str or bytes The internal ndarray bytes Returns ------- out : dict of str to numpy array or list of numpy array The output list or dict, depending on whether the saved type is list or dict. """ handle = NDListHandle() olen = mx_uint() nd_bytes = bytearray(nd_bytes) ptr = (ctypes.c_char * len(nd_bytes)).from_buffer(nd_bytes) _check_call(_LIB.MXNDListCreate( ptr, len(nd_bytes), ctypes.byref(handle), ctypes.byref(olen))) keys = [] arrs = [] for i in range(olen.value): key = ctypes.c_char_p() cptr = mx_float_p() pdata = ctypes.POINTER(mx_uint)() ndim = mx_uint() _check_call(_LIB.MXNDListGet( handle, mx_uint(i), ctypes.byref(key), ctypes.byref(cptr), ctypes.byref(pdata), ctypes.byref(ndim))) shape = tuple(pdata[:ndim.value]) dbuffer = (mx_float * np.prod(shape)).from_address(ctypes.addressof(cptr.contents)) ret = np.frombuffer(dbuffer, dtype=np.float32).reshape(shape) ret = np.array(ret, dtype=np.float32) keys.append(py_str(key.value)) arrs.append(ret) _check_call(_LIB.MXNDListFree(handle)) if len(keys) == 0 or len(keys[0]) == 0: return arrs else: return {keys[i] : arrs[i] for i in range(len(keys))}
python
def load_ndarray_file(nd_bytes): """Load ndarray file and return as list of numpy array. Parameters ---------- nd_bytes : str or bytes The internal ndarray bytes Returns ------- out : dict of str to numpy array or list of numpy array The output list or dict, depending on whether the saved type is list or dict. """ handle = NDListHandle() olen = mx_uint() nd_bytes = bytearray(nd_bytes) ptr = (ctypes.c_char * len(nd_bytes)).from_buffer(nd_bytes) _check_call(_LIB.MXNDListCreate( ptr, len(nd_bytes), ctypes.byref(handle), ctypes.byref(olen))) keys = [] arrs = [] for i in range(olen.value): key = ctypes.c_char_p() cptr = mx_float_p() pdata = ctypes.POINTER(mx_uint)() ndim = mx_uint() _check_call(_LIB.MXNDListGet( handle, mx_uint(i), ctypes.byref(key), ctypes.byref(cptr), ctypes.byref(pdata), ctypes.byref(ndim))) shape = tuple(pdata[:ndim.value]) dbuffer = (mx_float * np.prod(shape)).from_address(ctypes.addressof(cptr.contents)) ret = np.frombuffer(dbuffer, dtype=np.float32).reshape(shape) ret = np.array(ret, dtype=np.float32) keys.append(py_str(key.value)) arrs.append(ret) _check_call(_LIB.MXNDListFree(handle)) if len(keys) == 0 or len(keys[0]) == 0: return arrs else: return {keys[i] : arrs[i] for i in range(len(keys))}
[ "def", "load_ndarray_file", "(", "nd_bytes", ")", ":", "handle", "=", "NDListHandle", "(", ")", "olen", "=", "mx_uint", "(", ")", "nd_bytes", "=", "bytearray", "(", "nd_bytes", ")", "ptr", "=", "(", "ctypes", ".", "c_char", "*", "len", "(", "nd_bytes", ")", ")", ".", "from_buffer", "(", "nd_bytes", ")", "_check_call", "(", "_LIB", ".", "MXNDListCreate", "(", "ptr", ",", "len", "(", "nd_bytes", ")", ",", "ctypes", ".", "byref", "(", "handle", ")", ",", "ctypes", ".", "byref", "(", "olen", ")", ")", ")", "keys", "=", "[", "]", "arrs", "=", "[", "]", "for", "i", "in", "range", "(", "olen", ".", "value", ")", ":", "key", "=", "ctypes", ".", "c_char_p", "(", ")", "cptr", "=", "mx_float_p", "(", ")", "pdata", "=", "ctypes", ".", "POINTER", "(", "mx_uint", ")", "(", ")", "ndim", "=", "mx_uint", "(", ")", "_check_call", "(", "_LIB", ".", "MXNDListGet", "(", "handle", ",", "mx_uint", "(", "i", ")", ",", "ctypes", ".", "byref", "(", "key", ")", ",", "ctypes", ".", "byref", "(", "cptr", ")", ",", "ctypes", ".", "byref", "(", "pdata", ")", ",", "ctypes", ".", "byref", "(", "ndim", ")", ")", ")", "shape", "=", "tuple", "(", "pdata", "[", ":", "ndim", ".", "value", "]", ")", "dbuffer", "=", "(", "mx_float", "*", "np", ".", "prod", "(", "shape", ")", ")", ".", "from_address", "(", "ctypes", ".", "addressof", "(", "cptr", ".", "contents", ")", ")", "ret", "=", "np", ".", "frombuffer", "(", "dbuffer", ",", "dtype", "=", "np", ".", "float32", ")", ".", "reshape", "(", "shape", ")", "ret", "=", "np", ".", "array", "(", "ret", ",", "dtype", "=", "np", ".", "float32", ")", "keys", ".", "append", "(", "py_str", "(", "key", ".", "value", ")", ")", "arrs", ".", "append", "(", "ret", ")", "_check_call", "(", "_LIB", ".", "MXNDListFree", "(", "handle", ")", ")", "if", "len", "(", "keys", ")", "==", "0", "or", "len", "(", "keys", "[", "0", "]", ")", "==", "0", ":", "return", "arrs", "else", ":", "return", "{", "keys", "[", "i", "]", ":", "arrs", "[", "i", "]", "for", "i", "in", "range", "(", "len", "(", "keys", ")", ")", "}" ]
Load ndarray file and return as list of numpy array. Parameters ---------- nd_bytes : str or bytes The internal ndarray bytes Returns ------- out : dict of str to numpy array or list of numpy array The output list or dict, depending on whether the saved type is list or dict.
[ "Load", "ndarray", "file", "and", "return", "as", "list", "of", "numpy", "array", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/amalgamation/python/mxnet_predict.py#L234-L276
train
apache/incubator-mxnet
amalgamation/python/mxnet_predict.py
Predictor.forward
def forward(self, **kwargs): """Perform forward to get the output. Parameters ---------- **kwargs Keyword arguments of input variable name to data. Examples -------- >>> predictor.forward(data=mydata) >>> out = predictor.get_output(0) """ for k, v in kwargs.items(): if not isinstance(v, np.ndarray): raise ValueError("Expect numpy ndarray as input") v = np.asarray(v, dtype=np.float32, order='C') _check_call(_LIB.MXPredSetInput( self.handle, c_str(k), v.ctypes.data_as(mx_float_p), mx_uint(v.size))) _check_call(_LIB.MXPredForward(self.handle))
python
def forward(self, **kwargs): """Perform forward to get the output. Parameters ---------- **kwargs Keyword arguments of input variable name to data. Examples -------- >>> predictor.forward(data=mydata) >>> out = predictor.get_output(0) """ for k, v in kwargs.items(): if not isinstance(v, np.ndarray): raise ValueError("Expect numpy ndarray as input") v = np.asarray(v, dtype=np.float32, order='C') _check_call(_LIB.MXPredSetInput( self.handle, c_str(k), v.ctypes.data_as(mx_float_p), mx_uint(v.size))) _check_call(_LIB.MXPredForward(self.handle))
[ "def", "forward", "(", "self", ",", "*", "*", "kwargs", ")", ":", "for", "k", ",", "v", "in", "kwargs", ".", "items", "(", ")", ":", "if", "not", "isinstance", "(", "v", ",", "np", ".", "ndarray", ")", ":", "raise", "ValueError", "(", "\"Expect numpy ndarray as input\"", ")", "v", "=", "np", ".", "asarray", "(", "v", ",", "dtype", "=", "np", ".", "float32", ",", "order", "=", "'C'", ")", "_check_call", "(", "_LIB", ".", "MXPredSetInput", "(", "self", ".", "handle", ",", "c_str", "(", "k", ")", ",", "v", ".", "ctypes", ".", "data_as", "(", "mx_float_p", ")", ",", "mx_uint", "(", "v", ".", "size", ")", ")", ")", "_check_call", "(", "_LIB", ".", "MXPredForward", "(", "self", ".", "handle", ")", ")" ]
Perform forward to get the output. Parameters ---------- **kwargs Keyword arguments of input variable name to data. Examples -------- >>> predictor.forward(data=mydata) >>> out = predictor.get_output(0)
[ "Perform", "forward", "to", "get", "the", "output", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/amalgamation/python/mxnet_predict.py#L150-L171
train
apache/incubator-mxnet
amalgamation/python/mxnet_predict.py
Predictor.reshape
def reshape(self, input_shapes): """Change the input shape of the predictor. Parameters ---------- input_shapes : dict of str to tuple The new shape of input data. Examples -------- >>> predictor.reshape({'data':data_shape_tuple}) """ indptr = [0] sdata = [] keys = [] for k, v in input_shapes.items(): if not isinstance(v, tuple): raise ValueError("Expect input_shapes to be dict str->tuple") keys.append(c_str(k)) sdata.extend(v) indptr.append(len(sdata)) new_handle = PredictorHandle() _check_call(_LIB.MXPredReshape( mx_uint(len(indptr) - 1), c_array(ctypes.c_char_p, keys), c_array(mx_uint, indptr), c_array(mx_uint, sdata), self.handle, ctypes.byref(new_handle))) _check_call(_LIB.MXPredFree(self.handle)) self.handle = new_handle
python
def reshape(self, input_shapes): """Change the input shape of the predictor. Parameters ---------- input_shapes : dict of str to tuple The new shape of input data. Examples -------- >>> predictor.reshape({'data':data_shape_tuple}) """ indptr = [0] sdata = [] keys = [] for k, v in input_shapes.items(): if not isinstance(v, tuple): raise ValueError("Expect input_shapes to be dict str->tuple") keys.append(c_str(k)) sdata.extend(v) indptr.append(len(sdata)) new_handle = PredictorHandle() _check_call(_LIB.MXPredReshape( mx_uint(len(indptr) - 1), c_array(ctypes.c_char_p, keys), c_array(mx_uint, indptr), c_array(mx_uint, sdata), self.handle, ctypes.byref(new_handle))) _check_call(_LIB.MXPredFree(self.handle)) self.handle = new_handle
[ "def", "reshape", "(", "self", ",", "input_shapes", ")", ":", "indptr", "=", "[", "0", "]", "sdata", "=", "[", "]", "keys", "=", "[", "]", "for", "k", ",", "v", "in", "input_shapes", ".", "items", "(", ")", ":", "if", "not", "isinstance", "(", "v", ",", "tuple", ")", ":", "raise", "ValueError", "(", "\"Expect input_shapes to be dict str->tuple\"", ")", "keys", ".", "append", "(", "c_str", "(", "k", ")", ")", "sdata", ".", "extend", "(", "v", ")", "indptr", ".", "append", "(", "len", "(", "sdata", ")", ")", "new_handle", "=", "PredictorHandle", "(", ")", "_check_call", "(", "_LIB", ".", "MXPredReshape", "(", "mx_uint", "(", "len", "(", "indptr", ")", "-", "1", ")", ",", "c_array", "(", "ctypes", ".", "c_char_p", ",", "keys", ")", ",", "c_array", "(", "mx_uint", ",", "indptr", ")", ",", "c_array", "(", "mx_uint", ",", "sdata", ")", ",", "self", ".", "handle", ",", "ctypes", ".", "byref", "(", "new_handle", ")", ")", ")", "_check_call", "(", "_LIB", ".", "MXPredFree", "(", "self", ".", "handle", ")", ")", "self", ".", "handle", "=", "new_handle" ]
Change the input shape of the predictor. Parameters ---------- input_shapes : dict of str to tuple The new shape of input data. Examples -------- >>> predictor.reshape({'data':data_shape_tuple})
[ "Change", "the", "input", "shape", "of", "the", "predictor", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/amalgamation/python/mxnet_predict.py#L173-L204
train
apache/incubator-mxnet
amalgamation/python/mxnet_predict.py
Predictor.get_output
def get_output(self, index): """Get the index-th output. Parameters ---------- index : int The index of output. Returns ------- out : numpy array. The output array. """ pdata = ctypes.POINTER(mx_uint)() ndim = mx_uint() _check_call(_LIB.MXPredGetOutputShape( self.handle, index, ctypes.byref(pdata), ctypes.byref(ndim))) shape = tuple(pdata[:ndim.value]) data = np.empty(shape, dtype=np.float32) _check_call(_LIB.MXPredGetOutput( self.handle, mx_uint(index), data.ctypes.data_as(mx_float_p), mx_uint(data.size))) return data
python
def get_output(self, index): """Get the index-th output. Parameters ---------- index : int The index of output. Returns ------- out : numpy array. The output array. """ pdata = ctypes.POINTER(mx_uint)() ndim = mx_uint() _check_call(_LIB.MXPredGetOutputShape( self.handle, index, ctypes.byref(pdata), ctypes.byref(ndim))) shape = tuple(pdata[:ndim.value]) data = np.empty(shape, dtype=np.float32) _check_call(_LIB.MXPredGetOutput( self.handle, mx_uint(index), data.ctypes.data_as(mx_float_p), mx_uint(data.size))) return data
[ "def", "get_output", "(", "self", ",", "index", ")", ":", "pdata", "=", "ctypes", ".", "POINTER", "(", "mx_uint", ")", "(", ")", "ndim", "=", "mx_uint", "(", ")", "_check_call", "(", "_LIB", ".", "MXPredGetOutputShape", "(", "self", ".", "handle", ",", "index", ",", "ctypes", ".", "byref", "(", "pdata", ")", ",", "ctypes", ".", "byref", "(", "ndim", ")", ")", ")", "shape", "=", "tuple", "(", "pdata", "[", ":", "ndim", ".", "value", "]", ")", "data", "=", "np", ".", "empty", "(", "shape", ",", "dtype", "=", "np", ".", "float32", ")", "_check_call", "(", "_LIB", ".", "MXPredGetOutput", "(", "self", ".", "handle", ",", "mx_uint", "(", "index", ")", ",", "data", ".", "ctypes", ".", "data_as", "(", "mx_float_p", ")", ",", "mx_uint", "(", "data", ".", "size", ")", ")", ")", "return", "data" ]
Get the index-th output. Parameters ---------- index : int The index of output. Returns ------- out : numpy array. The output array.
[ "Get", "the", "index", "-", "th", "output", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/amalgamation/python/mxnet_predict.py#L206-L231
train
apache/incubator-mxnet
example/reinforcement-learning/dqn/atari_game.py
AtariGame.begin_episode
def begin_episode(self, max_episode_step=DEFAULT_MAX_EPISODE_STEP): """ Begin an episode of a game instance. We can play the game for a maximum of `max_episode_step` and after that, we are forced to restart """ if self.episode_step > self.max_episode_step or self.ale.game_over(): self.start() else: for i in range(self.screen_buffer_length): self.ale.act(0) self.ale.getScreenGrayscale(self.screen_buffer[i % self.screen_buffer_length, :, :]) self.max_episode_step = max_episode_step self.start_lives = self.ale.lives() self.episode_reward = 0 self.episode_step = 0
python
def begin_episode(self, max_episode_step=DEFAULT_MAX_EPISODE_STEP): """ Begin an episode of a game instance. We can play the game for a maximum of `max_episode_step` and after that, we are forced to restart """ if self.episode_step > self.max_episode_step or self.ale.game_over(): self.start() else: for i in range(self.screen_buffer_length): self.ale.act(0) self.ale.getScreenGrayscale(self.screen_buffer[i % self.screen_buffer_length, :, :]) self.max_episode_step = max_episode_step self.start_lives = self.ale.lives() self.episode_reward = 0 self.episode_step = 0
[ "def", "begin_episode", "(", "self", ",", "max_episode_step", "=", "DEFAULT_MAX_EPISODE_STEP", ")", ":", "if", "self", ".", "episode_step", ">", "self", ".", "max_episode_step", "or", "self", ".", "ale", ".", "game_over", "(", ")", ":", "self", ".", "start", "(", ")", "else", ":", "for", "i", "in", "range", "(", "self", ".", "screen_buffer_length", ")", ":", "self", ".", "ale", ".", "act", "(", "0", ")", "self", ".", "ale", ".", "getScreenGrayscale", "(", "self", ".", "screen_buffer", "[", "i", "%", "self", ".", "screen_buffer_length", ",", ":", ",", ":", "]", ")", "self", ".", "max_episode_step", "=", "max_episode_step", "self", ".", "start_lives", "=", "self", ".", "ale", ".", "lives", "(", ")", "self", ".", "episode_reward", "=", "0", "self", ".", "episode_step", "=", "0" ]
Begin an episode of a game instance. We can play the game for a maximum of `max_episode_step` and after that, we are forced to restart
[ "Begin", "an", "episode", "of", "a", "game", "instance", ".", "We", "can", "play", "the", "game", "for", "a", "maximum", "of", "max_episode_step", "and", "after", "that", "we", "are", "forced", "to", "restart" ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/example/reinforcement-learning/dqn/atari_game.py#L112-L126
train
apache/incubator-mxnet
python/mxnet/gluon/rnn/rnn_cell.py
RecurrentCell.reset
def reset(self): """Reset before re-using the cell for another graph.""" self._init_counter = -1 self._counter = -1 for cell in self._children.values(): cell.reset()
python
def reset(self): """Reset before re-using the cell for another graph.""" self._init_counter = -1 self._counter = -1 for cell in self._children.values(): cell.reset()
[ "def", "reset", "(", "self", ")", ":", "self", ".", "_init_counter", "=", "-", "1", "self", ".", "_counter", "=", "-", "1", "for", "cell", "in", "self", ".", "_children", ".", "values", "(", ")", ":", "cell", ".", "reset", "(", ")" ]
Reset before re-using the cell for another graph.
[ "Reset", "before", "re", "-", "using", "the", "cell", "for", "another", "graph", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/gluon/rnn/rnn_cell.py#L140-L145
train
apache/incubator-mxnet
python/mxnet/gluon/rnn/rnn_cell.py
RecurrentCell.begin_state
def begin_state(self, batch_size=0, func=ndarray.zeros, **kwargs): """Initial state for this cell. Parameters ---------- func : callable, default symbol.zeros Function for creating initial state. For Symbol API, func can be `symbol.zeros`, `symbol.uniform`, `symbol.var etc`. Use `symbol.var` if you want to directly feed input as states. For NDArray API, func can be `ndarray.zeros`, `ndarray.ones`, etc. batch_size: int, default 0 Only required for NDArray API. Size of the batch ('N' in layout) dimension of input. **kwargs : Additional keyword arguments passed to func. For example `mean`, `std`, `dtype`, etc. Returns ------- states : nested list of Symbol Starting states for the first RNN step. """ assert not self._modified, \ "After applying modifier cells (e.g. ZoneoutCell) the base " \ "cell cannot be called directly. Call the modifier cell instead." states = [] for info in self.state_info(batch_size): self._init_counter += 1 if info is not None: info.update(kwargs) else: info = kwargs state = func(name='%sbegin_state_%d'%(self._prefix, self._init_counter), **info) states.append(state) return states
python
def begin_state(self, batch_size=0, func=ndarray.zeros, **kwargs): """Initial state for this cell. Parameters ---------- func : callable, default symbol.zeros Function for creating initial state. For Symbol API, func can be `symbol.zeros`, `symbol.uniform`, `symbol.var etc`. Use `symbol.var` if you want to directly feed input as states. For NDArray API, func can be `ndarray.zeros`, `ndarray.ones`, etc. batch_size: int, default 0 Only required for NDArray API. Size of the batch ('N' in layout) dimension of input. **kwargs : Additional keyword arguments passed to func. For example `mean`, `std`, `dtype`, etc. Returns ------- states : nested list of Symbol Starting states for the first RNN step. """ assert not self._modified, \ "After applying modifier cells (e.g. ZoneoutCell) the base " \ "cell cannot be called directly. Call the modifier cell instead." states = [] for info in self.state_info(batch_size): self._init_counter += 1 if info is not None: info.update(kwargs) else: info = kwargs state = func(name='%sbegin_state_%d'%(self._prefix, self._init_counter), **info) states.append(state) return states
[ "def", "begin_state", "(", "self", ",", "batch_size", "=", "0", ",", "func", "=", "ndarray", ".", "zeros", ",", "*", "*", "kwargs", ")", ":", "assert", "not", "self", ".", "_modified", ",", "\"After applying modifier cells (e.g. ZoneoutCell) the base \"", "\"cell cannot be called directly. Call the modifier cell instead.\"", "states", "=", "[", "]", "for", "info", "in", "self", ".", "state_info", "(", "batch_size", ")", ":", "self", ".", "_init_counter", "+=", "1", "if", "info", "is", "not", "None", ":", "info", ".", "update", "(", "kwargs", ")", "else", ":", "info", "=", "kwargs", "state", "=", "func", "(", "name", "=", "'%sbegin_state_%d'", "%", "(", "self", ".", "_prefix", ",", "self", ".", "_init_counter", ")", ",", "*", "*", "info", ")", "states", ".", "append", "(", "state", ")", "return", "states" ]
Initial state for this cell. Parameters ---------- func : callable, default symbol.zeros Function for creating initial state. For Symbol API, func can be `symbol.zeros`, `symbol.uniform`, `symbol.var etc`. Use `symbol.var` if you want to directly feed input as states. For NDArray API, func can be `ndarray.zeros`, `ndarray.ones`, etc. batch_size: int, default 0 Only required for NDArray API. Size of the batch ('N' in layout) dimension of input. **kwargs : Additional keyword arguments passed to func. For example `mean`, `std`, `dtype`, etc. Returns ------- states : nested list of Symbol Starting states for the first RNN step.
[ "Initial", "state", "for", "this", "cell", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/gluon/rnn/rnn_cell.py#L151-L190
train
apache/incubator-mxnet
python/mxnet/gluon/rnn/rnn_cell.py
RecurrentCell.unroll
def unroll(self, length, inputs, begin_state=None, layout='NTC', merge_outputs=None, valid_length=None): """Unrolls an RNN cell across time steps. Parameters ---------- length : int Number of steps to unroll. inputs : Symbol, list of Symbol, or None If `inputs` is a single Symbol (usually the output of Embedding symbol), it should have shape (batch_size, length, ...) if `layout` is 'NTC', or (length, batch_size, ...) if `layout` is 'TNC'. If `inputs` is a list of symbols (usually output of previous unroll), they should all have shape (batch_size, ...). begin_state : nested list of Symbol, optional Input states created by `begin_state()` or output state of another cell. Created from `begin_state()` if `None`. layout : str, optional `layout` of input symbol. Only used if inputs is a single Symbol. merge_outputs : bool, optional If `False`, returns outputs as a list of Symbols. If `True`, concatenates output across time steps and returns a single symbol with shape (batch_size, length, ...) if layout is 'NTC', or (length, batch_size, ...) if layout is 'TNC'. If `None`, output whatever is faster. valid_length : Symbol, NDArray or None `valid_length` specifies the length of the sequences in the batch without padding. This option is especially useful for building sequence-to-sequence models where the input and output sequences would potentially be padded. If `valid_length` is None, all sequences are assumed to have the same length. If `valid_length` is a Symbol or NDArray, it should have shape (batch_size,). The ith element will be the length of the ith sequence in the batch. The last valid state will be return and the padded outputs will be masked with 0. Note that `valid_length` must be smaller or equal to `length`. Returns ------- outputs : list of Symbol or Symbol Symbol (if `merge_outputs` is True) or list of Symbols (if `merge_outputs` is False) corresponding to the output from the RNN from this unrolling. states : list of Symbol The new state of this RNN after this unrolling. The type of this symbol is same as the output of `begin_state()`. """ # pylint: disable=too-many-locals self.reset() inputs, axis, F, batch_size = _format_sequence(length, inputs, layout, False) begin_state = _get_begin_state(self, F, begin_state, inputs, batch_size) states = begin_state outputs = [] all_states = [] for i in range(length): output, states = self(inputs[i], states) outputs.append(output) if valid_length is not None: all_states.append(states) if valid_length is not None: states = [F.SequenceLast(F.stack(*ele_list, axis=0), sequence_length=valid_length, use_sequence_length=True, axis=0) for ele_list in zip(*all_states)] outputs = _mask_sequence_variable_length(F, outputs, length, valid_length, axis, True) outputs, _, _, _ = _format_sequence(length, outputs, layout, merge_outputs) return outputs, states
python
def unroll(self, length, inputs, begin_state=None, layout='NTC', merge_outputs=None, valid_length=None): """Unrolls an RNN cell across time steps. Parameters ---------- length : int Number of steps to unroll. inputs : Symbol, list of Symbol, or None If `inputs` is a single Symbol (usually the output of Embedding symbol), it should have shape (batch_size, length, ...) if `layout` is 'NTC', or (length, batch_size, ...) if `layout` is 'TNC'. If `inputs` is a list of symbols (usually output of previous unroll), they should all have shape (batch_size, ...). begin_state : nested list of Symbol, optional Input states created by `begin_state()` or output state of another cell. Created from `begin_state()` if `None`. layout : str, optional `layout` of input symbol. Only used if inputs is a single Symbol. merge_outputs : bool, optional If `False`, returns outputs as a list of Symbols. If `True`, concatenates output across time steps and returns a single symbol with shape (batch_size, length, ...) if layout is 'NTC', or (length, batch_size, ...) if layout is 'TNC'. If `None`, output whatever is faster. valid_length : Symbol, NDArray or None `valid_length` specifies the length of the sequences in the batch without padding. This option is especially useful for building sequence-to-sequence models where the input and output sequences would potentially be padded. If `valid_length` is None, all sequences are assumed to have the same length. If `valid_length` is a Symbol or NDArray, it should have shape (batch_size,). The ith element will be the length of the ith sequence in the batch. The last valid state will be return and the padded outputs will be masked with 0. Note that `valid_length` must be smaller or equal to `length`. Returns ------- outputs : list of Symbol or Symbol Symbol (if `merge_outputs` is True) or list of Symbols (if `merge_outputs` is False) corresponding to the output from the RNN from this unrolling. states : list of Symbol The new state of this RNN after this unrolling. The type of this symbol is same as the output of `begin_state()`. """ # pylint: disable=too-many-locals self.reset() inputs, axis, F, batch_size = _format_sequence(length, inputs, layout, False) begin_state = _get_begin_state(self, F, begin_state, inputs, batch_size) states = begin_state outputs = [] all_states = [] for i in range(length): output, states = self(inputs[i], states) outputs.append(output) if valid_length is not None: all_states.append(states) if valid_length is not None: states = [F.SequenceLast(F.stack(*ele_list, axis=0), sequence_length=valid_length, use_sequence_length=True, axis=0) for ele_list in zip(*all_states)] outputs = _mask_sequence_variable_length(F, outputs, length, valid_length, axis, True) outputs, _, _, _ = _format_sequence(length, outputs, layout, merge_outputs) return outputs, states
[ "def", "unroll", "(", "self", ",", "length", ",", "inputs", ",", "begin_state", "=", "None", ",", "layout", "=", "'NTC'", ",", "merge_outputs", "=", "None", ",", "valid_length", "=", "None", ")", ":", "# pylint: disable=too-many-locals", "self", ".", "reset", "(", ")", "inputs", ",", "axis", ",", "F", ",", "batch_size", "=", "_format_sequence", "(", "length", ",", "inputs", ",", "layout", ",", "False", ")", "begin_state", "=", "_get_begin_state", "(", "self", ",", "F", ",", "begin_state", ",", "inputs", ",", "batch_size", ")", "states", "=", "begin_state", "outputs", "=", "[", "]", "all_states", "=", "[", "]", "for", "i", "in", "range", "(", "length", ")", ":", "output", ",", "states", "=", "self", "(", "inputs", "[", "i", "]", ",", "states", ")", "outputs", ".", "append", "(", "output", ")", "if", "valid_length", "is", "not", "None", ":", "all_states", ".", "append", "(", "states", ")", "if", "valid_length", "is", "not", "None", ":", "states", "=", "[", "F", ".", "SequenceLast", "(", "F", ".", "stack", "(", "*", "ele_list", ",", "axis", "=", "0", ")", ",", "sequence_length", "=", "valid_length", ",", "use_sequence_length", "=", "True", ",", "axis", "=", "0", ")", "for", "ele_list", "in", "zip", "(", "*", "all_states", ")", "]", "outputs", "=", "_mask_sequence_variable_length", "(", "F", ",", "outputs", ",", "length", ",", "valid_length", ",", "axis", ",", "True", ")", "outputs", ",", "_", ",", "_", ",", "_", "=", "_format_sequence", "(", "length", ",", "outputs", ",", "layout", ",", "merge_outputs", ")", "return", "outputs", ",", "states" ]
Unrolls an RNN cell across time steps. Parameters ---------- length : int Number of steps to unroll. inputs : Symbol, list of Symbol, or None If `inputs` is a single Symbol (usually the output of Embedding symbol), it should have shape (batch_size, length, ...) if `layout` is 'NTC', or (length, batch_size, ...) if `layout` is 'TNC'. If `inputs` is a list of symbols (usually output of previous unroll), they should all have shape (batch_size, ...). begin_state : nested list of Symbol, optional Input states created by `begin_state()` or output state of another cell. Created from `begin_state()` if `None`. layout : str, optional `layout` of input symbol. Only used if inputs is a single Symbol. merge_outputs : bool, optional If `False`, returns outputs as a list of Symbols. If `True`, concatenates output across time steps and returns a single symbol with shape (batch_size, length, ...) if layout is 'NTC', or (length, batch_size, ...) if layout is 'TNC'. If `None`, output whatever is faster. valid_length : Symbol, NDArray or None `valid_length` specifies the length of the sequences in the batch without padding. This option is especially useful for building sequence-to-sequence models where the input and output sequences would potentially be padded. If `valid_length` is None, all sequences are assumed to have the same length. If `valid_length` is a Symbol or NDArray, it should have shape (batch_size,). The ith element will be the length of the ith sequence in the batch. The last valid state will be return and the padded outputs will be masked with 0. Note that `valid_length` must be smaller or equal to `length`. Returns ------- outputs : list of Symbol or Symbol Symbol (if `merge_outputs` is True) or list of Symbols (if `merge_outputs` is False) corresponding to the output from the RNN from this unrolling. states : list of Symbol The new state of this RNN after this unrolling. The type of this symbol is same as the output of `begin_state()`.
[ "Unrolls", "an", "RNN", "cell", "across", "time", "steps", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/gluon/rnn/rnn_cell.py#L192-L267
train
apache/incubator-mxnet
python/mxnet/gluon/rnn/rnn_cell.py
RecurrentCell._get_activation
def _get_activation(self, F, inputs, activation, **kwargs): """Get activation function. Convert if is string""" func = {'tanh': F.tanh, 'relu': F.relu, 'sigmoid': F.sigmoid, 'softsign': F.softsign}.get(activation) if func: return func(inputs, **kwargs) elif isinstance(activation, string_types): return F.Activation(inputs, act_type=activation, **kwargs) elif isinstance(activation, LeakyReLU): return F.LeakyReLU(inputs, act_type='leaky', slope=activation._alpha, **kwargs) return activation(inputs, **kwargs)
python
def _get_activation(self, F, inputs, activation, **kwargs): """Get activation function. Convert if is string""" func = {'tanh': F.tanh, 'relu': F.relu, 'sigmoid': F.sigmoid, 'softsign': F.softsign}.get(activation) if func: return func(inputs, **kwargs) elif isinstance(activation, string_types): return F.Activation(inputs, act_type=activation, **kwargs) elif isinstance(activation, LeakyReLU): return F.LeakyReLU(inputs, act_type='leaky', slope=activation._alpha, **kwargs) return activation(inputs, **kwargs)
[ "def", "_get_activation", "(", "self", ",", "F", ",", "inputs", ",", "activation", ",", "*", "*", "kwargs", ")", ":", "func", "=", "{", "'tanh'", ":", "F", ".", "tanh", ",", "'relu'", ":", "F", ".", "relu", ",", "'sigmoid'", ":", "F", ".", "sigmoid", ",", "'softsign'", ":", "F", ".", "softsign", "}", ".", "get", "(", "activation", ")", "if", "func", ":", "return", "func", "(", "inputs", ",", "*", "*", "kwargs", ")", "elif", "isinstance", "(", "activation", ",", "string_types", ")", ":", "return", "F", ".", "Activation", "(", "inputs", ",", "act_type", "=", "activation", ",", "*", "*", "kwargs", ")", "elif", "isinstance", "(", "activation", ",", "LeakyReLU", ")", ":", "return", "F", ".", "LeakyReLU", "(", "inputs", ",", "act_type", "=", "'leaky'", ",", "slope", "=", "activation", ".", "_alpha", ",", "*", "*", "kwargs", ")", "return", "activation", "(", "inputs", ",", "*", "*", "kwargs", ")" ]
Get activation function. Convert if is string
[ "Get", "activation", "function", ".", "Convert", "if", "is", "string" ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/gluon/rnn/rnn_cell.py#L270-L282
train
apache/incubator-mxnet
python/mxnet/gluon/rnn/rnn_cell.py
RecurrentCell.forward
def forward(self, inputs, states): """Unrolls the recurrent cell for one time step. Parameters ---------- inputs : sym.Variable Input symbol, 2D, of shape (batch_size * num_units). states : list of sym.Variable RNN state from previous step or the output of begin_state(). Returns ------- output : Symbol Symbol corresponding to the output from the RNN when unrolling for a single time step. states : list of Symbol The new state of this RNN after this unrolling. The type of this symbol is same as the output of `begin_state()`. This can be used as an input state to the next time step of this RNN. See Also -------- begin_state: This function can provide the states for the first time step. unroll: This function unrolls an RNN for a given number of (>=1) time steps. """ # pylint: disable= arguments-differ self._counter += 1 return super(RecurrentCell, self).forward(inputs, states)
python
def forward(self, inputs, states): """Unrolls the recurrent cell for one time step. Parameters ---------- inputs : sym.Variable Input symbol, 2D, of shape (batch_size * num_units). states : list of sym.Variable RNN state from previous step or the output of begin_state(). Returns ------- output : Symbol Symbol corresponding to the output from the RNN when unrolling for a single time step. states : list of Symbol The new state of this RNN after this unrolling. The type of this symbol is same as the output of `begin_state()`. This can be used as an input state to the next time step of this RNN. See Also -------- begin_state: This function can provide the states for the first time step. unroll: This function unrolls an RNN for a given number of (>=1) time steps. """ # pylint: disable= arguments-differ self._counter += 1 return super(RecurrentCell, self).forward(inputs, states)
[ "def", "forward", "(", "self", ",", "inputs", ",", "states", ")", ":", "# pylint: disable= arguments-differ", "self", ".", "_counter", "+=", "1", "return", "super", "(", "RecurrentCell", ",", "self", ")", ".", "forward", "(", "inputs", ",", "states", ")" ]
Unrolls the recurrent cell for one time step. Parameters ---------- inputs : sym.Variable Input symbol, 2D, of shape (batch_size * num_units). states : list of sym.Variable RNN state from previous step or the output of begin_state(). Returns ------- output : Symbol Symbol corresponding to the output from the RNN when unrolling for a single time step. states : list of Symbol The new state of this RNN after this unrolling. The type of this symbol is same as the output of `begin_state()`. This can be used as an input state to the next time step of this RNN. See Also -------- begin_state: This function can provide the states for the first time step. unroll: This function unrolls an RNN for a given number of (>=1) time steps.
[ "Unrolls", "the", "recurrent", "cell", "for", "one", "time", "step", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/gluon/rnn/rnn_cell.py#L284-L312
train
apache/incubator-mxnet
python/mxnet/module/base_module.py
_check_input_names
def _check_input_names(symbol, names, typename, throw): """Check that all input names are in symbol's arguments.""" args = symbol.list_arguments() for name in names: if name in args: continue candidates = [arg for arg in args if not arg.endswith('_weight') and not arg.endswith('_bias') and not arg.endswith('_gamma') and not arg.endswith('_beta')] msg = "\033[91mYou created Module with Module(..., %s_names=%s) but " \ "input with name '%s' is not found in symbol.list_arguments(). " \ "Did you mean one of:\n\t%s\033[0m"%( typename, str(names), name, '\n\t'.join(candidates)) if throw: raise ValueError(msg) else: warnings.warn(msg)
python
def _check_input_names(symbol, names, typename, throw): """Check that all input names are in symbol's arguments.""" args = symbol.list_arguments() for name in names: if name in args: continue candidates = [arg for arg in args if not arg.endswith('_weight') and not arg.endswith('_bias') and not arg.endswith('_gamma') and not arg.endswith('_beta')] msg = "\033[91mYou created Module with Module(..., %s_names=%s) but " \ "input with name '%s' is not found in symbol.list_arguments(). " \ "Did you mean one of:\n\t%s\033[0m"%( typename, str(names), name, '\n\t'.join(candidates)) if throw: raise ValueError(msg) else: warnings.warn(msg)
[ "def", "_check_input_names", "(", "symbol", ",", "names", ",", "typename", ",", "throw", ")", ":", "args", "=", "symbol", ".", "list_arguments", "(", ")", "for", "name", "in", "names", ":", "if", "name", "in", "args", ":", "continue", "candidates", "=", "[", "arg", "for", "arg", "in", "args", "if", "not", "arg", ".", "endswith", "(", "'_weight'", ")", "and", "not", "arg", ".", "endswith", "(", "'_bias'", ")", "and", "not", "arg", ".", "endswith", "(", "'_gamma'", ")", "and", "not", "arg", ".", "endswith", "(", "'_beta'", ")", "]", "msg", "=", "\"\\033[91mYou created Module with Module(..., %s_names=%s) but \"", "\"input with name '%s' is not found in symbol.list_arguments(). \"", "\"Did you mean one of:\\n\\t%s\\033[0m\"", "%", "(", "typename", ",", "str", "(", "names", ")", ",", "name", ",", "'\\n\\t'", ".", "join", "(", "candidates", ")", ")", "if", "throw", ":", "raise", "ValueError", "(", "msg", ")", "else", ":", "warnings", ".", "warn", "(", "msg", ")" ]
Check that all input names are in symbol's arguments.
[ "Check", "that", "all", "input", "names", "are", "in", "symbol", "s", "arguments", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/module/base_module.py#L37-L55
train
apache/incubator-mxnet
python/mxnet/module/base_module.py
_check_names_match
def _check_names_match(data_names, data_shapes, name, throw): """Check that input names matches input data descriptors.""" actual = [x[0] for x in data_shapes] if sorted(data_names) != sorted(actual): msg = "Data provided by %s_shapes don't match names specified by %s_names (%s vs. %s)"%( name, name, str(data_shapes), str(data_names)) if throw: raise ValueError(msg) else: warnings.warn(msg)
python
def _check_names_match(data_names, data_shapes, name, throw): """Check that input names matches input data descriptors.""" actual = [x[0] for x in data_shapes] if sorted(data_names) != sorted(actual): msg = "Data provided by %s_shapes don't match names specified by %s_names (%s vs. %s)"%( name, name, str(data_shapes), str(data_names)) if throw: raise ValueError(msg) else: warnings.warn(msg)
[ "def", "_check_names_match", "(", "data_names", ",", "data_shapes", ",", "name", ",", "throw", ")", ":", "actual", "=", "[", "x", "[", "0", "]", "for", "x", "in", "data_shapes", "]", "if", "sorted", "(", "data_names", ")", "!=", "sorted", "(", "actual", ")", ":", "msg", "=", "\"Data provided by %s_shapes don't match names specified by %s_names (%s vs. %s)\"", "%", "(", "name", ",", "name", ",", "str", "(", "data_shapes", ")", ",", "str", "(", "data_names", ")", ")", "if", "throw", ":", "raise", "ValueError", "(", "msg", ")", "else", ":", "warnings", ".", "warn", "(", "msg", ")" ]
Check that input names matches input data descriptors.
[ "Check", "that", "input", "names", "matches", "input", "data", "descriptors", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/module/base_module.py#L58-L67
train
apache/incubator-mxnet
python/mxnet/module/base_module.py
_parse_data_desc
def _parse_data_desc(data_names, label_names, data_shapes, label_shapes): """parse data_attrs into DataDesc format and check that names match""" data_shapes = [x if isinstance(x, DataDesc) else DataDesc(*x) for x in data_shapes] _check_names_match(data_names, data_shapes, 'data', True) if label_shapes is not None: label_shapes = [x if isinstance(x, DataDesc) else DataDesc(*x) for x in label_shapes] _check_names_match(label_names, label_shapes, 'label', False) else: _check_names_match(label_names, [], 'label', False) return data_shapes, label_shapes
python
def _parse_data_desc(data_names, label_names, data_shapes, label_shapes): """parse data_attrs into DataDesc format and check that names match""" data_shapes = [x if isinstance(x, DataDesc) else DataDesc(*x) for x in data_shapes] _check_names_match(data_names, data_shapes, 'data', True) if label_shapes is not None: label_shapes = [x if isinstance(x, DataDesc) else DataDesc(*x) for x in label_shapes] _check_names_match(label_names, label_shapes, 'label', False) else: _check_names_match(label_names, [], 'label', False) return data_shapes, label_shapes
[ "def", "_parse_data_desc", "(", "data_names", ",", "label_names", ",", "data_shapes", ",", "label_shapes", ")", ":", "data_shapes", "=", "[", "x", "if", "isinstance", "(", "x", ",", "DataDesc", ")", "else", "DataDesc", "(", "*", "x", ")", "for", "x", "in", "data_shapes", "]", "_check_names_match", "(", "data_names", ",", "data_shapes", ",", "'data'", ",", "True", ")", "if", "label_shapes", "is", "not", "None", ":", "label_shapes", "=", "[", "x", "if", "isinstance", "(", "x", ",", "DataDesc", ")", "else", "DataDesc", "(", "*", "x", ")", "for", "x", "in", "label_shapes", "]", "_check_names_match", "(", "label_names", ",", "label_shapes", ",", "'label'", ",", "False", ")", "else", ":", "_check_names_match", "(", "label_names", ",", "[", "]", ",", "'label'", ",", "False", ")", "return", "data_shapes", ",", "label_shapes" ]
parse data_attrs into DataDesc format and check that names match
[ "parse", "data_attrs", "into", "DataDesc", "format", "and", "check", "that", "names", "match" ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/module/base_module.py#L70-L79
train
apache/incubator-mxnet
python/mxnet/module/base_module.py
BaseModule.forward_backward
def forward_backward(self, data_batch): """A convenient function that calls both ``forward`` and ``backward``.""" self.forward(data_batch, is_train=True) self.backward()
python
def forward_backward(self, data_batch): """A convenient function that calls both ``forward`` and ``backward``.""" self.forward(data_batch, is_train=True) self.backward()
[ "def", "forward_backward", "(", "self", ",", "data_batch", ")", ":", "self", ".", "forward", "(", "data_batch", ",", "is_train", "=", "True", ")", "self", ".", "backward", "(", ")" ]
A convenient function that calls both ``forward`` and ``backward``.
[ "A", "convenient", "function", "that", "calls", "both", "forward", "and", "backward", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/module/base_module.py#L193-L196
train
apache/incubator-mxnet
python/mxnet/module/base_module.py
BaseModule.score
def score(self, eval_data, eval_metric, num_batch=None, batch_end_callback=None, score_end_callback=None, reset=True, epoch=0, sparse_row_id_fn=None): """Runs prediction on ``eval_data`` and evaluates the performance according to the given ``eval_metric``. Checkout `Module Tutorial <http://mxnet.io/tutorials/basic/module.html>`_ to see a end-to-end use-case. Parameters ---------- eval_data : DataIter Evaluation data to run prediction on. eval_metric : EvalMetric or list of EvalMetrics Evaluation metric to use. num_batch : int Number of batches to run. Defaults to ``None``, indicating run until the `DataIter` finishes. batch_end_callback : function Could also be a list of functions. reset : bool Defaults to ``True``. Indicates whether we should reset `eval_data` before starting evaluating. epoch : int Defaults to 0. For compatibility, this will be passed to callbacks (if any). During training, this will correspond to the training epoch number. sparse_row_id_fn : A callback function The function takes `data_batch` as an input and returns a dict of str -> NDArray. The resulting dict is used for pulling row_sparse parameters from the kvstore, where the str key is the name of the param, and the value is the row id of the param to pull. Examples -------- >>> # An example of using score for prediction. >>> # Evaluate accuracy on val_dataiter >>> metric = mx.metric.Accuracy() >>> mod.score(val_dataiter, metric) >>> mod.score(val_dataiter, ['mse', 'acc']) """ assert self.binded and self.params_initialized if reset: eval_data.reset() if not isinstance(eval_metric, metric.EvalMetric): eval_metric = metric.create(eval_metric) eval_metric.reset() actual_num_batch = 0 for nbatch, eval_batch in enumerate(eval_data): if num_batch is not None and nbatch == num_batch: break self.prepare(eval_batch, sparse_row_id_fn=sparse_row_id_fn) self.forward(eval_batch, is_train=False) if isinstance(eval_batch, list): self.update_metric(eval_metric, [eb.label for eb in eval_batch], pre_sliced=True) else: self.update_metric(eval_metric, eval_batch.label) if batch_end_callback is not None: batch_end_params = BatchEndParam(epoch=epoch, nbatch=nbatch, eval_metric=eval_metric, locals=locals()) for callback in _as_list(batch_end_callback): callback(batch_end_params) actual_num_batch += 1 if score_end_callback: params = BatchEndParam(epoch=epoch, nbatch=actual_num_batch, eval_metric=eval_metric, locals=locals()) for callback in _as_list(score_end_callback): callback(params) return eval_metric.get_name_value()
python
def score(self, eval_data, eval_metric, num_batch=None, batch_end_callback=None, score_end_callback=None, reset=True, epoch=0, sparse_row_id_fn=None): """Runs prediction on ``eval_data`` and evaluates the performance according to the given ``eval_metric``. Checkout `Module Tutorial <http://mxnet.io/tutorials/basic/module.html>`_ to see a end-to-end use-case. Parameters ---------- eval_data : DataIter Evaluation data to run prediction on. eval_metric : EvalMetric or list of EvalMetrics Evaluation metric to use. num_batch : int Number of batches to run. Defaults to ``None``, indicating run until the `DataIter` finishes. batch_end_callback : function Could also be a list of functions. reset : bool Defaults to ``True``. Indicates whether we should reset `eval_data` before starting evaluating. epoch : int Defaults to 0. For compatibility, this will be passed to callbacks (if any). During training, this will correspond to the training epoch number. sparse_row_id_fn : A callback function The function takes `data_batch` as an input and returns a dict of str -> NDArray. The resulting dict is used for pulling row_sparse parameters from the kvstore, where the str key is the name of the param, and the value is the row id of the param to pull. Examples -------- >>> # An example of using score for prediction. >>> # Evaluate accuracy on val_dataiter >>> metric = mx.metric.Accuracy() >>> mod.score(val_dataiter, metric) >>> mod.score(val_dataiter, ['mse', 'acc']) """ assert self.binded and self.params_initialized if reset: eval_data.reset() if not isinstance(eval_metric, metric.EvalMetric): eval_metric = metric.create(eval_metric) eval_metric.reset() actual_num_batch = 0 for nbatch, eval_batch in enumerate(eval_data): if num_batch is not None and nbatch == num_batch: break self.prepare(eval_batch, sparse_row_id_fn=sparse_row_id_fn) self.forward(eval_batch, is_train=False) if isinstance(eval_batch, list): self.update_metric(eval_metric, [eb.label for eb in eval_batch], pre_sliced=True) else: self.update_metric(eval_metric, eval_batch.label) if batch_end_callback is not None: batch_end_params = BatchEndParam(epoch=epoch, nbatch=nbatch, eval_metric=eval_metric, locals=locals()) for callback in _as_list(batch_end_callback): callback(batch_end_params) actual_num_batch += 1 if score_end_callback: params = BatchEndParam(epoch=epoch, nbatch=actual_num_batch, eval_metric=eval_metric, locals=locals()) for callback in _as_list(score_end_callback): callback(params) return eval_metric.get_name_value()
[ "def", "score", "(", "self", ",", "eval_data", ",", "eval_metric", ",", "num_batch", "=", "None", ",", "batch_end_callback", "=", "None", ",", "score_end_callback", "=", "None", ",", "reset", "=", "True", ",", "epoch", "=", "0", ",", "sparse_row_id_fn", "=", "None", ")", ":", "assert", "self", ".", "binded", "and", "self", ".", "params_initialized", "if", "reset", ":", "eval_data", ".", "reset", "(", ")", "if", "not", "isinstance", "(", "eval_metric", ",", "metric", ".", "EvalMetric", ")", ":", "eval_metric", "=", "metric", ".", "create", "(", "eval_metric", ")", "eval_metric", ".", "reset", "(", ")", "actual_num_batch", "=", "0", "for", "nbatch", ",", "eval_batch", "in", "enumerate", "(", "eval_data", ")", ":", "if", "num_batch", "is", "not", "None", "and", "nbatch", "==", "num_batch", ":", "break", "self", ".", "prepare", "(", "eval_batch", ",", "sparse_row_id_fn", "=", "sparse_row_id_fn", ")", "self", ".", "forward", "(", "eval_batch", ",", "is_train", "=", "False", ")", "if", "isinstance", "(", "eval_batch", ",", "list", ")", ":", "self", ".", "update_metric", "(", "eval_metric", ",", "[", "eb", ".", "label", "for", "eb", "in", "eval_batch", "]", ",", "pre_sliced", "=", "True", ")", "else", ":", "self", ".", "update_metric", "(", "eval_metric", ",", "eval_batch", ".", "label", ")", "if", "batch_end_callback", "is", "not", "None", ":", "batch_end_params", "=", "BatchEndParam", "(", "epoch", "=", "epoch", ",", "nbatch", "=", "nbatch", ",", "eval_metric", "=", "eval_metric", ",", "locals", "=", "locals", "(", ")", ")", "for", "callback", "in", "_as_list", "(", "batch_end_callback", ")", ":", "callback", "(", "batch_end_params", ")", "actual_num_batch", "+=", "1", "if", "score_end_callback", ":", "params", "=", "BatchEndParam", "(", "epoch", "=", "epoch", ",", "nbatch", "=", "actual_num_batch", ",", "eval_metric", "=", "eval_metric", ",", "locals", "=", "locals", "(", ")", ")", "for", "callback", "in", "_as_list", "(", "score_end_callback", ")", ":", "callback", "(", "params", ")", "return", "eval_metric", ".", "get_name_value", "(", ")" ]
Runs prediction on ``eval_data`` and evaluates the performance according to the given ``eval_metric``. Checkout `Module Tutorial <http://mxnet.io/tutorials/basic/module.html>`_ to see a end-to-end use-case. Parameters ---------- eval_data : DataIter Evaluation data to run prediction on. eval_metric : EvalMetric or list of EvalMetrics Evaluation metric to use. num_batch : int Number of batches to run. Defaults to ``None``, indicating run until the `DataIter` finishes. batch_end_callback : function Could also be a list of functions. reset : bool Defaults to ``True``. Indicates whether we should reset `eval_data` before starting evaluating. epoch : int Defaults to 0. For compatibility, this will be passed to callbacks (if any). During training, this will correspond to the training epoch number. sparse_row_id_fn : A callback function The function takes `data_batch` as an input and returns a dict of str -> NDArray. The resulting dict is used for pulling row_sparse parameters from the kvstore, where the str key is the name of the param, and the value is the row id of the param to pull. Examples -------- >>> # An example of using score for prediction. >>> # Evaluate accuracy on val_dataiter >>> metric = mx.metric.Accuracy() >>> mod.score(val_dataiter, metric) >>> mod.score(val_dataiter, ['mse', 'acc'])
[ "Runs", "prediction", "on", "eval_data", "and", "evaluates", "the", "performance", "according", "to", "the", "given", "eval_metric", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/module/base_module.py#L198-L276
train
apache/incubator-mxnet
python/mxnet/module/base_module.py
BaseModule.iter_predict
def iter_predict(self, eval_data, num_batch=None, reset=True, sparse_row_id_fn=None): """Iterates over predictions. Examples -------- >>> for pred, i_batch, batch in module.iter_predict(eval_data): ... # pred is a list of outputs from the module ... # i_batch is a integer ... # batch is the data batch from the data iterator Parameters ---------- eval_data : DataIter Evaluation data to run prediction on. num_batch : int Default is ``None``, indicating running all the batches in the data iterator. reset : bool Default is ``True``, indicating whether we should reset the data iter before start doing prediction. sparse_row_id_fn : A callback function The function takes `data_batch` as an input and returns a dict of str -> NDArray. The resulting dict is used for pulling row_sparse parameters from the kvstore, where the str key is the name of the param, and the value is the row id of the param to pull. """ assert self.binded and self.params_initialized if reset: eval_data.reset() for nbatch, eval_batch in enumerate(eval_data): if num_batch is not None and nbatch == num_batch: break self.prepare(eval_batch, sparse_row_id_fn=sparse_row_id_fn) self.forward(eval_batch, is_train=False) pad = eval_batch.pad outputs = [out[0:out.shape[0]-pad] for out in self.get_outputs()] yield (outputs, nbatch, eval_batch)
python
def iter_predict(self, eval_data, num_batch=None, reset=True, sparse_row_id_fn=None): """Iterates over predictions. Examples -------- >>> for pred, i_batch, batch in module.iter_predict(eval_data): ... # pred is a list of outputs from the module ... # i_batch is a integer ... # batch is the data batch from the data iterator Parameters ---------- eval_data : DataIter Evaluation data to run prediction on. num_batch : int Default is ``None``, indicating running all the batches in the data iterator. reset : bool Default is ``True``, indicating whether we should reset the data iter before start doing prediction. sparse_row_id_fn : A callback function The function takes `data_batch` as an input and returns a dict of str -> NDArray. The resulting dict is used for pulling row_sparse parameters from the kvstore, where the str key is the name of the param, and the value is the row id of the param to pull. """ assert self.binded and self.params_initialized if reset: eval_data.reset() for nbatch, eval_batch in enumerate(eval_data): if num_batch is not None and nbatch == num_batch: break self.prepare(eval_batch, sparse_row_id_fn=sparse_row_id_fn) self.forward(eval_batch, is_train=False) pad = eval_batch.pad outputs = [out[0:out.shape[0]-pad] for out in self.get_outputs()] yield (outputs, nbatch, eval_batch)
[ "def", "iter_predict", "(", "self", ",", "eval_data", ",", "num_batch", "=", "None", ",", "reset", "=", "True", ",", "sparse_row_id_fn", "=", "None", ")", ":", "assert", "self", ".", "binded", "and", "self", ".", "params_initialized", "if", "reset", ":", "eval_data", ".", "reset", "(", ")", "for", "nbatch", ",", "eval_batch", "in", "enumerate", "(", "eval_data", ")", ":", "if", "num_batch", "is", "not", "None", "and", "nbatch", "==", "num_batch", ":", "break", "self", ".", "prepare", "(", "eval_batch", ",", "sparse_row_id_fn", "=", "sparse_row_id_fn", ")", "self", ".", "forward", "(", "eval_batch", ",", "is_train", "=", "False", ")", "pad", "=", "eval_batch", ".", "pad", "outputs", "=", "[", "out", "[", "0", ":", "out", ".", "shape", "[", "0", "]", "-", "pad", "]", "for", "out", "in", "self", ".", "get_outputs", "(", ")", "]", "yield", "(", "outputs", ",", "nbatch", ",", "eval_batch", ")" ]
Iterates over predictions. Examples -------- >>> for pred, i_batch, batch in module.iter_predict(eval_data): ... # pred is a list of outputs from the module ... # i_batch is a integer ... # batch is the data batch from the data iterator Parameters ---------- eval_data : DataIter Evaluation data to run prediction on. num_batch : int Default is ``None``, indicating running all the batches in the data iterator. reset : bool Default is ``True``, indicating whether we should reset the data iter before start doing prediction. sparse_row_id_fn : A callback function The function takes `data_batch` as an input and returns a dict of str -> NDArray. The resulting dict is used for pulling row_sparse parameters from the kvstore, where the str key is the name of the param, and the value is the row id of the param to pull.
[ "Iterates", "over", "predictions", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/module/base_module.py#L278-L316
train
apache/incubator-mxnet
python/mxnet/module/base_module.py
BaseModule.predict
def predict(self, eval_data, num_batch=None, merge_batches=True, reset=True, always_output_list=False, sparse_row_id_fn=None): """Runs prediction and collects the outputs. When `merge_batches` is ``True`` (by default), the return value will be a list ``[out1, out2, out3]``, where each element is formed by concatenating the outputs for all the mini-batches. When `always_output_list` is ``False`` (as by default), then in the case of a single output, `out1` is returned instead of ``[out1]``. When `merge_batches` is ``False``, the return value will be a nested list like ``[[out1_batch1, out2_batch1], [out1_batch2], ...]``. This mode is useful because in some cases (e.g. bucketing), the module does not necessarily produce the same number of outputs. The objects in the results have type `NDArray`. If you need to work with a numpy array, just call ``.asnumpy()`` on each `NDArray`. Parameters ---------- eval_data : DataIter or NDArray or numpy array Evaluation data to run prediction on. num_batch : int Defaults to ``None``, indicates running all the batches in the data iterator. merge_batches : bool Defaults to ``True``, see above for return values. reset : bool Defaults to ``True``, indicates whether we should reset the data iter before doing prediction. always_output_list : bool Defaults to ``False``, see above for return values. sparse_row_id_fn : A callback function The function takes `data_batch` as an input and returns a dict of str -> NDArray. The resulting dict is used for pulling row_sparse parameters from the kvstore, where the str key is the name of the param, and the value is the row id of the param to pull. Returns ------- list of NDArray or list of list of NDArray Prediction results. Examples -------- >>> # An example of using `predict` for prediction. >>> # Predict on the first 10 batches of val_dataiter >>> mod.predict(eval_data=val_dataiter, num_batch=10) """ assert self.binded and self.params_initialized if isinstance(eval_data, (ndarray.NDArray, np.ndarray)): if isinstance(eval_data, np.ndarray): eval_data = ndarray.array(eval_data) self.forward(DataBatch([eval_data])) return self.get_outputs()[0] if not isinstance(eval_data, DataIter): raise ValueError('eval_data must be of type NDArray or DataIter') if reset: eval_data.reset() output_list = [] for nbatch, eval_batch in enumerate(eval_data): if num_batch is not None and nbatch == num_batch: break self.prepare(eval_batch, sparse_row_id_fn=sparse_row_id_fn) self.forward(eval_batch, is_train=False) pad = eval_batch.pad outputs = [out[0:out.shape[0]-pad].copy() for out in self.get_outputs()] output_list.append(outputs) if len(output_list) == 0: return output_list if merge_batches: num_outputs = len(output_list[0]) for out in output_list: assert len(out) == num_outputs, \ 'Cannot merge batches, as num of outputs is not the same ' + \ 'in mini-batches. Maybe bucketing is used?' output_list2 = [ndarray.concatenate([out[i] for out in output_list]) for i in range(num_outputs)] if num_outputs == 1 and not always_output_list: return output_list2[0] return output_list2 return output_list
python
def predict(self, eval_data, num_batch=None, merge_batches=True, reset=True, always_output_list=False, sparse_row_id_fn=None): """Runs prediction and collects the outputs. When `merge_batches` is ``True`` (by default), the return value will be a list ``[out1, out2, out3]``, where each element is formed by concatenating the outputs for all the mini-batches. When `always_output_list` is ``False`` (as by default), then in the case of a single output, `out1` is returned instead of ``[out1]``. When `merge_batches` is ``False``, the return value will be a nested list like ``[[out1_batch1, out2_batch1], [out1_batch2], ...]``. This mode is useful because in some cases (e.g. bucketing), the module does not necessarily produce the same number of outputs. The objects in the results have type `NDArray`. If you need to work with a numpy array, just call ``.asnumpy()`` on each `NDArray`. Parameters ---------- eval_data : DataIter or NDArray or numpy array Evaluation data to run prediction on. num_batch : int Defaults to ``None``, indicates running all the batches in the data iterator. merge_batches : bool Defaults to ``True``, see above for return values. reset : bool Defaults to ``True``, indicates whether we should reset the data iter before doing prediction. always_output_list : bool Defaults to ``False``, see above for return values. sparse_row_id_fn : A callback function The function takes `data_batch` as an input and returns a dict of str -> NDArray. The resulting dict is used for pulling row_sparse parameters from the kvstore, where the str key is the name of the param, and the value is the row id of the param to pull. Returns ------- list of NDArray or list of list of NDArray Prediction results. Examples -------- >>> # An example of using `predict` for prediction. >>> # Predict on the first 10 batches of val_dataiter >>> mod.predict(eval_data=val_dataiter, num_batch=10) """ assert self.binded and self.params_initialized if isinstance(eval_data, (ndarray.NDArray, np.ndarray)): if isinstance(eval_data, np.ndarray): eval_data = ndarray.array(eval_data) self.forward(DataBatch([eval_data])) return self.get_outputs()[0] if not isinstance(eval_data, DataIter): raise ValueError('eval_data must be of type NDArray or DataIter') if reset: eval_data.reset() output_list = [] for nbatch, eval_batch in enumerate(eval_data): if num_batch is not None and nbatch == num_batch: break self.prepare(eval_batch, sparse_row_id_fn=sparse_row_id_fn) self.forward(eval_batch, is_train=False) pad = eval_batch.pad outputs = [out[0:out.shape[0]-pad].copy() for out in self.get_outputs()] output_list.append(outputs) if len(output_list) == 0: return output_list if merge_batches: num_outputs = len(output_list[0]) for out in output_list: assert len(out) == num_outputs, \ 'Cannot merge batches, as num of outputs is not the same ' + \ 'in mini-batches. Maybe bucketing is used?' output_list2 = [ndarray.concatenate([out[i] for out in output_list]) for i in range(num_outputs)] if num_outputs == 1 and not always_output_list: return output_list2[0] return output_list2 return output_list
[ "def", "predict", "(", "self", ",", "eval_data", ",", "num_batch", "=", "None", ",", "merge_batches", "=", "True", ",", "reset", "=", "True", ",", "always_output_list", "=", "False", ",", "sparse_row_id_fn", "=", "None", ")", ":", "assert", "self", ".", "binded", "and", "self", ".", "params_initialized", "if", "isinstance", "(", "eval_data", ",", "(", "ndarray", ".", "NDArray", ",", "np", ".", "ndarray", ")", ")", ":", "if", "isinstance", "(", "eval_data", ",", "np", ".", "ndarray", ")", ":", "eval_data", "=", "ndarray", ".", "array", "(", "eval_data", ")", "self", ".", "forward", "(", "DataBatch", "(", "[", "eval_data", "]", ")", ")", "return", "self", ".", "get_outputs", "(", ")", "[", "0", "]", "if", "not", "isinstance", "(", "eval_data", ",", "DataIter", ")", ":", "raise", "ValueError", "(", "'eval_data must be of type NDArray or DataIter'", ")", "if", "reset", ":", "eval_data", ".", "reset", "(", ")", "output_list", "=", "[", "]", "for", "nbatch", ",", "eval_batch", "in", "enumerate", "(", "eval_data", ")", ":", "if", "num_batch", "is", "not", "None", "and", "nbatch", "==", "num_batch", ":", "break", "self", ".", "prepare", "(", "eval_batch", ",", "sparse_row_id_fn", "=", "sparse_row_id_fn", ")", "self", ".", "forward", "(", "eval_batch", ",", "is_train", "=", "False", ")", "pad", "=", "eval_batch", ".", "pad", "outputs", "=", "[", "out", "[", "0", ":", "out", ".", "shape", "[", "0", "]", "-", "pad", "]", ".", "copy", "(", ")", "for", "out", "in", "self", ".", "get_outputs", "(", ")", "]", "output_list", ".", "append", "(", "outputs", ")", "if", "len", "(", "output_list", ")", "==", "0", ":", "return", "output_list", "if", "merge_batches", ":", "num_outputs", "=", "len", "(", "output_list", "[", "0", "]", ")", "for", "out", "in", "output_list", ":", "assert", "len", "(", "out", ")", "==", "num_outputs", ",", "'Cannot merge batches, as num of outputs is not the same '", "+", "'in mini-batches. Maybe bucketing is used?'", "output_list2", "=", "[", "ndarray", ".", "concatenate", "(", "[", "out", "[", "i", "]", "for", "out", "in", "output_list", "]", ")", "for", "i", "in", "range", "(", "num_outputs", ")", "]", "if", "num_outputs", "==", "1", "and", "not", "always_output_list", ":", "return", "output_list2", "[", "0", "]", "return", "output_list2", "return", "output_list" ]
Runs prediction and collects the outputs. When `merge_batches` is ``True`` (by default), the return value will be a list ``[out1, out2, out3]``, where each element is formed by concatenating the outputs for all the mini-batches. When `always_output_list` is ``False`` (as by default), then in the case of a single output, `out1` is returned instead of ``[out1]``. When `merge_batches` is ``False``, the return value will be a nested list like ``[[out1_batch1, out2_batch1], [out1_batch2], ...]``. This mode is useful because in some cases (e.g. bucketing), the module does not necessarily produce the same number of outputs. The objects in the results have type `NDArray`. If you need to work with a numpy array, just call ``.asnumpy()`` on each `NDArray`. Parameters ---------- eval_data : DataIter or NDArray or numpy array Evaluation data to run prediction on. num_batch : int Defaults to ``None``, indicates running all the batches in the data iterator. merge_batches : bool Defaults to ``True``, see above for return values. reset : bool Defaults to ``True``, indicates whether we should reset the data iter before doing prediction. always_output_list : bool Defaults to ``False``, see above for return values. sparse_row_id_fn : A callback function The function takes `data_batch` as an input and returns a dict of str -> NDArray. The resulting dict is used for pulling row_sparse parameters from the kvstore, where the str key is the name of the param, and the value is the row id of the param to pull. Returns ------- list of NDArray or list of list of NDArray Prediction results. Examples -------- >>> # An example of using `predict` for prediction. >>> # Predict on the first 10 batches of val_dataiter >>> mod.predict(eval_data=val_dataiter, num_batch=10)
[ "Runs", "prediction", "and", "collects", "the", "outputs", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/module/base_module.py#L318-L407
train
apache/incubator-mxnet
python/mxnet/module/base_module.py
BaseModule.set_params
def set_params(self, arg_params, aux_params, allow_missing=False, force_init=True, allow_extra=False): """Assigns parameter and aux state values. Parameters ---------- arg_params : dict Dictionary of name to value (`NDArray`) mapping. aux_params : dict Dictionary of name to value (`NDArray`) mapping. allow_missing : bool If ``True``, params could contain missing values, and the initializer will be called to fill those missing params. force_init : bool If ``True``, will force re-initialize even if already initialized. allow_extra : boolean, optional Whether allow extra parameters that are not needed by symbol. If this is True, no error will be thrown when arg_params or aux_params contain extra parameters that is not needed by the executor. Examples -------- >>> # An example of setting module parameters. >>> sym, arg_params, aux_params = mx.model.load_checkpoint(model_prefix, n_epoch_load) >>> mod.set_params(arg_params=arg_params, aux_params=aux_params) """ self.init_params(initializer=None, arg_params=arg_params, aux_params=aux_params, allow_missing=allow_missing, force_init=force_init, allow_extra=allow_extra)
python
def set_params(self, arg_params, aux_params, allow_missing=False, force_init=True, allow_extra=False): """Assigns parameter and aux state values. Parameters ---------- arg_params : dict Dictionary of name to value (`NDArray`) mapping. aux_params : dict Dictionary of name to value (`NDArray`) mapping. allow_missing : bool If ``True``, params could contain missing values, and the initializer will be called to fill those missing params. force_init : bool If ``True``, will force re-initialize even if already initialized. allow_extra : boolean, optional Whether allow extra parameters that are not needed by symbol. If this is True, no error will be thrown when arg_params or aux_params contain extra parameters that is not needed by the executor. Examples -------- >>> # An example of setting module parameters. >>> sym, arg_params, aux_params = mx.model.load_checkpoint(model_prefix, n_epoch_load) >>> mod.set_params(arg_params=arg_params, aux_params=aux_params) """ self.init_params(initializer=None, arg_params=arg_params, aux_params=aux_params, allow_missing=allow_missing, force_init=force_init, allow_extra=allow_extra)
[ "def", "set_params", "(", "self", ",", "arg_params", ",", "aux_params", ",", "allow_missing", "=", "False", ",", "force_init", "=", "True", ",", "allow_extra", "=", "False", ")", ":", "self", ".", "init_params", "(", "initializer", "=", "None", ",", "arg_params", "=", "arg_params", ",", "aux_params", "=", "aux_params", ",", "allow_missing", "=", "allow_missing", ",", "force_init", "=", "force_init", ",", "allow_extra", "=", "allow_extra", ")" ]
Assigns parameter and aux state values. Parameters ---------- arg_params : dict Dictionary of name to value (`NDArray`) mapping. aux_params : dict Dictionary of name to value (`NDArray`) mapping. allow_missing : bool If ``True``, params could contain missing values, and the initializer will be called to fill those missing params. force_init : bool If ``True``, will force re-initialize even if already initialized. allow_extra : boolean, optional Whether allow extra parameters that are not needed by symbol. If this is True, no error will be thrown when arg_params or aux_params contain extra parameters that is not needed by the executor. Examples -------- >>> # An example of setting module parameters. >>> sym, arg_params, aux_params = mx.model.load_checkpoint(model_prefix, n_epoch_load) >>> mod.set_params(arg_params=arg_params, aux_params=aux_params)
[ "Assigns", "parameter", "and", "aux", "state", "values", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/module/base_module.py#L671-L699
train
apache/incubator-mxnet
python/mxnet/module/base_module.py
BaseModule.save_params
def save_params(self, fname): """Saves model parameters to file. Parameters ---------- fname : str Path to output param file. Examples -------- >>> # An example of saving module parameters. >>> mod.save_params('myfile') """ arg_params, aux_params = self.get_params() save_dict = {('arg:%s' % k) : v.as_in_context(cpu()) for k, v in arg_params.items()} save_dict.update({('aux:%s' % k) : v.as_in_context(cpu()) for k, v in aux_params.items()}) ndarray.save(fname, save_dict)
python
def save_params(self, fname): """Saves model parameters to file. Parameters ---------- fname : str Path to output param file. Examples -------- >>> # An example of saving module parameters. >>> mod.save_params('myfile') """ arg_params, aux_params = self.get_params() save_dict = {('arg:%s' % k) : v.as_in_context(cpu()) for k, v in arg_params.items()} save_dict.update({('aux:%s' % k) : v.as_in_context(cpu()) for k, v in aux_params.items()}) ndarray.save(fname, save_dict)
[ "def", "save_params", "(", "self", ",", "fname", ")", ":", "arg_params", ",", "aux_params", "=", "self", ".", "get_params", "(", ")", "save_dict", "=", "{", "(", "'arg:%s'", "%", "k", ")", ":", "v", ".", "as_in_context", "(", "cpu", "(", ")", ")", "for", "k", ",", "v", "in", "arg_params", ".", "items", "(", ")", "}", "save_dict", ".", "update", "(", "{", "(", "'aux:%s'", "%", "k", ")", ":", "v", ".", "as_in_context", "(", "cpu", "(", ")", ")", "for", "k", ",", "v", "in", "aux_params", ".", "items", "(", ")", "}", ")", "ndarray", ".", "save", "(", "fname", ",", "save_dict", ")" ]
Saves model parameters to file. Parameters ---------- fname : str Path to output param file. Examples -------- >>> # An example of saving module parameters. >>> mod.save_params('myfile')
[ "Saves", "model", "parameters", "to", "file", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/module/base_module.py#L701-L717
train
apache/incubator-mxnet
python/mxnet/module/base_module.py
BaseModule.load_params
def load_params(self, fname): """Loads model parameters from file. Parameters ---------- fname : str Path to input param file. Examples -------- >>> # An example of loading module parameters. >>> mod.load_params('myfile') """ save_dict = ndarray.load(fname) arg_params = {} aux_params = {} for k, value in save_dict.items(): arg_type, name = k.split(':', 1) if arg_type == 'arg': arg_params[name] = value elif arg_type == 'aux': aux_params[name] = value else: raise ValueError("Invalid param file " + fname) self.set_params(arg_params, aux_params)
python
def load_params(self, fname): """Loads model parameters from file. Parameters ---------- fname : str Path to input param file. Examples -------- >>> # An example of loading module parameters. >>> mod.load_params('myfile') """ save_dict = ndarray.load(fname) arg_params = {} aux_params = {} for k, value in save_dict.items(): arg_type, name = k.split(':', 1) if arg_type == 'arg': arg_params[name] = value elif arg_type == 'aux': aux_params[name] = value else: raise ValueError("Invalid param file " + fname) self.set_params(arg_params, aux_params)
[ "def", "load_params", "(", "self", ",", "fname", ")", ":", "save_dict", "=", "ndarray", ".", "load", "(", "fname", ")", "arg_params", "=", "{", "}", "aux_params", "=", "{", "}", "for", "k", ",", "value", "in", "save_dict", ".", "items", "(", ")", ":", "arg_type", ",", "name", "=", "k", ".", "split", "(", "':'", ",", "1", ")", "if", "arg_type", "==", "'arg'", ":", "arg_params", "[", "name", "]", "=", "value", "elif", "arg_type", "==", "'aux'", ":", "aux_params", "[", "name", "]", "=", "value", "else", ":", "raise", "ValueError", "(", "\"Invalid param file \"", "+", "fname", ")", "self", ".", "set_params", "(", "arg_params", ",", "aux_params", ")" ]
Loads model parameters from file. Parameters ---------- fname : str Path to input param file. Examples -------- >>> # An example of loading module parameters. >>> mod.load_params('myfile')
[ "Loads", "model", "parameters", "from", "file", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/module/base_module.py#L719-L743
train