예제 #1
0
async def convolvetensor(x, W, b):
    logging.info('- - - - - - - - conv2d  - - - - - - -')
    # 2D convolutions on m*n sized images from X with s*s sized filters from W.
    # b is of dimension v
    k, r, m, n = dim(x)
    x = x.tolist()
    v, r, s, s = dim(W)
    W = W.tolist()
    stype = type(x[0][0][0][0])
    await mpc.returnType(stype, k, v, m, n)
    x, W = await mpc.gather(x, W)
    Y = [[[[b[j]] * m for _ in range(n)] for j in range(v)] for _ in range(k)]
    counter = 0
    for i in range(k):
        for j in range(v):
            for l in range(r):
                counter += 1
                if counter % 500 == 0:
                    await mpc.barrier()
                Y[i][j] = mpc.matrix_add(Y[i][j], inprod2D(x[i][l], W[j][l]))
    Y = await mpc.gather(Y)
    for i in range(k):
        for j in range(v):
            for im in range(m):
                Y[i][j][im] = mpc._reshare(Y[i][j][im])
    Y = await mpc.gather(Y)
    if stype.field.frac_length > 0:
        l = stype.bit_length
        Y = [[[mpc.trunc(y, l=l) for y in _] for _ in _] for _ in Y]
    Y = await mpc.gather(Y)
    return Y
예제 #2
0
def truncate(f):
    logging.info("- - - - - - - - truncate- - - - - - -")
    return np.vectorize(lambda a: mpc.trunc(a + (1<<(f-1)), f))