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


Python Connector.add_dot方法代码示例

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


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

示例1: MeanPoolingBlock

# 需要导入模块: from quagga.connector import Connector [as 别名]
# 或者: from quagga.connector.Connector import add_dot [as 别名]
class MeanPoolingBlock(object):
    """
    MeanPoolingBlock pools matrix along the specified axis. Can handle matrix with
    varying number of columns. Number of rows is fixed.

    Parameters
    ----------
    matrix : Matrix (GpuMatrix or CpuMatrix)
    axis : int
    device_id : int
        Defines the device's id on which the computation will take place

    Returns
    -------
    """

    def __init__(self, matrix, axis=1, device_id=None):
        self.context = Context(device_id)
        self._ctype = matrix.c_dtype
        self._zero = self._ctype(0.0)
        if axis == 0:
            self._ones = Matrix.empty(1, matrix.nrows, matrix.dtype, device_id)
            self.output = Matrix.empty(1, matrix.ncols, matrix.dtype, device_id)
            self.alpha = self._ctype(1.0 / matrix.nrows)
        elif axis == 1:
            self._ones = Matrix.empty(matrix.ncols, 1, matrix.dtype, device_id)
            self.output = Matrix.empty(matrix.nrows, 1, matrix.dtype, device_id)
            self.alpha = None
        else:
            raise ValueError('Invalid axis!')
        self._ones.sync_fill(1.0)
        self.axis = axis

        if matrix.bpropagable:
            self.matrix, self.dL_dmatrix = matrix.register_usage(self.context, self.context)
            self.output = Connector(self.output, self.context, self.context)
        else:
            self.matrix = matrix.register_usage(self.context)
            self.output = Connector(self.output, self.context)

    def fprop(self):
        if self.axis == 0:
            self.output.ncols = self.matrix.ncols
            self.output.add_dot(self.context, self._ones, self.matrix, alpha=self.alpha, beta=self._zero)
        else:
            self._ones.nrows = self.matrix.ncols
            self.alpha = self._ctype(1.0 / self.matrix.ncols)
            self.output.add_dot(self.context, self.matrix, self._ones, alpha=self.alpha, beta=self._zero)
        self.output.fprop()

    def bprop(self):
        dL_doutput = self.output.backward_matrix
        dL_doutput.scale(self.context, self.alpha)
        if hasattr(self, 'dL_dmatrix'):
            self.dL_dmatrix.tile(self.context, self.axis, dL_doutput)
开发者ID:Sandy4321,项目名称:quagga,代码行数:57,代码来源:MeanPoolingBlock.py


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