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


Python Connector.assign_dot方法代码示例

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


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

示例1: DotBlock

# 需要导入模块: from quagga.connector import Connector [as 别名]
# 或者: from quagga.connector.Connector import assign_dot [as 别名]
class DotBlock(object):
    """
    Computes dot product (scalar product) between matrices ``W`` and ``x``, also
    adds bias ``b``.

    Parameters
    ----------
    W : Matrix (GpuMatrix or CpuMatrix)
        Weigh matrix
    b : Matrix (GpuMatrix or CpuMatrix)
        Bias matrix (one dimesion equals 1, can be view as a vector)
    x : Matrix (GpuMatrix or CpuMatrix)
        Block's input
    device_id : int
        Defines the device's id on which the computation will take place
    """
    def __init__(self, W, b, x, device_id=None):
        self.f_context = Context(device_id)
        device_id = self.f_context.device_id

        if W.bpropagable:
            self.W, self.dL_dW = W.register_usage(device_id, device_id)
        else:
            self.W = W.register_usage(device_id)
        if b:
            if b.bpropagable:
                self.b, self.dL_db = b.register_usage(device_id, device_id)
                self.ones = Matrix.empty(x.nrows, 1, self.b.dtype, device_id)
                self.ones.sync_fill(1.0)
            else:
                self.b = b.register_usage(device_id)
        if x.bpropagable:
            self.x, self.dL_dx = x.register_usage(device_id, device_id)
        else:
            self.x = x.register_usage(device_id)

        output = Matrix.empty(x.nrows, self.W.ncols, device_id=device_id)
        self.learning = hasattr(self, 'dL_dW') or hasattr(self, 'dL_db') or \
                        hasattr(self, 'dL_dx')
        if self.learning:
            self.b_context = Context(device_id)
            self.output = Connector(output, device_id)
        else:
            self.output = Connector(output)

    def fprop(self):
        self.output.assign_dot(self.f_context, self.x, self.W)
        if hasattr(self, 'b'):
            self.output.add(self.f_context, self.b)
        self.output.fprop()

    def bprop(self):
        if not self.learning:
            return
        dL_doutput = self.output.backward_matrix
        # dL/dW = x.T * dL_doutput
        if hasattr(self, 'dL_dW'):
            self.dL_dW.add_dot(self.b_context, self.x, dL_doutput, 'T')
        # TODO(sergii): replace this modification with reduction kernel along axis=0
        # dL/db = 1.T * dL_doutput
        if hasattr(self, 'dL_db'):
            self.dL_db.add_dot(self.b_context, self.ones, dL_doutput, 'T')
        # dL/dx = dL_doutput * W.T
        if hasattr(self, 'dL_dx'):
            self.dL_dx.add_dot(self.b_context, dL_doutput, self.W, 'N', 'T')
开发者ID:Sandy4321,项目名称:quagga,代码行数:67,代码来源:DotBlock.py


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