def feedForward(x, params,B):
    x = layers.quantizeAct(x,B)
    l=0
    current_params = params[l]
    current_params[0] = layers.quantizeWeight(current_params[0],B+2)
    current_params[1] = layers.quantizeWeight(current_params[1],B+2)
    c1 = layers.linOutermost(x,current_params)
    c1 = layers.slopedClipping(c1)
    c1 = layers.quantizeAct(c1,B)

    l+=1
    current_params = params[l]
    current_params[0] = layers.quantizeWeight(current_params[0],B+2)
    current_params[1] = layers.quantizeWeight(current_params[1],B+2)
    c2 = layers.linOutermost(c1,current_params)
    c2 = layers.slopedClipping(c2)
    c2 = layers.quantizeAct(c2,B)

    l+=1
    current_params = params[l]
    current_params[0] = layers.quantizeWeight(current_params[0],B+2)
    current_params[1] = layers.quantizeWeight(current_params[1],B+2)
    c3 = layers.linOutermost(c2,current_params)
    c3 = layers.slopedClipping(c3)
    c3 = layers.quantizeAct(c3,B)

    l+=1
    current_params = params[l]
    current_params[0] = layers.quantizeWeight(current_params[0],B+2)
    current_params[1] = layers.quantizeWeight(current_params[1],B+2)

    z = layers.linOutermost(c3,current_params)

    return z
예제 #2
0
def resBlock(preAct, resParams, train, BA1, BA2, BW1, BW2):
    current_params = resParams[0]
    current_params[0] = layers.quantizeWeight(current_params[0], BW1)
    inAct, _, _ = layers.convBNAct(preAct, current_params, train)
    inAct = layers.quantizeAct(inAct, BA1)
    inAct = 0.8 * inAct

    current_params = resParams[1]
    current_params[0] = layers.quantizeWeight(current_params[0], BW2)
    outAct, _, _ = layers.convBN(inAct, current_params, train)

    outAct = layers.slopedClipping(outAct + preAct)
    outAct = layers.quantizeAct(outAct, BA2)
    return outAct
예제 #3
0
def feedForward(x, params, B, BA, BW):
    train = 0
    res0Params = params[0]
    current_params = res0Params[0]
    current_params[0] = layers.quantizeWeight(current_params[0],
                                              BW.take(0) + B)
    outAct, _, _ = layers.convBNAct(x, current_params, train)
    outAct = layers.quantizeAct(outAct, BA.take(1) + B)

    outAct = resBlock(outAct, params[1], train,
                      BA.take(2) + B,
                      BA.take(3) + B,
                      BW.take(1) + B,
                      BW.take(2) + B)

    outAct = resBlock(outAct, params[2], train,
                      BA.take(4) + B,
                      BA.take(5) + B,
                      BW.take(3) + B,
                      BW.take(4) + B)

    outAct = resBlock(outAct, params[3], train,
                      BA.take(6) + B,
                      BA.take(7) + B,
                      BW.take(5) + B,
                      BW.take(6) + B)

    outAct = resBlockStride(outAct, params[4], train,
                            BA.take(8) + B,
                            BA.take(9) + B,
                            BW.take(7) + B,
                            BW.take(8) + B,
                            BW.take(9) + B)

    outAct = resBlock(outAct, params[5], train,
                      BA.take(10) + B,
                      BA.take(11) + B,
                      BW.take(10) + B,
                      BW.take(11) + B)

    outAct = resBlock(outAct, params[6], train,
                      BA.take(12) + B,
                      BA.take(13) + B,
                      BW.take(12) + B,
                      BW.take(13) + B)

    outAct = resBlockStride(outAct, params[7], train,
                            BA.take(14) + B,
                            BA.take(15) + B,
                            BW.take(14) + B,
                            BW.take(15) + B,
                            BW.take(16) + B)

    outAct = resBlock(outAct, params[8], train,
                      BA.take(16) + B,
                      BA.take(17) + B,
                      BW.take(17) + B,
                      BW.take(18) + B)

    outAct = resBlock(outAct, params[9], train,
                      BA.take(18) + B,
                      BA.take(19) + B,
                      BW.take(19) + B,
                      BW.take(20) + B)

    pooled = pool_2d(outAct,
                     ws=(8, 8),
                     ignore_border=True,
                     mode='average_exc_pad')
    pooled = pooled.flatten(2)

    res10Params = params[10]
    current_params = res10Params[0]
    current_params[0] = layers.quantizeWeight(current_params[0],
                                              BW.take(21) + B)
    z = layers.linOutermost(pooled, current_params)
    #
    return z
