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
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)
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
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
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)
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
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)
def updateGrad(self, grad): self.grad = Blas.mulMatrixOnMatrix(grad, self.W, transpB=not self.transpose)