How to use the dragon.core.tensor.Tensor function in dragon

To help you get started, we’ve selected a few dragon examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github seetaresearch / Dragon / Dragon / python / dragon / vm / tensorflow / client / session.py View on Github external
def _run(self, fetches, feed_dict):
        if self._closed:
            raise RuntimeError('Attempted to use a closed Session.')

        # Unpack opts and tensors
        tensors, optimizers = [], []

        for e in fetches:
            if isinstance(e, Optimizer): optimizers.append(e)
            elif isinstance(e, VariablesInitializer): tensors.extend(e.var_list)
            elif isinstance(e, _Tensor): tensors.append(e)

        # Find minimum solving targets
        targets = set()
        for e in tensors: targets.add(e)
        for optimizer in optimizers:
            for t in optimizer._targets: targets.add(t)

        targets = list(targets)
        flow_key = tuple(e.name for e in targets)

        # Exist this data flow before?
        flow = _DataFlow.try_get(id(self._graph), flow_key)

        # Run by feeding
        if feed_dict is not None:
            # Check the feed dict
github seetaresearch / Dragon / Dragon / python / dragon / vm / theano / compile / function.py View on Github external
if you want to feed the inputs as ``workspace.FeedTensor(self.inputs)``

        Parameters
        ----------
        meta_graph : GraphDef
            The definition of graph.
        explicit_inputs : boolean
            Whether to enforce feeding on executing.

        Returns
        -------
        Function
            The self.

        """
        self.inputs = [_Tensor(input).Variable() for input in graph_def.input]
        self.outputs = [_Tensor(output) for output in graph_def.output]

        _inject_device(graph_def)
        _inject_optimization(graph_def)
        _inject_phase(graph_def, self.outputs)

        # Store for future development
        self.meta_graph = graph_def

        # Call c api to create graph
        self.graph_name = _workspace.CreateGraph(graph_def)

        # Bind a lambda callback to run this graph
        callback_inputs = self.inputs if explicit_inputs else []
        self.callback = lambda *args, **kwargs: \
            _workspace.RunGraph(
github seetaresearch / Dragon / Dragon / python / dragon / operators / rnn / rnn_wrapper.py View on Github external
Parameters
        ----------
        x : Tensor
            The input tensor.
        hx : Tensor, optional
            The h(0) state.
        cx : Tensor, optional
            The c(0) state.
        required_hidden : bool, optional
            Return ``y`` and ``hidden`` if ``True``.
        required_hidden : bool, optional
            Return ``y``, ``hidden``, ``cell`` if ``True``.

        """
        if hx and not isinstance(hx, _Tensor):
            raise TypeError('Excepted hx as a Tensor, got {}.'.format(type(hx)))
        if cx and not isinstance(cx, _Tensor):
            raise TypeError('Excepted cx as a Tensor, got {}.'.format(type(cx)))

        if not self._init_params: self._reset_params()

        arguments = {
            'op_type': 'Recurrent',
            'inputs': [x, self.weights] +
                          ([hx] if hx else []) +
                              ([cx] if cx else []),
            'hidden_size': self.hidden_size,
            'num_layers': self.num_layers,
            'bidirectional': self.bidirectional,
            'rnn_mode': self.mode,
            'rnn_input_mode': 'linear',
github seetaresearch / Dragon / Dragon / python / dragon / vm / tensorflow / ops / constant_op.py View on Github external
def constant(value, dtype=None, shape=None, name=None):
    if dtype == None: dtype = dtypes.float32
    if isinstance(value, np.ndarray): feed = value.astype(dtype)
    elif isinstance(value, list): feed = np.array(value, dtype)
    else: feed = np.array([value], dtype)
    if shape is not None:
      if feed.size == 1:
        c = feed[0]
        feed = np.zeros(shape, dtype)
        feed.fill(c)
      else: feed = feed.reshape(shape)
    tensor = Tensor(name)
    tensor.shape = list(feed.shape)
    ws.FeedTensor(tensor, feed)
    return tensor
github seetaresearch / Dragon / Dragon / python / dragon / core / tensor_utils.py View on Github external
def _try_get_tensor(name=None):
    """Try to create or get a tensor"""
    if name is None or name == '': return _Tensor()
    else: return _Tensor.Ref(name)
github seetaresearch / Dragon / Dragon / python / dragon / operators / __init__.py View on Github external
def Verify(inputs, min_num, max_num):
                if min_num == max_num and min_num == 0: return
                # It is a special case
                if min_num != max_num and min_num == 1:
                    if isinstance(inputs, Tensor):
                        inputs = [inputs]
                # Type checking
                if min_num == max_num and min_num == 1:
                    if not isinstance(inputs, Tensor):
                        raise ValueError(
                            'Excepted a Tensor as inputs, '
                                'got {}.'.format(type(inputs).__name__))
                else:
                    if not isinstance(inputs, list) or \
                            not all([isinstance(input, Tensor) for input in inputs]):
                        print(inputs)
                        raise ValueError('The inputs should be a list of Tensor.')
                # Range checking
                if not isinstance(inputs, list): inputs = [inputs]
                if len(inputs) < min_num or len(inputs) > max_num:
                    raise ValueError(
                        'The inputs size {}, is not in range: '
                            '[min={}, max={}].'.format(
                                len(inputs), min_num, max_num))
            @functools.wraps(op_func)
github seetaresearch / Dragon / Dragon / python / dragon / vm / caffe / layer.py View on Github external
def AddBlob(self, value=None, filler=None, enforce_no_grad=None):
        # Use a a fixed name in the current workspace
        # Note that a non-empty tensor scope will make it
        # impossible to load/save caffe models. You should use
        # a new workspace instead of the terrible name scope
        scoped_name = _scope.get_default_name_scope() + self._name
        param_name = scoped_name + '/param:{}'.format(len(self._blobs))

        # Set the name explicitly
        variable = _Tensor.Ref(param_name)
        variable_grad = _Tensor.Ref(param_name + '_grad')

        if filler is not None:
            variable.Fill(**filler)
        else:
            # Register a constant filler by default
            value = value if value else 0
            variable.Constant(value=value)

        # Determine whether we have disabled the gradients explicitly
        if enforce_no_grad is not None:
            variable_grad = None

        # Append to the blobs
        self._blobs.append({'data': variable, 'diff': variable_grad})
github seetaresearch / Dragon / Dragon / python / dragon / vm / tensorflow / training / saver.py View on Github external
def save(self,
             sess,
             save_path,
             global_step=None):
        from ..core.variables import VARIABLES
        global VARIABLES
        var_list = VARIABLES if self.var_list is None else self.var_list
        filename = save_path
        if global_step is not None:
            if isinstance(global_step, Tensor):
                __ndarray__global_step = ws.FetchTensor(global_step)
                if __ndarray__global_step.size != 1:
                    raise ValueError('global step must be a scalar of length 1.')
                filename += '-' + str(__ndarray__global_step.flatten()[0])
        ws.Snapshot(var_list.values(), filename=filename, suffix='')