Exemplo n.º 1
0
    def test_minibatch(self, arguments, device=None):
        '''
        Test the model on the specified batch of samples using the evaluation
        Function specified during construction of the Trainer.

        Args:
            arguments: maps variables to their
             input data. The interpretation depends on the input type:

               * `dict`: keys are input variable or names, and values are the input data.
                 See :meth:`~cntk.ops.functions.Function.forward` for details on passing input data.

               * any other type: if node has a unique input, ``arguments`` is mapped to this input.
                 For nodes with more than one input, only `dict` is allowed.

             In both cases, every sample in the data will be interpreted
             as a new sequence. To mark samples as continuations of the
             previous sequence, specify ``arguments`` as `tuple`: the
             first element will be used as ``arguments``, and the second one will
             be used as a list of bools, denoting whether a sequence is a new
             one (`True`) or a continuation of the previous one (`False`).
             Data should be either NumPy arrays or a
             :class:`~cntk.io.MinibatchData` instance.
            device (:class:`~cntk.device.DeviceDescriptor`): the device descriptor that
             contains the type and id of the device on which the computation is
             to be performed.

        Note:
             See :meth:`~cntk.ops.functions.Function.forward` for examples on
             passing input data.

        Returns:
            `float`: the average evaluation criterion value per sample for the
            tested minibatch.
        '''
        if not device:
            device = use_default_device()

        # pass all args of all parts (model, loss, eval)
        all_args = set(self.loss_function.arguments)
        if self.model:
            all_args |= set(self.model.arguments)
        if self.evaluation_function:
            all_args |= set(self.evaluation_function.arguments)
        arguments = sanitize_var_map(tuple(all_args), arguments)

        return super(Trainer, self).test_minibatch(arguments, device)
Exemplo n.º 2
0
    def test_minibatch(self, arguments, device=None):
        '''
        Test the model on the specified batch of samples using the evaluation
        Function specified during construction of the Trainer.

        Args:
            arguments: maps variables to their
             input data. The interpretation depends on the input type:

               * `dict`: keys are input variable or names, and values are the input data.
                 See :meth:`~cntk.ops.functions.Function.forward` for details on passing input data.

               * any other type: if node has a unique input, ``arguments`` is mapped to this input.
                 For nodes with more than one input, only `dict` is allowed.

             In both cases, every sample in the data will be interpreted
             as a new sequence. To mark samples as continuations of the
             previous sequence, specify ``arguments`` as `tuple`: the
             first element will be used as ``arguments``, and the second one will
             be used as a list of bools, denoting whether a sequence is a new
             one (`True`) or a continuation of the previous one (`False`).
             Data should be either NumPy arrays or a
             :class:`~cntk.io.MinibatchData` instance.
            device (:class:`~cntk.device.DeviceDescriptor`): the device descriptor that
             contains the type and id of the device on which the computation is
             to be performed.

        Note:
             See :meth:`~cntk.ops.functions.Function.forward` for examples on
             passing input data.

        Returns:
            `float`: the average evaluation criterion value per sample for the
            tested minibatch.
        '''
        if not device:
            device = use_default_device()

        # pass all args of all parts (model, loss, eval)
        all_args = set(self.loss_function.arguments)
        if self.model:
            all_args |= set(self.model.arguments)
        if self.evaluation_function:
            all_args |= set(self.evaluation_function.arguments)
        arguments = sanitize_var_map(tuple(all_args), arguments)

        return super(Trainer, self).test_minibatch(arguments, device)
