本文整理汇总了Python中mxnet.ndarray方法的典型用法代码示例。如果您正苦于以下问题:Python mxnet.ndarray方法的具体用法?Python mxnet.ndarray怎么用?Python mxnet.ndarray使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类mxnet
的用法示例。
在下文中一共展示了mxnet.ndarray方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: hybrid_forward
# 需要导入模块: import mxnet [as 别名]
# 或者: from mxnet import ndarray [as 别名]
def hybrid_forward(self, F, x):
feat1 = self.conv5a(x)
sa_feat = self.sa(feat1)
sa_conv = self.conv51(sa_feat)
sa_output = self.conv6(sa_conv)
feat2 = self.conv5c(x)
sc_feat = self.sc(feat2)
sc_conv = self.conv52(sc_feat)
sc_output = self.conv7(sc_conv)
feat_sum = sa_conv + sc_conv
sasc_output = self.conv8(feat_sum)
output = [sasc_output]
output.append(sa_output)
output.append(sc_output)
return tuple(output)
示例2: predict_batch
# 需要导入模块: import mxnet [as 别名]
# 或者: from mxnet import ndarray [as 别名]
def predict_batch(model, ctx, x, n_pred):
'''
Parameters
----------
x: mx.ndarray, shape is (batch_size, 1, n_his, num_of_vertices)
Returns
----------
mx.ndarray, shape is (batch_size, 1, n_pred, num_of_vertices)
'''
predicts = []
for pred_idx in range(n_pred):
x_input = nd.concat(x, *predicts, dim=2)[:, :, - n_pred:, :]
predicts.append(model(x_input.as_in_context(ctx))
.as_in_context(mx.cpu()))
return nd.concat(*predicts, dim=2)
示例3: _prepare_anchors
# 需要导入模块: import mxnet [as 别名]
# 或者: from mxnet import ndarray [as 别名]
def _prepare_anchors(F, feat_list, anchor_list, num_image, num_anchor):
""" crop pre-comuputed anchors into the shape of the features
inputs:
F: symbol or ndarray
feat_list: list of symbols or ndarrays, [(#img, #c, #h1, #w1), ...]
anchor_list: list of symbols or ndarrays, [(1, 1, #h1', #w1', #anchor * 4), ...]
num_image: int
num_anchor: int
outputs:
anchors: symbol or ndarray, (#img, H * W * #anchor, 4)
"""
lvl_anchors = []
for features, anchors in zip(feat_list, anchor_list):
anchors = F.slice_like(anchors, features, axes=(2, 3)) # (1, 1, h, w, #anchor * 4)
anchors = F.reshape(anchors, shape=(1, -1, num_anchor * 4)) # (1, h * w, #anchor * 4)
lvl_anchors.append(anchors)
anchors = F.concat(*lvl_anchors, dim=1) # (1, H * W, #anchor * 4)
anchors = F.reshape(anchors, shape=(0, -1, 4)) # (1, H * W * #anchor, 4)
anchors = F.broadcast_axis(anchors, axis=0, size=num_image) # (#img, H * W * #anchor, 4)
return anchors
示例4: get_serializable
# 需要导入模块: import mxnet [as 别名]
# 或者: from mxnet import ndarray [as 别名]
def get_serializable(self):
"""
Returns three dicts:
1. MXNet parameters {uuid: mxnet parameters, mx.nd.array}.
2. MXNet constants {uuid: mxnet parameter (only constant types), mx.nd.array}
3. Other constants {uuid: primitive numeric types (int, float)}
:returns: Three dictionaries: MXNet parameters, MXNet constants, and other constants (in that order)
:rtypes: {uuid: mx.nd.array}, {uuid: mx.nd.array}, {uuid: primitive (int/float)}
"""
mxnet_parameters = {key: value._reduce() for key, value in self._params.items()}
mxnet_constants = {uuid: value
for uuid, value in self._constants.items()
if isinstance(value, mx.ndarray.ndarray.NDArray)}
variable_constants = {uuid: value
for uuid, value in self._constants.items()
if uuid not in mxnet_constants}
return mxnet_parameters, mxnet_constants, variable_constants
示例5: _sample_univariate
# 需要导入模块: import mxnet [as 别名]
# 或者: from mxnet import ndarray [as 别名]
def _sample_univariate(func, shape=None, dtype=None, out=None, ctx=None, F=None, **kwargs):
"""
Wrapper for univariate sampling functions (Normal, Gamma etc.)
:param func: The function to use for sampling, e.g. F.random.normal
:param shape: The shape of the samples
:param dtype: The data type
:param out: Output variable
:param ctx: The execution context
:param F: MXNet node
:param kwargs: keyword arguments for the sampling function (loc, scale etc)
:return: Array of samples
"""
dtype = get_default_dtype() if dtype is None else dtype
if F is mx.ndarray:
# This is required because MXNet uses _Null instead of None as shape default
if shape is None:
return func(dtype=dtype, ctx=ctx, out=out, **kwargs)
else:
return func(shape=shape, dtype=dtype, ctx=ctx, out=out, **kwargs)
else:
return func(shape=shape, dtype=dtype, out=out, **kwargs)
示例6: histogram_summary
# 需要导入模块: import mxnet [as 别名]
# 或者: from mxnet import ndarray [as 别名]
def histogram_summary(tag, values, bins):
"""Outputs a `Summary` protocol buffer with a histogram.
Adding a histogram summary makes it possible to visualize the data's distribution in
TensorBoard. See detailed explanation of the TensorBoard histogram dashboard at
https://www.tensorflow.org/get_started/tensorboard_histograms
This op reports an `InvalidArgument` error if any value is not finite.
Adapted from the TensorFlow function `histogram()` at
https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/summary/summary.py
Parameters
----------
tag : str
A name for the summary of the histogram. Will also serve as a series name in
TensorBoard.
values : MXNet `NDArray` or `numpy.ndarray`
Values for building the histogram.
Returns
-------
A `Summary` protobuf of the histogram.
"""
tag = _clean_tag(tag)
values = _make_numpy_array(values)
hist = _make_histogram(values.astype(float), bins)
return Summary(value=[Summary.Value(tag=tag, histo=hist)])
示例7: image_summary
# 需要导入模块: import mxnet [as 别名]
# 或者: from mxnet import ndarray [as 别名]
def image_summary(tag, image):
"""Outputs a `Summary` protocol buffer with image(s).
Parameters
----------
tag : str
A name for the generated summary. Will also serve as a series name in TensorBoard.
image : MXNet `NDArray` or `numpy.ndarray`
Image data that is one of the following layout: (H, W), (C, H, W), (N, C, H, W).
The pixel values of the image are assumed to be normalized in the range [0, 1].
The image will be rescaled to the range [0, 255] and cast to `np.uint8` before creating
the image protobuf.
Returns
-------
A `Summary` protobuf of the image.
"""
tag = _clean_tag(tag)
image = _prepare_image(image)
image = _make_image(image)
return Summary(value=[Summary.Value(tag=tag, image=image)])
示例8: _make_metadata_tsv
# 需要导入模块: import mxnet [as 别名]
# 或者: from mxnet import ndarray [as 别名]
def _make_metadata_tsv(metadata, save_path):
"""Given an `NDArray` or a `numpy.ndarray` or a list as metadata e.g. labels, save the
flattened array into the file metadata.tsv under the path provided by the user. The
labels can be 1D or 2D with multiple labels per data point.
Made to satisfy the requirement in the following link:
https://www.tensorflow.org/programmers_guide/embedding#metadata"""
if isinstance(metadata, NDArray):
metadata = metadata.asnumpy()
elif isinstance(metadata, list):
metadata = np.array(metadata)
elif not isinstance(metadata, np.ndarray):
raise TypeError('expected NDArray or np.ndarray or 1D/2D list, while received '
'type {}'.format(str(type(metadata))))
if len(metadata.shape) > 2:
raise TypeError('expected a 1D/2D NDArray, np.ndarray or list, while received '
'shape {}'.format(str(metadata.shape)))
if len(metadata.shape) == 1:
metadata = metadata.reshape(-1, 1)
with open(os.path.join(save_path, 'metadata.tsv'), 'w') as f:
for row in metadata:
f.write('\t'.join([str(x) for x in row]) + '\n')
示例9: _make_sprite_image
# 需要导入模块: import mxnet [as 别名]
# 或者: from mxnet import ndarray [as 别名]
def _make_sprite_image(images, save_path):
"""Given an NDArray as a batch images, make a sprite image out of it following the rule
defined in
https://www.tensorflow.org/programmers_guide/embedding
and save it in sprite.png under the path provided by the user."""
if isinstance(images, np.ndarray):
images = nd.array(images, dtype=images.dtype, ctx=current_context())
elif not isinstance(images, (NDArray, np.ndarray)):
raise TypeError('images must be an MXNet NDArray or numpy.ndarray,'
' while received type {}'.format(str(type(images))))
assert isinstance(images, NDArray)
shape = images.shape
nrow = int(np.ceil(np.sqrt(shape[0])))
_save_image(
images, os.path.join(save_path, 'sprite.png'), nrow=nrow, padding=0, square_image=True)
示例10: event_shape
# 需要导入模块: import mxnet [as 别名]
# 或者: from mxnet import ndarray [as 别名]
def event_shape(self) -> Tuple:
r"""
Shape of each individual event contemplated by the distribution.
For example, distributions over scalars have `event_shape = ()`,
over vectors have `event_shape = (d, )` where `d` is the length
of the vectors, over matrices have `event_shape = (d1, d2)`, and
so on.
Invoking `sample()` from a distribution yields a tensor of shape
`batch_shape + event_shape`.
This property is available in general only in mx.ndarray mode,
when the shape of the distribution arguments can be accessed.
"""
raise NotImplementedError()
示例11: test_broadcast
# 需要导入模块: import mxnet [as 别名]
# 或者: from mxnet import ndarray [as 别名]
def test_broadcast():
sample_num = 1000
def test_broadcast_to():
for i in range(sample_num):
ndim = np.random.randint(1, 6)
target_shape = np.random.randint(1, 11, size=ndim)
shape = target_shape.copy()
axis_flags = np.random.randint(0, 2, size=ndim)
axes = []
for (axis, flag) in enumerate(axis_flags):
if flag:
shape[axis] = 1
dat = np.random.rand(*shape) - 0.5
numpy_ret = dat
ndarray_ret = mx.nd.array(dat).broadcast_to(shape=target_shape)
if type(ndarray_ret) is mx.ndarray.NDArray:
ndarray_ret = ndarray_ret.asnumpy()
assert (ndarray_ret.shape == target_shape).all()
err = np.square(ndarray_ret - numpy_ret).mean()
assert err < 1E-8
test_broadcast_to()
示例12: test_ndarray_saveload
# 需要导入模块: import mxnet [as 别名]
# 或者: from mxnet import ndarray [as 别名]
def test_ndarray_saveload():
nrepeat = 10
fname = 'tmp_list.bin'
for repeat in range(nrepeat):
data = []
# test save/load as list
for i in range(10):
data.append(random_ndarray(np.random.randint(1, 5)))
mx.nd.save(fname, data)
data2 = mx.nd.load(fname)
assert len(data) == len(data2)
for x, y in zip(data, data2):
assert np.sum(x.asnumpy() != y.asnumpy()) == 0
# test save/load as dict
dmap = {'ndarray xx %s' % i : x for i, x in enumerate(data)}
mx.nd.save(fname, dmap)
dmap2 = mx.nd.load(fname)
assert len(dmap2) == len(dmap)
for k, x in dmap.items():
y = dmap2[k]
assert np.sum(x.asnumpy() != y.asnumpy()) == 0
# test save/load as ndarray
# we expect the single ndarray to be converted into a list containing the ndarray
single_ndarray = data[0]
mx.nd.save(fname, single_ndarray)
single_ndarray_loaded = mx.nd.load(fname)
assert len(single_ndarray_loaded) == 1
single_ndarray_loaded = single_ndarray_loaded[0]
assert np.sum(single_ndarray.asnumpy() != single_ndarray_loaded.asnumpy()) == 0
os.remove(fname)
示例13: test_assign_a_row_to_ndarray
# 需要导入模块: import mxnet [as 别名]
# 或者: from mxnet import ndarray [as 别名]
def test_assign_a_row_to_ndarray():
"""Test case from https://github.com/apache/incubator-mxnet/issues/9976"""
H, W = 10, 10
dtype = np.float32
a_np = np.random.random((H, W)).astype(dtype)
a_nd = mx.nd.array(a_np)
# assign directly
a_np[0] = a_np[1]
a_nd[0] = a_nd[1]
assert same(a_np, a_nd.asnumpy())
# assign a list
v = np.random.random(W).astype(dtype).tolist()
a_np[1] = v
a_nd[1] = v
assert same(a_np, a_nd.asnumpy())
# assign a np.ndarray
v = np.random.random(W).astype(dtype)
a_np[2] = v
a_nd[2] = v
assert same(a_np, a_nd.asnumpy())
# assign by slice
a_np[0, :] = a_np[1]
a_nd[0, :] = a_nd[1]
assert same(a_np, a_nd.asnumpy())
示例14: test_ndarray_astype
# 需要导入模块: import mxnet [as 别名]
# 或者: from mxnet import ndarray [as 别名]
def test_ndarray_astype():
x = mx.nd.zeros((2, 3), dtype='int32')
y = x.astype('float32')
assert (y.dtype==np.float32)
# Test that a new ndarray has been allocated
assert (id(x) != id(y))
x = mx.nd.zeros((2, 3), dtype='int32')
y = x.astype('float32', copy=False)
assert (y.dtype==np.float32)
# Test that a new ndarray has been allocated
assert (id(x) != id(y))
x = mx.nd.zeros((2, 3), dtype='int32')
y = x.astype('int32')
assert (y.dtype==np.int32)
# Test that a new ndarray has been allocated
# even though they have same dtype
assert (id(x) != id(y))
# Test that a new ndarray has not been allocated
x = mx.nd.zeros((2, 3), dtype='int32')
y = x.astype('int32', copy=False)
assert (id(x) == id(y))
# Test the string version 'int32'
# has the same behaviour as the np.int32
x = mx.nd.zeros((2, 3), dtype='int32')
y = x.astype(np.int32, copy=False)
assert (id(x) == id(y))
示例15: test_ndarray
# 需要导入模块: import mxnet [as 别名]
# 或者: from mxnet import ndarray [as 别名]
def test_ndarray():
globs = {'np': numpy, 'mx': mxnet}
doctest.testmod(mxnet.ndarray, globs=globs, verbose=True)