Пример #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 updateData(self, data):
        self.data = Blas.mulMatrixOnMatrix(data,
                                           self.W,
                                           transpB=self.transpose)

        if self.useBias:
            MatVec.addVecToMat(self.b, self.data, axis=1, out=self.data)
Пример #3
0
    def visibleFromHidden(self, hidden):
        visible = Blas.mulMatrixOnMatrix(hidden, self.W, transpB=True)

        if self.useBias:
            addVecToMat(self.b, visible, axis=1, out=visible)

        self.activateNeurons(visible)
        return visible
Пример #4
0
    def hiddenFromVisible(self, visible):
        hidden = Blas.mulMatrixOnMatrix(visible, self.W)

        if self.useBias:
            addVecToMat(self.c, hidden, axis=1, out=hidden)

        self.activateNeurons(hidden)
        return hidden
Пример #5
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)
Пример #6
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
Пример #7
0
 def updateData(self, data):
     reshape = len(data.shape) > 2
     if reshape:
         reshape2d = Reshape(
             (int(np.prod(data.shape[:-1])), data.shape[-1]))
         reshape2d.calcMode(self.calctype)
         reshapeNd = Reshape(data.shape[:-1] + tuple([self.W.shape[1]]))
         reshapeNd.calcMode(self.calctype)
         data = reshape2d(data)
     self.data = Blas.mulMatrixOnMatrix(data,
                                        self.W,
                                        transpB=self.transpose)
     if self.useBias:
         MatVec.addVecToMat(self.b, self.data, axis=1, out=self.data)
     if reshape:
         self.data = reshapeNd(self.data)
Пример #8
0
 def updateGrad(self, grad):
     self.grad = Blas.mulMatrixOnMatrix(grad,
                                        self.W,
                                        transpB=not self.transpose)