Beispiel #1
0
def der_error(w,l,sample,sampleSize):
    d = std.sparse_mult(l, w)
    sum = {}
    for i in range(sampleSize):
        label = sample[i].get(-1,0)
        example = std.take_out_label(sample[i])
        if (label*(std.sparse_dot(w,example)) < 1):
            sum = std.sparse_vsum(sum,std.sparse_mult(-label,example))
    #print("der_err summ part = " + str(sum))
    dcost = std.sparse_vsum(d,sum)
    return dcost
Beispiel #2
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"))

    # 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)
data[0][-1] = 1
data[1][-1] = -1
data[2][-1] = 1

print('')
print("############## Test of the sparse scalar product. ##############")
print('')

spdsp = std.sparse_dot(spV1, spV2)
print("spdsp = " + str(spdsp))

print('')
print("################ Test of the sparse sum. #################")
print('')

spdsu = std.sparse_vsum(spV1, spV2)
print("spdsum = " + str(spdsu))

empty = std.sparse_vsum({}, spV1)
print("empty = " + str(empty))

print('')
print("############### Test of the sparse map. ###############")
print('')


def opp(x):
    return (-x)


spdmap = std.sparse_map(opp, spV1)