コード例 #1
0
ファイル: net.py プロジェクト: zhangkaij/Dragon
    def function(self):
        """ the CC Graph will create only once get this attr """
        if hasattr(self, '_function'): return self._function

        for cost in self._costs:
            for wrt in self._wrts:
                T.grad(cost, wrt)

        self._function = \
            theano.function(outputs=[self._blobs[name]['data']
                        for name in self._net_outputs], swaps=self._swap_blobs)

        if hasattr(self, '_model'): ws.Restore(self._model, format=1)
        return self._function
コード例 #2
0
ファイル: gradients.py プロジェクト: zhangkaij/Dragon
def gradients(loss, var_list=None):
    if var_list is None:
        from dragon.vm.tensorflow.core.variables import TRAINABLE_VARIABLES
        global TRAINABLE_VARIABLES
        var_list = TRAINABLE_VARIABLES.values()
    grads = T.grad(loss, var_list)
    grads_and_vars = list(zip(grads, var_list))
    return grads_and_vars
コード例 #3
0
ファイル: optimizer.py プロジェクト: neopenx/Dragon
    def compute_gradients(self, loss, var_list=None, **kwargs):
        if var_list is None:
            var_list = variables.trainable_variables() + \
                ops.get_collection(ops.GraphKeys.TRAINABLE_RESOURCE_VARIABLES)

        self.loss = loss
        grads = T.grad(loss, var_list)
        grads_and_vars = list(zip(grads, var_list))
        return grads_and_vars
コード例 #4
0
ファイル: optimizer.py プロジェクト: zfxu/Dragon
    def compute_gradients(self, loss, var_list=None, **kwargs):
        if var_list is None:
            var_list = variables.trainable_variables() + \
                ops.get_collection(ops.GraphKeys.TRAINABLE_RESOURCE_VARIABLES)

        self.loss = loss
        grads = T.grad(loss, var_list)
        grads_and_vars = list(zip(grads, var_list))
        return grads_and_vars
コード例 #5
0
    def function(self, givens=None):
        """Returns the function the ``ForwardBackward``.

        Parameters
        ----------
        givens : None or dict
            The givens to replace existing blobs.

        Returns
        -------
        lambda
            The function.

        See Also
        --------
        `theano.function(*args, **kwargs)`_ - How to make a graph. [**Theano Style**]

        References
        ----------
        The implementation of `ForwardBackward(net.cpp, L85)`_.

        """
        if hasattr(self, '_function'): return self._function

        for cost in self._costs:
            for wrt in self._wrts:
                T.grad(cost, wrt)

        if givens is not None:
            if not isinstance(givens, dict):
                raise TypeError('The givens should be a dict.')
            for k, v in givens.items():
                if not isinstance(v, Tensor):
                    raise ValueError('The value of givens should be a Tensor.')
                self._swap_tensors[k] = v

        self._function = \
            theano.function(outputs=[self._blobs[name]['data']
                        for name in self._net_outputs], givens=self._swap_tensors)

        if hasattr(self, '_model'): ws.Restore(self._model, format='caffe')
        return self._function
コード例 #6
0
ファイル: net.py プロジェクト: neopenx/Dragon
    def function(self, givens=None):
        """Returns the function the ``ForwardBackward``.

        Parameters
        ----------
        givens : None or dict
            The givens to replace existing blobs.

        Returns
        -------
        lambda
            The function.

        See Also
        --------
        `theano.function(*args, **kwargs)`_ - How to make a graph. [**Theano Style**]

        References
        ----------
        The implementation of `ForwardBackward(net.cpp, L85)`_.

        """
        if hasattr(self, '_function'): return self._function

        for cost in self._costs:
            for wrt in self._wrts:
                T.grad(cost, wrt)

        if givens is not None:
            if not isinstance(givens, dict):
                raise TypeError('The givens should be a dict.')
            for k, v in givens.items():
                if not isinstance(v, Tensor):
                    raise ValueError('The value of givens should be a Tensor.')
                self._swap_tensors[k] = v

        self._function = \
            theano.function(outputs=[self._blobs[name]['data']
                        for name in self._net_outputs], givens=self._swap_tensors)

        if hasattr(self, '_model'): ws.Restore(self._model, format='caffe')
        return self._function
