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


Python Array.reset方法代码示例

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


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

示例1: clone

# 需要导入模块: from veles.memory import Array [as 别名]
# 或者: from veles.memory.Array import reset [as 别名]
 def clone(self):
     for unit, attrs in self.reals.items():
         for attr in attrs:
             value = getattr(unit, attr)
             if self.is_immutable(value):
                 setattr(self, attr, value)
                 continue
             if not isinstance(value, Array):
                 cloned = getattr(self, attr, None)
                 if cloned is None:
                     setattr(self, attr, deepcopy(value))
                     continue
                 if isinstance(value, list):
                     del cloned[:]
                     cloned.extend(value)
                 elif isinstance(value, (dict, set)):
                     cloned.clear()
                     cloned.update(value)
                 elif isinstance(value, Bool):
                     cloned <<= value
                 elif isinstance(value, numpy.ndarray):
                     cloned[:] = value
                 else:
                     setattr(self, attr, deepcopy(value))
                 continue
             vec = getattr(self, attr, None)
             if vec is None:
                 vec = Array()
                 self.vectors[value] = vec
                 setattr(self, attr, vec)
             else:
                 assert isinstance(vec, Array)
             if not vec and value:
                 vec.reset(value.mem.copy())
开发者ID:2php,项目名称:veles,代码行数:36,代码来源:avatar.py

示例2: Summator

# 需要导入模块: from veles.memory import Array [as 别名]
# 或者: from veles.memory.Array import reset [as 别名]
class Summator(AcceleratedUnit):
    """Multiplies two vectors pointwise.
    """
    def __init__(self, workflow, **kwargs):
        super(Summator, self).__init__(workflow, **kwargs)
        self.output = Array()
        self.demand("x", "y")

    def initialize(self, device, **kwargs):
        super(Summator, self).initialize(device, **kwargs)
        if not self.output:
            self.output.reset(numpy.zeros_like(self.x.mem))
        else:
            assert self.output.shape == self.x.shape
        self.init_vectors(self.x, self.y, self.output)

    def init_unpickled(self):
        super(Summator, self).init_unpickled()
        self.sources_["summator"] = {}

    def _gpu_init(self):
        self.build_program({"OUTPUT_SIZE": self.output.size},
                           "%s_%d" %
                           (self.__class__.__name__, self.output.size),
                           dtype=self.x.dtype)
        self.assign_kernel("add_forward")
        self.set_args(self.x, self.y, self.output)

    def cuda_init(self):
        self._gpu_init()
        block_size = self.device.suggest_block_size(self._kernel_)
        self._global_size = (
            int(numpy.ceil(self.output.size / block_size)), 1, 1)
        self._local_size = (block_size, 1, 1)

    def ocl_init(self):
        self._gpu_init()
        self._global_size = (self.output.size, 1, 1)
        self._local_size = None

    def numpy_init(self):
        pass  # nothing to init

    def _gpu_run(self):
        self.unmap_vectors(self.x, self.y, self.output)
        self.execute_kernel(self._global_size, self._local_size)

    def cuda_run(self):
        self._gpu_run()

    def ocl_run(self):
        self._gpu_run()

    def numpy_run(self):
        self.x.map_read()
        self.y.map_read()
        self.output.map_invalidate()
        numpy.add(self.x.mem, self.y.mem, self.output.mem)
开发者ID:vmarkovtsev,项目名称:veles.znicz,代码行数:60,代码来源:summator.py

示例3: GDSummator

