Esempio n. 1
0
 def get_index_and_name(cls, prefix='Op'):
     name = _workspace.GetDummyName(prefix, domain='Operator')
     try:
         _, op_idx = name.split('_')
     except:
         name = _workspace.GetDummyName(prefix, domain='Operator')
         _, op_idx = name.split('_')
     return int(op_idx), name
Esempio n. 2
0
    def Convert(cls, value, dtype='float32'):
        """Convert the given value to a tensor.

        Parameters
        ----------
        value : number or Tensor
            The value to convert.
        dtype : str, optional, default='float32'
            The data type of the tensor.

        Returns
        -------
        Tensor
            The tensor converted with given value.

        """
        if isinstance(value, Tensor): return value
        else:
            if not isinstance(value, np.ndarray):
                try:
                    if dtype:
                        value = np.array(value, dtype=dtype)
                    else:
                        value = np.array(value)
                except:
                    raise TypeError('{} value can not be '
                                    'converted to Tensor.'.format(
                                        type(value).__name__))
            ref_tensor = Tensor.Ref(name=ws.GetDummyName('Constant',
                                                         domain='Tensor',
                                                         zero_based=False),
                                    shape=list(value.shape),
                                    dtype=str(value.dtype))
            ref_tensor.set_value(value)
            return ref_tensor
Esempio n. 3
0
 def name(self, value):
     if value != '':
         self._name = ws.GetDummyName(get_default_name_scope() +
                                      value if value else 'Tensor',
                                      domain='Tensor')
     else:
         # Set it manually for same cases
         self._name = value
Esempio n. 4
0
 def _from_constants(self, value):
     if not isinstance(value, np.ndarray):
         try:
             value = np.array(value,
                              dtype=self.dtype if self.dtype else 'float32')
         except:
             raise TypeError(
                 'Can not convert the value to Tensor or numpy array.')
     ref_tensor = Tensor.Ref(name=ws.GetDummyName('Constant',
                                                  domain='Tensor',
                                                  zero_based=False),
                             shape=list(value.shape),
                             dtype=str(value.dtype))
     ref_tensor.set_value(value)
     return ref_tensor
Esempio n. 5
0
 def _from_constant(self, value, name=None):
     if not isinstance(value, numpy.ndarray):
         try:
             value = numpy.array(
                 value, dtype=self.dtype if self.dtype else 'float32')
         except:
             raise TypeError(
                 'Can not convert the value to Tensor or numpy array.')
     return Tensor.Ref(
         name=_workspace.GetDummyName(
             basename=_scope.get_default_name_scope() +
             (name if name else 'Const'),
             suffix=':0',
             domain='Tensor'),
         shape=list(value.shape),
         dtype=str(value.dtype),
     ).set_value(value)
Esempio n. 6
0
    def __init__(
        self,
        initial_value=None,
        trainable=True,
        collections=None,
        validate_shape=True,
        name=None,
        dtype=None,
        regularizer=None,
        **kwargs
    ):
        super(Variable, self).__init__()

        if initial_value is None:
            raise ValueError('initial_value must be specified.')

        if collections is None:
            collections = [ops.GraphKeys.GLOBAL_VARIABLES]

        if not isinstance(collections, (list, tuple, set)):
            raise ValueError(
                'collections argument to Variable constructor must be a list, tuple, '
                    'or set. Got the type {}'.format(type(collections)))

        if trainable and ops.GraphKeys.TRAINABLE_VARIABLES not in collections:
            collections = list(collections) + [ops.GraphKeys.TRAINABLE_VARIABLES]

        if name is not None:
            # Get a known name from the name scope
            defined_name = _scope.get_default_name_scope() + name
        else:
            if 'name_from_variable_scope' in kwargs:
                # Has a name from the variable scope
                defined_name = kwargs['name_from_variable_scope']
            else:
                # Get a auto name from the name scope
                defined_name = _scope.get_default_name_scope() + 'Variable'

        # Set the name explicitly
        self.set_name(_workspace.GetDummyName(
            defined_name, suffix=':0', domain='Tensor'))

        # Initializer
        if isinstance(initial_value, _Tensor) and \
            len(initial_value.expressions) == 1:
                # From a initializing ops
                self.shape, self.dtype = \
                    initial_value.shape[:], \
                        initial_value.dtype
                init_expr = copy.deepcopy(initial_value.expressions)
                for k, v in init_expr.items():
                    init_expr[k].output[0] = self.name
                self.__init_expr__ = init_expr
        else:
            # From a const tensor
            if not isinstance(initial_value, _Tensor):
                initial_value = constant_op.constant(
                    initial_value, name=name, dtype=dtype)
            self.set_value(initial_value.get_value())
            self.shape, self.dtype = \
                initial_value.shape, \
                    initial_value.dtype

        # Regularizer
        self.__regularizer__ = regularizer

        # Registration
        self.Variable()

        if validate_shape:
            initial_value_shape = self.shape
            if initial_value_shape is None:
                raise ValueError('initial_value must have a shape specified.')

        ops.add_to_collections(collections, self)
