示例#1
0
    def posPhaseGrad(self, data):
        hidden = self.hiddenFromVisible(data)
        Blas.mulMatrixOnMatrix(data,
                               hidden,
                               out=self.vars["W"].grad,
                               transpA=True)

        if self.useBias:
            Blas.sumOnMatrix(data, out=self.vars["b"].grad)
            Blas.sumOnMatrix(hidden, out=self.vars["c"].grad)

        return hidden
示例#2
0
    def accGradParams(self, grad, scale=1.0, momentum=0.0):
        if self.wmode == GroupMode.full:
            if self.useW:
                A, B = (grad, self.inData) if self.transpW else (self.inData,
                                                                 grad)

                BlasGroup.mulTensorBatch(A,
                                         B,
                                         out=self.vars["W"].grad,
                                         formatA=self.format,
                                         formatB=self.format,
                                         formatOut="gbp",
                                         transpA=True,
                                         alpha=scale,
                                         beta=momentum)

            if self.useBias:
                BlasGroup.sumOnTensorGroup(grad,
                                           out=self.vars["b"].grad,
                                           formatT=self.format)

        else:
            if self.useW:
                A, B = (grad, self.inData) if self.transpW else (self.inData,
                                                                 grad)

                wgrad = BlasGroup.mulTensorBatch(A,
                                                 B,
                                                 transpA=True,
                                                 formatA=self.format,
                                                 formatB=self.format,
                                                 formatOut="gbp",
                                                 alpha=scale,
                                                 beta=momentum)

                Blas.sumOnMatrix(wgrad.reshape(wgrad.shape[0], -1),
                                 out=self.vars["W"].grad.ravel())

            if self.useBias:
                Blas.sumOnMatrix(grad.reshape(grad.shape[0] * grad.shape[1],
                                              grad.shape[2]),
                                 out=self.vars["b"].grad[0])
示例#3
0
    def accGradParams(self, grad, scale=1.0, momentum=0.0):
        if not self.transpose:
            Blas.mulMatrixOnMatrix(self.inData,
                                   grad,
                                   out=self.vars["W"].grad,
                                   transpA=True,
                                   alpha=scale,
                                   beta=momentum)
        else:
            Blas.mulMatrixOnMatrix(grad,
                                   self.inData,
                                   out=self.vars["W"].grad,
                                   transpA=True,
                                   alpha=scale,
                                   beta=momentum)

        if self.useBias:
            Blas.sumOnMatrix(grad,
                             out=self.vars["b"].grad,
                             alpha=scale,
                             beta=momentum)
示例#4
0
    def negPhaseGrad(self, hidden):
        visible = self.visibleFromHidden(hidden)
        hidden = self.hiddenFromVisible(visible)

        Blas.mulMatrixOnMatrix(visible,
                               hidden,
                               out=self.vars["W"].grad,
                               transpA=True,
                               alpha=-1.0,
                               beta=1.0)

        if self.useBias:
            Blas.sumOnMatrix(visible,
                             out=self.vars["b"].grad,
                             alpha=-1.0,
                             beta=1.0)
            Blas.sumOnMatrix(hidden,
                             out=self.vars["c"].grad,
                             alpha=-1.0,
                             beta=1.0)

        return hidden
示例#5
0
    def updateGrad(self, grad):
        if self.useW:
            formatOut = self.format if self.inmode == GroupMode.full else "gbp"

            self.grad = BlasGroup.mulTensorBatch(grad,
                                                 self.W,
                                                 formatA=self.format,
                                                 formatB="gbp",
                                                 transpB=not self.transpW,
                                                 formatOut=formatOut)

            if self.inmode != GroupMode.full:
                self.grad = Blas.sumOnMatrix(
                    self.grad.reshape(self.groups,
                                      grad.shape[0] * self.W.shape[1]))
                self.grad = self.grad.reshape(grad.shape[0], 1,
                                              self.W.shape[1])

        else:
            self.grad = grad