コード例 #1
0
def guide_get_feature(stub):

    # A variable to count the number of iteration of the client, which must coincide with the epoch in the server.
    it = 1

    # We make a first call to the server to get the data : after that call, vect is the data set. Then we store it.
    vect = stub.GetFeature(route_guide_pb2.Vector(poids="pret"))
    dataInfo = vect.poids.split("<depre>")
    nbChunks = int(dataInfo[0])

    computeInfo = dataInfo[1].split("<samples>")

    # The depreciation of the SVM norm cost
    l = float(computeInfo[0])

    # Number of samples in each subtraining set
    numSamples = float(computeInfo[1])

    # Get the dataset from the server, by receiving all the chunks
    k = 1
    dataSampleSet = []
    while (k <= nbChunks):
        vect = stub.GetFeature(route_guide_pb2.Vector(poids="chunk<nb>" + str(k)))
        dataSampleSet += std.str2datadict(vect.poids)
        k +=1

    # This second call serves to get the departure vector.
    vect = stub.GetFeature(route_guide_pb2.Vector(poids="getw0"))


    while (vect.poids != 'stop'):

        print("iteration : " + str(it))

        # We save the vector on which we base the computations
        wt = std.str2dict(vect.poids)

        # Gradient descent on the sample.
        nw = sgd.descent(dataSampleSet, std.str2dict(vect.poids), numSamples, l)

        # Normalization of the vector of parameters
        normnW = math.sqrt(std.sparse_dot(nw, nw))
        nw = std.sparse_mult(1/normnW, nw)

        # The result is sent to the server.
        vect.poids = std.dict2str(nw) + "<delay>" + std.dict2str(wt)
        vect = stub.GetFeature(route_guide_pb2.Vector(poids=vect.poids))

        it += 1

        #time.sleep(1.7)

    print(vect)
コード例 #2
0
ファイル: clientTopkSGD.py プロジェクト: kiwi974/topk_sgd
def guide_get_feature(stub):

    # A variable to count the number of iteration of the client, which must coincide with the epoch in the server.
    it = 1

    # We make a first call to the server to get the data : after that call, vect is the data set. Then we store it.
    vect = stub.GetFeature(route_guide_pb2.Vector(poids="pret"))

    # We convert the set of data in the good format.
    data = vect.poids.split("<samples>")
    dataSampleSet = std.str2datadict(data[0])
    data2 = data[1].split("<#compo>")
    numSamples = int(data2[0])
    nbCompo = int(data2[1])


    # This second call serves to get the departure vector.
    vect = stub.GetFeature(route_guide_pb2.Vector(poids="getw0"))
    info = vect.poids.split("<<||>>")
    vect.poids = info[0]
    step = float(info[1])


    # Vector of the rests
    m = {}

    while (vect.poids != 'stop'):

        print("iteration : " + str(it))

        # Gradient descent on the sample.
        nw = sgd.descent(dataSampleSet, std.str2dict(vect.poids), numSamples, step)

        # Updating of m
        stepedGradient = std.sparse_mult(step,nw)
        m = std.sparse_vsum(m,stepedGradient)

        # Get the nbCompo biggest values in m, put it in g and remove them of m
        g = std.infiniteNormInd(m,nbCompo)

        # The result is sent to the server.
        strVect = std.dict2str(g)
        vect.poids = strVect + "<bytes>" + str(sys.getsizeof(strVect))
        vect = stub.GetFeature(route_guide_pb2.Vector(poids=vect.poids))

        if (vect.poids != 'stop'):
            info = vect.poids.split("<<||>>")
            vect.poids = info[0]
            step = float(info[1])

        it += 1

    print(vect)
