本文整理汇总了Python中numpy.prod方法的典型用法代码示例。如果您正苦于以下问题:Python numpy.prod方法的具体用法?Python numpy.prod怎么用?Python numpy.prod使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类numpy
的用法示例。
在下文中一共展示了numpy.prod方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_fans
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import prod [as 别名]
def get_fans(shape, dim_ordering='th'):
if len(shape) == 2:
fan_in = shape[0]
fan_out = shape[1]
elif len(shape) == 4 or len(shape) == 5:
# assuming convolution kernels (2D or 3D).
# TH kernel shape: (depth, input_depth, ...)
# TF kernel shape: (..., input_depth, depth)
if dim_ordering == 'th':
receptive_field_size = np.prod(shape[2:])
fan_in = shape[1] * receptive_field_size
fan_out = shape[0] * receptive_field_size
elif dim_ordering == 'tf':
receptive_field_size = np.prod(shape[:2])
fan_in = shape[-2] * receptive_field_size
fan_out = shape[-1] * receptive_field_size
else:
raise ValueError('Invalid dim_ordering: ' + dim_ordering)
else:
# no specific assumptions
fan_in = np.sqrt(np.prod(shape))
fan_out = np.sqrt(np.prod(shape))
return fan_in, fan_out
示例2: print_layers
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import prod [as 别名]
def print_layers(self, title=None, hide_layers_with_no_params=False):
if title is None: title = self.name
print()
print('%-28s%-12s%-24s%-24s' % (title, 'Params', 'OutputShape', 'WeightShape'))
print('%-28s%-12s%-24s%-24s' % (('---',) * 4))
total_params = 0
for layer_name, layer_output, layer_trainables in self.list_layers():
weights = [var for var in layer_trainables if var.name.endswith('/weight:0')]
num_params = sum(np.prod(shape_to_list(var.shape)) for var in layer_trainables)
total_params += num_params
if hide_layers_with_no_params and num_params == 0:
continue
print('%-28s%-12s%-24s%-24s' % (
layer_name,
num_params if num_params else '-',
layer_output.shape,
weights[0].shape if len(weights) == 1 else '-'))
print('%-28s%-12s%-24s%-24s' % (('---',) * 4))
print('%-28s%-12s%-24s%-24s' % ('Total', total_params, '', ''))
print()
# Construct summary ops to include histograms of all trainable parameters in TensorBoard.
示例3: generate_fake_images
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import prod [as 别名]
def generate_fake_images(run_id, snapshot=None, grid_size=[1,1], num_pngs=1, image_shrink=1, png_prefix=None, random_seed=1000, minibatch_size=8):
network_pkl = misc.locate_network_pkl(run_id, snapshot)
if png_prefix is None:
png_prefix = misc.get_id_string_for_network_pkl(network_pkl) + '-'
random_state = np.random.RandomState(random_seed)
print('Loading network from "%s"...' % network_pkl)
G, D, Gs = misc.load_network_pkl(run_id, snapshot)
result_subdir = misc.create_result_subdir(config.result_dir, config.desc)
for png_idx in range(num_pngs):
print('Generating png %d / %d...' % (png_idx, num_pngs))
latents = misc.random_latents(np.prod(grid_size), Gs, random_state=random_state)
labels = np.zeros([latents.shape[0], 0], np.float32)
images = Gs.run(latents, labels, minibatch_size=minibatch_size, num_gpus=config.num_gpus, out_mul=127.5, out_add=127.5, out_shrink=image_shrink, out_dtype=np.uint8)
misc.save_image_grid(images, os.path.join(result_subdir, '%s%06d.png' % (png_prefix, png_idx)), [0,255], grid_size)
open(os.path.join(result_subdir, '_done.txt'), 'wt').close()
#----------------------------------------------------------------------------
# Generate MP4 video of random interpolations using a previously trained network.
# To run, uncomment the appropriate line in config.py and launch train.py.
示例4: ensure_flat_frame
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import prod [as 别名]
def ensure_flat_frame(f, frame=None):
def _wrapper_func(self, coords, **kwargs):
if (frame is not None) and (coords.frame.name != frame):
coords_transf = coords.transform_to(frame)
else:
coords_transf = coords
is_array = not coords.isscalar
if is_array:
orig_shape = coords.shape
shape_flat = (np.prod(orig_shape),)
coords_transf = coords_to_shape(coords_transf, shape_flat)
else:
coords_transf = coords_to_shape(coords_transf, (1,))
out = f(self, coords_transf, **kwargs)
if is_array:
out.shape = orig_shape + out.shape[1:]
else:
out = out[0]
return out
return _wrapper_func
示例5: fapply
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import prod [as 别名]
def fapply(f, x, tz=False):
'''
fapply(f,x) yields the result of applying f either to x, if x is a normal value or array, or to
x.data if x is a sparse matrix. Does not modify x (unless f modifiex x).
The optional argument tz (default: False) may be set to True to specify that, if x is a sparse
matrix that contains at least 1 element that is a sparse-zero, then f(0) should replace all the
sparse-zeros in x (unless f(0) == 0).
'''
if sps.issparse(x):
y = x.copy()
y.data = f(x.data)
if tz and y.getnnz() < np.prod(y.shape):
z = f(np.array(0))
if z != 0:
y = y.toarray()
y[y == 0] = z
return y
else: return f(x)
示例6: update
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import prod [as 别名]
def update(self, labels, preds):
"""Updates the internal evaluation result.
Parameters
----------
labels : list of `NDArray`
The labels of the data.
preds : list of `NDArray`
Predicted values.
"""
labels, preds = check_label_shapes(labels, preds, True)
for label, pred in zip(labels, preds):
label = label.asnumpy()
pred = pred.asnumpy()
if len(label.shape) == 1:
label = label.reshape(label.shape[0], 1)
if len(pred.shape) == 1:
pred = pred.reshape(pred.shape[0], 1)
self.sum_metric += numpy.abs(label - pred).mean()
self.num_inst += 1 # numpy.prod(label.shape)
示例7: _init_weight
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import prod [as 别名]
def _init_weight(self, name, arr):
shape = arr.shape
hw_scale = 1.
if len(shape) < 2:
raise ValueError('Xavier initializer cannot be applied to vector {0}. It requires at'
' least 2D.'.format(name))
if len(shape) > 2:
hw_scale = np.prod(shape[2:])
fan_in, fan_out = shape[1] * hw_scale, shape[0] * hw_scale
factor = 1.
if self.factor_type == "avg":
factor = (fan_in + fan_out) / 2.0
elif self.factor_type == "in":
factor = fan_in
elif self.factor_type == "out":
factor = fan_out
else:
raise ValueError("Incorrect factor type")
scale = np.sqrt(self.magnitude / factor)
if self.rnd_type == "uniform":
random.uniform(-scale, scale, out=arr)
elif self.rnd_type == "gaussian":
random.normal(0, scale, out=arr)
else:
raise ValueError("Unknown random type")
示例8: size
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import prod [as 别名]
def size(self):
"""Number of elements in the array.
Equivalent to the product of the array's dimensions.
Examples
--------
>>> import numpy as np
>>> x = mx.nd.zeros((3, 5, 2))
>>> x.size
30
>>> np.prod(x.shape)
30
"""
size = 1
for i in self.shape:
size *= i
return size
示例9: _finish_deferred_init
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import prod [as 别名]
def _finish_deferred_init(self):
"""Finishes deferred initialization."""
if not self._deferred_init:
return
init, ctx, default_init, data = self._deferred_init
self._deferred_init = ()
assert self.shape is not None and np.prod(self.shape) > 0, \
"Cannot initialize Parameter '%s' because it has " \
"invalid shape: %s. Please specify in_units, " \
"in_channels, etc for `Block`s."%(
self.name, str(self.shape))
with autograd.pause():
if data is None:
data = ndarray.zeros(shape=self.shape, dtype=self.dtype,
ctx=context.cpu(), stype=self._stype)
initializer.create(default_init)(
initializer.InitDesc(self.name, {'__init__': init}), data)
self._init_impl(data, ctx)
示例10: test_sparse_nd_storage_fallback
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import prod [as 别名]
def test_sparse_nd_storage_fallback():
def check_output_fallback(shape):
ones = mx.nd.ones(shape)
out = mx.nd.zeros(shape=shape, stype='csr')
mx.nd.broadcast_add(ones, ones * 2, out=out)
assert(np.sum(out.asnumpy() - 3) == 0)
def check_input_fallback(shape):
ones = mx.nd.ones(shape)
out = mx.nd.broadcast_add(ones.tostype('csr'), ones.tostype('row_sparse'))
assert(np.sum(out.asnumpy() - 2) == 0)
def check_fallback_with_temp_resource(shape):
ones = mx.nd.ones(shape)
out = mx.nd.sum(ones)
assert(out.asscalar() == np.prod(shape))
shape = rand_shape_2d()
check_output_fallback(shape)
check_input_fallback(shape)
check_fallback_with_temp_resource(shape)
示例11: count_weights
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import prod [as 别名]
def count_weights(scope=None, exclude=None, graph=None):
"""Count learnable parameters.
Args:
scope: Resrict the count to a variable scope.
exclude: Regex to match variable names to exclude.
graph: Operate on a graph other than the current default graph.
Returns:
Number of learnable parameters as integer.
"""
if scope:
scope = scope if scope.endswith('/') else scope + '/'
graph = graph or tf.get_default_graph()
vars_ = graph.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES)
if scope:
vars_ = [var for var in vars_ if var.name.startswith(scope)]
if exclude:
exclude = re.compile(exclude)
vars_ = [var for var in vars_ if not exclude.match(var.name)]
shapes = [var.get_shape().as_list() for var in vars_]
return int(sum(np.prod(shape) for shape in shapes))
示例12: sample
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import prod [as 别名]
def sample(self, shape):
import numpy as np
rng = lasagne.random.get_rng()
if len(shape) >= 4:
# convolutions use Gaussians with stddev of sqrt(2/fan_out), see
# https://github.com/liuzhuang13/DenseNet/blob/cbb6bff/densenet.lua#L85-L86
# and https://github.com/facebook/fb.resnet.torch/issues/106
fan_out = shape[0] * np.prod(shape[2:])
W = rng.normal(0, np.sqrt(2. / fan_out),
size=shape)
elif len(shape) == 2:
# the dense layer uses Uniform of range sqrt(1/fan_in), see
# https://github.com/torch/nn/blob/651103f/Linear.lua#L21-L43
fan_in = shape[0]
W = rng.uniform(-np.sqrt(1. / fan_in), np.sqrt(1. / fan_in),
size=shape)
return lasagne.utils.floatX(W)
示例13: ortho_init
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import prod [as 别名]
def ortho_init(scale=1.0):
def _ortho_init(shape, dtype, partition_info=None):
#lasagne ortho init for tf
shape = tuple(shape)
if len(shape) == 2:
flat_shape = shape
elif len(shape) == 4: # assumes NHWC
flat_shape = (np.prod(shape[:-1]), shape[-1])
else:
raise NotImplementedError
a = np.random.normal(0.0, 1.0, flat_shape)
u, _, v = np.linalg.svd(a, full_matrices=False)
q = u if u.shape == flat_shape else v # pick the one with the correct shape
q = q.reshape(shape)
return (scale * q[:shape[0], :shape[1]]).astype(np.float32)
return _ortho_init
示例14: _cac_conv
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import prod [as 别名]
def _cac_conv(layer, input, output):
# bs, ic, ih, iw = input[0].shape
oh, ow = output.shape[-2:]
kh, kw = layer.kernel_size
ic, oc = layer.in_channels, layer.out_channels
g = layer.groups
tb_params = 0
ntb__params = 0
flops = 0
if hasattr(layer, 'weight') and hasattr(layer.weight, 'shape'):
params = np.prod(layer.weight.shape)
t, n = _cac_grad_params(params, layer.weight)
tb_params += t
ntb__params += n
flops += (2 * ic * kh * kw - 1) * oh * ow * (oc // g)
if hasattr(layer, 'bias') and hasattr(layer.bias, 'shape'):
params = np.prod(layer.bias.shape)
t, n = _cac_grad_params(params, layer.bias)
tb_params += t
ntb__params += n
flops += oh * ow * (oc // g)
return tb_params, ntb__params, flops
示例15: _cac_xx_norm
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import prod [as 别名]
def _cac_xx_norm(layer, input, output):
tb_params = 0
ntb__params = 0
if hasattr(layer, 'weight') and hasattr(layer.weight, 'shape'):
params = np.prod(layer.weight.shape)
t, n = _cac_grad_params(params, layer.weight)
tb_params += t
ntb__params += n
if hasattr(layer, 'bias') and hasattr(layer.bias, 'shape'):
params = np.prod(layer.bias.shape)
t, n = _cac_grad_params(params, layer.bias)
tb_params += t
ntb__params += n
if hasattr(layer, 'running_mean') and hasattr(layer.running_mean, 'shape'):
params = np.prod(layer.running_mean.shape)
ntb__params += params
if hasattr(layer, 'running_var') and hasattr(layer.running_var, 'shape'):
params = np.prod(layer.running_var.shape)
ntb__params += params
in_shape = input[0]
flops = np.prod(in_shape.shape)
if layer.affine:
flops *= 2
return tb_params, ntb__params, flops