当前位置: 首页>>代码示例>>Python>>正文


Python mxnet.Context方法代码示例

本文整理汇总了Python中mxnet.Context方法的典型用法代码示例。如果您正苦于以下问题:Python mxnet.Context方法的具体用法?Python mxnet.Context怎么用?Python mxnet.Context使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在mxnet的用法示例。


在下文中一共展示了mxnet.Context方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: benchmarking

# 需要导入模块: import mxnet [as 别名]
# 或者: from mxnet import Context [as 别名]
def benchmarking(net, opt, ctx):
    if isinstance(ctx, mx.Context):
        ctx = [ctx]

    bs = opt.batch_size
    num_iterations = opt.num_iterations
    input_shape = (bs, 3,) + tuple(input_size)
    size = num_iterations * bs
    data = mx.random.uniform(-1.0, 1.0, shape=input_shape, ctx=ctx[0], dtype='float32')
    dry_run = 5

    from tqdm import tqdm
    with tqdm(total=size + dry_run * bs) as pbar:
        for n in range(dry_run + num_iterations):
            if n == dry_run:
                tic = time.time()
            output = net(data)
            output.wait_to_read()
            pbar.update(bs)
    speed = size / (time.time() - tic)
    print('With batch size %d , %d batches, throughput is %f imgs/sec' % (bs, num_iterations, speed)) 
开发者ID:dmlc,项目名称:gluon-cv,代码行数:23,代码来源:validate.py

示例2: _append_arrs

# 需要导入模块: import mxnet [as 别名]
# 或者: from mxnet import Context [as 别名]
def _append_arrs(arrs, use_shared_mem=False, expand=False, batch_axis=0):
    """Internal impl for returning appened arrays as list."""
    if isinstance(arrs[0], mx.nd.NDArray):
        if use_shared_mem:
            out = [x.as_in_context(mx.Context('cpu_shared', 0)) for x in arrs]
        else:
            out = arrs
    else:
        if use_shared_mem:
            out = [mx.nd.array(x, ctx=mx.Context('cpu_shared', 0)) for x in arrs]
        else:
            out = [mx.nd.array(x) for x in arrs]

    # add batch axis
    if expand:
        out = [x.expand_dims(axis=batch_axis) for x in out]
    return out 
开发者ID:dmlc,项目名称:gluon-cv,代码行数:19,代码来源:batchify.py

示例3: _stack_arrs

# 需要导入模块: import mxnet [as 别名]
# 或者: from mxnet import Context [as 别名]
def _stack_arrs(arrs, use_shared_mem=False):
        """
        Internal imple for stacking arrays.
        """
        if isinstance(arrs[0], mx.nd.NDArray):
            if use_shared_mem:
                out = mx.nd.empty((len(arrs),) + arrs[0].shape, dtype=arrs[0].dtype,
                                  ctx=mx.Context("cpu_shared", 0))
                return mx.nd.stack(*arrs, out=out)
            else:
                return mx.nd.stack(*arrs)
        else:
            out = np.asarray(arrs)
            if use_shared_mem:
                return mx.nd.array(out, ctx=mx.Context("cpu_shared", 0))
            else:
                return mx.nd.array(out) 
开发者ID:osmr,项目名称:imgclsmob,代码行数:19,代码来源:coco_det_dataset.py

示例4: seed_rngs

# 需要导入模块: import mxnet [as 别名]
# 或者: from mxnet import Context [as 别名]
def seed_rngs(seed: int, ctx: Optional[Union[mx.Context, List[mx.Context]]] = None) -> None:
    """
    Seed the random number generators (Python, Numpy and MXNet).

    :param seed: The random seed.
    :param ctx: Random number generators in MXNet are device specific.
           If None, MXNet will set the state of each generator of each device using seed and device id. This will lead
           to different results on different devices. If ctx is provided, this function will seed
           device-specific generators with a fixed offset. E.g. for 2 devices and seed=13, seed for gpu(0) will be 13,
           14 for gpu(1). See https://beta.mxnet.io/api/gluon-related/_autogen/mxnet.random.seed.html.
    """
    logger.info("Random seed: %d", seed)
    np.random.seed(seed)
    random.seed(seed)
    if ctx is None:
        mx.random.seed(seed, ctx='all')
    else:
        if isinstance(ctx, mx.Context):
            ctx = [ctx]
        for i, c in enumerate(ctx):
            mx.random.seed(seed + i, ctx=c) 
开发者ID:awslabs,项目名称:sockeye,代码行数:23,代码来源:utils.py

示例5: get_gpu_memory_usage

