Ejemplo n.º 1
0
Archivo: sgd.py Proyecto: uczlgx0/MLR
    def cal_derivative(self, W_w, W_u, item):
        """
        calculate derivative only with l2 norm
        :param weight:
        :return:
        """
        label, featureDic = item
        dir_W = []
        dir_U = []
        temp_eux = []
        temp_sywx = []
        sum_eux = 0.0
        sum_eux_sywx = 0.0
        for i in range(self.M):
            #get all the temp exp(uj * x) and sigmoid(y * wj * x)
            #and get sum at the same time
            eux = np.exp(fc.dotProduct(W_u[i], featureDic))
            sywx = fc.sigmoid(label * fc.dotProduct(W_w[i], featureDic))
            temp_eux.append(eux)
            temp_sywx.append(sywx)
            sum_eux += eux
            sum_eux_sywx += eux * sywx
        for i in range(self.M):
            #calculate array uj and array wj
            dir_w = {}
            dir_u = {}
            for index in featureDic:
                dir_u[index] = temp_eux[i] * featureDic[index] / sum_eux - \
                    temp_eux[i] * temp_sywx[i] * featureDic[index] / sum_eux_sywx
                dir_w[index] = label * temp_sywx[i] * (
                    temp_sywx[i] - 1) * featureDic[index] / sum_eux_sywx
            dir_W.append(dir_w)
            dir_U.append(dir_u)

        return dir_W, dir_U
Ejemplo n.º 2
0
    def shift(self,data, sList, yList, roList, W, newW, LW, LU):
        newLW, newLU = fc.sumCalDerivative(newW[:len(newW)//2],newW[len(newW)//2:],data)
        newGradient = newLW+newLU
        gradient = LW + LU
        for i in range(len(sList)):
            slist = sList[i]
            ylist = yList[i]
            rolist = roList[i]
            w = W[i]
            neww = newW[i]
            g = gradient[i]
            newg = newGradient[i]

            size = len(slist)
            if size == self.memoryNum:
                # print >> sys.stdout, "pop 老的S, Y, RO"
                slist.pop(0)
                ylist.pop(0)
                rolist.pop(0)

            nextS = {}
            nextY = {}
            fc.addMultInto(self.feaNum, nextS, neww, w, -1)
            # print "newG: %s" % newGradient
            fc.addMultInto(self.feaNum, nextY, newg, g, -1)
            # print "nextS: %s" % nextS
            # print "nextY: %s" % nextY
            ro = fc.dotProduct(nextS, nextY)
            slist.append(nextS)
            ylist.append(nextY)
            rolist.append(ro)
Ejemplo n.º 3
0
Archivo: sgd.py Proyecto: uczlgx0/MLR
 def cal_loss(self, W_w, W_u, data):
     """
     calculate the loss over all data
     :param w_w:
     :param w_u:
     :param data:
     :return:
     """
     loss = 0.0
     for label, featureDic in data:
         #sum_u is sum(exp(uj * x))
         #sum_us is sum(exp(uj * x) * sigmoid(y * wi * x))
         sum_u = 0
         sum_us = 0
         for i in range(self.M):
             wx = fc.dotProduct(W_w[i], featureDic)
             eux = np.exp(fc.dotProduct(W_u[i], featureDic))
             sum_u += eux
             sum_us += eux * fc.sigmoid(label * wx)
         loss += np.log(sum_u) - np.log(sum_us)
     print("loss is:  %s" % loss)