def feedForward(x, params, B, BA, BW ):
    x = layers.quantizeAct(x, B+BA.take(0))
    l=0
    current_params = params[l]
    current_params[0] = layers.quantizeNormalizedWeight(current_params[0],B+BW.take(l),16.0,0.0625)
    current_params[1] = layers.quantizeNormalizedWeight(current_params[1],B+BW.take(l),16.0,0.0625)
    c1 = conv2d(x,current_params[0]) + current_params[1].dimshuffle('x',0,'x','x')
    c1 = layers.quantizeAct(layers.slopedClipping(c1),B+BA.take(l+1))

    l+=1
    current_params = params[l]
    current_params[0] = layers.quantizeNormalizedWeight(current_params[0],B+BW.take(l),2.0,0.5)
    current_params[1] = layers.quantizeNormalizedWeight(current_params[1],B+BW.take(l),2.0,0.5)
    c2 = conv2d(c1,current_params[0]) + current_params[1].dimshuffle('x',0,'x','x')
    c2 = layers.quantizeAct(layers.slopedClipping(c2),B+BA.take(l+1))

    p3 = pool_2d(c2,ws=(2,2),ignore_border=True)

    l+=1
    current_params = params[l]
    current_params[0] = layers.quantizeNormalizedWeight(current_params[0],B+BW.take(l),4.0,0.25)
    current_params[1] = layers.quantizeNormalizedWeight(current_params[1],B+BW.take(l),4.0,0.25)
    c4 = conv2d(p3,current_params[0]) + current_params[1].dimshuffle('x',0,'x','x')
    c4 = layers.quantizeAct(layers.slopedClipping(c4),B+BA.take(l+1))

    l+=1
    current_params = params[l]
    current_params[0] = layers.quantizeNormalizedWeight(current_params[0],B+BW.take(l),2.0,0.5)
    current_params[1] = layers.quantizeNormalizedWeight(current_params[1],B+BW.take(l),2.0,0.5)
    c5 = conv2d(c4,current_params[0]) + current_params[1].dimshuffle('x',0,'x','x')
    c5 = layers.quantizeAct(layers.slopedClipping(c5),B+BA.take(l+1))

    p6 = pool_2d(c5,ws=(2,2),ignore_border=True)

    l+=1
    current_params = params[l]
    current_params[0] = layers.quantizeNormalizedWeight(current_params[0],B+BW.take(l),2.0,0.5)
    current_params[1] = layers.quantizeNormalizedWeight(current_params[1],B+BW.take(l),2.0,0.5)
    c7 = conv2d(p6,current_params[0]) + current_params[1].dimshuffle('x',0,'x','x')
    c7 = layers.quantizeAct(layers.slopedClipping(c7),B+BA.take(l+1))

    l+=1
    current_params = params[l]
    current_params[0] = layers.quantizeNormalizedWeight(current_params[0],B+BW.take(l),1.0,1.0)
    current_params[1] = layers.quantizeNormalizedWeight(current_params[1],B+BW.take(l),1.0,1.0)
    c8 = conv2d(c7,current_params[0]) + current_params[1].dimshuffle('x',0,'x','x')
    c8 = layers.quantizeAct(layers.slopedClipping(c8),B+BA.take(l+1))

    f9 = c8.flatten(2)

    l+=1
    current_params = params[l]
    current_params[0] = layers.quantizeNormalizedWeight(current_params[0],B+BW.take(l),2.0,0.5)
    current_params[1] = layers.quantizeNormalizedWeight(current_params[1],B+BW.take(l),2.0,0.5)
    h1 = T.dot(f9,current_params[0]) + current_params[1]
    h1 = layers.quantizeAct(layers.slopedClipping(h1),B+BA.take(l+1))

    l+=1
    current_params = params[l]
    current_params[0] = layers.quantizeNormalizedWeight(current_params[0],B+BW.take(l),1.0,1.0)
    current_params[1] = layers.quantizeNormalizedWeight(current_params[1],B+BW.take(l),1.0,1.0)
    h2 = layers.linOutermost(h1,current_params)
    h2 = layers.quantizeAct(layers.slopedClipping(h2),B+BA.take(l+1))

    l+=1
    current_params = params[l]
    current_params[0] = layers.quantizeNormalizedWeight(current_params[0],B+BW.take(l),1.0,1.0)
    current_params[1] = layers.quantizeNormalizedWeight(current_params[1],B+BW.take(l),1.0,1.0)
    z = layers.linOutermost(h2,current_params)
    #
    return z