Code
stringlengths
103
85.9k
Summary
sequencelengths
0
94
Please provide a description of the function:def setting_ctx(num_gpus): if num_gpus > 0: ctx = [mx.gpu(i) for i in range(num_gpus)] else: ctx = [mx.cpu()] return ctx
[ "\n Description : set gpu module\n " ]
Please provide a description of the function:def char_beam_search(out): out_conv = list() for idx in range(out.shape[0]): probs = out[idx] prob = probs.softmax().asnumpy() line_string_proposals = ctcBeamSearch(prob, ALPHABET, None, k=4, beamWidth=25) out_conv.append(line_string_proposals[0]) return out_conv
[ "\n Description : apply beam search for prediction result\n " ]
Please provide a description of the function:def build_model(self, dr_rate=0, path=None): #set network self.net = LipNet(dr_rate) self.net.hybridize() self.net.initialize(ctx=self.ctx) if path is not None: self.load_model(path) #set optimizer self.loss_fn = gluon.loss.CTCLoss() self.trainer = gluon.Trainer(self.net.collect_params(), \ optimizer='SGD')
[ "\n Description : build network\n " ]
Please provide a description of the function:def save_model(self, epoch, loss): prefix = 'checkpoint/epoches' file_name = "{prefix}_{epoch}_loss_{l:.4f}".format(prefix=prefix, epoch=str(epoch), l=loss) self.net.save_parameters(file_name)
[ "\n Description : save parameter of network weight\n " ]
Please provide a description of the function:def load_dataloader(self): input_transform = transforms.Compose([transforms.ToTensor(), \ transforms.Normalize((0.7136, 0.4906, 0.3283), \ (0.1138, 0.1078, 0.0917))]) training_dataset = LipsDataset(self.image_path, self.align_path, mode='train', transform=input_transform, seq_len=self.seq_len) self.train_dataloader = mx.gluon.data.DataLoader(training_dataset, batch_size=self.batch_size, shuffle=True, num_workers=self.num_workers) valid_dataset = LipsDataset(self.image_path, self.align_path, mode='valid', transform=input_transform, seq_len=self.seq_len) self.valid_dataloader = mx.gluon.data.DataLoader(valid_dataset, batch_size=self.batch_size, shuffle=True, num_workers=self.num_workers)
[ "\n Description : Setup the dataloader\n " ]
Please provide a description of the function:def train(self, data, label, batch_size): # pylint: disable=no-member sum_losses = 0 len_losses = 0 with autograd.record(): losses = [self.loss_fn(self.net(X), Y) for X, Y in zip(data, label)] for loss in losses: sum_losses += mx.nd.array(loss).sum().asscalar() len_losses += len(loss) loss.backward() self.trainer.step(batch_size) return sum_losses, len_losses
[ "\n Description : training for LipNet\n " ]
Please provide a description of the function:def infer(self, input_data, input_label): sum_losses = 0 len_losses = 0 for data, label in zip(input_data, input_label): pred = self.net(data) sum_losses += mx.nd.array(self.loss_fn(pred, label)).sum().asscalar() len_losses += len(data) pred_convert = char_beam_search(pred) label_convert = char_conv(label.asnumpy()) for target, pred in zip(label_convert, pred_convert): print("target:{t} pred:{p}".format(t=target, p=pred)) return sum_losses, len_losses
[ "\n Description : Print sentence for prediction result\n " ]
Please provide a description of the function:def train_batch(self, dataloader): sum_losses = 0 len_losses = 0 for input_data, input_label in tqdm(dataloader): data = gluon.utils.split_and_load(input_data, self.ctx, even_split=False) label = gluon.utils.split_and_load(input_label, self.ctx, even_split=False) batch_size = input_data.shape[0] sum_losses, len_losses = self.train(data, label, batch_size) sum_losses += sum_losses len_losses += len_losses return sum_losses, len_losses
[ "\n Description : training for LipNet\n " ]
Please provide a description of the function:def infer_batch(self, dataloader): sum_losses = 0 len_losses = 0 for input_data, input_label in dataloader: data = gluon.utils.split_and_load(input_data, self.ctx, even_split=False) label = gluon.utils.split_and_load(input_label, self.ctx, even_split=False) sum_losses, len_losses = self.infer(data, label) sum_losses += sum_losses len_losses += len_losses return sum_losses, len_losses
[ "\n Description : inference for LipNet\n " ]
Please provide a description of the function:def run(self, epochs): best_loss = sys.maxsize for epoch in trange(epochs): iter_no = 0 ## train sum_losses, len_losses = self.train_batch(self.train_dataloader) if iter_no % 20 == 0: current_loss = sum_losses / len_losses print("[Train] epoch:{e} iter:{i} loss:{l:.4f}".format(e=epoch, i=iter_no, l=current_loss)) ## validating sum_val_losses, len_val_losses = self.infer_batch(self.valid_dataloader) current_val_loss = sum_val_losses / len_val_losses print("[Vaild] epoch:{e} iter:{i} loss:{l:.4f}".format(e=epoch, i=iter_no, l=current_val_loss)) if best_loss > current_val_loss: self.save_model(epoch, current_val_loss) best_loss = current_val_loss iter_no += 1
[ "\n Description : Run training for LipNet\n " ]
Please provide a description of the function:def sample_categorical(prob, rng): ret = numpy.empty(prob.shape[0], dtype=numpy.float32) for ind in range(prob.shape[0]): ret[ind] = numpy.searchsorted(numpy.cumsum(prob[ind]), rng.rand()).clip(min=0.0, max=prob.shape[ 1] - 0.5) return ret
[ "Sample from independent categorical distributions\n\n Each batch is an independent categorical distribution.\n\n Parameters\n ----------\n prob : numpy.ndarray\n Probability of the categorical distribution. Shape --> (batch_num, category_num)\n rng : numpy.random.RandomState\n\n Returns\n -------\n ret : numpy.ndarray\n Sampling result. Shape --> (batch_num,)\n " ]
Please provide a description of the function:def sample_normal(mean, var, rng): ret = numpy.sqrt(var) * rng.randn(*mean.shape) + mean return ret
[ "Sample from independent normal distributions\n\n Each element is an independent normal distribution.\n\n Parameters\n ----------\n mean : numpy.ndarray\n Means of the normal distribution. Shape --> (batch_num, sample_dim)\n var : numpy.ndarray\n Variance of the normal distribution. Shape --> (batch_num, sample_dim)\n rng : numpy.random.RandomState\n\n Returns\n -------\n ret : numpy.ndarray\n The sampling result. Shape --> (batch_num, sample_dim)\n " ]
Please provide a description of the function:def sample_mog(prob, mean, var, rng): gaussian_inds = sample_categorical(prob, rng).astype(numpy.int32) mean = mean[numpy.arange(mean.shape[0]), gaussian_inds, :] var = var[numpy.arange(mean.shape[0]), gaussian_inds, :] ret = sample_normal(mean=mean, var=var, rng=rng) return ret
[ "Sample from independent mixture of gaussian (MoG) distributions\n\n Each batch is an independent MoG distribution.\n\n Parameters\n ----------\n prob : numpy.ndarray\n mixture probability of each gaussian. Shape --> (batch_num, center_num)\n mean : numpy.ndarray\n mean of each gaussian. Shape --> (batch_num, center_num, sample_dim)\n var : numpy.ndarray\n variance of each gaussian. Shape --> (batch_num, center_num, sample_dim)\n rng : numpy.random.RandomState\n\n Returns\n -------\n ret : numpy.ndarray\n sampling result. Shape --> (batch_num, sample_dim)\n " ]
Please provide a description of the function:def nce_loss_subwords( data, label, label_mask, label_weight, embed_weight, vocab_size, num_hidden): # get subword-units embedding. label_units_embed = mx.sym.Embedding(data=label, input_dim=vocab_size, weight=embed_weight, output_dim=num_hidden) # get valid subword-units embedding with the help of label_mask # it's achieved by multiplying zeros to useless units in order to handle variable-length input. label_units_embed = mx.sym.broadcast_mul(lhs=label_units_embed, rhs=label_mask, name='label_units_embed') # sum over them to get label word embedding. label_embed = mx.sym.sum(label_units_embed, axis=2, name='label_embed') # by boardcast_mul and sum you can get prediction scores in all label_embed inputs, # which is easy to feed into LogisticRegressionOutput and make your code more concise. data = mx.sym.Reshape(data=data, shape=(-1, 1, num_hidden)) pred = mx.sym.broadcast_mul(data, label_embed) pred = mx.sym.sum(data=pred, axis=2) return mx.sym.LogisticRegressionOutput(data=pred, label=label_weight)
[ "NCE-Loss layer under subword-units input.\n " ]
Please provide a description of the function:def get_dataset(prefetch=False): if path.exists(data_dir): print( "Directory {} already exists, skipping.\n" "To force download and extraction, delete the directory and re-run." "".format(data_dir), file=sys.stderr, ) else: print("Downloading dataset...", file=sys.stderr) downloaded_file = download(dataset_url, dirname=datasets_tmpdir) print("done", file=sys.stderr) print("Extracting files...", end="", file=sys.stderr) os.makedirs(data_dir) os.makedirs(tmp_dir) with zipfile.ZipFile(downloaded_file) as archive: archive.extractall(tmp_dir) shutil.rmtree(datasets_tmpdir) shutil.copytree( path.join(tmp_dir, "BSDS500-master", "BSDS500", "data", "images"), path.join(data_dir, "images"), ) shutil.copytree( path.join(tmp_dir, "BSDS500-master", "BSDS500", "data", "groundTruth"), path.join(data_dir, "groundTruth"), ) shutil.rmtree(tmp_dir) print("done", file=sys.stderr) crop_size = 256 crop_size -= crop_size % upscale_factor input_crop_size = crop_size // upscale_factor input_transform = [CenterCropAug((crop_size, crop_size)), ResizeAug(input_crop_size)] target_transform = [CenterCropAug((crop_size, crop_size))] iters = ( ImagePairIter( path.join(data_dir, "images", "train"), (input_crop_size, input_crop_size), (crop_size, crop_size), batch_size, color_flag, input_transform, target_transform, ), ImagePairIter( path.join(data_dir, "images", "test"), (input_crop_size, input_crop_size), (crop_size, crop_size), test_batch_size, color_flag, input_transform, target_transform, ), ) return [PrefetchingIter(i) for i in iters] if prefetch else iters
[ "Download the BSDS500 dataset and return train and test iters." ]
Please provide a description of the function:def evaluate(mod, data_iter, epoch, log_interval): start = time.time() total_L = 0.0 nbatch = 0 density = 0 mod.set_states(value=0) for batch in data_iter: mod.forward(batch, is_train=False) outputs = mod.get_outputs(merge_multi_context=False) states = outputs[:-1] total_L += outputs[-1][0] mod.set_states(states=states) nbatch += 1 # don't include padding data in the test perplexity density += batch.data[1].mean() if (nbatch + 1) % log_interval == 0: logging.info("Eval batch %d loss : %.7f" % (nbatch, (total_L / density).asscalar())) data_iter.reset() loss = (total_L / density).asscalar() ppl = math.exp(loss) if loss < 100 else 1e37 end = time.time() logging.info('Iter[%d]\t\t CE loss %.7f, ppl %.7f. Eval duration = %.2f seconds ' % \ (epoch, loss, ppl, end - start)) return loss
[ " Run evaluation on cpu. " ]
Please provide a description of the function:def _read(self): _, data_img_name, label_img_name = self.f.readline().strip('\n').split("\t") data = {} label = {} data[self.data_name], label[self.label_name] = self._read_img(data_img_name, label_img_name) return list(data.items()), list(label.items())
[ "get two list, each list contains two elements: name and nd.array value" ]
Please provide a description of the function:def next(self): if self.iter_next(): self.data, self.label = self._read() return {self.data_name : self.data[0][1], self.label_name : self.label[0][1]} else: raise StopIteration
[ "return one dict which contains \"data\" and \"label\" " ]
Please provide a description of the function:def _convert_operator(self, node_name, op_name, attrs, inputs): if op_name in convert_map: op_name, new_attrs, inputs = convert_map[op_name](attrs, inputs, self) else: raise NotImplementedError("Operator {} not implemented.".format(op_name)) if isinstance(op_name, string_types): new_op = getattr(symbol, op_name, None) if not new_op: raise RuntimeError("Unable to map op_name {} to sym".format(op_name)) if node_name is None: mxnet_sym = new_op(*inputs, **new_attrs) else: mxnet_sym = new_op(name=node_name, *inputs, **new_attrs) return mxnet_sym return op_name
[ "Convert from onnx operator to mxnet operator.\n The converter must specify conversions explicitly for incompatible name, and\n apply handlers to operator attributes.\n\n Parameters\n ----------\n :param node_name : str\n name of the node to be translated.\n :param op_name : str\n Operator name, such as Convolution, FullyConnected\n :param attrs : dict\n Dict of operator attributes\n :param inputs: list\n list of inputs to the operator\n Returns\n -------\n :return mxnet_sym\n Converted mxnet symbol\n " ]
Please provide a description of the function:def from_onnx(self, graph): # get input, output shapes self.model_metadata = self.get_graph_metadata(graph) # parse network inputs, aka parameters for init_tensor in graph.initializer: if not init_tensor.name.strip(): raise ValueError("Tensor's name is required.") self._params[init_tensor.name] = self._parse_array(init_tensor) # converting GraphProto message for i in graph.input: if i.name in self._params: # i is a param instead of input self._nodes[i.name] = symbol.Variable(name=i.name, shape=self._params[i.name].shape) else: self._nodes[i.name] = symbol.Variable(name=i.name) # constructing nodes, nodes are stored as directed acyclic graph # converting NodeProto message for node in graph.node: op_name = node.op_type node_name = node.name.strip() node_name = node_name if node_name else None onnx_attr = self._parse_attr(node.attribute) inputs = [self._nodes[i] for i in node.input] mxnet_sym = self._convert_operator(node_name, op_name, onnx_attr, inputs) for k, i in zip(list(node.output), range(len(mxnet_sym.list_outputs()))): self._nodes[k] = mxnet_sym[i] # splitting params into args and aux params for args in mxnet_sym.list_arguments(): if args in self._params: self.arg_dict.update({args: nd.array(self._params[args])}) for aux in mxnet_sym.list_auxiliary_states(): if aux in self._params: self.aux_dict.update({aux: nd.array(self._params[aux])}) # now return the outputs out = [self._nodes[i.name] for i in graph.output] if len(out) > 1: out = symbol.Group(out) else: out = out[0] return out, self.arg_dict, self.aux_dict
[ "Construct symbol from onnx graph.\n\n Parameters\n ----------\n graph : onnx protobuf object\n The loaded onnx graph\n\n Returns\n -------\n sym :symbol.Symbol\n The returned mxnet symbol\n params : dict\n A dict of name: nd.array pairs, used as pretrained weights\n " ]
Please provide a description of the function:def get_graph_metadata(self, graph): _params = set() for tensor_vals in graph.initializer: _params.add(tensor_vals.name) input_data = [] for graph_input in graph.input: if graph_input.name not in _params: shape = [val.dim_value for val in graph_input.type.tensor_type.shape.dim] input_data.append((graph_input.name, tuple(shape))) output_data = [] for graph_out in graph.output: shape = [val.dim_value for val in graph_out.type.tensor_type.shape.dim] output_data.append((graph_out.name, tuple(shape))) metadata = {'input_tensor_data' : input_data, 'output_tensor_data' : output_data } return metadata
[ "\n Get the model metadata from a given onnx graph.\n " ]
Please provide a description of the function:def graph_to_gluon(self, graph, ctx): sym, arg_params, aux_params = self.from_onnx(graph) metadata = self.get_graph_metadata(graph) data_names = [input_tensor[0] for input_tensor in metadata['input_tensor_data']] data_inputs = [symbol.var(data_name) for data_name in data_names] from ....gluon import SymbolBlock net = SymbolBlock(outputs=sym, inputs=data_inputs) net_params = net.collect_params() for param in arg_params: if param in net_params: net_params[param].shape = arg_params[param].shape net_params[param]._load_init(arg_params[param], ctx=ctx) for param in aux_params: if param in net_params: net_params[param].shape = aux_params[param].shape net_params[param]._load_init(aux_params[param], ctx=ctx) return net
[ "Construct SymbolBlock from onnx graph.\n\n Parameters\n ----------\n graph : onnx protobuf object\n The loaded onnx graph\n ctx : Context or list of Context\n Loads the model into one or many context(s).\n\n Returns\n -------\n sym_block :gluon.nn.SymbolBlock\n The returned gluon SymbolBlock\n " ]
Please provide a description of the function:def _parse_array(self, tensor_proto): try: from onnx.numpy_helper import to_array except ImportError: raise ImportError("Onnx and protobuf need to be installed. " + "Instructions to install - https://github.com/onnx/onnx") if len(tuple(tensor_proto.dims)) > 0: np_array = to_array(tensor_proto).reshape(tuple(tensor_proto.dims)) else: # If onnx's params are scalar values without dims mentioned. np_array = np.array([to_array(tensor_proto)]) return nd.array(np_array)
[ "Grab data in TensorProto and convert to numpy array." ]
Please provide a description of the function:def _parse_attr(self, attr_proto): attrs = {} for a in attr_proto: for f in ['f', 'i', 's']: if a.HasField(f): attrs[a.name] = getattr(a, f) # Needed for supporting python version > 3.5 if isinstance(attrs[a.name], bytes): attrs[a.name] = attrs[a.name].decode(encoding='utf-8') for f in ['floats', 'ints', 'strings']: if list(getattr(a, f)): assert a.name not in attrs, "Only one type of attr is allowed" attrs[a.name] = tuple(getattr(a, f)) for f in ['t', 'g']: if a.HasField(f): attrs[a.name] = getattr(a, f) for f in ['tensors', 'graphs']: if list(getattr(a, f)): raise NotImplementedError("Filed {} is not supported in mxnet.".format(f)) if a.name not in attrs: raise ValueError("Cannot parse attribute: \n{}\n.".format(a)) return attrs
[ "Convert a list of AttributeProto to a dict, with names as keys." ]
Please provide a description of the function:def reshape(self, data_shapes, label_shapes=None): super(SVRGModule, self).reshape(data_shapes, label_shapes=label_shapes) self._mod_aux.reshape(data_shapes, label_shapes=label_shapes)
[ "Reshapes both modules for new input shapes.\n\n Parameters\n ----------\n data_shapes : list of (str, tuple)\n Typically is ``data_iter.provide_data``.\n label_shapes : list of (str, tuple)\n Typically is ``data_iter.provide_label``.\n " ]
Please provide a description of the function:def init_optimizer(self, kvstore='local', optimizer='sgd', optimizer_params=(('learning_rate', 0.01),), force_init=False): # Init dict for storing average of full gradients for each device self._param_dict = [{key: mx.nd.zeros(shape=value.shape, ctx=self._context[i]) for key, value in self.get_params()[0].items()} for i in range(self._ctx_len)] svrg_optimizer = self._create_optimizer(_SVRGOptimizer.__name__, default_opt=optimizer, kvstore=kvstore, optimizer_params=optimizer_params) super(SVRGModule, self).init_optimizer(kvstore=kvstore, optimizer=svrg_optimizer, optimizer_params=optimizer_params, force_init=force_init) # Init additional keys for accumulating full grads in KVStore if self._kvstore: for idx, param_on_devs in enumerate(self._exec_group.param_arrays): name = self._exec_group.param_names[idx] self._kvstore.init(name + "_full", mx.nd.zeros(shape=self._arg_params[name].shape)) if self._update_on_kvstore: self._kvstore.pull(name + "_full", param_on_devs, priority=-idx)
[ "Installs and initializes SVRGOptimizer. The SVRGOptimizer is a wrapper class for a regular optimizer that is\n passed in and a special AssignmentOptimizer to accumulate the full gradients. If KVStore is 'local' or None,\n the full gradients will be accumulated locally without pushing to the KVStore. Otherwise, additional keys will\n be pushed to accumulate the full gradients in the KVStore.\n\n Parameters\n ----------\n kvstore : str or KVStore\n Default `'local'`.\n optimizer : str or Optimizer\n Default `'sgd'`\n optimizer_params : dict\n Default `(('learning_rate', 0.01),)`. The default value is not a dictionary,\n just to avoid pylint warning of dangerous default values.\n force_init : bool\n Default ``False``, indicating whether we should force re-initializing the\n optimizer in the case an optimizer is already installed.\n " ]
Please provide a description of the function:def _create_optimizer(self, optimizer, default_opt, kvstore, optimizer_params): # code partially copied from mxnet module.init_optimizer() to accomodate svrg_optimizer batch_size = self._exec_group.batch_size (kv_store, update_on_kvstore) = mx.model._create_kvstore(kvstore, self._ctx_len, self._arg_params) if kv_store and 'dist' in kv_store.type and '_sync' in kv_store.type: batch_size *= kv_store.num_workers rescale_grad = 1.0 / batch_size idx2name = {} if update_on_kvstore: idx2name.update(enumerate(self._exec_group.param_names)) else: for k in range(self._ctx_len): idx2name.update({i * self._ctx_len + k: n for i, n in enumerate(self._exec_group.param_names)}) # update idx2name to include new keys for key in self._param_dict[0].keys(): max_key = max(list(idx2name.keys())) + 1 idx2name[max_key] = key + "_full" optimizer_params = dict(optimizer_params) if 'rescale_grad' not in optimizer_params: optimizer_params['rescale_grad'] = rescale_grad optimizer_params["default_optimizer"] = default_opt optimizer_params["param_idx2name"] = idx2name optimizer = mx.optimizer.create(optimizer, **optimizer_params) return optimizer
[ "Helper function to create a svrg optimizer. SVRG optimizer encapsulates two optimizers and\n will redirect update() to the correct optimizer based on the key.\n\n Parameters\n ----------\n kvstore : str or KVStore\n Default `'local'`.\n optimizer: str\n Name for SVRGOptimizer\n default_opt : str or Optimizer that was passed in.\n optimizer_params : dict\n optimizer params that was passed in.\n " ]
Please provide a description of the function:def bind(self, data_shapes, label_shapes=None, for_training=True, inputs_need_grad=False, force_rebind=False, shared_module=None, grad_req='write'): # force rebinding is typically used when one want to switch from # training to prediction phase. super(SVRGModule, self).bind(data_shapes, label_shapes, for_training, inputs_need_grad, force_rebind, shared_module, grad_req) if for_training: self._mod_aux.bind(data_shapes, label_shapes, for_training, inputs_need_grad, force_rebind, shared_module, grad_req)
[ "Binds the symbols to construct executors for both two modules. This is necessary before one\n can perform computation with the SVRGModule.\n\n Parameters\n ----------\n data_shapes : list of (str, tuple)\n Typically is ``data_iter.provide_data``.\n label_shapes : list of (str, tuple)\n Typically is ``data_iter.provide_label``.\n for_training : bool\n Default is ``True``. Whether the executors should be bound for training.\n inputs_need_grad : bool\n Default is ``False``. Whether the gradients to the input data need to be computed.\n Typically this is not needed. But this might be needed when implementing composition\n of modules.\n force_rebind : bool\n Default is ``False``. This function does nothing if the executors are already\n bound. But with this ``True``, the executors will be forced to rebind.\n shared_module : Module\n Default is ``None``. This is used in bucketing. When not ``None``, the shared module\n essentially corresponds to a different bucket -- a module with different symbol\n but with the same sets of parameters (e.g. unrolled RNNs with different lengths).\n " ]
Please provide a description of the function:def forward(self, data_batch, is_train=None): super(SVRGModule, self).forward(data_batch, is_train) if is_train: self._mod_aux.forward(data_batch, is_train)
[ "Forward computation for both two modules. It supports data batches with different shapes, such as\n different batch sizes or different image sizes.\n If reshaping of data batch relates to modification of symbol or module, such as\n changing image layout ordering or switching from training to predicting, module\n rebinding is required.\n\n See Also\n ----------\n :meth:`BaseModule.forward`.\n\n Parameters\n ----------\n data_batch : DataBatch\n Could be anything with similar API implemented.\n is_train : bool\n Default is ``None``, which means ``is_train`` takes the value of ``self.for_training``.\n " ]
Please provide a description of the function:def backward(self, out_grads=None): super(SVRGModule, self).backward(out_grads) if self._mod_aux.binded: self._mod_aux.backward(out_grads)
[ "Backward computation.\n\n See Also\n ----------\n :meth:`BaseModule.backward`.\n\n Parameters\n ----------\n out_grads : NDArray or list of NDArray, optional\n Gradient on the outputs to be propagated back.\n This parameter is only needed when bind is called\n on outputs that are not a loss function.\n " ]
Please provide a description of the function:def update_full_grads(self, train_data): param_names = self._exec_group.param_names arg, aux = self.get_params() self._mod_aux.set_params(arg_params=arg, aux_params=aux) train_data.reset() nbatch = 0 padding = 0 for batch in train_data: self._mod_aux.forward(batch, is_train=True) self._mod_aux.backward() nbatch += 1 for ctx in range(self._ctx_len): for index, name in enumerate(param_names): grads = self._mod_aux._exec_group.grad_arrays[index][ctx] self._param_dict[ctx][name] = mx.nd.broadcast_add(self._param_dict[ctx][name], grads, axis=0) padding = batch.pad true_num_batch = nbatch - padding / train_data.batch_size for name in param_names: grad_list = [] for i in range(self._ctx_len): self._param_dict[i][name] /= true_num_batch grad_list.append(self._param_dict[i][name]) if self._kvstore: # If in distributed mode, push a list of gradients from each worker/device to the KVStore self._accumulate_kvstore(name, grad_list)
[ "Computes the gradients over all data w.r.t weights of past\n m epochs. For distributed env, it will accumulate full grads in the kvstore.\n\n Parameters\n ----------\n train_data: DataIter\n Train data iterator\n " ]
Please provide a description of the function:def _accumulate_kvstore(self, key, value): # Accumulate full gradients for current epochs self._kvstore.push(key + "_full", value) self._kvstore._barrier() self._kvstore.pull(key + "_full", value) self._allocate_gradients(key, value)
[ "Accumulate gradients over all data in the KVStore. In distributed setting, each worker sees a portion of\n data. The full gradients will be aggregated from each worker in the KVStore.\n\n Parameters\n ----------\n\n key: int or str\n Key in the KVStore.\n value: NDArray, RowSparseNDArray\n Average of the full gradients.\n " ]
Please provide a description of the function:def _allocate_gradients(self, key, value): for i in range(self._ctx_len): self._param_dict[i][key] = value[i] / self._ctx_len
[ "Allocate average of full gradients accumulated in the KVStore to each device.\n\n Parameters\n ----------\n\n key: int or str\n Key in the kvstore.\n value: List of NDArray, List of RowSparseNDArray\n A list of average of the full gradients in the KVStore.\n " ]
Please provide a description of the function:def _svrg_grads_update_rule(self, g_curr_batch_curr_weight, g_curr_batch_special_weight, g_special_weight_all_batch): for index, grad in enumerate(g_curr_batch_curr_weight): grad -= g_curr_batch_special_weight[index] grad += g_special_weight_all_batch[index] return g_curr_batch_curr_weight
[ "Calculates the gradient based on the SVRG update rule.\n Parameters\n ----------\n g_curr_batch_curr_weight : NDArray\n gradients of current weight of self.mod w.r.t current batch of data\n g_curr_batch_special_weight: NDArray\n gradients of the weight of past m epochs of self._mod_special w.r.t current batch of data\n g_special_weight_all_batch: NDArray\n average of full gradients over full pass of data\n\n Returns\n ----------\n Gradients calculated using SVRG update rule:\n grads = g_curr_batch_curr_weight - g_curr_batch_special_weight + g_special_weight_all_batch\n " ]
Please provide a description of the function:def _update_svrg_gradients(self): param_names = self._exec_group.param_names for ctx in range(self._ctx_len): for index, name in enumerate(param_names): g_curr_batch_reg = self._exec_group.grad_arrays[index][ctx] g_curr_batch_special = self._mod_aux._exec_group.grad_arrays[index][ctx] g_special_weight_all_batch = self._param_dict[ctx][name] g_svrg = self._svrg_grads_update_rule(g_curr_batch_reg, g_curr_batch_special, g_special_weight_all_batch) self._exec_group.grad_arrays[index][ctx] = g_svrg
[ "Calculates gradients based on the SVRG update rule.\n " ]
Please provide a description of the function:def fit(self, train_data, eval_data=None, eval_metric='acc', epoch_end_callback=None, batch_end_callback=None, kvstore='local', optimizer='sgd', optimizer_params=(('learning_rate', 0.01),), eval_end_callback=None, eval_batch_end_callback=None, initializer=mx.init.Uniform(0.01), arg_params=None, aux_params=None, allow_missing=False, force_rebind=False, force_init=False, begin_epoch=0, num_epoch=None, validation_metric=None, monitor=None, sparse_row_id_fn=None): assert num_epoch is not None, 'please specify number of epochs' self.bind(data_shapes=train_data.provide_data, label_shapes=train_data.provide_label, for_training=True, force_rebind=force_rebind) if monitor is not None: self.install_monitor(monitor) self.init_params(initializer=initializer, arg_params=arg_params, aux_params=aux_params, allow_missing=allow_missing, force_init=force_init) self.init_optimizer(kvstore=kvstore, optimizer=optimizer, optimizer_params=optimizer_params) if validation_metric is None: validation_metric = eval_metric if not isinstance(eval_metric, mx.metric.EvalMetric): eval_metric = mx.metric.create(eval_metric) ################################################################################ # training loop ################################################################################ for epoch in range(begin_epoch, num_epoch): eval_metric.reset() tic = time.time() if epoch % self.update_freq == 0: self.update_full_grads(train_data) train_data.reset() data_iter = iter(train_data) end_of_batch = False nbatch = 0 next_data_batch = next(data_iter) while not end_of_batch: data_batch = next_data_batch if monitor is not None: monitor.tic() self.forward_backward(data_batch) self.update() if isinstance(data_batch, list): self.update_metric(eval_metric, [db.label for db in data_batch], pre_sliced=True) else: self.update_metric(eval_metric, data_batch.label) try: # pre fetch next batch next_data_batch = next(data_iter) self.prepare(next_data_batch, sparse_row_id_fn=sparse_row_id_fn) except StopIteration: end_of_batch = True if monitor is not None: monitor.toc_print() if end_of_batch: eval_name_vals = eval_metric.get_name_value() if batch_end_callback is not None: batch_end_params = mx.model.BatchEndParam(epoch=epoch, nbatch=nbatch, eval_metric=eval_metric, locals=locals()) for callback in mx.base._as_list(batch_end_callback): callback(batch_end_params) nbatch += 1 for name, val in eval_name_vals: self.logger.info('Epoch[%d] Train-%s=%f', epoch, name, val) toc = time.time() self.logger.info('Epoch[%d] Time cost=%.3f', epoch, (toc - tic)) # sync aux params across devices arg_params, aux_params = self.get_params() self.set_params(arg_params, aux_params) if epoch_end_callback is not None: for callback in mx.base._as_list(epoch_end_callback): callback(epoch, self.symbol, arg_params, aux_params) # ---------------------------------------- # evaluation on validation set if eval_data: res = self.score(eval_data, validation_metric, score_end_callback=eval_end_callback, batch_end_callback=eval_batch_end_callback, epoch=epoch) for name, val in res: self.logger.info('Epoch[%d] Validation-%s=%f', epoch, name, val)
[ "Trains the module parameters.\n\n Parameters\n ----------\n train_data : DataIter\n Train DataIter.\n eval_data : DataIter\n If not ``None``, will be used as validation set and the performance\n after each epoch will be evaluated.\n eval_metric : str or EvalMetric\n Defaults to 'accuracy'. The performance measure used to display during training.\n Other possible predefined metrics are:\n 'ce' (CrossEntropy), 'f1', 'mae', 'mse', 'rmse', 'top_k_accuracy'.\n epoch_end_callback : function or list of functions\n Each callback will be called with the current `epoch`, `symbol`, `arg_params`\n and `aux_params`.\n batch_end_callback : function or list of function\n Each callback will be called with a `BatchEndParam`.\n kvstore : str or KVStore\n Defaults to 'local'.\n optimizer : str or Optimizer\n Defaults to 'sgd'.\n optimizer_params : dict\n Defaults to ``(('learning_rate', 0.01),)``. The parameters for\n the optimizer constructor.\n The default value is not a dict, just to avoid pylint warning on dangerous\n default values.\n eval_end_callback : function or list of function\n These will be called at the end of each full evaluation, with the metrics over\n the entire evaluation set.\n eval_batch_end_callback : function or list of function\n These will be called at the end of each mini-batch during evaluation.\n initializer : Initializer\n The initializer is called to initialize the module parameters when they are\n not already initialized.\n arg_params : dict\n Defaults to ``None``, if not ``None``, should be existing parameters from a trained\n model or loaded from a checkpoint (previously saved model). In this case,\n the value here will be used to initialize the module parameters, unless they\n are already initialized by the user via a call to `init_params` or `fit`.\n `arg_params` has a higher priority than `initializer`.\n aux_params : dict\n Defaults to ``None``. Similar to `arg_params`, except for auxiliary states.\n allow_missing : bool\n Defaults to ``False``. Indicates whether to allow missing parameters when `arg_params`\n and `aux_params` are not ``None``. If this is ``True``, then the missing parameters\n will be initialized via the `initializer`.\n force_rebind : bool\n Defaults to ``False``. Whether to force rebinding the executors if already bound.\n force_init : bool\n Defaults to ``False``. Indicates whether to force initialization even if the\n parameters are already initialized.\n begin_epoch : int\n Defaults to 0. Indicates the starting epoch. Usually, if resumed from a\n checkpoint saved at a previous training phase at epoch N, then this value should be\n N+1.\n num_epoch : int\n Number of epochs for training.\n sparse_row_id_fn : A callback function\n The function takes `data_batch` as an input and returns a dict of\n str -> NDArray. The resulting dict is used for pulling row_sparse\n parameters from the kvstore, where the str key is the name of the param,\n and the value is the row id of the param to pull.\n validation_metric: str or EvalMetric\n The performance measure used to display during validation.\n " ]
Please provide a description of the function:def prepare(self, data_batch, sparse_row_id_fn=None): super(SVRGModule, self).prepare(data_batch, sparse_row_id_fn=sparse_row_id_fn) self._mod_aux.prepare(data_batch, sparse_row_id_fn=sparse_row_id_fn)
[ "Prepares two modules for processing a data batch.\n\n Usually involves switching bucket and reshaping.\n For modules that contain `row_sparse` parameters in KVStore,\n it prepares the `row_sparse` parameters based on the sparse_row_id_fn.\n\n When KVStore is used to update parameters for multi-device or multi-machine training,\n a copy of the parameters are stored in KVStore. Note that for `row_sparse` parameters,\n the `update()` updates the copy of parameters in KVStore, but doesn't broadcast\n the updated parameters to all devices / machines. The `prepare` function is used to\n broadcast `row_sparse` parameters with the next batch of data.\n\n Parameters\n ----------\n data_batch : DataBatch\n The current batch of data for forward computation.\n\n sparse_row_id_fn : A callback function\n The function takes `data_batch` as an input and returns a dict of\n str -> NDArray. The resulting dict is used for pulling row_sparse\n parameters from the kvstore, where the str key is the name of the param,\n and the value is the row id of the param to pull.\n " ]
Please provide a description of the function:def _load_image_set_index(self, shuffle): assert os.path.exists(self.list_file), 'Path does not exists: {}'.format(self.list_file) with open(self.list_file, 'r') as f: image_set_index = [x.strip() for x in f.readlines()] if shuffle: np.random.shuffle(image_set_index) return image_set_index
[ "\n find out which indexes correspond to given image set (train or val)\n\n Parameters:\n ----------\n shuffle : boolean\n whether to shuffle the image list\n Returns:\n ----------\n entire list of images specified in the setting\n " ]
Please provide a description of the function:def _label_path_from_index(self, index): label_file = os.path.join(self.label_dir, index + self.label_extension) assert os.path.exists(label_file), 'Path does not exist: {}'.format(label_file) return label_file
[ "\n given image index, find out annotation path\n\n Parameters:\n ----------\n index: int\n index of a specific image\n\n Returns:\n ----------\n full path of annotation file\n " ]
Please provide a description of the function:def _load_image_labels(self): temp = [] # load ground-truths for idx in self.image_set_index: label_file = self._label_path_from_index(idx) with open(label_file, 'r') as f: label = [] for line in f.readlines(): temp_label = line.strip().split() assert len(temp_label) == 5, "Invalid label file" + label_file cls_id = int(temp_label[0]) x = float(temp_label[1]) y = float(temp_label[2]) half_width = float(temp_label[3]) / 2 half_height = float(temp_label[4]) / 2 xmin = x - half_width ymin = y - half_height xmax = x + half_width ymax = y + half_height label.append([cls_id, xmin, ymin, xmax, ymax]) temp.append(np.array(label)) return temp
[ "\n preprocess all ground-truths\n\n Returns:\n ----------\n labels packed in [num_images x max_num_objects x 5] tensor\n " ]
Please provide a description of the function:def get_register_func(base_class, nickname): if base_class not in _REGISTRY: _REGISTRY[base_class] = {} registry = _REGISTRY[base_class] def register(klass, name=None): assert issubclass(klass, base_class), \ "Can only register subclass of %s"%base_class.__name__ if name is None: name = klass.__name__ name = name.lower() if name in registry: warnings.warn( "\033[91mNew %s %s.%s registered with name %s is" "overriding existing %s %s.%s\033[0m"%( nickname, klass.__module__, klass.__name__, name, nickname, registry[name].__module__, registry[name].__name__), UserWarning, stacklevel=2) registry[name] = klass return klass register.__doc__ = "Register %s to the %s factory"%(nickname, nickname) return register
[ "Get registrator function.\n\n Parameters\n ----------\n base_class : type\n base class for classes that will be reigstered\n nickname : str\n nickname of base_class for logging\n\n Returns\n -------\n a registrator function\n ", "Register functions" ]
Please provide a description of the function:def get_alias_func(base_class, nickname): register = get_register_func(base_class, nickname) def alias(*aliases): def reg(klass): for name in aliases: register(klass, name) return klass return reg return alias
[ "Get registrator function that allow aliases.\n\n Parameters\n ----------\n base_class : type\n base class for classes that will be reigstered\n nickname : str\n nickname of base_class for logging\n\n Returns\n -------\n a registrator function\n ", "alias registrator", "registrator function" ]
Please provide a description of the function:def get_create_func(base_class, nickname): if base_class not in _REGISTRY: _REGISTRY[base_class] = {} registry = _REGISTRY[base_class] def create(*args, **kwargs): if len(args): name = args[0] args = args[1:] else: name = kwargs.pop(nickname) if isinstance(name, base_class): assert len(args) == 0 and len(kwargs) == 0, \ "%s is already an instance. Additional arguments are invalid"%(nickname) return name if isinstance(name, dict): return create(**name) assert isinstance(name, string_types), "%s must be of string type"%nickname if name.startswith('['): assert not args and not kwargs name, kwargs = json.loads(name) return create(name, **kwargs) elif name.startswith('{'): assert not args and not kwargs kwargs = json.loads(name) return create(**kwargs) name = name.lower() assert name in registry, \ "%s is not registered. Please register with %s.register first"%( str(name), nickname) return registry[name](*args, **kwargs) create.__doc__ = %(nickname, nickname, base_class.__name__) return create
[ "Get creator function\n\n Parameters\n ----------\n base_class : type\n base class for classes that will be reigstered\n nickname : str\n nickname of base_class for logging\n\n Returns\n -------\n a creator function\n ", "Create instance from config", "Create a %s instance from config.\n\nParameters\n----------\n%s : str or %s instance\n class name of desired instance. If is a instance,\n it will be returned directly.\n**kwargs : dict\n arguments to be passed to constructor" ]
Please provide a description of the function:def parse_args(): parser = argparse.ArgumentParser( formatter_class=argparse.ArgumentDefaultsHelpFormatter, description='Diagnose script for checking the current system.') choices = ['python', 'pip', 'mxnet', 'os', 'hardware', 'network'] for choice in choices: parser.add_argument('--' + choice, default=1, type=int, help='Diagnose {}.'.format(choice)) parser.add_argument('--region', default='', type=str, help="Additional sites in which region(s) to test. \ Specify 'cn' for example to test mirror sites in China.") parser.add_argument('--timeout', default=10, type=int, help="Connection test timeout threshold, 0 to disable.") args = parser.parse_args() return args
[ "Parse arguments." ]
Please provide a description of the function:def clean_str(string): string = re.sub(r"[^A-Za-z0-9(),!?\'\`]", " ", string) string = re.sub(r"\'s", " \'s", string) string = re.sub(r"\'ve", " \'ve", string) string = re.sub(r"n\'t", " n\'t", string) string = re.sub(r"\'re", " \'re", string) string = re.sub(r"\'d", " \'d", string) string = re.sub(r"\'ll", " \'ll", string) string = re.sub(r",", " , ", string) string = re.sub(r"!", " ! ", string) string = re.sub(r"\(", r" \( ", string) string = re.sub(r"\)", r" \) ", string) string = re.sub(r"\?", r" \? ", string) string = re.sub(r"\s{2,}", " ", string) return string.strip().lower()
[ "Tokenization/string cleaning for all datasets except for SST.\n Original taken from https://github.com/yoonkim/CNN_sentence/blob/master/process_data.py\n " ]
Please provide a description of the function:def load_data_and_labels(): # Load data from files pos_path = "./data/rt-polaritydata/rt-polarity.pos" neg_path = "./data/rt-polaritydata/rt-polarity.neg" if not os.path.exists(pos_path): os.system("git clone https://github.com/dennybritz/cnn-text-classification-tf.git") os.system('mv cnn-text-classification-tf/data .') os.system('rm -rf cnn-text-classification-tf') positive_examples = list(open(pos_path).readlines()) positive_examples = [s.strip() for s in positive_examples] negative_examples = list(open(neg_path).readlines()) negative_examples = [s.strip() for s in negative_examples] # Split by words x_text = positive_examples + negative_examples x_text = [clean_str(sent) for sent in x_text] x_text = [s.split(" ") for s in x_text] # Generate labels positive_labels = [1 for _ in positive_examples] negative_labels = [0 for _ in negative_examples] y = np.concatenate([positive_labels, negative_labels], 0) return [x_text, y]
[ "Loads MR polarity data from files, splits the data into words and generates labels.\n Returns split sentences and labels.\n " ]
Please provide a description of the function:def pad_sentences(sentences, padding_word="</s>"): sequence_length = max(len(x) for x in sentences) padded_sentences = [] for i, sentence in enumerate(sentences): num_padding = sequence_length - len(sentence) new_sentence = sentence + [padding_word] * num_padding padded_sentences.append(new_sentence) return padded_sentences
[ "Pads all sentences to the same length. The length is defined by the longest sentence.\n Returns padded sentences.\n " ]
Please provide a description of the function:def build_input_data(sentences, labels, vocabulary): x = np.array([[vocabulary[word] for word in sentence] for sentence in sentences]) y = np.array(labels) return [x, y]
[ "Maps sentencs and labels to vectors based on a vocabulary." ]
Please provide a description of the function:def build_input_data_with_word2vec(sentences, labels, word2vec_list): x_vec = [] for sent in sentences: vec = [] for word in sent: if word in word2vec_list: vec.append(word2vec_list[word]) else: vec.append(word2vec_list['</s>']) x_vec.append(vec) x_vec = np.array(x_vec) y_vec = np.array(labels) return [x_vec, y_vec]
[ "\n Map sentences and labels to vectors based on a pretrained word2vec\n " ]
Please provide a description of the function:def load_data_with_word2vec(word2vec_list): # Load and preprocess data sentences, labels = load_data_and_labels() sentences_padded = pad_sentences(sentences) # vocabulary, vocabulary_inv = build_vocab(sentences_padded) return build_input_data_with_word2vec(sentences_padded, labels, word2vec_list)
[ "Loads and preprocessed data for the MR dataset.\n Returns input vectors, labels, vocabulary, and inverse vocabulary.\n " ]
Please provide a description of the function:def load_data(): # Load and preprocess data sentences, labels = load_data_and_labels() sentences_padded = pad_sentences(sentences) vocabulary, vocabulary_inv = build_vocab(sentences_padded) x, y = build_input_data(sentences_padded, labels, vocabulary) return [x, y, vocabulary, vocabulary_inv]
[ "Loads and preprocessed data for the MR dataset.\n Returns input vectors, labels, vocabulary, and inverse vocabulary.\n " ]
Please provide a description of the function:def batch_iter(data, batch_size, num_epochs): data = np.array(data) data_size = len(data) num_batches_per_epoch = int(len(data)/batch_size) + 1 for epoch in range(num_epochs): # Shuffle the data at each epoch shuffle_indices = np.random.permutation(np.arange(data_size)) shuffled_data = data[shuffle_indices] for batch_num in range(num_batches_per_epoch): start_index = batch_num * batch_size end_index = min((batch_num + 1) * batch_size, data_size) yield shuffled_data[start_index:end_index]
[ "Generates a batch iterator for a dataset." ]
Please provide a description of the function:def load_pretrained_word2vec(infile): if isinstance(infile, str): infile = open(infile) word2vec_list = {} for idx, line in enumerate(infile): if idx == 0: vocab_size, dim = line.strip().split() else: tks = line.strip().split() word2vec_list[tks[0]] = map(float, tks[1:]) return word2vec_list
[ "Load the pre-trained word2vec from file." ]
Please provide a description of the function:def generate_batch(im_tensor, im_info): data = [im_tensor, im_info] data_shapes = [('data', im_tensor.shape), ('im_info', im_info.shape)] data_batch = mx.io.DataBatch(data=data, label=None, provide_data=data_shapes, provide_label=None) return data_batch
[ "return batch" ]
Please provide a description of the function:def get_symbol(num_classes=1000, **kwargs): data = mx.symbol.Variable(name="data") label = mx.symbol.Variable(name="label") # group 1 conv1_1 = mx.symbol.Convolution( data=data, kernel=(3, 3), pad=(1, 1), num_filter=64, name="conv1_1") relu1_1 = mx.symbol.Activation(data=conv1_1, act_type="relu", name="relu1_1") conv1_2 = mx.symbol.Convolution( data=relu1_1, kernel=(3, 3), pad=(1, 1), num_filter=64, name="conv1_2") relu1_2 = mx.symbol.Activation(data=conv1_2, act_type="relu", name="relu1_2") pool1 = mx.symbol.Pooling( data=relu1_2, pool_type="max", kernel=(2, 2), stride=(2, 2), name="pool1") # group 2 conv2_1 = mx.symbol.Convolution( data=pool1, kernel=(3, 3), pad=(1, 1), num_filter=128, name="conv2_1") relu2_1 = mx.symbol.Activation(data=conv2_1, act_type="relu", name="relu2_1") conv2_2 = mx.symbol.Convolution( data=relu2_1, kernel=(3, 3), pad=(1, 1), num_filter=128, name="conv2_2") relu2_2 = mx.symbol.Activation(data=conv2_2, act_type="relu", name="relu2_2") pool2 = mx.symbol.Pooling( data=relu2_2, pool_type="max", kernel=(2, 2), stride=(2, 2), name="pool2") # group 3 conv3_1 = mx.symbol.Convolution( data=pool2, kernel=(3, 3), pad=(1, 1), num_filter=256, name="conv3_1") relu3_1 = mx.symbol.Activation(data=conv3_1, act_type="relu", name="relu3_1") conv3_2 = mx.symbol.Convolution( data=relu3_1, kernel=(3, 3), pad=(1, 1), num_filter=256, name="conv3_2") relu3_2 = mx.symbol.Activation(data=conv3_2, act_type="relu", name="relu3_2") conv3_3 = mx.symbol.Convolution( data=relu3_2, kernel=(3, 3), pad=(1, 1), num_filter=256, name="conv3_3") relu3_3 = mx.symbol.Activation(data=conv3_3, act_type="relu", name="relu3_3") pool3 = mx.symbol.Pooling( data=relu3_3, pool_type="max", kernel=(2, 2), stride=(2, 2), \ pooling_convention="full", name="pool3") # group 4 conv4_1 = mx.symbol.Convolution( data=pool3, kernel=(3, 3), pad=(1, 1), num_filter=512, name="conv4_1") relu4_1 = mx.symbol.Activation(data=conv4_1, act_type="relu", name="relu4_1") conv4_2 = mx.symbol.Convolution( data=relu4_1, kernel=(3, 3), pad=(1, 1), num_filter=512, name="conv4_2") relu4_2 = mx.symbol.Activation(data=conv4_2, act_type="relu", name="relu4_2") conv4_3 = mx.symbol.Convolution( data=relu4_2, kernel=(3, 3), pad=(1, 1), num_filter=512, name="conv4_3") relu4_3 = mx.symbol.Activation(data=conv4_3, act_type="relu", name="relu4_3") pool4 = mx.symbol.Pooling( data=relu4_3, pool_type="max", kernel=(2, 2), stride=(2, 2), name="pool4") # group 5 conv5_1 = mx.symbol.Convolution( data=pool4, kernel=(3, 3), pad=(1, 1), num_filter=512, name="conv5_1") relu5_1 = mx.symbol.Activation(data=conv5_1, act_type="relu", name="relu5_1") conv5_2 = mx.symbol.Convolution( data=relu5_1, kernel=(3, 3), pad=(1, 1), num_filter=512, name="conv5_2") relu5_2 = mx.symbol.Activation(data=conv5_2, act_type="relu", name="relu5_2") conv5_3 = mx.symbol.Convolution( data=relu5_2, kernel=(3, 3), pad=(1, 1), num_filter=512, name="conv5_3") relu5_3 = mx.symbol.Activation(data=conv5_3, act_type="relu", name="relu5_3") pool5 = mx.symbol.Pooling( data=relu5_3, pool_type="max", kernel=(3, 3), stride=(1, 1), pad=(1,1), name="pool5") # group 6 conv6 = mx.symbol.Convolution( data=pool5, kernel=(3, 3), pad=(6, 6), dilate=(6, 6), num_filter=1024, name="fc6") relu6 = mx.symbol.Activation(data=conv6, act_type="relu", name="relu6") # drop6 = mx.symbol.Dropout(data=relu6, p=0.5, name="drop6") # group 7 conv7 = mx.symbol.Convolution( data=relu6, kernel=(1, 1), pad=(0, 0), num_filter=1024, name="fc7") relu7 = mx.symbol.Activation(data=conv7, act_type="relu", name="relu7") # drop7 = mx.symbol.Dropout(data=relu7, p=0.5, name="drop7") gpool = mx.symbol.Pooling(data=relu7, pool_type='avg', kernel=(7, 7), global_pool=True, name='global_pool') conv8 = mx.symbol.Convolution(data=gpool, num_filter=num_classes, kernel=(1, 1), name='fc8') flat = mx.symbol.Flatten(data=conv8) softmax = mx.symbol.SoftmaxOutput(data=flat, name='softmax') return softmax
[ "\n VGG 16 layers network\n This is a modified version, with fc6/fc7 layers replaced by conv layers\n And the network is slightly smaller than original VGG 16 network\n " ]
Please provide a description of the function:def get_mlp(): data = mx.symbol.Variable('data') fc1 = mx.symbol.CaffeOp(data_0=data, num_weight=2, name='fc1', prototxt="layer{type:\"InnerProduct\" inner_product_param{num_output: 128} }") act1 = mx.symbol.CaffeOp(data_0=fc1, prototxt="layer{type:\"TanH\"}") fc2 = mx.symbol.CaffeOp(data_0=act1, num_weight=2, name='fc2', prototxt="layer{type:\"InnerProduct\" inner_product_param{num_output: 64} }") act2 = mx.symbol.CaffeOp(data_0=fc2, prototxt="layer{type:\"TanH\"}") fc3 = mx.symbol.CaffeOp(data_0=act2, num_weight=2, name='fc3', prototxt="layer{type:\"InnerProduct\" inner_product_param{num_output: 10}}") if use_caffe_loss: label = mx.symbol.Variable('softmax_label') mlp = mx.symbol.CaffeLoss(data=fc3, label=label, grad_scale=1, name='softmax', prototxt="layer{type:\"SoftmaxWithLoss\"}") else: mlp = mx.symbol.SoftmaxOutput(data=fc3, name='softmax') return mlp
[ "Get multi-layer perceptron" ]
Please provide a description of the function:def get_lenet(): data = mx.symbol.Variable('data') # first conv conv1 = mx.symbol.CaffeOp(data_0=data, num_weight=2, prototxt="layer{type:\"Convolution\" " "convolution_param { num_output: 20 kernel_size: 5 stride: 1} }") act1 = mx.symbol.CaffeOp(data_0=conv1, prototxt="layer{type:\"TanH\"}") pool1 = mx.symbol.CaffeOp(data_0=act1, prototxt="layer{type:\"Pooling\" pooling_param { pool: MAX kernel_size: 2 stride: 2}}") # second conv conv2 = mx.symbol.CaffeOp(data_0=pool1, num_weight=2, prototxt="layer{type:\"Convolution\" " "convolution_param { num_output: 50 kernel_size: 5 stride: 1} }") act2 = mx.symbol.CaffeOp(data_0=conv2, prototxt="layer{type:\"TanH\"}") pool2 = mx.symbol.CaffeOp(data_0=act2, prototxt="layer{type:\"Pooling\" pooling_param { pool: MAX kernel_size: 2 stride: 2}}") fc1 = mx.symbol.CaffeOp(data_0=pool2, num_weight=2, prototxt="layer{type:\"InnerProduct\" inner_product_param{num_output: 500} }") act3 = mx.symbol.CaffeOp(data_0=fc1, prototxt="layer{type:\"TanH\"}") # second fullc fc2 = mx.symbol.CaffeOp(data_0=act3, num_weight=2, prototxt="layer{type:\"InnerProduct\"inner_product_param{num_output: 10} }") if use_caffe_loss: label = mx.symbol.Variable('softmax_label') lenet = mx.symbol.CaffeLoss(data=fc2, label=label, grad_scale=1, name='softmax', prototxt="layer{type:\"SoftmaxWithLoss\"}") else: lenet = mx.symbol.SoftmaxOutput(data=fc2, name='softmax') return lenet
[ "LeCun, Yann, Leon Bottou, Yoshua Bengio, and Patrick\n Haffner. \"Gradient-based learning applied to document recognition.\"\n Proceedings of the IEEE (1998)\n " ]
Please provide a description of the function:def parse_args(): parser = argparse.ArgumentParser(description='train an image classifier on mnist') parser.add_argument('--network', type=str, default='lenet', help='the cnn to use (mlp | lenet | <path to network json file>') parser.add_argument('--caffe-loss', type=int, default=0, help='Use CaffeLoss symbol') parser.add_argument('--caffe-data', action='store_true', help='Use Caffe input-data layer only if specified') parser.add_argument('--data-dir', type=str, default='mnist/', help='the input data directory') parser.add_argument('--gpus', type=str, help='the gpus will be used, e.g "0,1,2,3"') parser.add_argument('--num-examples', type=int, default=60000, help='the number of training examples') parser.add_argument('--batch-size', type=int, default=128, help='the batch size') parser.add_argument('--lr', type=float, default=.1, help='the initial learning rate') parser.add_argument('--model-prefix', type=str, help='the prefix of the model to load/save') parser.add_argument('--save-model-prefix', type=str, help='the prefix of the model to save') parser.add_argument('--num-epochs', type=int, default=10, help='the number of training epochs') parser.add_argument('--load-epoch', type=int, help="load the model on an epoch using the model-prefix") parser.add_argument('--kv-store', type=str, default='local', help='the kvstore type') parser.add_argument('--lr-factor', type=float, default=1, help='times the lr with a factor for every lr-factor-epoch epoch') parser.add_argument('--lr-factor-epoch', type=float, default=1, help='the number of epoch to factor the lr, could be .5') return parser.parse_args()
[ "Parse the arguments" ]
Please provide a description of the function:def forward(self, is_train, req, in_data, out_data, aux): data = in_data[0] label = in_data[1] pred = mx.nd.SoftmaxOutput(data, label) self.assign(out_data[0], req[0], pred)
[ "Implements forward computation.\n\n is_train : bool, whether forwarding for training or testing.\n req : list of {'null', 'write', 'inplace', 'add'}, how to assign to out_data. 'null' means skip assignment, etc.\n in_data : list of NDArray, input data.\n out_data : list of NDArray, pre-allocated output buffers.\n aux : list of NDArray, mutable auxiliary states. Usually not used.\n " ]
Please provide a description of the function:def backward(self, req, out_grad, in_data, out_data, in_grad, aux): label = in_data[1] pred = out_data[0] dx = pred - mx.nd.one_hot(label, 2) pos_cls_weight = self.positive_cls_weight scale_factor = ((1 + label * pos_cls_weight) / pos_cls_weight).reshape((pred.shape[0],1)) rescaled_dx = scale_factor * dx self.assign(in_grad[0], req[0], rescaled_dx)
[ "Implements backward computation\n\n req : list of {'null', 'write', 'inplace', 'add'}, how to assign to in_grad\n out_grad : list of NDArray, gradient w.r.t. output data.\n in_grad : list of NDArray, gradient w.r.t. input data. This is the output buffer.\n " ]
Please provide a description of the function:def _reset_bind(self): self.binded = False self._buckets = {} self._curr_module = None self._curr_bucket_key = None
[ "Internal utility function to reset binding." ]
Please provide a description of the function:def data_names(self): if self.binded: return self._curr_module.data_names else: _, data_names, _ = self._call_sym_gen(self._default_bucket_key) return data_names
[ "A list of names for data required by this module." ]
Please provide a description of the function:def output_names(self): if self.binded: return self._curr_module.output_names else: symbol, _, _ = self._call_sym_gen(self._default_bucket_key) return symbol.list_outputs()
[ "A list of names for the outputs of this module." ]
Please provide a description of the function:def get_params(self): assert self.binded and self.params_initialized self._curr_module._params_dirty = self._params_dirty params = self._curr_module.get_params() self._params_dirty = False return params
[ "Gets current parameters.\n\n Returns\n -------\n `(arg_params, aux_params)`\n A pair of dictionaries each mapping parameter names to NDArray values.\n " ]
Please provide a description of the function:def init_params(self, initializer=Uniform(0.01), arg_params=None, aux_params=None, allow_missing=False, force_init=False, allow_extra=False): if self.params_initialized and not force_init: return assert self.binded, 'call bind before initializing the parameters' self._curr_module.init_params(initializer=initializer, arg_params=arg_params, aux_params=aux_params, allow_missing=allow_missing, force_init=force_init, allow_extra=allow_extra) self._params_dirty = False self.params_initialized = True
[ "Initializes parameters.\n\n Parameters\n ----------\n initializer : Initializer\n arg_params : dict\n Defaults to ``None``. Existing parameters. This has higher priority\n than `initializer`.\n aux_params : dict\n Defaults to ``None``. Existing auxiliary states. This has higher priority\n than `initializer`.\n allow_missing : bool\n Allow missing values in `arg_params` and `aux_params` (if not ``None``).\n In this case, missing values will be filled with `initializer`.\n force_init : bool\n Defaults to ``False``.\n allow_extra : boolean, optional\n Whether allow extra parameters that are not needed by symbol.\n If this is True, no error will be thrown when arg_params or aux_params\n contain extra parameters that is not needed by the executor.\n " ]
Please provide a description of the function:def get_states(self, merge_multi_context=True): assert self.binded and self.params_initialized return self._curr_module.get_states(merge_multi_context=merge_multi_context)
[ "Gets states from all devices.\n\n Parameters\n ----------\n merge_multi_context : bool\n Default is `True`. In the case when data-parallelism is used, the states\n will be collected from multiple devices. A `True` value indicate that we\n should merge the collected results so that they look like from a single\n executor.\n\n Returns\n -------\n list of NDArrays or list of list of NDArrays\n If `merge_multi_context` is ``True``, it is like ``[out1, out2]``. Otherwise, it\n is like ``[[out1_dev1, out1_dev2], [out2_dev1, out2_dev2]]``. All the output\n elements are `NDArray`.\n " ]
Please provide a description of the function:def set_states(self, states=None, value=None): assert self.binded and self.params_initialized self._curr_module.set_states(states, value)
[ "Sets value for states. Only one of states & values can be specified.\n\n Parameters\n ----------\n states : list of list of NDArrays\n Source states arrays formatted like ``[[state1_dev1, state1_dev2],\n [state2_dev1, state2_dev2]]``.\n value : number\n A single scalar value for all state arrays.\n " ]
Please provide a description of the function:def bind(self, data_shapes, label_shapes=None, for_training=True, inputs_need_grad=False, force_rebind=False, shared_module=None, grad_req='write'): # in case we already initialized params, keep it if self.params_initialized: arg_params, aux_params = self.get_params() # force rebinding is typically used when one want to switch from # training to prediction phase. if force_rebind: self._reset_bind() if self.binded: self.logger.warning('Already bound, ignoring bind()') return assert shared_module is None, 'shared_module for BucketingModule is not supported' self.for_training = for_training self.inputs_need_grad = inputs_need_grad self.binded = True self._grad_req = grad_req symbol, data_names, label_names = self._call_sym_gen(self._default_bucket_key) module = Module(symbol, data_names, label_names, logger=self.logger, context=self._context, work_load_list=self._work_load_list, fixed_param_names=self._fixed_param_names, state_names=self._state_names, group2ctxs=self._group2ctxs, compression_params=self._compression_params) module.bind(data_shapes, label_shapes, for_training, inputs_need_grad, force_rebind=False, shared_module=None, grad_req=self._grad_req) self._curr_module = module self._curr_bucket_key = self._default_bucket_key self._buckets[self._default_bucket_key] = module # copy back saved params, if already initialized if self.params_initialized: self.set_params(arg_params, aux_params)
[ "Binding for a `BucketingModule` means setting up the buckets and binding the\n executor for the default bucket key. Executors corresponding to other keys are\n bound afterwards with `switch_bucket`.\n\n Parameters\n ----------\n data_shapes : list of (str, tuple)\n This should correspond to the symbol for the default bucket.\n label_shapes : list of (str, tuple)\n This should correspond to the symbol for the default bucket.\n for_training : bool\n Default is ``True``.\n inputs_need_grad : bool\n Default is ``False``.\n force_rebind : bool\n Default is ``False``.\n shared_module : BucketingModule\n Default is ``None``. This value is currently not used.\n grad_req : str, list of str, dict of str to str\n Requirement for gradient accumulation. Can be 'write', 'add', or 'null'\n (default to 'write').\n Can be specified globally (str) or for each argument (list, dict).\n bucket_key : str (or any python object)\n bucket key for binding. by default use the default_bucket_key\n " ]
Please provide a description of the function:def switch_bucket(self, bucket_key, data_shapes, label_shapes=None): assert self.binded, 'call bind before switching bucket' if not bucket_key in self._buckets: symbol, data_names, label_names = self._call_sym_gen(bucket_key) module = Module(symbol, data_names, label_names, logger=self.logger, context=self._context, work_load_list=self._work_load_list, fixed_param_names=self._fixed_param_names, state_names=self._state_names, group2ctxs=self._group2ctxs, compression_params=self._compression_params) module.bind(data_shapes, label_shapes, self._curr_module.for_training, self._curr_module.inputs_need_grad, force_rebind=False, shared_module=self._buckets[self._default_bucket_key], grad_req=self._grad_req) if self._monitor is not None: module.install_monitor(self._monitor) self._buckets[bucket_key] = module self._curr_module = self._buckets[bucket_key] self._curr_bucket_key = bucket_key
[ "Switches to a different bucket. This will change ``self.curr_module``.\n\n Parameters\n ----------\n bucket_key : str (or any python object)\n The key of the target bucket.\n data_shapes : list of (str, tuple)\n Typically ``data_batch.provide_data``.\n label_shapes : list of (str, tuple)\n Typically ``data_batch.provide_label``.\n " ]
Please provide a description of the function:def init_optimizer(self, kvstore='local', optimizer='sgd', optimizer_params=(('learning_rate', 0.01),), force_init=False): assert self.binded and self.params_initialized if self.optimizer_initialized and not force_init: self.logger.warning('optimizer already initialized, ignoring.') return self._curr_module.init_optimizer(kvstore, optimizer, optimizer_params, force_init=force_init) for mod in self._buckets.values(): if mod is not self._curr_module: mod.borrow_optimizer(self._curr_module) self.optimizer_initialized = True
[ "Installs and initializes optimizers.\n\n Parameters\n ----------\n kvstore : str or KVStore\n Defaults to `'local'`.\n optimizer : str or Optimizer\n Defaults to `'sgd'`\n optimizer_params : dict\n Defaults to `(('learning_rate', 0.01),)`. The default value is not a dictionary,\n just to avoid pylint warning of dangerous default values.\n force_init : bool\n Defaults to ``False``, indicating whether we should force re-initializing the\n optimizer in the case an optimizer is already installed.\n " ]
Please provide a description of the function:def prepare(self, data_batch, sparse_row_id_fn=None): '''Prepares the module for processing a data batch. Usually involves switching bucket and reshaping. For modules that contain `row_sparse` parameters in KVStore, it prepares the `row_sparse` parameters based on the sparse_row_id_fn. Parameters ---------- data_batch : DataBatch The current batch of data for forward computation. 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. ''' # perform bind if haven't done so assert self.binded and self.params_initialized bucket_key = data_batch.bucket_key original_bucket_key = self._curr_bucket_key data_shapes = data_batch.provide_data label_shapes = data_batch.provide_label self.switch_bucket(bucket_key, data_shapes, label_shapes) self._curr_module.prepare(data_batch, sparse_row_id_fn=sparse_row_id_fn) # switch back self.switch_bucket(original_bucket_key, None, None)
[]
Please provide a description of the function:def forward(self, data_batch, is_train=None): assert self.binded and self.params_initialized self.switch_bucket(data_batch.bucket_key, data_batch.provide_data, data_batch.provide_label) self._curr_module.forward(data_batch, is_train=is_train)
[ "Forward computation.\n\n Parameters\n ----------\n data_batch : DataBatch\n is_train : bool\n Defaults to ``None``, in which case `is_train` is take as ``self.for_training``.\n " ]
Please provide a description of the function:def backward(self, out_grads=None): assert self.binded and self.params_initialized self._curr_module.backward(out_grads=out_grads)
[ "Backward computation." ]
Please provide a description of the function:def update(self): assert self.binded and self.params_initialized and self.optimizer_initialized self._params_dirty = True self._curr_module.update()
[ "Updates parameters according to installed optimizer and the gradient computed\n in the previous forward-backward cycle.\n\n When KVStore is used to update parameters for multi-device or multi-machine training,\n a copy of the parameters are stored in KVStore. Note that for `row_sparse` parameters,\n this function does update the copy of parameters in KVStore, but doesn't broadcast the\n updated parameters to all devices / machines. Please call `prepare` to broadcast\n `row_sparse` parameters with the next batch of data.\n\n " ]
Please provide a description of the function:def get_outputs(self, merge_multi_context=True): assert self.binded and self.params_initialized return self._curr_module.get_outputs(merge_multi_context=merge_multi_context)
[ "Gets outputs from a previous forward computation.\n\n Parameters\n ----------\n merge_multi_context : bool\n Defaults to ``True``. In the case when data-parallelism is used, the outputs\n will be collected from multiple devices. A ``True`` value indicate that we\n should merge the collected results so that they look like from a single\n executor.\n\n Returns\n -------\n list of numpy arrays or list of list of numpy arrays\n If `merge_multi_context` is ``True``, it is like ``[out1, out2]``. Otherwise, it\n is like ``[[out1_dev1, out1_dev2], [out2_dev1, out2_dev2]]``. All the output\n elements are numpy arrays.\n " ]
Please provide a description of the function:def get_input_grads(self, merge_multi_context=True): assert self.binded and self.params_initialized and self.inputs_need_grad return self._curr_module.get_input_grads(merge_multi_context=merge_multi_context)
[ "Gets the gradients with respect to the inputs of the module.\n\n Parameters\n ----------\n merge_multi_context : bool\n Defaults to ``True``. In the case when data-parallelism is used, the outputs\n will be collected from multiple devices. A ``True`` value indicate that we\n should merge the collected results so that they look like from a single\n executor.\n\n Returns\n -------\n list of NDArrays or list of list of NDArrays\n If `merge_multi_context` is ``True``, it is like ``[grad1, grad2]``. Otherwise, it\n is like ``[[grad1_dev1, grad1_dev2], [grad2_dev1, grad2_dev2]]``. All the output\n elements are `NDArray`.\n " ]
Please provide a description of the function:def update_metric(self, eval_metric, labels, pre_sliced=False): assert self.binded and self.params_initialized self._curr_module.update_metric(eval_metric, labels, pre_sliced)
[ "Evaluates and accumulates evaluation metric on outputs of the last forward computation.\n\n Parameters\n ----------\n eval_metric : EvalMetric\n labels : list of NDArray\n Typically ``data_batch.label``.\n " ]
Please provide a description of the function:def install_monitor(self, mon): assert self.binded self._monitor = mon for mod in self._buckets.values(): mod.install_monitor(mon)
[ "Installs monitor on all executors " ]
Please provide a description of the function:def set_recording(is_recording): #pylint: disable=redefined-outer-name prev = ctypes.c_int() check_call(_LIB.MXAutogradSetIsRecording( ctypes.c_int(is_recording), ctypes.byref(prev))) return bool(prev.value)
[ "Set status to recording/not recording. When recording, graph will be constructed\n for gradient computation.\n\n Parameters\n ----------\n is_recording: bool\n\n Returns\n -------\n previous state before this set.\n " ]
Please provide a description of the function:def set_training(train_mode): #pylint: disable=redefined-outer-name prev = ctypes.c_int() check_call(_LIB.MXAutogradSetIsTraining( ctypes.c_int(train_mode), ctypes.byref(prev))) return bool(prev.value)
[ "Set status to training/predicting. This affects ctx.is_train in operator\n running context. For example, Dropout will drop inputs randomly when\n train_mode=True while simply passing through if train_mode=False.\n\n Parameters\n ----------\n train_mode: bool\n\n Returns\n -------\n previous state before this set.\n " ]
Please provide a description of the function:def is_recording(): curr = ctypes.c_bool() check_call(_LIB.MXAutogradIsRecording(ctypes.byref(curr))) return curr.value
[ "Get status on recording/not recording.\n\n Returns\n -------\n Current state of recording.\n " ]
Please provide a description of the function:def is_training(): curr = ctypes.c_bool() check_call(_LIB.MXAutogradIsTraining(ctypes.byref(curr))) return curr.value
[ "Get status on training/predicting.\n\n Returns\n -------\n Current state of training/predicting.\n " ]
Please provide a description of the function:def mark_variables(variables, gradients, grad_reqs='write'): if isinstance(variables, NDArray): assert isinstance(gradients, NDArray) variables = [variables] gradients = [gradients] if isinstance(grad_reqs, string_types): grad_reqs = [_GRAD_REQ_MAP[grad_reqs]]*len(variables) else: grad_reqs = [_GRAD_REQ_MAP[i] for i in grad_reqs] check_call(_LIB.MXAutogradMarkVariables( len(variables), c_handle_array(variables), c_array_buf(mx_uint, array('I', grad_reqs)), c_handle_array(gradients)))
[ "Mark NDArrays as variables to compute gradient for autograd.\n\n Parameters\n ----------\n variables: NDArray or list of NDArray\n gradients: NDArray or list of NDArray\n grad_reqs: str or list of str\n " ]
Please provide a description of the function:def _parse_head(heads, head_grads): if isinstance(heads, NDArray): heads = [heads] if isinstance(head_grads, NDArray): head_grads = [head_grads] head_handles = c_handle_array(heads) if head_grads is None: hgrad_handles = ctypes.c_void_p(0) else: assert len(heads) == len(head_grads), \ "heads and head_grads must be lists of the same length" hgrad_handles = c_array(NDArrayHandle, [i.handle if i is not None else NDArrayHandle(0) for i in head_grads]) return head_handles, hgrad_handles
[ "parse head gradient for backward and grad." ]
Please provide a description of the function:def backward(heads, head_grads=None, retain_graph=False, train_mode=True): #pylint: disable=redefined-outer-name head_handles, hgrad_handles = _parse_head(heads, head_grads) check_call(_LIB.MXAutogradBackwardEx( len(head_handles), head_handles, hgrad_handles, 0, ctypes.c_void_p(0), ctypes.c_int(retain_graph), ctypes.c_int(0), ctypes.c_int(train_mode), ctypes.c_void_p(0), ctypes.c_void_p(0)))
[ "Compute the gradients of heads w.r.t previously marked variables.\n\n Parameters\n ----------\n heads: NDArray or list of NDArray\n Output NDArray(s)\n head_grads: NDArray or list of NDArray or None\n Gradients with respect to heads.\n train_mode: bool, optional\n Whether to do backward for training or predicting.\n " ]
Please provide a description of the function:def grad(heads, variables, head_grads=None, retain_graph=None, create_graph=False, train_mode=True): #pylint: disable=redefined-outer-name head_handles, hgrad_handles = _parse_head(heads, head_grads) if isinstance(variables, NDArray): variables = [variables] else: assert len(variables), "variables cannot be an empty list." var_handles = c_handle_array(variables) retain_graph = retain_graph if retain_graph is not None else create_graph grad_vars = ctypes.POINTER(NDArrayHandle)() grad_stypes = ctypes.POINTER(ctypes.c_int)() check_call(_LIB.MXAutogradBackwardEx( len(head_handles), head_handles, hgrad_handles, len(var_handles), var_handles, ctypes.c_int(retain_graph), ctypes.c_int(create_graph), ctypes.c_int(train_mode), ctypes.byref(grad_vars), ctypes.byref(grad_stypes))) ret = [_ndarray_cls(ctypes.cast(grad_vars[i], NDArrayHandle), stype=grad_stypes[i]) for i in range(len(var_handles))] if isinstance(variables, NDArray): return ret[0] return ret
[ "Compute the gradients of heads w.r.t variables. Gradients will be\n returned as new NDArrays instead of stored into `variable.grad`.\n Supports recording gradient graph for computing higher order gradients.\n\n .. note::\n\n Currently only a very limited set of operators support higher order \\\n gradients.\n\n Parameters\n ----------\n heads: NDArray or list of NDArray\n Output NDArray(s)\n variables: NDArray or list of NDArray\n Input variables to compute gradients for.\n head_grads: NDArray or list of NDArray or None\n Gradients with respect to heads.\n retain_graph: bool\n Whether to keep computation graph to differentiate again, instead\n of clearing history and release memory. Defaults to the same value\n as create_graph.\n create_graph: bool\n Whether to record gradient graph for computing higher order\n train_mode: bool, optional\n Whether to do backward for training or prediction.\n\n Returns\n -------\n NDArray or list of NDArray:\n Gradients with respect to variables.\n\n Examples\n --------\n >>> x = mx.nd.ones((1,))\n >>> x.attach_grad()\n >>> with mx.autograd.record():\n ... z = mx.nd.elemwise_add(mx.nd.exp(x), x)\n >>> dx = mx.autograd.grad(z, [x], create_graph=True)\n >>> print(dx)\n [\n [ 3.71828175]\n <NDArray 1 @cpu(0)>]\n " ]
Please provide a description of the function:def get_symbol(x): hdl = SymbolHandle() check_call(_LIB.MXAutogradGetSymbol(x.handle, ctypes.byref(hdl))) return Symbol(hdl)
[ "Retrieve recorded computation history as `Symbol`.\n\n Parameters\n ----------\n x : NDArray\n Array representing the head of computation graph.\n\n Returns\n -------\n Symbol\n The retrieved Symbol.\n " ]
Please provide a description of the function:def load_mldataset(filename): user = [] item = [] score = [] with open(filename) as f: for line in f: tks = line.strip().split('\t') if len(tks) != 4: continue user.append(int(tks[0])) item.append(int(tks[1])) score.append(float(tks[2])) user = mx.nd.array(user) item = mx.nd.array(item) score = mx.nd.array(score) return gluon.data.ArrayDataset(user, item, score)
[ "Not particularly fast code to parse the text file and load it into three NDArray's\n and product an NDArrayIter\n " ]
Please provide a description of the function:def ParseAllOps(): cdll.libmxnet = cdll.LoadLibrary(sys.argv[1]) ListOP = cdll.libmxnet.MXSymbolListAtomicSymbolCreators GetOpInfo = cdll.libmxnet.MXSymbolGetAtomicSymbolInfo ListOP.argtypes=[POINTER(c_int), POINTER(POINTER(c_void_p))] GetOpInfo.argtypes=[c_void_p, \ POINTER(c_char_p), \ POINTER(c_char_p), \ POINTER(c_int), \ POINTER(POINTER(c_char_p)), \ POINTER(POINTER(c_char_p)), \ POINTER(POINTER(c_char_p)), \ POINTER(c_char_p), \ POINTER(c_char_p) ] nOps = c_int() opHandlers = POINTER(c_void_p)() r = ListOP(byref(nOps), byref(opHandlers)) ret = '' ret2 = '' for i in range(0, nOps.value): handler = opHandlers[i] name = c_char_p() description = c_char_p() nArgs = c_int() argNames = POINTER(c_char_p)() argTypes = POINTER(c_char_p)() argDescs = POINTER(c_char_p)() varArgName = c_char_p() return_type = c_char_p() GetOpInfo(handler, byref(name), byref(description), \ byref(nArgs), byref(argNames), byref(argTypes), \ byref(argDescs), byref(varArgName), byref(return_type)) if name.value.decode('utf-8').startswith('_'): # get rid of functions like __init__ continue args = [] for i in range(0, nArgs.value): arg = Arg(name.value.decode('utf-8'), argNames[i].decode('utf-8'), argTypes[i].decode('utf-8'), argDescs[i].decode('utf-8')) args.append(arg) op = Op(name.value.decode('utf-8'), description.value.decode('utf-8'), args) ret = ret + op.GetOpDefinitionString(True) + "\n" ret2 = ret2 + op.GetOpDefinitionString(False) + "\n" return ret + ret2
[ "\n MXNET_DLL int MXSymbolListAtomicSymbolCreators(mx_uint *out_size,\n AtomicSymbolCreator **out_array);\n\n MXNET_DLL int MXSymbolGetAtomicSymbolInfo(AtomicSymbolCreator creator,\n const char **name,\n const char **description,\n mx_uint *num_args,\n const char ***arg_names,\n const char ***arg_type_infos,\n const char ***arg_descriptions,\n const char **key_var_num_args);\n " ]
Please provide a description of the function:def main(): parser = argparse.ArgumentParser(description='.caffemodel to MXNet .params converter.') parser.add_argument('caffemodel', help='Path to the .caffemodel file to convert.') parser.add_argument('output_file_name', help='Name of the output .params file.') args = parser.parse_args() converter = CaffeModelConverter() converter.convert(args.caffemodel, args.output_file_name)
[ "Read .caffemodel path and .params path as input from command line\n and use CaffeModelConverter to do the conversion" ]
Please provide a description of the function:def add_param(self, param_name, layer_index, blob_index): blobs = self.layers[layer_index].blobs self.dict_param[param_name] = mx.nd.array(caffe.io.blobproto_to_array(blobs[blob_index]))
[ "Add a param to the .params file" ]
Please provide a description of the function:def add_arg_param(self, param_name, layer_index, blob_index): self.add_param('arg:%s' % param_name, layer_index, blob_index)
[ "Add an arg param to .params file. Example: weights of a fully connected layer." ]
Please provide a description of the function:def add_aux_param(self, param_name, layer_index, blob_index): self.add_param('aux:%s' % param_name, layer_index, blob_index)
[ "Add an aux param to .params file. Example: moving_mean in BatchNorm layer " ]
Please provide a description of the function:def add_optional_arg_param(self, param_name, layer_index, blob_index): blobs = self.layers[layer_index].blobs if blob_index < len(blobs): self.add_arg_param(param_name, layer_index, blob_index)
[ "Add an arg param. If there is no such param in .caffemodel fie, silently ignore it." ]
Please provide a description of the function:def convert(self, caffemodel_path, outmodel_path): net_param = caffe_pb2.NetParameter() with open(caffemodel_path, 'rb') as caffe_model_file: net_param.ParseFromString(caffe_model_file.read()) layers = net_param.layer self.layers = layers for idx, layer in enumerate(layers): layer_name = str(layer.name) if layer.blobs: # If this is a layer that has only weight and bias as parameter if layer.type == 'Convolution' or layer.type == 'InnerProduct' \ or layer.type == 'Deconvolution': # Add weight and bias to the dictionary self.add_arg_param('%s_weight' % layer_name, layer_index=idx, blob_index=0) self.add_optional_arg_param('%s_bias' % layer_name, layer_index=idx, blob_index=1) elif layer.type == 'BatchNorm': gamma_param_name = '%s_gamma' % layer_name beta_param_name = '%s_beta' % layer_name next_layer = layers[idx + 1] if next_layer.type == 'Scale': # If next layer is scale layer, get gamma and beta from there self.add_arg_param(gamma_param_name, layer_index=idx+1, blob_index=0) self.add_arg_param(beta_param_name, layer_index=idx+1, blob_index=1) mean_param_name = '%s_moving_mean' % layer_name var_param_name = '%s_moving_var' % layer_name self.add_aux_param(mean_param_name, layer_index=idx, blob_index=0) self.add_aux_param(var_param_name, layer_index=idx, blob_index=1) elif layer.type == 'Scale': prev_layer = layers[idx - 1] if prev_layer.type == 'BatchNorm': continue else: # Use the naming convention used by CaffeOp self.add_arg_param('%s_0_weight' % layer_name, layer_index=idx, blob_index=0) self.add_optional_arg_param('%s_1_bias' % layer_name, layer_index=idx, blob_index=1) mx.nd.save(outmodel_path, self.dict_param)
[ "Convert a Caffe .caffemodel file to MXNet .params file" ]
Please provide a description of the function:def sample_rois(rois, gt_boxes, num_classes, rois_per_image, fg_rois_per_image, fg_overlap, box_stds): overlaps = bbox_overlaps(rois[:, 1:], gt_boxes[:, :4]) gt_assignment = overlaps.argmax(axis=1) labels = gt_boxes[gt_assignment, 4] max_overlaps = overlaps.max(axis=1) # select foreground RoI with FG_THRESH overlap fg_indexes = np.where(max_overlaps >= fg_overlap)[0] # guard against the case when an image has fewer than fg_rois_per_image foreground RoIs fg_rois_this_image = min(fg_rois_per_image, len(fg_indexes)) # sample foreground regions without replacement if len(fg_indexes) > fg_rois_this_image: fg_indexes = np.random.choice(fg_indexes, size=fg_rois_this_image, replace=False) # select background RoIs as those within [0, FG_THRESH) bg_indexes = np.where(max_overlaps < fg_overlap)[0] # compute number of background RoIs to take from this image (guarding against there being fewer than desired) bg_rois_this_image = rois_per_image - fg_rois_this_image bg_rois_this_image = min(bg_rois_this_image, len(bg_indexes)) # sample bg rois without replacement if len(bg_indexes) > bg_rois_this_image: bg_indexes = np.random.choice(bg_indexes, size=bg_rois_this_image, replace=False) # indexes selected keep_indexes = np.append(fg_indexes, bg_indexes) # pad more bg rois to ensure a fixed minibatch size while len(keep_indexes) < rois_per_image: gap = min(len(bg_indexes), rois_per_image - len(keep_indexes)) gap_indexes = np.random.choice(range(len(bg_indexes)), size=gap, replace=False) keep_indexes = np.append(keep_indexes, bg_indexes[gap_indexes]) # sample rois and labels rois = rois[keep_indexes] labels = labels[keep_indexes] # set labels of bg rois to be 0 labels[fg_rois_this_image:] = 0 # load or compute bbox_target targets = bbox_transform(rois[:, 1:], gt_boxes[gt_assignment[keep_indexes], :4], box_stds=box_stds) bbox_targets = np.zeros((rois_per_image, 4 * num_classes), dtype=np.float32) bbox_weights = np.zeros((rois_per_image, 4 * num_classes), dtype=np.float32) for i in range(fg_rois_this_image): cls_ind = int(labels[i]) bbox_targets[i, cls_ind * 4:(cls_ind + 1) * 4] = targets[i] bbox_weights[i, cls_ind * 4:(cls_ind + 1) * 4] = 1 return rois, labels, bbox_targets, bbox_weights
[ "\n generate random sample of ROIs comprising foreground and background examples\n :param rois: [n, 5] (batch_index, x1, y1, x2, y2)\n :param gt_boxes: [n, 5] (x1, y1, x2, y2, cls)\n :param num_classes: number of classes\n :param rois_per_image: total roi number\n :param fg_rois_per_image: foreground roi number\n :param fg_overlap: overlap threshold for fg rois\n :param box_stds: std var of bbox reg\n :return: (rois, labels, bbox_targets, bbox_weights)\n " ]
Please provide a description of the function:def register(reg_name): def do_register(prop_cls): fb_functype = CFUNCTYPE(c_int, c_int, POINTER(c_void_p), POINTER(c_int), POINTER(c_int), c_int, c_void_p) del_functype = CFUNCTYPE(c_int, c_void_p) infershape_functype = CFUNCTYPE(c_int, c_int, POINTER(c_int), POINTER(POINTER(mx_int)), c_void_p) infertype_functype = CFUNCTYPE(c_int, c_int, POINTER(c_int), c_void_p) inferstorage_functype = CFUNCTYPE(c_int, c_int, POINTER(c_int), c_void_p) inferstorage_backward_functype = CFUNCTYPE(c_int, c_int, POINTER(c_int), \ POINTER(c_int), c_void_p) list_functype = CFUNCTYPE(c_int, POINTER(POINTER(POINTER(c_char))), c_void_p) deps_functype = CFUNCTYPE(c_int, c_int_p, c_int_p, c_int_p, c_int_p, POINTER(c_int_p), c_void_p) createop_functype = CFUNCTYPE(c_int, c_char_p, c_int, POINTER(POINTER(mx_uint)), POINTER(c_int), POINTER(c_int), POINTER(MXCallbackList), c_void_p) req_enum = ('null', 'write', 'inplace', 'add') def creator(op_type, argc, keys, vals, ret): assert py_str(op_type) == reg_name kwargs = dict([(py_str(keys[i]), py_str(vals[i])) for i in range(argc)]) op_prop = prop_cls(**kwargs) def infer_shape_entry(num_tensor, tensor_dims, tensor_shapes, _): try: n_in = len(op_prop.list_arguments()) n_out = len(op_prop.list_outputs()) n_aux = len(op_prop.list_auxiliary_states()) assert num_tensor == n_in + n_out + n_aux shapes = [[tensor_shapes[i][j] for j in range(tensor_dims[i])] for i in range(n_in)] ret = op_prop.infer_shape(shapes) if len(ret) == 2: ishape, oshape = ret ashape = [] elif len(ret) == 3: ishape, oshape, ashape = ret else: raise AssertionError("infer_shape must return 2 or 3 lists") assert len(oshape) == n_out, \ "InferShape Error: expecting %d entries in returned output " \ "shapes, got %d."%(n_out, len(oshape)) assert len(ishape) == n_in, \ "InferShape Error: expecting %d entries in returned input " \ "shapes, got %d."%(n_in, len(ishape)) assert len(ashape) == n_aux, \ "InferShape Error: expecting %d entries in returned aux state " \ "shapes, got %d."%(n_aux, len(ashape)) rshape = list(ishape) + list(oshape) + list(ashape) for i in range(n_in+n_out+n_aux): tensor_shapes[i] = cast(c_array_buf(mx_int, array('i', rshape[i])), POINTER(mx_int)) tensor_dims[i] = len(rshape[i]) infer_shape_entry._ref_holder = [tensor_shapes] except Exception: print('Error in %s.infer_shape: %s' % (reg_name, traceback.format_exc())) return False return True def infer_storage_type_backward_entry(num_tensor, tensor_stypes, tags, _): # pylint: disable=C0301 try: tensors = [[] for i in range(5)] for i in range(num_tensor): tensors[tags[i]].append(_STORAGE_TYPE_ID_TO_STR[tensor_stypes[i]]) # Ordering of stypes: ograd, input, output, igrad, aux tensors = [tensors[3], tensors[0], tensors[1], tensors[2], tensors[4]] ret = op_prop.infer_storage_type_backward(tensors[0], tensors[1], tensors[2], tensors[3], tensors[4]) if len(ret) == 4: ret += [] elif len(ret) == 5: pass else: raise AssertionError("infer_storage_type_backward must return 4 or 5 lists") assert len(ret[0]) == len(tensors[0]), \ "InferStorageTypeBackward Error: expecting == %d " \ "entries in returned output gradient " \ "stypes, got %d."%(len(tensors[0]), len(ret[0])) assert len(ret[1]) == len(tensors[1]), \ "InferStorageTypeBackward Error: expecting == %d " \ "entries in returned input stypes, " \ "got %d."%(len(tensors[1]), len(ret[1])) assert len(ret[2]) == len(tensors[2]), \ "InferStorageTypeBackward Error: expecting == %d " \ "entries in returned output stypes, " \ "got %d."%(len(tensors[2]), len(ret[2])) assert len(ret[3]) == len(tensors[3]), \ "InferStorageTypeBackward Error: expecting == %d " \ "entries in returned input gradient stypes, " \ "got %d."%(len(tensors[3]), len(ret[3])) assert len(ret[4]) == len(tensors[4]), \ "InferStorageTypeBackward Error: expecting == %d " \ "entries in returned aux stypes, " \ "got %d."%(len(tensors[4]), len(ret[4])) rstype = [] for i, ret_list in enumerate(ret): rstype.extend(ret_list) for i, stype in enumerate(rstype): assert stype != _STORAGE_TYPE_ID_TO_STR[_STORAGE_TYPE_UNDEFINED], \ "stype should not be undefined" assert stype in _STORAGE_TYPE_STR_TO_ID, \ "Provided stype: %s is not valid " \ "valid stypes are %s, %s, %s"%(stype, _STORAGE_TYPE_ID_TO_STR[_STORAGE_TYPE_DEFAULT], _STORAGE_TYPE_ID_TO_STR[_STORAGE_TYPE_ROW_SPARSE], _STORAGE_TYPE_ID_TO_STR[_STORAGE_TYPE_CSR]) tensor_stypes[i] = _STORAGE_TYPE_STR_TO_ID[stype] infer_storage_type_backward_entry._ref_holder = [tensor_stypes] except Exception: print('Error in %s.infer_type: %s' % (reg_name, traceback.format_exc())) return False return True def infer_storage_type_entry(num_tensor, tensor_stypes, _): try: n_in = len(op_prop.list_arguments()) n_out = len(op_prop.list_outputs()) n_aux = len(op_prop.list_auxiliary_states()) assert num_tensor == n_in + n_out + n_aux stypes = [_STORAGE_TYPE_ID_TO_STR[tensor_stypes[i]] for i in range(n_in)] ret = op_prop.infer_storage_type(stypes) if len(ret) == 2: istype, ostype = ret astype = [] elif len(ret) == 3: istype, ostype, astype = ret else: raise AssertionError("infer_storage_type must return 2 or 3 lists") assert len(ostype) == n_out, \ "InferStorageType Error: expecting %d entries in returned output " \ "stypes, got %d."%(n_out, len(ostype)) assert len(istype) == n_in, \ "InferStorageType Error: expecting %d entries in returned input " \ "stypes, got %d."%(n_in, len(istype)) assert len(astype) == n_aux, \ "InferStorageType Error: expecting %d entries in returned aux state " \ "stypes, got %d."%(n_aux, len(astype)) rtype = list(istype) + list(ostype) + list(astype) for i, dtype in enumerate(rtype): tensor_stypes[i] = _STORAGE_TYPE_STR_TO_ID[dtype] infer_storage_type_entry._ref_holder = [tensor_stypes] except Exception: print('Error in %s.infer_type: %s' % (reg_name, traceback.format_exc())) return False return True def infer_type_entry(num_tensor, tensor_types, _): try: n_in = len(op_prop.list_arguments()) n_out = len(op_prop.list_outputs()) n_aux = len(op_prop.list_auxiliary_states()) assert num_tensor == n_in + n_out + n_aux types = [_DTYPE_MX_TO_NP[tensor_types[i]] for i in range(n_in)] ret = op_prop.infer_type(types) if len(ret) == 2: itype, otype = ret atype = [] elif len(ret) == 3: itype, otype, atype = ret else: raise AssertionError("infer_type must return 2 or 3 lists") assert len(otype) == n_out, \ "InferType Error: expecting %d entries in returned output " \ "types, got %d."%(n_out, len(otype)) assert len(itype) == n_in, \ "InferType Error: expecting %d entries in returned input " \ "types, got %d."%(n_in, len(itype)) assert len(atype) == n_aux, \ "InferType Error: expecting %d entries in returned aux state " \ "types, got %d."%(n_aux, len(atype)) rtype = list(itype) + list(otype) + list(atype) for i, dtype in enumerate(rtype): tensor_types[i] = _DTYPE_NP_TO_MX[dtype] infer_type_entry._ref_holder = [tensor_types] except Exception: print('Error in %s.infer_type: %s' % (reg_name, traceback.format_exc())) return False return True def list_outputs_entry(out, _): try: ret = op_prop.list_outputs() ret = [c_str(i) for i in ret] + [c_char_p(0)] ret = c_array(c_char_p, ret) out[0] = cast(ret, POINTER(POINTER(c_char))) list_outputs_entry._ref_holder = [out] except Exception: print('Error in %s.list_outputs: %s' % (reg_name, traceback.format_exc())) return False return True def list_arguments_entry(out, _): try: ret = op_prop.list_arguments() ret = [c_str(i) for i in ret] + [c_char_p(0)] ret = c_array(c_char_p, ret) out[0] = cast(ret, POINTER(POINTER(c_char))) list_arguments_entry._ref_holder = [out] except Exception: print('Error in %s.list_arguments: %s' % (reg_name, traceback.format_exc())) return False return True def list_auxiliary_states_entry(out, _): try: ret = op_prop.list_auxiliary_states() ret = [c_str(i) for i in ret] + [c_char_p(0)] ret = c_array(c_char_p, ret) out[0] = cast(ret, POINTER(POINTER(c_char))) list_auxiliary_states_entry._ref_holder = [out] except Exception: tb = traceback.format_exc() print('Error in %s.list_auxiliary_states: %s' % (reg_name, tb)) return False return True def declare_backward_dependency_entry(out_grad, in_data, out_data, num_dep, deps, _): try: out_grad = [out_grad[i] for i in range(len(op_prop.list_outputs()))] in_data = [in_data[i] for i in range(len(op_prop.list_arguments()))] out_data = [out_data[i] for i in range(len(op_prop.list_outputs()))] rdeps = op_prop.declare_backward_dependency(out_grad, in_data, out_data) num_dep[0] = len(rdeps) _registry.result_deps = set() for dep in rdeps: _registry.result_deps.add(dep) rdeps = cast(c_array_buf(c_int, array('i', rdeps)), c_int_p) deps[0] = rdeps declare_backward_dependency_entry._ref_holder = [deps] except Exception: tb = traceback.format_exc() print('Error in %s.declare_backward_dependency: %s' % (reg_name, tb)) return False return True def create_operator_entry(ctx, num_inputs, shapes, ndims, dtypes, ret, _): try: ctx = py_str(ctx) sep = ctx.find('(') ctx = context.Context(ctx[:sep], int(ctx[sep+1:-1])) ndims = [ndims[i] for i in range(num_inputs)] shapes = [[shapes[i][j] for j in range(ndims[i])] for i in range(num_inputs)] dtypes = [dtypes[i] for i in range(num_inputs)] op = op_prop.create_operator(ctx, shapes, dtypes) def forward_entry(num_ndarray, ndarraies, tags, reqs, is_train, _): try: tensors = [[] for i in range(5)] for i in range(num_ndarray): if tags[i] == 1 or tags[i] == 4: tensors[tags[i]].append(_ndarray_cls(cast(ndarraies[i], NDArrayHandle), writable=True)) else: tensors[tags[i]].append(_ndarray_cls(cast(ndarraies[i], NDArrayHandle), writable=False)) reqs = [req_enum[reqs[i]] for i in range(len(tensors[1]))] with ctx: op.forward(is_train=is_train, req=reqs, in_data=tensors[0], out_data=tensors[1], aux=tensors[4]) except Exception: print('Error in CustomOp.forward: %s' % traceback.format_exc()) return False return True def backward_entry(num_ndarray, ndarraies, tags, reqs, is_train, _): # pylint: disable=W0613 try: tensors = [[] for i in range(5)] num_outputs = len(op_prop.list_outputs()) num_args = len(op_prop.list_arguments()) for i in range(num_ndarray): if i in _registry.result_deps or i >= (num_outputs * 2 + num_args): # If it is a backward dependency or output or aux: # Set stype as undefined so that it returns # ndarray based on existing stype stype = _STORAGE_TYPE_UNDEFINED else: # If it is some input, output or out grad ndarray not part of # backward dependency it is empty and thus the ndarray should # be set to default stype = _STORAGE_TYPE_DEFAULT if tags[i] == 2 or tags[i] == 4: tensors[tags[i]].append(_ndarray_cls(cast(ndarraies[i], NDArrayHandle), writable=True, stype=stype)) else: tensors[tags[i]].append(_ndarray_cls(cast(ndarraies[i], NDArrayHandle), writable=False, stype=stype)) reqs = [req_enum[reqs[i]] for i in range(len(tensors[2]))] with ctx: op.backward(req=reqs, in_data=tensors[0], out_data=tensors[1], in_grad=tensors[2], out_grad=tensors[3], aux=tensors[4]) except Exception: print('Error in CustomOp.backward: %s' % traceback.format_exc()) return False return True cur = _registry.inc() def delete_entry(_): try: del _registry.ref_holder[cur] except Exception: print('Error in CustomOp.delete: %s' % traceback.format_exc()) return False return True callbacks = [del_functype(delete_entry), fb_functype(forward_entry), fb_functype(backward_entry)] callbacks = [cast(i, CFUNCTYPE(c_int)) for i in callbacks] contexts = [None, None, None] ret[0] = MXCallbackList(c_int(len(callbacks)), cast(c_array(CFUNCTYPE(c_int), callbacks), POINTER(CFUNCTYPE(c_int))), cast(c_array(c_void_p, contexts), POINTER(c_void_p))) op._ref_holder = [ret] _registry.ref_holder[cur] = op except Exception: print('Error in %s.create_operator: %s' % (reg_name, traceback.format_exc())) return False return True cur = _registry.inc() def delete_entry(_): try: del _registry.ref_holder[cur] except Exception: print('Error in CustomOpProp.delete: %s' % traceback.format_exc()) return False return True callbacks = [del_functype(delete_entry), list_functype(list_arguments_entry), list_functype(list_outputs_entry), list_functype(list_auxiliary_states_entry), infershape_functype(infer_shape_entry), deps_functype(declare_backward_dependency_entry), createop_functype(create_operator_entry), infertype_functype(infer_type_entry), inferstorage_functype(infer_storage_type_entry), inferstorage_backward_functype(infer_storage_type_backward_entry)] callbacks = [cast(i, CFUNCTYPE(c_int)) for i in callbacks] contexts = [None]*len(callbacks) ret[0] = MXCallbackList(c_int(len(callbacks)), cast(c_array(CFUNCTYPE(c_int), callbacks), POINTER(CFUNCTYPE(c_int))), cast(c_array(c_void_p, contexts), POINTER(c_void_p))) op_prop._ref_holder = [ret] _registry.ref_holder[cur] = op_prop return True creator_functype = CFUNCTYPE(c_int, c_char_p, c_int, POINTER(c_char_p), POINTER(c_char_p), POINTER(MXCallbackList)) creator_func = creator_functype(creator) check_call(_LIB.MXCustomOpRegister(c_str(reg_name), creator_func)) cur = _registry.inc() _registry.ref_holder[cur] = creator_func return prop_cls return do_register
[ "Register a subclass of CustomOpProp to the registry with name reg_name.", "Register a subclass of CustomOpProp to the registry.", "internal function", "C Callback for ``CustomOpProp::InferShape``.", "C Callback for CustomOpProp::InferStorageTypeBackward", "C Callback for CustomOpProp::InferStorageType", "C Callback for CustomOpProp::InferType", "C Callback for CustomOpProp::ListOutputs", "C Callback for CustomOpProp::ListArguments", "C Callback for CustomOpProp::ListAuxiliaryStates", "C Callback for CustomOpProp::DeclareBacwardDependency", "C Callback for CustomOpProp::CreateOperator", "C Callback for CustomOp::Forward", "C Callback for CustomOp::Backward", "C Callback for CustomOp::del", "C Callback for CustomOpProp::del" ]
Please provide a description of the function:def declare_backward_dependency(self, out_grad, in_data, out_data): deps = [] if self.need_top_grad(): deps.extend(out_grad) deps.extend(in_data) deps.extend(out_data) return deps
[ "Declare dependencies of this operator for backward pass.\n\n Parameters\n ----------\n out_grad : list of int\n ids of out_grad blobs.\n in_data : list of int\n ids of in_data blobs.\n out_data: list of int\n ids of out_data blobs.\n\n Returns\n -------\n deps : list of int\n ids of the needed blobs.\n " ]
Please provide a description of the function:def assign(self, dst, req, src): if req == 'null': return elif req in ('write', 'inplace'): dst[:] = src elif req == 'add': dst[:] += src
[ "Helper function for assigning into dst depending on requirements." ]
Please provide a description of the function:def infer_type(self, in_type): return in_type, [in_type[0]]*len(self.list_outputs()), \ [in_type[0]]*len(self.list_auxiliary_states())
[ "infer_type interface. override to create new operators\n\n Parameters\n ----------\n in_type : list of np.dtype\n list of argument types in the same order as\n declared in list_arguments.\n\n Returns\n -------\n in_type : list\n list of argument types. Can be modified from in_type.\n out_type : list\n list of output types calculated from in_type,\n in the same order as declared in list_outputs.\n aux_type : Optional, list\n list of aux types calculated from in_type,\n in the same order as declared in list_auxiliary_states.\n " ]