# 需要导入模块: import mxnet [as 别名]
# 或者: from mxnet import Context [as 别名]
def get_gpu_memory_usage(ctx: Union[mx.context.Context, List[mx.context.Context]]) -> Dict[int, Tuple[int, int]]:
    """
    Returns used and total memory for GPUs identified by the given context list.

    :param ctx: List of MXNet context devices.
    :return: Dictionary of device id mapping to a tuple of (memory used, memory total).
    """
    if isinstance(ctx, mx.context.Context):
        ctx = [ctx]
    ctx = [c for c in ctx if c.device_type == 'gpu']
    if not ctx:
        return {}

    memory_data = {}  # type: Dict[int, Tuple[int, int]]
    for c in ctx:
        try:
            free, total = mx.context.gpu_memory_info(device_id=c.device_id)  # in bytes
            used = total - free
            memory_data[c.device_id] = (used * 1e-06, total * 1e-06)
        except mx.MXNetError:
            logger.exception("Failed retrieving memory data for gpu%d", c.device_id)
            continue
    log_gpu_memory_usage(memory_data)
    return memory_data 
开发者ID:awslabs,项目名称:sockeye,代码行数:26,代码来源:utils.py

示例6: train

# 需要导入模块: import mxnet [as 别名]
# 或者: from mxnet import Context [as 别名]
def train(train_iter, test_iter, net, loss, trainer, ctx, num_epochs):
    """Train and evaluate a model."""
    print('training on', ctx)
    if isinstance(ctx, mx.Context):
        ctx = [ctx]
    for epoch in range(num_epochs):
        train_l_sum, train_acc_sum, n, m, start = 0.0, 0.0, 0, 0, time.time()
        for i, batch in enumerate(train_iter):
            Xs, ys, batch_size = _get_batch(batch, ctx)   
            with autograd.record():
                y_hats = [net(X) for X in Xs]
                ls = [loss(y_hat, y) for y_hat, y in zip(y_hats, ys)]
            for l in ls:
                l.backward()
            trainer.step(batch_size)
            train_l_sum += sum([l.sum().asscalar() for l in ls])
            n += sum([l.size for l in ls])
            train_acc_sum += sum([(y_hat.argmax(axis=1) == y).sum().asscalar()
                                 for y_hat, y in zip(y_hats, ys)])
            m += sum([y.size for y in ys])
        test_acc = evaluate_accuracy(test_iter, net, ctx)
        print('epoch %d, loss %.4f, train acc %.3f, test acc %.3f, '
              'time %.1f sec'
              % (epoch + 1, train_l_sum / n, train_acc_sum / m, test_acc,
                 time.time() - start)) 
开发者ID:d2l-ai,项目名称:d2l-zh,代码行数:27,代码来源:utils.py

示例7: batchify

# 需要导入模块: import mxnet [as 别名]
# 或者: from mxnet import Context [as 别名]
def batchify(
    data: List[dict],
    dtype: DType,
    multi_processing: bool,
    single_process_ctx: Optional[mx.Context] = None,
    variable_length: bool = False,
) -> DataBatch:
    """reduce the list of dictionaries to a single dictionary, where values
        referenced by identical key are reduced using the stack function"""
    return {
        key: stack(
            data=[item[key] for item in data],
            multi_processing=multi_processing,
            dtype=dtype,
            single_process_ctx=single_process_ctx,
            variable_length=variable_length,
        )
        for key in data[0].keys()
    } 
开发者ID:awslabs,项目名称:gluon-ts,代码行数:21,代码来源:parallelized_loader.py

示例8: _as_in_context

# 需要导入模块: import mxnet [as 别名]
# 或者: from mxnet import Context [as 别名]
def _as_in_context(batch: dict, ctx: mx.Context) -> DataBatch:
    """Move data into new context, should only be in main process."""
    assert (
        not MPWorkerInfo.worker_process
    ), "This function is not meant to be used in workers."
    batch = {
        k: v.as_in_context(ctx) if isinstance(v, nd.NDArray)
        # Workaround due to MXNet not being able to handle NDArrays with 0 in shape properly:
        else (
            stack(v, False, v.dtype, ctx)
            if isinstance(v[0], np.ndarray) and 0 in v[0].shape
            else v
        )
        for k, v in batch.items()
    }
    return batch 
开发者ID:awslabs,项目名称:gluon-ts,代码行数:18,代码来源:parallelized_loader.py

示例9: __init__

# 需要导入模块: import mxnet [as 别名]
# 或者: from mxnet import Context [as 别名]
def __init__(
        self,
        dataset: Dataset,
        *,
        transform: Transformation,
        batch_size: int,
        ctx: mx.Context,
        num_workers: Optional[int] = None,
        num_prefetch: Optional[int] = None,
        dtype: DType = np.float32,
        **kwargs,
    ) -> None:
        super().__init__(
            dataset=dataset,
            transform=transform,
            is_train=True,
            batch_size=batch_size,
            ctx=ctx,
            dtype=dtype,
            cyclic=False,
            num_workers=num_workers,
            num_prefetch=num_prefetch,
            shuffle_buffer_length=None,
            **kwargs,
        ) 
开发者ID:awslabs,项目名称:gluon-ts,代码行数:27,代码来源:loader.py

示例10: initialize_from_array

# 需要导入模块: import mxnet [as 别名]
# 或者: from mxnet import Context [as 别名]
def initialize_from_array(
        self, input_array: np.ndarray, ctx: mx.Context = get_mxnet_context()
    ):
        r"""
        Initialize the representation based on a numpy array.

        Parameters
        ----------
        input_array
            Numpy array.
        ctx
            MXNet context.
        """
        pass

    # noinspection PyMethodOverriding 