コード例 #7
0
    def _create_graph(self):
        self.x = Tensor(shape=[None, self.img_channels, self.img_height, self.img_width]).Variable()
        self.y_r = Tensor(shape=[None], name='Yr').Variable()

        # As implemented in A3C paper
        self.n1 = ops.Relu(ops.Conv2D([self.x] + self.weight_bias(), kernel_size=8, stride=4, num_output=16))
        self.n2 = ops.Relu(ops.Conv2D([self.n1] + self.weight_bias(), kernel_size=4, stride=2, num_output=32))

        self.action_index = Tensor(shape=[None, self.num_actions]).Variable()

        self.d1 = ops.Relu(ops.InnerProduct([self.n2] + self.weight_bias(), num_output=256))

        self.logits_v = ops.InnerProduct([self.d1] + self.weight_bias(), num_output=1)
        self.cost_v = ops.L2Loss([self.y_r, self.logits_v])

        self.logits_p = ops.InnerProduct([self.d1] + self.weight_bias(), num_output=self.num_actions)

        if Config.USE_LOG_SOFTMAX: raise NotImplementedError()
        else:
            self.softmax_p = ops.Softmax(self.logits_p)
            self.selected_action_prob = ops.Sum(self.softmax_p * self.action_index, axis=1)
            self.cost_p_1 = ops.Log(ops.Clip(self.selected_action_prob, self.log_epsilon, None)) * \
                            (self.y_r - ops.StopGradient(self.logits_v))
            self.cost_p_2 = ops.Sum(ops.Log(ops.Clip(self.softmax_p, self.log_epsilon, None)) *
                                      self.softmax_p, axis=1) * (-self.beta)
        self.cost_p_1_agg = ops.Sum(self.cost_p_1)
        self.cost_p_2_agg = ops.Sum(self.cost_p_2)
        self.cost_p = -(self.cost_p_1_agg + self.cost_p_2_agg)
        self.cost_all = self.cost_p + self.cost_v
        
        if Config.DUAL_RMSPROP: raise NotImplementedError()
        else:
            if Config.USE_GRAD_CLIP:
                self.opt = updaters.RMSPropUpdater(decay=Config.RMSPROP_DECAY,
                                                   eps=Config.RMSPROP_EPSILON,
                                                   clip_gradient=Config.GRAD_CLIP_NORM)
            else:
                self.opt = updaters.RMSPropUpdater(decay=Config.RMSPROP_DECAY,
                                                   eps=Config.RMSPROP_EPSILON)

        grads = T.grad(self.cost_all, self.network_params)
        for p, g in zip(self.network_params, grads):
            self.opt.append((p, g), lr_mult=1.0)
コード例 #8
0
ファイル: gradients_impl.py プロジェクト: zfxu/Dragon
def gradients(ys, xs, **kwargs):
    """Compute the gradients for variables with respect to the cost.

    Parameters
    ----------
    ys : Tensor or list of Tensor
        The tensor(s) to be differentiated.
    xs : Tensor or list of Tensor
        The tensor(s to be used for differentiation.

    Returns
    -------
    Tensor or list of Tensor
        The gradients of variables.

    """
    if not isinstance(ys, list):
        ys = [ys]
    for y in ys:
        dxs = T.grad(y, xs)
    return dxs
コード例 #9
0
ファイル: vec_mult.py プロジェクト: neopenx/Dragon
        """
        x1 = ws.FetchTensor(inputs[0])
        x2 = ws.FetchTensor(inputs[1])
        dy = ws.FetchTensor(inputs[-1])
        dx1 = dy * x2
        dx2 = dy * x1
        ws.FeedTensor(outputs[0], dx1)
        ws.FeedTensor(outputs[1], dx2)


if __name__ == '__main__':

    # def
    x1 = Tensor('x1').Variable()
    x2 = Tensor('x2').Variable()
    y = ops.Template([x1, x2], module=__name__, op='VecMultOp', nout=1)
    dx1 = T.grad(y, x1)
    dx2 = T.grad(y, x2)
    foo = theano.function(outputs=y)

    # feed
    ws.FeedTensor(x1, np.ones((5, 3), dtype=np.float32))
    ws.FeedTensor(x2, np.ones((5, 3), dtype=np.float32) * 5.0)

    # run
    foo()

    # fetch
    print('y \n-------------- \n', y.get_value(), '\n')
    print('dx1 \n-------------- \n', dx1.get_value(), '\n')
    print('dx2 \n-------------- \n', dx2.get_value(), '\n')
コード例 #10
0
ファイル: vec_mult.py プロジェクト: Junotja/Dragon
        x1 = ws.FetchTensor(inputs[0])
        x2 = ws.FetchTensor(inputs[1])
        dy = ws.FetchTensor(inputs[-1])
        dx1 = dy * x2
        dx2 = dy * x1
        ws.FeedTensor(outputs[0], dx1)
        ws.FeedTensor(outputs[1], dx2)


if __name__ == '__main__':

    # def
    x1 = Tensor('x1').Variable()
    x2 = Tensor('x2').Variable()
    y = ops.Template([x1, x2], module=__name__, op='VecMult', nout=1)
    dx1 = T.grad(y, x1)
    dx2 = T.grad(y, x2)
    foo = theano.function(outputs=y)

    # feed
    ws.FeedTensor(x1, np.ones((5, 3), dtype=np.float32))
    ws.FeedTensor(x2, np.ones((5, 3), dtype=np.float32) * 5.0)

    # run
    foo()

    # fetch
    print('y \n-------------- \n', y.get_value(), '\n')
    print('dx1 \n-------------- \n', dx1.get_value(), '\n')
    print('dx2 \n-------------- \n', dx2.get_value(), '\n')