コード例 #3
0
    def GetFeature(self, request, context):

        ######################################################################
        # Section 1 : wait for all the clients -> get their vectors and
        # appoint one of them as the printer.

        self.iterator += 1
        if (request.poids == "pret" or request.poids == "getw0"):
            self.vectors.append(request.poids)
        else:
            entry = request.poids.split("<bytes>")
            b = int(entry[1])
            if (self.epoch in self.bytesTab):
                self.bytesTab[self.epoch] += b
            else:
                self.bytesTab[self.epoch] = b
            v = std.str2dict(entry[0])
            self.vectors.append(v)
        self.enter_condition = (self.iterator == nbClients)
        waiting.wait(lambda: self.enter_condition)

        self.printerThreadName = threading.current_thread().name

        ######################################################################

        ######################################################################
        # Section 2 : compute the new vector -> send the data, a merge of
        # all the vectors we got from the clients or the message 'stop' the
        # signal to the client that we converged.

        normDiff = 0
        normGradW = 0
        normPrecW = 0
        if (request.poids == 'pret'):
            vector = std.datadict2Sstr(trainingSet) + "<samples>" + str(
                numSamples) + "<#compo>" + str(nbCompo)
        elif (request.poids == 'getw0'):
            vector = std.dict2str(w0) + "<<||>>" + str(self.step)
        else:
            # Modification of the vector of parameters
            gradParam = std.mergeSGD(self.vectors)
            vector = std.sparse_vsous(self.oldParam, gradParam)
            # Normalization of the vector of parameters
            normW = math.sqrt(std.sparse_dot(vector, vector))
            vector = std.sparse_mult(1 / normW, vector)
            # Checking of the stoping criterion
            diff = std.sparse_vsous(self.oldParam, vector)
            normDiff = math.sqrt(std.sparse_dot(diff, diff))
            normGradW = math.sqrt(std.sparse_dot(gradParam, gradParam))
            normPrecW = math.sqrt(std.sparse_dot(self.oldParam, self.oldParam))
            if ((normDiff <= c1 * normPrecW) or (self.epoch > nbMaxCall)
                    or (normGradW <= c2 * self.normGW0)):
                self.paramVector = vector
                vector = 'stop'
            else:
                vector = std.dict2str(vector) + "<<||>>" + str(self.step)

        ######################################################################

        ######################################################################
        # Section 3 : wait that all the threads pass the computation area, and
        # store the new computed vector.

        realComputation = (request.poids != 'pret') and (
            request.poids != 'getw0') and (vector != 'stop')

        self.iterator -= 1

        self.exit_condition = (self.iterator == 0)
        waiting.wait(lambda: self.exit_condition)

        if (realComputation):
            self.oldParam = std.str2dict(vector.split("<<||>>")[0])

        ######################################################################

        ###################### PRINT OF THE CURRENT STATE ######################
        ##################### AND DO CRITICAL MODIFICATIONS ####################
        if (threading.current_thread().name == self.printerThreadName):
            std.printTraceRecData(self.epoch, vector, self.paramVector,
                                  self.testingErrors, self.trainingErrors,
                                  normDiff, normGradW, normPrecW, normGW0,
                                  realComputation, self.oldParam, trainingSet,
                                  testingSet, nbTestingData, nbExamples, c1,
                                  c2, l, nbCompo, filePath)
            self.merged.append(self.oldParam)
            self.epoch += 1
            self.step *= 0.9  #std.stepSize(nbExamples, self.epoch, nbDesc, nbCompo)
            ############################### END OF PRINT ###########################

            dataTest = trainingSet[9]
            label = dataTest.get(-1, 0)
            example = std.take_out_label(dataTest)
            print("label = " + str(label))
            print("SVM says = " + str(std.sparse_dot(self.oldParam, example)))

            ######################################################################
            # Section 4 : empty the storage list of the vectors, and wait for all
            # the threads.

            self.vectors = []
            waiting.wait(lambda: (self.vectors == []))

            ######################################################################

        #time.sleep(1)
        return route_guide_pb2.Vector(poids=vector)