# 需要导入模块: from veles.memory import Array [as 别名]
# 或者: from veles.memory.Array import reset [as 别名]
class GDSummator(AcceleratedUnit):
    """Gradient descent for Summator.
    """
    def __init__(self, workflow, **kwargs):
        super(GDSummator, self).__init__(workflow, **kwargs)
        self.err_x = Array()
        self.err_y = Array()
        self.demand("err_output")

    def initialize(self, device, **kwargs):
        super(GDSummator, self).initialize(device, **kwargs)

        if self.err_x:
            assert self.err_x.shape[1:] == self.err_output.shape[1:]
        if not self.err_x or self.err_x.shape[0] != self.err_output.shape[0]:
            self.err_x.reset(numpy.zeros_like(self.err_output.mem))
        if self.err_y:
            assert self.err_y.shape[1:] == self.err_output.shape[1:]
        if not self.err_y or self.err_y.shape[0] != self.err_output.shape[0]:
            self.err_y.reset(numpy.zeros_like(self.err_output.mem))
        self.init_vectors(self.err_x, self.err_y, self.err_output)

    def cuda_init(self):
        pass  # nothing to init

    def ocl_init(self):
        pass  # nothing to init

    def numpy_init(self):
        pass  # nothing to init

    def cuda_run(self):
        self.unmap_vectors(self.err_output, self.err_x, self.err_y)
        self.err_x.devmem.from_device_async(self.err_output.devmem)
        self.err_y.devmem.from_device_async(self.err_output.devmem)

    def ocl_run(self):
        self.unmap_vectors(self.err_output, self.err_x, self.err_y)
        self.device.queue_.copy_buffer(
            self.err_output.devmem, self.err_x.devmem, 0, 0,
            self.err_output.nbytes, need_event=False)
        self.device.queue_.copy_buffer(
            self.err_output.devmem, self.err_y.devmem, 0, 0,
            self.err_output.nbytes, need_event=False)

    def numpy_run(self):
        self.err_output.map_read()
        self.err_x.map_invalidate()
        self.err_y.map_invalidate()
        self.err_x.mem[:] = self.err_output.mem[:]
        self.err_y.mem[:] = self.err_output.mem[:]
开发者ID:Samsung,项目名称:veles.znicz,代码行数:53,代码来源:summator.py

示例4: MemCpy

# 需要导入模块: from veles.memory import Array [as 别名]
# 或者: from veles.memory.Array import reset [as 别名]
class MemCpy(AcceleratedUnit):
    def __init__(self, workflow, **kwargs):
        super(MemCpy, self).__init__(workflow, **kwargs)
        self.output = Array()
        self.demand("input")

    def initialize(self, device, **kwargs):
        super(MemCpy, self).initialize(device, **kwargs)
        if (self.output.mem is None or
                self.output.mem.size != self.input.mem.size):
            self.output.reset()
            self.output.mem = numpy.zeros(self.input.mem.shape,
                                          dtype=self.input.mem.dtype)
        self.input.initialize(self.device)
        self.output.initialize(self.device)

    def cuda_init(self):
        pass

    def ocl_init(self):
        pass

    def _gpu_run(self):
        self.input.unmap()
        self.output.unmap()

    def ocl_run(self):
        self._gpu_run()
        self.device.queue_.copy_buffer(self.input.devmem, self.output.devmem,
                                       0, 0, self.input.nbytes)

    def cuda_run(self):
        self._gpu_run()
        self.output.devmem.from_device_async(self.input.devmem)

    def numpy_run(self):
        self.input.map_read()
        self.output.map_invalidate()
        numpy.copyto(self.output.mem, self.input.mem)
开发者ID:Samsung,项目名称:veles.znicz,代码行数:41,代码来源:rbm_units.py

示例5: GradientsCalculator