开发者ID:awslabs,项目名称:gluon-ts,代码行数:18,代码来源:representation.py

示例11: __init__

# 需要导入模块: import mxnet [as 别名]
# 或者: from mxnet import Context [as 别名]
def __init__(self, num_classes, data_shape, max_iter, dtype):
        self.batch_size = data_shape[0]
        self.cur_iter = 0
        self.max_iter = max_iter
        self.dtype = dtype
        label = np.random.randint(0, num_classes, [self.batch_size,])
        data = np.random.uniform(-1, 1, data_shape)
        self.data = mx.nd.array(data, dtype=self.dtype, ctx=mx.Context('cpu_pinned', 0))
        self.label = mx.nd.array(label, dtype=self.dtype, ctx=mx.Context('cpu_pinned', 0)) 
开发者ID:awslabs,项目名称:dynamic-training-with-apache-mxnet-on-aws,代码行数:11,代码来源:data.py

示例12: test_ndarray_copy

# 需要导入模块: import mxnet [as 别名]
# 或者: from mxnet import Context [as 别名]
def test_ndarray_copy():
    c = mx.nd.array(np.random.uniform(-10, 10, (10, 10)))
    d = c.copyto(mx.Context('cpu', 0))
    assert np.sum(np.abs(c.asnumpy() != d.asnumpy())) == 0.0 
开发者ID:awslabs,项目名称:dynamic-training-with-apache-mxnet-on-aws,代码行数:6,代码来源:profiler_ndarray.py

示例13: train

# 需要导入模块: import mxnet [as 别名]
# 或者: from mxnet import Context [as 别名]
def train(epoch, ctx):
    if isinstance(ctx, mx.Context):
        ctx = [ctx]
    net.initialize(mx.init.Orthogonal(), ctx=ctx)
    # re-initialize conv4's weight to be Orthogonal
    net.conv4.initialize(mx.init.Orthogonal(scale=1), force_reinit=True, ctx=ctx)
    trainer = gluon.Trainer(net.collect_params(), 'adam', {'learning_rate': opt.lr})
    loss = gluon.loss.L2Loss()

    for i in range(epoch):
        train_data.reset()
        for batch in train_data:
            data = gluon.utils.split_and_load(batch.data[0], ctx_list=ctx, batch_axis=0)
            label = gluon.utils.split_and_load(batch.label[0], ctx_list=ctx, batch_axis=0)
            outputs = []
            with ag.record():
                for x, y in zip(data, label):
                    z = net(x)
                    L = loss(z, y)
                    L.backward()
                    outputs.append(z)
            trainer.step(batch.data[0].shape[0])
            metric.update(label, outputs)

        name, acc = metric.get()
        metric.reset()
        print('training mse at epoch %d: %s=%f'%(i, name, acc))
        test(ctx)

    net.save_parameters('superres.params') 
开发者ID:awslabs,项目名称:dynamic-training-with-apache-mxnet-on-aws,代码行数:32,代码来源:super_resolution.py

示例14: test_ndarray_cpu_shared_ctx

# 需要导入模块: import mxnet [as 别名]
# 或者: from mxnet import Context [as 别名]
def test_ndarray_cpu_shared_ctx():
    ctx = mx.Context('cpu_shared', 0)
    res = mx.nd.zeros((1, 2, 3), ctx=ctx)
    assert(res.context == ctx) 
开发者ID:awslabs,项目名称:dynamic-training-with-apache-mxnet-on-aws,代码行数:6,代码来源:test_ndarray.py

示例15: test_aggregator

# 需要导入模块: import mxnet [as 别名]
# 或者: from mxnet import Context [as 别名]
def test_aggregator():
    """aggregate value on muliple devices"""

    def check_aggregator(kv, key, key_list, stype):
        # devices
        num_devs = 4
        devs = [mx.Context('cpu', i) for i in range(num_devs)]

        # single
        vals = [mx.nd.ones(shape, d).tostype(stype) for d in devs]
        outs = [mx.nd.empty(shape, d) for d in devs]

        kv.push(key, vals)
        kv.pull(key, out=outs)

        for out in outs:
            check_diff_to_scalar(out, num_devs)

        # list
        vals = [[mx.nd.ones(shape, d).tostype(stype)*2.0 for d in devs]] * len(key_list)
        outs = [[mx.nd.empty(shape, d) for d in devs]] * len(key_list)
        kv.push(key_list, vals)
        kv.pull(key_list, out=outs)

        for out in outs:
            for o in out:
                check_diff_to_scalar(o, num_devs * 2.0)

    stypes = ['default', 'row_sparse']
    for stype in stypes:
        check_aggregator(init_kv(), 3, keys, stype)
        check_aggregator(init_kv_with_str(), 'a', str_keys, stype) 
开发者ID:awslabs,项目名称:dynamic-training-with-apache-mxnet-on-aws,代码行数:34,代码来源:test_kvstore.py


注:本文中的mxnet.Context方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。