Esempio n. 7
0
def constant(
    value,
    dtype=None,
    shape=None,
    name=None,
    verify_shape=False,
):
    if dtype is not None:
        if isinstance(value, numpy.ndarray):
            value = value.astype(dtype.as_numpy_dtype)
        else:
            value = numpy.array(value, dtype.as_numpy_dtype)
    else:
        if not isinstance(value, numpy.ndarray):
            value = numpy.array(value)
            # Discard the default float64
            if value.dtype == numpy.float64:
                value = value.astype(numpy.float32)

    # Determine the shape
    if shape is not None:
        if value.size == 1:
            # Case 1: Broadcast with scalar value
            scalar = value.flatten()[0]
            value = numpy.empty(shape, value.dtype)
            value.fill(scalar)
        else:
            # Case 2: Reshape directly
            if verify_shape:
                if shape is not None:
                    if len(shape) != len(value.shape):
                        raise RuntimeError(
                            'The constant was limited to {} dimensions, ' \
                            'while feed a value with {} dimensions.'
                            .format(len(shape), len(value.shape)))
                    for i in range(len(shape)):
                        if shape[i] is None: continue
                        if shape[i] != value.shape[i]:
                            raise RuntimeError(
                                'The shape of constant was limited as (' +
                                ','.join([str(dim) for dim in shape]) + '), ' +
                                'while feed a value with (' +
                                ','.join([str(dim)
                                          for dim in value.shape]) + ').')
            value = value.reshape(shape)

    # Get a available name
    defined_name = \
        _workspace.GetDummyName(
            basename=_scope.get_default_name_scope() +
                (name if name else 'Const'),
            suffix=':0',
            domain='Tensor',
        )

    # Feed into the workspace
    return _Tensor.Ref(
        name=defined_name,
        shape=list(value.shape),
        dtype=str(value.dtype),
    ).set_value(value)
Esempio n. 8
0
    def CreateOperator(cls,
                       op_type,
                       inputs,
                       num_outputs=1,
                       existing_outputs=None,
                       extra_inputs=None,
                       name=None,
                       **kwargs):
        """Construct a new Tensor with specific operator descriptor.

        Parameters
        ----------
        inputs : list of Tensor or Tensor
            The inputs for this operator.
        op_type : str
            The operator type.
        num_outputs : int, optional
            The number of outputs to return.
            Discarded if ``existing_outputs`` is not None.
        existing_outputs : sequence of Tensor, optional
            The existing outputs for this operator.
        extra_inputs : sequence of Tensor, optional
            The inputs that should be attached to solving targets, e.g. dynamic shape.
        name : str, optional
            The optional name to use. ``Op_xxx`` will be used automatically if it is None.

        Returns
        -------
        sequence of Tensor
            The outputs of this operator.

        Examples
        --------
        >>> import dragon as dg
        >>> a = Tensor().Variable()
        >>> b = Tensor().Variable()
        >>> c = Tensor.CreateOperator(inputs=[a, b], op_type='Add')

        >>> a = Tensor().Variable()
        >>> b = Tensor().Variable()
        >>> c = Tensor().Variable()
        >>> c = Tensor.CreateOperator(inputs=[a, b], op_type='Add', existing_outputs=c)

        >>> dynamic_shape = Tensor().Variable()
        >>> dg.workspace.FeedTensor(dynamic_shape, [1, 2, 3, 4])
        >>> a = dg.Fill(shape=dynamic_shape, value=5.0)
        >>> print dg.function(outputs=a)
        >>> [[ 5.  5.  5.]
             [ 5.  5.  5.]]

        """
        expressions = dict()

        # 1. Collect inputs
        if not isinstance(inputs, list): inputs = [inputs]
        for input in inputs:
            for op_idx, expr in input.expressions.items():
                if not op_idx in expressions:
                    expressions[op_idx] = expr

        if extra_inputs is not None:
            if not isinstance(extra_inputs, list):
                extra_inputs = [extra_inputs]
            for input in extra_inputs:
                for op_idx, expr in input.expressions.items():
                    if not op_idx in expressions:
                        expressions[op_idx] = expr

        # 2. Generate outputs
        outputs = []
        if existing_outputs is None:
            name_scope = get_default_name_scope()
            for idx in range(num_outputs):
                outputs.append(
                    Tensor.Ref(
                        ws.GetDummyName(name_scope +
                                        (name if name else op_type),
                                        suffix=':{}'.format(idx),
                                        domain='Tensor')))
        else:
            if not isinstance(existing_outputs, list):
                existing_outputs = [existing_outputs]
            outputs = existing_outputs
            num_outputs = len(outputs)
            if not isinstance(outputs, list): outputs = [outputs]

        # 3. Construct OperatorDef
        inputs_name = [input.name for input in inputs]
        outputs_name = [output.name for output in outputs]
        op_idx, op_name = OperatorHelper.get_index_and_name()

        device_option = GetDefaultDeviceOption()

        op_def = MakeOperatorDef(op_type,
                                 inputs_name,
                                 outputs_name,
                                 op_name,
                                 device_option=device_option,
                                 **kwargs)

        expressions[op_idx] = op_def

        # 4. Add outputs
        for idx, output in enumerate(outputs):
            # Deliver expressions
            output.expressions = expressions
            # Deliver extra targets
            for input in inputs:
                output.extra_targets = \
                    output.extra_targets.union(input.extra_targets)
            if extra_inputs is not None:
                for input in extra_inputs:
                    output.extra_targets.add(input.name)

        # 5. Refine the shape and data type
        outputs = OperatorHelper.apply(op_type,
                                       arguments=kwargs,
                                       inputs=inputs,
                                       outputs=outputs)

        # 6. Returns
        if num_outputs > 1: return outputs
        elif num_outputs == 1: return outputs[0]
        else: return None