# 需要导入模块: from veles.memory import Array [as 别名]
# 或者: from veles.memory.Array import reset [as 别名]
class GradientsCalculator(AcceleratedUnit, EmptyDeviceMethodsMixin):
    """
    Making gradients for weights, hbias and vbias, using hbias0, vbias0
    and vbias1, hbias1, which calculated with help BatchWeights.
    Must be assigned before initialize():
    * hbias0
    * vbias0
    * hbias1
    * vbias1
    * weights1
    * weights0

    Updates after run():
    * hbias_grad
    * vbias_grad
    * weights_grad

    Creates within initialize():
    * hbias_grad
    * vbias_grad
    * weights_grad

    Attributes:
        vbias0: calculated with help BatchWeights from v0
        hbias0: calculated with help BatchWeights from h0
        vbias1: calculated with help BatchWeights from v1
        hbias1: calculated with help BatchWeights from h1
        weights1: calculated with help BatchWeights from v1.
        weights0: calculated with help BatchWeights from h1.
        hbias_grad: gradient for hbias
        vbias_grad: gradient for vbias
        weights_grad: gradient for weights
    """
    def __init__(self, workflow, **kwargs):
        super(GradientsCalculator, self).__init__(workflow, **kwargs)
        self.vbias_grad = Array()
        self.hbias_grad = Array()
        self.weights_grad = Array()
        self.demand("hbias1", "vbias1", "hbias0", "vbias0", "weights0",
                    "weights1")

    def initialize(self, device, **kwargs):
        super(GradientsCalculator, self).initialize(device=device, **kwargs)
        if not self.hbias_grad:
            self.hbias_grad.reset(numpy.zeros(self.hbias0.shape,
                                              dtype=self.hbias0.dtype))
        else:
            assert self.hbias_grad.shape == self.hbias0.shape
        if not self.vbias_grad:
            self.vbias_grad.reset(numpy.zeros(self.vbias0.shape,
                                              dtype=self.vbias0.dtype))
        else:
            assert self.vbias_grad.shape == self.vbias0.shape
        if not self.weights_grad:
            self.weights_grad.reset(numpy.zeros(self.weights0.shape,
                                                dtype=self.weights0.dtype))
        else:
            assert self.weights_grad.shape == self.weights0.shape
        for v in (self.weights_grad, self.hbias_grad, self.vbias_grad,
                  self.hbias0, self.vbias0, self.weights0, self.hbias1,
                  self.vbias1, self.weights1):
            v.initialize(self.device)

    def run(self):
        for v in (self.hbias0, self.vbias0, self.weights0,
                  self.hbias1, self.vbias1, self.weights1):
            v.map_read()

        for v in (self.weights_grad, self.vbias_grad, self.hbias_grad):
            v.map_invalidate()

        self.vbias_grad.mem[:] = self.vbias0.mem - self.vbias1.mem
        self.hbias_grad.mem[:] = self.hbias0.mem - self.hbias1.mem
        self.weights_grad.mem[:] = self.weights0.mem - self.weights1.mem
开发者ID:Samsung,项目名称:veles.znicz,代码行数:76,代码来源:rbm_units.py

示例6: EvaluatorMSE