コード例 #4
0
    def GetFeature(self, request, context):

        ######################################################################
        # Section 1 : wait for all the clients -> get their vectors and
        # appoint one of them as the printer.

        print(self.epoch)

        self.printerThreadName = threading.current_thread().name

        if (request.poids == "pret" or request.poids == "getw0"
                or request.poids[:5] == "chunk"):
            self.iterator += 1
            self.vectors.append(request.poids)
            self.enter_condition = (self.iterator == nbClients)
            waiting.wait(lambda: self.enter_condition)

        if ((way2work == "sync") and (request.poids != "pret")
                and (request.poids != "getw0")
                and (request.poids[:5] != "chunk")):
            self.iterator += 1
            self.vectors.append(std.str2dict(
                request.poids.split("<delay>")[0]))
            self.enter_condition = (self.iterator == nbClients)
            waiting.wait(lambda: self.enter_condition)

        if ((threading.current_thread().name == self.printerThreadName)
                and (self.epoch == 1)):
            ############ Starting of the timer to time the run ############
            self.startTime = time.time()

        ######################################################################

        ######################################################################
        # Section 2 : compute the new vector -> send the data, a merge of
        # all the vectors we got from the clients or the message 'stop' the
        # signal to the client that we converged.

        normDiff = 0
        normGradW = 0
        normPrecW = 0

        if (request.poids == 'pret'):
            vector = str(nbChunks) + "<depre>" + str(l) + "<samples>" + str(
                numSamples)
        elif (request.poids[:5] == 'chunk'):
            chunk = request.poids.split("<nb>")
            chunk = int(chunk[1])
            vector = std.datadict2Sstr(
                trainingSet[(chunk - 1) * chunkSize:chunk * chunkSize])
        elif (request.poids == 'getw0'):
            vector = std.dict2str(w0)
        else:
            if (way2work == "sync"):
                gradParam = std.mergeSGD(self.vectors)
                if (self.epoch == 2):
                    self.normGradW0 = math.sqrt(
                        std.sparse_dot(gradParam, gradParam))
                normGradW = math.sqrt(std.sparse_dot(gradParam, gradParam))
                gradParam = std.sparse_mult(self.step, gradParam)
                vector = std.sparse_vsous(self.oldParam, gradParam)
            else:
                info = request.poids.split("<delay>")
                grad_vector = std.str2dict(info[0])
                if (self.epoch == 2):
                    self.normGradW0 = math.sqrt(
                        std.sparse_dot(grad_vector, grad_vector))
                normGradW = math.sqrt(std.sparse_dot(grad_vector, grad_vector))
                wt = std.str2dict(info[1])
                vector = std.asynchronousUpdate(self.oldParam, grad_vector, wt,
                                                l, self.step)

            ######## NORMALIZATION OF THE VECTOR OF PARAMETERS #########
            normW = math.sqrt(std.sparse_dot(vector, vector))
            vector = std.sparse_mult(1. / normW, vector)

            ############################################################
            diff = std.sparse_vsous(self.oldParam, vector)
            normDiff = math.sqrt(std.sparse_dot(diff, diff))
            normPrecW = math.sqrt(std.sparse_dot(self.oldParam, self.oldParam))
            if ((normDiff <= c1 * normPrecW) or (self.epoch > nbMaxCall)
                    or (normGradW <= c2 * normGW0)):
                self.paramVector = vector
                print("1 : " + str((normDiff <= c1 * normPrecW)))
                print("2 : " + str((self.epoch > nbMaxCall)))
                print("3 : " + str((normGradW <= c2 * normGW0)))
                vector = 'stop'
            else:
                vector = std.dict2str(vector)

        ######################################################################

        ######################################################################
        # Section 3 : wait that all the threads pass the computation area, and
        # store the new computed vector.

        realComputation = (request.poids != 'pret') and (
            request.poids !=
            'getw0') and (vector != 'stop') and (request.poids[:5] != 'chunk')

        if (realComputation):
            self.oldParam = std.str2dict(vector)

        ######################################################################

        ###################### PRINT OF THE CURRENT STATE ######################
        ##################### AND DO CRITICAL MODIFICATIONS ####################
        if (((threading.current_thread().name == self.printerThreadName) and
             (way2work == "sync")) or (way2work == "async")):
            print("oooooooo")
            endTime = time.time()
            duration = endTime - self.startTime

            if (vector == 'stop'):
                print("The server ran during : " + str(duration))

            std.printTraceRecData(self.epoch, vector, self.testingErrors,
                                  self.trainingErrors, normDiff, normGradW,
                                  normPrecW, normGW0, realComputation,
                                  self.oldParam, trainingSet, testingSet,
                                  nbTestingData, nbExamples, c1, c2, l,
                                  duration, filePath)

            self.merged.append(self.oldParam)
            if (realComputation):
                self.epoch += 1
                self.step *= 0.9
            ############################### END OF PRINT ###########################

            ######################################################################
            # Section 4 : empty the storage list of the vectors, and wait for all
            # the threads.

            self.vectors = []

            ######################################################################

        ######################################################################
        # Section 5 : synchronize all clients at the end of a server iteration
        if (way2work == "sync"):
            self.iterator -= 1
            self.exit_condition = (self.iterator == 0)
            waiting.wait(lambda: self.exit_condition)

        #time.sleep(1)
        return route_guide_pb2.Vector(poids=vector)
