本文整理汇总了Python中veles.memory.Array.map_read方法的典型用法代码示例。如果您正苦于以下问题:Python Array.map_read方法的具体用法?Python Array.map_read怎么用?Python Array.map_read使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类veles.memory.Array
的用法示例。
在下文中一共展示了Array.map_read方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: Forward
# 需要导入模块: from veles.memory import Array [as 别名]
# 或者: from veles.memory.Array import map_read [as 别名]
class Forward(ForwardBase):
"""Class for forward propagation units.
Attributes:
input: input layer values.
output: output layer values.
weights: weights.
bias: bias.
weights_stddev: magnitude of the random distribution for weights.
bias_stddev: magnitude of the random distribution for bias.
rand: prng.Rand() object for initial weights generation.
"""
hide_from_registry = True
MAPPING = set()
def __init__(self, workflow, **kwargs):
kwargs["view_group"] = kwargs.get("view_group", "WORKER")
super(Forward, self).__init__(workflow, **kwargs)
self.weights_stddev = kwargs.get("weights_stddev")
self.bias_stddev = kwargs.get("bias_stddev", self.weights_stddev)
self.weights_filling = kwargs.get("weights_filling", "uniform")
self.bias_filling = kwargs.get("bias_filling", "uniform")
self.rand = kwargs.get("rand", prng.get())
self.weights_transposed = kwargs.get("weights_transposed", False)
self.include_bias = kwargs.get("include_bias", True)
self.demand("input")
self.output = Array(shallow_pickle=True)
self.weights = Array()
self.bias = Array()
self.forward_mode = False
self.exports = ["weights", "bias", "include_bias", "weights_transposed"]
def package_export(self):
data = {}
for attr in self.exports:
value = getattr(self, attr)
if value is not None:
if isinstance(value, Array):
value.map_read()
value = value.mem
data[attr] = value
return data
@property
def forward_mode(self):
return self._forward_mode
@forward_mode.setter
def forward_mode(self, value):
if not isinstance(value, bool):
raise TypeError("forward_mode must be boolean (got %s)" % type(value))
self._forward_mode = value
def initialize(self, device, **kwargs):
self.forward_mode = kwargs.get("forward_mode", False)
super(Forward, self).initialize(device=device, **kwargs)
def generate_data_for_slave(self, slave):
if self.forward_mode:
return None
data = [None, None]
if self.weights:
self.weights.map_read()
data[0] = self.weights.mem
if self.bias:
self.bias.map_read()
data[1] = self.bias.mem
return data
def generate_data_for_master(self):
return None
def apply_data_from_master(self, data):
if self.forward_mode:
return
if self.weights:
self.weights.map_invalidate()
numpy.copyto(self.weights.mem, data[0])
else:
self.weights.reset(data[0])
if self.bias:
self.bias.map_invalidate()
numpy.copyto(self.bias.mem, data[1])
else:
self.bias.reset(data[1])
def apply_data_from_slave(self, data, slave):
pass
def drop_slave(self, slave):
pass
示例2: GradientDescentBase
# 需要导入模块: from veles.memory import Array [as 别名]
# 或者: from veles.memory.Array import map_read [as 别名]
#.........这里部分代码省略.........
self.device.skip(5),
self._bias_const[5:6],
self._bias_const[6:7],
self._bias_const[7:8],
self._bias_const[8:9],
self._bias_const[9:10],
self._bias_const[10:11],
self._bias_const[11:12],
self._bias_const[12:13],
)
self.execute_kernel(self._global_size_bias, self._local_size_bias, self.krn_bias_)
def gpu_err_output_update(self):
"""Multiply err_output by activation derivative by output.
"""
if self.krn_err_output_ is None:
return
self.err_output.unmap()
self.output.unmap()
self.execute_kernel(self._global_size_err_output, self._local_size_err_output, self.krn_err_output_)
def numpy_err_output_update(self):
"""Multiply err_output by activation derivative by output.
"""
pass
def print_debug_data(self):
"""
Show weights statistics
"""
if not self.logger.isEnabledFor(logging.DEBUG):
return
self.weights.map_read()
self.bias.map_read()
self.gradient_bias.map_read()
self.gradient_weights.map_read()
weights = self.weights.mem
bias = self.bias.mem
grad_weights = self.gradient_weights.mem
grad_bias = self.gradient_bias.mem
weight_table = PrettyTable("TYPE", "Mean", "StdDev", "Min", "Max")
weight_table.float_format = ".10"
for (w_name, w_array) in [
("Weight", weights),
("Bias", bias),
("Grad Weight", grad_weights),
("Grad Bias", grad_bias),
]:
w_mean = w_stddev = w_min = w_max = None
if w_array is not None and w_array.size > 0:
w_mean = numpy.mean(w_array)
w_stddev = numpy.std(w_array)
w_min = numpy.min(w_array)
w_max = numpy.max(w_array)
weight_table.add_row(w_name, w_mean, w_stddev, w_min, w_max)
self.debug("\n" + weight_table.get_string())
def generate_data_for_slave(self, slave):
return (
self.learning_rate,
self.weights_decay,
self.gradient_moment,
self.learning_rate_bias,
self.weights_decay_bias,
示例3: ZeroFiller
# 需要导入模块: from veles.memory import Array [as 别名]
# 或者: from veles.memory.Array import map_read [as 别名]
class ZeroFiller(ForwardBase, TriviallyDistributable):
"""Fills weights of given unit with zero on every step"""
MAPPING = {"zero_filter"}
def __init__(self, workflow, **kwargs):
super(ZeroFiller, self).__init__(workflow, **kwargs)
self.mask = Array()
self.grouping = kwargs.get("grouping", 1)
self.demand("weights")
def init_unpickled(self):
super(ZeroFiller, self).init_unpickled()
self.sources_["weights_zerofilling"] = {}
@property
def effective_shape(self):
return (self.weights.shape[0],
self.weights.size // self.weights.shape[0])
@property
def grouping(self):
return self._grouping
@grouping.setter
def grouping(self, value):
if not isinstance(value, int):
raise TypeError(
"grouping value must be an integer (got %s)" % type(value))
if value < 2:
raise ValueError("grouping value %d is invalid" % value)
self._grouping = value
def initialize(self, device=None, **kwargs):
super(ZeroFiller, self).initialize(device, **kwargs)
if not self.weights:
return True
if not self.mask:
if self.effective_shape[1] % self.grouping != 0:
raise ValueError(
"Non-multiple of grouping weights shape detected: "
"%s, grouping=%d" %
(self.weights.shape, self.grouping))
self.mask.reset(numpy.zeros(self.effective_shape,
dtype=self.weights.dtype))
self.mask.map_invalidate()
# TODO(a.kazantsev): add check for transposed weights.
for kernel in range(self.effective_shape[0]):
for chan in range(self.effective_shape[1]):
self.mask[kernel, chan] = not (
kernel % self.grouping == chan % self.grouping)
else:
assert self.mask.shape == self.effective_shape
for vec in self.mask, self.weights:
vec.initialize(device)
def _gpu_init(self):
self.build_program(cache_file_name="zero_filling_%d" % self.grouping,
dtype=self.weights.dtype)
self.assign_kernel("multiply_by_mask")
self.set_args(self.mask, self.weights)
def ocl_init(self):
self._gpu_init()
self._global_size = [self.weights.size]
self._local_size = None
def cuda_init(self):
self._gpu_init()
self._global_size = (self.weights.size, 1, 1)
self._local_size = (1, 1, 1)
def numpy_run(self):
self.mask.map_read()
self.weights.map_write()
self.weights.mem *= self.mask.mem
def _gpu_run(self):
self.weights.unmap()
self.mask.unmap()
self.execute_kernel(self._global_size, self._local_size)
def ocl_run(self):
self._gpu_run()
def cuda_run(self):
self._gpu_run()