# 需要导入模块: from veles.memory import Array [as 别名]
# 或者: from veles.memory.Array import reset [as 别名]
class EvaluatorMSE(EvaluatorBase):

    MAPPING = "evaluator_mse"
    LOSS = "mse"

    """Evaluator for nn softmax output from the batch labels.

    Must be assigned before initialize():
        output
        target
        batch_size
        labels (may be None)
        class_targets (may be None)

    Updates after run():
        err_output
        confusion_matrix
        max_err_output_sum
        n_err (only if labels and class_targets is not None)

    Creates within initialize():
        err_output
        n_err (only if labels and class_targets is not None)
        max_err_output_sum

    Attributes:
        output: output of the network_common as Batch.
        target: target for the current Batch.
        err_output: backpropagation errors.
        batch_size: number of elements in output to evaluate.
        metrics: [0] - sum of sample's mse, [1] - max of sample's mse,
                 [2] - min of sample's mse.
        mse: array of mse for each sample in minibatch.
        krn_constants_i_: numpy array for constant arguments to kernel.
        labels: labels for a batch (may be None).
        class_targets: target for each class (may be None).
        n_err: number of wrongly recognized samples
            (if labels and class_targets is not None).
    """
    def __init__(self, workflow, **kwargs):
        super(EvaluatorMSE, self).__init__(workflow, **kwargs)
        self.metrics = Array()
        self.mse = Array()
        self.labels = None
        self.class_targets = None
        self.n_err = Array()
        self.root = kwargs.get("root", True)
        self.demand("target", "normalizer")

    @property
    def root(self):
        """
        :return: True if error metric is RMSE, otherwise, MSE (mean sum of
        squares). Default is True.
        """
        return self._root

    @root.setter
    def root(self, value):
        if not isinstance(value, bool):
            raise TypeError("root must be boolean (got %s)" % type(value))
        self._root = value

    def initialize(self, device, **kwargs):
        super(EvaluatorMSE, self).initialize(device=device, **kwargs)
        if self.testing:
            return

        if self.target.size != self.output.size:
            raise error.BadFormatError(
                "target.size != output.size (%s != %s)" %
                (self.target.size, self.output.size))

        self.sources_["evaluator_mse"] = {}
        self.sources_["denormalization"] = {}

        dtype = self.output.dtype

        self.metrics.reset(numpy.zeros(3, dtype=dtype))
        self.metrics[2] = 1.0e30  # mse_min
        self.mse.reset(numpy.zeros(self.err_output.mem.shape[0], dtype))
        self.n_err.reset(numpy.zeros(2, dtype=numpy.int32))
        self.init_vectors(self.n_err, self.target, self.metrics, self.mse)
        if self.class_targets:
            self.class_targets.initialize(self.device)

    def _gpu_init(self):
        dtype = self.output.dtype
        block_size = min(self.err_output.shape[0], 128)
        if self.class_targets:
            self.sources_["mse_find_closest"] = {
                "target_dtype": numpy_dtype_to_opencl(self.class_targets.dtype)
            }

        self.build_program(
            cache_file_name="%s_%d_%d" % (self.__class__.__name__,
                                          self.output.shape[0],
                                          self.output.sample_size),
            dtype=dtype, max_batch_size=self.err_output.shape[0],
            block_size=block_size, output_size=self.err_output.sample_size,
#.........这里部分代码省略.........
开发者ID:Samsung,项目名称:veles.znicz,代码行数:103,代码来源:evaluator.py

示例7: EvaluatorBase

# 需要导入模块: from veles.memory import Array [as 别名]
# 或者: from veles.memory.Array import reset [as 别名]
class EvaluatorBase(AcceleratedUnit, TriviallyDistributable):
    hide_from_registry = True
    """Base class for evaluators.
    """
    def __init__(self, workflow, **kwargs):
        kwargs["view_group"] = kwargs.get("view_group", "EVALUATOR")
        super(EvaluatorBase, self).__init__(workflow, **kwargs)
        self.mean = kwargs.get("mean", True)
        self.err_output = Array()
        self._merged_output = Array()
        self.krn_constants_i_ = None
        self.krn_constants_f_ = None
        self.demand("output", "batch_size")
        if self.testing:
            self.demand("class_lengths", "offset")

    @property
    def mean(self):
        """
        :return: True if the error function averages values. Default is True.
        """
        return self._mean

    @mean.setter
    def mean(self, value):
        if not isinstance(value, bool):
            raise TypeError("mean must be boolean (got %s)" % type(value))
        self._mean = value

    @property
    def merged_output(self):
        assert self.testing
        return self._merged_output.mem

    def initialize(self, device, **kwargs):
        super(EvaluatorBase, self).initialize(device, **kwargs)
        dtype = self.output.dtype
        if self.testing:
            self._merged_output.reset(numpy.zeros(
                (self.class_lengths[TEST],) + self.output.shape[1:], dtype))
            return

        self.krn_constants_i_ = numpy.zeros(1, numpy.int32)
        self.krn_constants_f_ = numpy.zeros(1, dtype)
        self.err_output.reset(numpy.zeros_like(self.output.mem, dtype))

        for vec in self.output, self.err_output:
            vec.initialize(self.device)

    def run(self):
        if self.testing:
            self.output.map_read()
            self.merge_output()
            return
        return super(EvaluatorBase, self).run()

    def merge_output(self):
        self.merged_output[self.offset - self.batch_size:self.offset] = \
            self.output[:self.batch_size]

    def get_metric_names(self):
        if self.testing:
            return {"Output"}
        return set()

    def get_metric_values(self):
        if self.testing:
            return {"Output": self.merged_output}
        return {}
开发者ID:Samsung,项目名称:veles.znicz,代码行数:71,代码来源:evaluator.py

示例8: ImageLoader

# 需要导入模块: from veles.memory import Array [as 别名]
# 或者: from veles.memory.Array import reset [as 别名]

#.........这里部分代码省略.........
            for keys in self.class_keys:
                for key in keys:
                    progress.inc()
                    size, _ = self.get_effective_image_info(key)
                    if size != self.uncropped_shape:
                        raise error.BadFormatError(
                            "%s changed the effective size (now %s, was %s)" %
                            (key, size, self.uncropped_shape))
            progress.finish()
            return
        for keys in self.class_keys:
            del keys[:]
        for index, class_name in enumerate(CLASS_NAME):
            keys = set(self.get_keys(index))
            self.class_keys[index].extend(keys)
            self.class_lengths[index] = len(keys) * self.samples_inflation
            self.class_keys[index].sort()

        if self.uncropped_shape == tuple():
            raise error.BadFormatError(
                "original_shape was not initialized in get_keys()")
        self.info(
            "Found %d samples of shape %s (%d TEST, %d VALIDATION, %d TRAIN)",
            self.total_samples, self.shape, *self.class_lengths)

        # Perform a quick (unreliable) test to determine if we have labels
        keys = next(k for k in self.class_keys if len(k) > 0)
        self._has_labels = self.load_keys(
            (keys[RandomGenerator(None).randint(len(keys))],),
            None, None, None, None)
        self._resize_validation_keys(self.load_labels())

    def create_minibatch_data(self):
        self.minibatch_data.reset(numpy.zeros(
            (self.max_minibatch_size,) + self.shape, dtype=self.dtype))

        self.minibatch_label_values.reset(numpy.zeros(
            self.max_minibatch_size, numpy.float32))

    def keys_from_indices(self, indices):
        for index in indices:
            class_index, origin_index, _ = \
                self._get_class_origin_distortion_from_index(index)
            yield self.class_keys[class_index][origin_index]

    def fill_minibatch(self):
        indices = self.minibatch_indices.mem[:self.minibatch_size]
        assert self.has_labels == self.load_keys(
            self.keys_from_indices(indices), None, self.minibatch_data.mem,
            self.raw_minibatch_labels, self.minibatch_label_values)
        if self.samples_inflation == 1:
            return
        for pos, index in enumerate(indices):
            _, _, dist_index = \
                self._get_class_origin_distortion_from_index(index)
            self.minibatch_data[pos] = self.distort(
                self.minibatch_data[pos],
                *self.get_distortion_by_index(dist_index))

    def _resize_validation_keys(self, label_analysis):
        if label_analysis is None:
            return
        different_labels, label_key_map = label_analysis
        if self.validation_ratio is None:
            self._setup_labels_mapping(different_labels)
            return
开发者ID:EgBulychev,项目名称:veles,代码行数:70,代码来源:image.py

示例9: EvaluatorSoftmax

# 需要导入模块: from veles.memory import Array [as 别名]
# 或者: from veles.memory.Array import reset [as 别名]
class EvaluatorSoftmax(EvaluatorBase):

    MAPPING = "evaluator_softmax"
    LOSS = "softmax"

    """Evaluator for nn softmax output from the batch labels.

    Must be assigned before initialize():
        output
        labels
        batch_size
        max_idx

    Updates after run():
        err_output
        n_err
        confusion_matrix
        max_err_output_sum

    Creates within initialize():
        err_output
        n_err
        confusion_matrix
        max_err_output_sum

    Attributes:
        labels: labels for Batch.
        output: output of the network_common as Batch.
        err_output: backpropagation errors based on labels.
        batch_size: number of elements in output to evaluate.
        confusion_matrix: confusion matrix for the output.
        compute_confusion_matrix: compute confusion matrix or not.
        max_idx: indexes of element with maximum real value for each sample.
        max_err_output_sum: maximum of backpropagated error sum by sample.
    """
    def __init__(self, workflow, **kwargs):
        super(EvaluatorSoftmax, self).__init__(workflow, **kwargs)
        self.compute_confusion_matrix = kwargs.get(
            "compute_confusion_matrix", True)
        self.confusion_matrix = Array()
        self.n_err = Array()
        self.max_err_output_sum = Array()
        self.class_keys = None
        self.demand("labels", "max_idx")
        if self.testing:
            self.demand("labels_mapping")

    def initialize(self, device, **kwargs):
        super(EvaluatorSoftmax, self).initialize(device=device, **kwargs)
        if self.testing:
            return
        self.sources_["evaluator"] = {}

        dtype = self.output.dtype

        if not self.n_err:
            self.n_err.reset(numpy.zeros(2, dtype=numpy.int32))
        else:
            assert self.n_err.size == 2

        out_size = self.output.sample_size
        if self.compute_confusion_matrix:
            if not self.confusion_matrix:
                self.confusion_matrix.reset(
                    numpy.zeros([out_size, out_size], numpy.int32))
            else:
                assert self.confusion_matrix.size == out_size * out_size
        else:
            self.confusion_matrix.reset()

        if not self.max_err_output_sum:
            self.max_err_output_sum.reset(numpy.zeros(1, dtype))
        else:
            assert self.max_err_output_sum.size == 1

        self.init_vectors(self.confusion_matrix, self.n_err, self.max_idx,
                          self.labels, self.max_err_output_sum)

    def _gpu_init(self):
        dtype = self.output.dtype
        block_size = min(self.err_output.shape[0], 256)
        self.build_program(
            cache_file_name="%s_%d_%d" % (self.__class__.__name__,
                                          self.output.shape[0],
                                          self.output.sample_size),
            dtype=dtype, block_size=block_size,
            max_batch_size=self.err_output.shape[0],
            output_size=self.err_output.sample_size)
        self.assign_kernel("evaluate_softmax")
        self.set_args(self.output, self.max_idx, self.labels,
                      self.skip_args(2), self.n_err, self.confusion_matrix,
                      self.max_err_output_sum, self.err_output)
        return block_size

    def ocl_init(self):
        if self.testing:
            return
        block_size = self._gpu_init()
        self._global_size = [block_size]
        self._local_size = [block_size]
#.........这里部分代码省略.........
开发者ID:Samsung,项目名称:veles.znicz,代码行数:103,代码来源:evaluator.py

示例10: Forward

# 需要导入模块: from veles.memory import Array [as 别名]
# 或者: from veles.memory.Array import reset [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
开发者ID:vmarkovtsev,项目名称:veles.znicz,代码行数:94,代码来源:nn_units.py

示例11: GradientDescentBase

# 需要导入模块: from veles.memory import Array [as 别名]
# 或者: from veles.memory.Array import reset [as 别名]

#.........这里部分代码省略.........
        self.gradient_bias_with_moment = Array()

        # Sets to True when gradient changes
        self.gradient_changed = False

        # Gradient will be applied to weights immediately just after computing
        self.apply_gradient = kwargs.get("apply_gradient", not workflow.is_slave)

    @property
    def current_batch_size(self):
        batch_size = getattr(self, "batch_size", None)
        if batch_size is None:
            return self.err_output.mem.shape[0]
        return int(batch_size)

    def initialize(self, device, **kwargs):
        super(GradientDescentBase, self).initialize(device, **kwargs)

        if self.weights:
            assert len(self.weights.shape) == 2
            self.weights_shape = tuple(reversed(self.weights.shape)) if self.weights_transposed else self.weights.shape
        else:
            self.weights_shape = None

        self.learning_rate = kwargs.get("learning_rate", self.learning_rate)
        self.weights_decay = kwargs.get("weights_decay", self.weights_decay)
        self.gradient_moment = kwargs.get("gradient_moment", self.gradient_moment)
        self.learning_rate_bias = kwargs.get("learning_rate_bias", self.learning_rate_bias)
        self.weights_decay_bias = kwargs.get("weights_decay_bias", self.weights_decay_bias)
        self.gradient_moment_bias = kwargs.get("gradient_moment_bias", self.gradient_moment_bias)

        if self.weights:
            if not self.gradient_weights:
                self.gradient_weights.reset(numpy.zeros_like(self.weights.mem))
            else:
                assert self.gradient_weights.size == self.weights.size

        if self.weights and self.accumulate_gradient:
            if not self.accumulated_gradient_weights:
                self.accumulated_gradient_weights.reset(numpy.zeros_like(self.weights.mem))
            else:
                assert self.accumulated_gradient_weights.size == self.weights.size

        if self.weights and (self.gradient_moment or not self.is_standalone):
            if not self.gradient_weights_with_moment:
                self.gradient_weights_with_moment.reset(numpy.zeros_like(self.weights.mem))
            else:
                assert self.gradient_weights_with_moment.size == self.weights.size

        if self.include_bias and self.bias and (not self.gradient_bias or self.gradient_bias.size != self.bias.size):
            self.gradient_bias.reset(numpy.zeros_like(self.bias.mem))

        if (
            self.include_bias
            and self.bias
            and self.accumulate_gradient
            and (not self.accumulated_gradient_bias or self.accumulated_gradient_bias.size != self.bias.size)
        ):
            self.accumulated_gradient_bias.reset(numpy.zeros_like(self.bias.mem))

        if self.include_bias and self.bias and (self.gradient_moment_bias or not self.is_standalone):
            if not self.gradient_bias_with_moment:
                self.gradient_bias_with_moment.reset(numpy.zeros_like(self.bias.mem))
            else:
                assert self.gradient_bias_with_moment.size == self.bias.size
开发者ID:vmarkovtsev,项目名称:veles.znicz,代码行数:69,代码来源:nn_units.py

示例12: OffsetPooling

# 需要导入模块: from veles.memory import Array [as 别名]
# 或者: from veles.memory.Array import reset [as 别名]
class OffsetPooling(Pooling):
    """Pooling by offset forward propagation.

    Must be assigned before initialize():

    Updates after run():
        input_offset

    Creates within initialize():
        input_offset

    Attributes:
        input_offset: offsets in the input where elements are passed through.
    """

    MAPPING = set()
    hide_from_registry = True

    def __init__(self, workflow, **kwargs):
        super(OffsetPooling, self).__init__(workflow, **kwargs)
        self.input_offset = Array()
        self.demand("input")

    def initialize(self, device, **kwargs):
        super(OffsetPooling, self).initialize(device=device, **kwargs)

        if self._no_output:
            return
        if self.input_offset:
            assert self.input_offset.shape[1:] == self.output.shape[1:]
        if (not self.input_offset or
                self.input_offset.shape[0] != self.output.shape[0]):
            self.input_offset.reset(numpy.zeros(self.output.shape,
                                                dtype=numpy.int32))
        self.input_offset.initialize(self.device)

    def set_args(self, *args):
        super(OffsetPooling, self).set_args(self.input, self.output,
                                            self.input_offset, *args)

    def ocl_run(self):
        self.input_offset.unmap()
        super(OffsetPooling, self).ocl_run()

    def cuda_run(self):
        self.input_offset.unmap()
        super(OffsetPooling, self).cuda_run()

    def numpy_run(self):
        self.input_offset.map_invalidate()
        super(OffsetPooling, self).numpy_run()

    def numpy_run_cut(self, cut, coords):
        batch, y1, x1, ch, out_y, out_x = coords
        cut_index = self.numpy_run_cut_offset(
            cut, numpy.ravel_multi_index((batch, out_y, out_x, ch),
                                         self.output.shape))
        i, j = numpy.unravel_index(cut_index, cut.shape)
        idx = numpy.ravel_multi_index((batch, y1 + i, x1 + j, ch),
                                      self.input.shape)
        val = numpy.ravel(self.input.mem)[idx]
        self.input_offset.mem[batch, out_y, out_x, ch] = idx
        return val
开发者ID:Samsung,项目名称:veles.znicz,代码行数:65,代码来源:pooling.py

示例13: ZeroFiller

# 需要导入模块: from veles.memory import Array [as 别名]
# 或者: from veles.memory.Array import reset [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()
开发者ID:Samsung,项目名称:veles.znicz,代码行数:94,代码来源:weights_zerofilling.py

示例14: BatchWeights

# 需要导入模块: from veles.memory import Array [as 别名]
# 或者: from veles.memory.Array import reset [as 别名]
class BatchWeights(AcceleratedUnit, EmptyDeviceMethodsMixin):
    """Make weigths and biases from batch v and h.
    Must be assigned before initialize():
    * v
    * h
    * batch_size

    Updates after run():
    * hbias_batch
    * vbias_batch
    * W_batch

    Creates within initialize():
    * hbias_batch
    * vbias_batch
    * W_batch

    Attributes:
        v: input data  batch
        h: hidden states of input batch
        batch_size: size of batch
        hbias_batch: bias calculated from h
        vbias_batch: bias calculated from v
        W_batch: weigths calculated from batch v and h
    """
    def __init__(self, workflow, **kwargs):
        super(BatchWeights, self).__init__(workflow, **kwargs)
        self.vbias_batch = Array()
        self.hbias_batch = Array()
        self.weights_batch = Array()
        self.demand("v", "h", "batch_size")

    def initialize(self, device, **kwargs):
        super(BatchWeights, self).initialize(device=device, **kwargs)
        vbias_size = self.v.size // self.v.shape[0]
        hbias_size = self.h.size // self.h.shape[0]
        W_size = vbias_size * hbias_size
        if not self.hbias_batch:
            self.hbias_batch.reset(numpy.zeros((1, hbias_size),
                                               dtype=self.h.mem.dtype))
        else:
            assert self.hbias_batch.size == hbias_size
        if not self.vbias_batch:
            self.vbias_batch.reset(numpy.zeros((1, vbias_size),
                                               dtype=self.h.mem.dtype))
        else:
            assert self.vbias_batch.size == vbias_size
        if not self.weights_batch:
            self.weights_batch.reset(numpy.zeros((vbias_size, hbias_size),
                                                 dtype=self.h.mem.dtype))
        else:
            assert self.weights_batch.size == W_size
        self.init_vectors(self.weights_batch, self.vbias_batch,
                          self.hbias_batch, self.v, self.h)

    def run(self):
        self.v.map_read()
        self.h.map_read()
        for v in self.weights_batch, self.hbias_batch, self.vbias_batch:
            v.map_invalidate()
        self.weights_batch.mem[:] = numpy.dot(
            numpy.transpose(self.v.mem[0: self.batch_size, :]),
            self.h.mem[0: self.batch_size, :]) / \
            self.batch_size
        for bv in (self.vbias_batch, self.v), (self.hbias_batch, self.h):
            bv[0].mem[:] = (numpy.sum(bv[1].mem[:self.batch_size, :], 0) /
                            self.batch_size)
            bv[0].shape = (1, bv[0].size)
开发者ID:Samsung,项目名称:veles.znicz,代码行数:70,代码来源:rbm_units.py

示例15: Deconv

# 需要导入模块: from veles.memory import Array [as 别名]
# 或者: from veles.memory.Array import reset [as 别名]

#.........这里部分代码省略.........
        if (len(self.weights_shape) != 2 or
                self.weights_shape[0] != self.n_kernels or
                self.weights_shape[1] % (self.kx * self.ky) != 0):
            raise ValueError("Incorrectly shaped weights encountered")

        output_shape = tuple(self.output_shape_source.shape)
        if len(output_shape) != 4:
            raise ValueError("Incorrect output_shape_source shape")
        if output_shape[0] != self.input.shape[0]:
            raise ValueError(
                "output_shape_source.shape[0] != input.shape[0]")

        try:
            self.check_padding_is_safe(self.kx, self.ky, self.sliding)
        except ValueError as e:
            if not self.unsafe_padding:
                raise from_none(e)
            self.warning("The padding will be unsafe")
            self._create_hits(output_shape)

        padding = Deconv.compute_padding(
            output_shape[2], output_shape[1], self.kx, self.ky, self.sliding)
        if self.padding is None:  # pylint: disable=E0203
            self.padding = padding
        elif self.padding != padding:
            if not self.unsafe_padding:
                raise ValueError(
                    "Expected padding %s but got %s" % (padding, self.padding))
            self._create_hits(output_shape)

        if self.output:
            assert self.output.shape[1:] == output_shape[1:]
        if not self.output or self.output.shape[0] != output_shape[0]:
            self.output.reset(numpy.zeros(output_shape,
                                          dtype=self._dtype))

        self._output_shape = output_shape

        self._sy, self._sx, self._n_channels = self._output_shape[1:]
        self._kernel_size = self.kx * self.ky * self._n_channels

        self._kernel_app_per_image = self.input.sample_size // self.n_kernels
        self._kernel_app_total = (self._kernel_app_per_image *
                                  self.input.shape[0])

        self.init_vectors(self.input, self.weights, self.output, self.hits)

    def _create_hits(self, output_shape):
        if not self.hits:
            self.hits.reset(
                numpy.zeros(output_shape, dtype=numpy.int32))
        else:
            assert self.hits.size == int(numpy.prod(output_shape))

    def _gpu_init(self, blas_class):
        defines = {
            "USE_ATOMICS": 1,
            "WEIGHTS_TRANSPOSED": int(self.weights_transposed),
            "BATCH": self._output_shape[0],
            "SX": self._sx,
            "SY": self._sy,
            "N_CHANNELS": self._n_channels,
            "KX": self.kx,
            "KY": self.ky,
            "N_KERNELS": self.n_kernels,
            "PAD_LEFT": self.padding[0],
开发者ID:Samsung,项目名称:veles.znicz,代码行数:70,代码来源:deconv.py


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