コード例 #5
0
    def GetFeature(self, request, context):

        ######################################################################
        # Section 1 : wait for all the clients -> get their vectors and
        # appoint one of them as the printer.

        if (way2work == "sync"):
            self.iterator += 1
            if (request.poids == "pret" or request.poids == "getw0"):
                self.vectors.append(request.poids)
            else:
                self.vectors.append(
                    std.str2dict(request.poids.split("<delay>")[0]))
            self.enter_condition = (self.iterator == nbClients)
            waiting.wait(lambda: self.enter_condition)

            self.printerThreadName = threading.current_thread().name

        ######################################################################

        ######################################################################
        # Section 2 : compute the new vector -> send the data, a merge of
        # all the vectors we got from the clients or the message 'stop' the
        # signal to the client that we converged.

        normDiff = 0
        normGradW = 0
        normPrecW = 0
        if (request.poids == 'pret'):
            vector = std.datadict2Sstr(trainingSet) + "<depre>" + str(
                l) + "<samples>" + str(numSamples)
        elif (request.poids == 'getw0'):
            vector = std.dict2str(w0)
        else:
            if (way2work == "sync"):
                gradParam = std.mergeSGD(self.vectors)
                gradParam = std.sparse_mult(self.step, gradParam)
                vector = std.sparse_vsous(self.oldParam, gradParam)
            else:
                info = request.poids.split("<delay>")
                grad_vector = std.str2dict(info[0])
                wt = std.str2dict(info[1])
                vector = std.asynchronousUpdate(self.oldParam, grad_vector, wt,
                                                l, self.step)

            ######## NORMALIZATION OF THE VECTOR OF PARAMETERS #########
            normW = math.sqrt(std.sparse_dot(vector, vector))
            vector = std.sparse_mult(1. / normW, vector)

            ############################################################
            diff = std.sparse_vsous(self.oldParam, vector)
            normDiff = math.sqrt(std.sparse_dot(diff, diff))
            normGradW = math.sqrt(std.sparse_dot(vector, vector))
            normPrecW = math.sqrt(std.sparse_dot(self.oldParam, self.oldParam))
            if ((normDiff <= c1 * normPrecW) or (self.epoch > nbMaxCall)
                    or (normGradW <= c2 * self.normgW0)):
                self.paramVector = vector
                vector = 'stop'
            else:
                vector = std.dict2str(vector)

        ######################################################################

        ######################################################################
        # Section 3 : wait that all the threads pass the computation area, and
        # store the new computed vector.

        realComputation = (request.poids != 'pret') and (
            request.poids != 'getw0') and (vector != 'stop')

        if (way2work == "sync"):
            self.iterator -= 1

            self.exit_condition = (self.iterator == 0)
            waiting.wait(lambda: self.exit_condition)

        if (realComputation):
            self.oldParam = std.str2dict(vector)

        ######################################################################

        ###################### PRINT OF THE CURRENT STATE ######################
        ##################### AND DO CRITICAL MODIFICATIONS ####################
        if ((threading.current_thread().name == self.printerThreadName) &
            (way2work == "sync") or (way2work == "async")):
            std.printTraceGenData(self.epoch, vector, self.paramVector,
                                  self.testingErrors, self.trainingErrors,
                                  trainaA, trainaB, trainoA, trainoB, hypPlace,
                                  normDiff, normGradW, normPrecW, self.normgW0,
                                  w0, realComputation, self.oldParam,
                                  trainingSet, testingSet, nbTestingData,
                                  nbExamples, nbMaxCall, self.merged, "", c1,
                                  c2, l)
            self.merged.append(self.oldParam)
            self.epoch += 1
            self.step *= 0.9

            dataTest = trainingSet[9]
            label = dataTest.get(-1, 0)
            example = std.take_out_label(dataTest)
            print("label = " + str(label))
            print("SVM says = " + str(std.sparse_dot(self.oldParam, example)))
            ############################### END OF PRINT ###########################

            ######################################################################
            # Section 4 : empty the storage list of the vectors, and wait for all
            # the threads.

            self.vectors = []
            waiting.wait(lambda: (self.vectors == []))

            ######################################################################

        #time.sleep(1)
        return route_guide_pb2.Vector(poids=vector)