Exemplo n.º 3
0
    def train_minibatch(self, arguments, outputs=None, device=None, is_sweep_end=None):
        '''
        Optimize model parameters using the specified 'arguments' minibatch of training samples.

        Args:
            arguments: maps variables to their input data. Empty map signifies
             end of local training data.
             The interpretation depends on the input type:

               * `dict`: keys are input variable or names, and values are the input data.

               * any other type: if node has a unique input, ``arguments`` is mapped to this input.
                 For nodes with more than one input, only `dict` is allowed.

             In both cases, every sample in the data will be interpreted
             as a new sequence. To mark samples as continuations of the
             previous sequence, specify ``arguments`` as `tuple`: the
             first element will be used as ``arguments``, and the second one will
             be used as a list of bools, denoting whether a sequence is a new
             one (`True`) or a continuation of the previous one (`False`).
             Data should be either NumPy arrays or a
             :class:`~cntk.io.MinibatchData` instance.
            outputs (iterable): outputs to fetch values for.
            device (:class:`~cntk.device.DeviceDescriptor`): the device descriptor that
             contains the type and id of the device on which the computation is
             to be performed.
            is_sweep_end (bool): indicate whether this minibatch is at the end of a sweep (of an eopoch), default to None.
            This is used in combination with `arguments` being fed with numpy arrays data; when the data is from
             :class:`~cntk.io.MinibatchData`, `is_sweep_end` is provided by :class:`~cntk.io.MinibatchData` so there is
             no need to specify it manually.

        Note:
             See :meth:`~cntk.ops.functions.Function.forward` for examples on
             passing input data.

        Returns:
            `bool` or `tuple`:
            If ``outputs`` have not been provided, the returned value is `True`
            if updates have been performed, `False` if all parameter learners
            indicate end of learning (through their update). Otherwise, the
            return value is a tuple of the that `bool` and a dictionary that
            maps the variables in `outputs` to their respective NumPy arrays.
        '''
        if not device:
            device = use_default_device()

        if arguments: # arguments must feed all inputs (model, loss, eval)
            all_args = set(self.loss_function.arguments)
            if self.model:
                all_args |= set(self.model.arguments)
            if self.evaluation_function:
                all_args |= set(self.evaluation_function.arguments)
            arguments = sanitize_var_map(tuple(all_args), arguments,
                extract_values_from_minibatch_data = False, device=device)

        contains_minibatch_data = False
        if (len(arguments) > 0):
            value = next(iter(arguments.values()))
            contains_minibatch_data = isinstance(value, MinibatchData)

        if contains_minibatch_data and is_sweep_end is not None:
            raise ValueError("is_sweep_end is ignored by Trainer::train_minibatch when it is fed with MinibatchData!")

        if not contains_minibatch_data and is_sweep_end is None:
            #for legacy code when is_sweep_end is not specified.
            is_sweep_end = False

        if outputs:
            output_map = {v: None for v in outputs}

            if contains_minibatch_data:
                updated = super(Trainer, self).train_minibatch_overload_for_minibatchdata(
                    arguments, output_map, device)
            else:
                updated = super(Trainer, self).train_minibatch(arguments, is_sweep_end,
                    output_map, device)

            for k, v in output_map.items():
                output_map[k] = _value_as_sequence_or_array(v, k)

            return updated, output_map
        else:

            if contains_minibatch_data:
                updated = super(Trainer, self).train_minibatch_overload_for_minibatchdata(
                    arguments, device)
            else:
                updated = super(Trainer, self).train_minibatch(arguments, is_sweep_end,
                    device)

        return updated
Exemplo n.º 4
0
    def train_minibatch(self, arguments, outputs=None, device=None):
        '''
        Optimize model parameters using the specified 'arguments' minibatch of training samples.

        Args:
            arguments: maps variables to their input data. Empty map signifies
             end of local training data.
             The interpretation depends on the input type:

               * `dict`: keys are input variable or names, and values are the input data.

               * any other type: if node has a unique input, ``arguments`` is mapped to this input.
                 For nodes with more than one input, only `dict` is allowed.

             In both cases, every sample in the data will be interpreted
             as a new sequence. To mark samples as continuations of the
             previous sequence, specify ``arguments`` as `tuple`: the
             first element will be used as ``arguments``, and the second one will
             be used as a list of bools, denoting whether a sequence is a new
             one (`True`) or a continuation of the previous one (`False`).
             Data should be either NumPy arrays or a
             :class:`~cntk.io.MinibatchData` instance.
            outputs (iterable): outputs to fetch values for.
            device (:class:`~cntk.device.DeviceDescriptor`): the device descriptor that
             contains the type and id of the device on which the computation is
             to be performed.

        Note:
             See :meth:`~cntk.ops.functions.Function.forward` for examples on
             passing input data.

        Returns:
            `bool` or `tuple`:
            If ``outputs`` have not been provided, the returned value is `True`
            if updates have been performed, `False` if all parameter learners
            indicate end of learning (through their update). Otherwise, the
            return value is a tuple of the that `bool` and a dictionary that
            maps the variables in `outputs` to their respective NumPy arrays.
        '''
        if not device:
            device = use_default_device()

        if arguments:  # arguments must feed all inputs (model, loss, eval)
            all_args = set(self.loss_function.arguments)
            if self.model:
                all_args |= set(self.model.arguments)
            if self.evaluation_function:
                all_args |= set(self.evaluation_function.arguments)
            arguments = sanitize_var_map(
                tuple(all_args),
                arguments,
                extract_values_from_minibatch_data=False,
                device=device)

        contains_minibatch_data = False
        if (len(arguments) > 0):
            value = next(iter(arguments.values()))
            contains_minibatch_data = isinstance(value, MinibatchData)

        if outputs:
            output_map = {v: None for v in outputs}

            if contains_minibatch_data:
                updated = super(
                    Trainer, self).train_minibatch_overload_for_minibatchdata(
                        arguments, output_map, device)
            else:
                updated = super(Trainer,
                                self).train_minibatch(arguments, output_map,
                                                      device)

            for k, v in output_map.items():
                output_map[k] = _value_as_sequence_or_array(v, k)

            return updated, output_map
        else:

            if contains_minibatch_data:
                updated = super(
                    Trainer, self).train_minibatch_overload_for_minibatchdata(
                        arguments, device)
            else:
                updated = super(Trainer,
                                self).train_minibatch(arguments, device)

        return updated