def rae_encoding(vect, w, w_size, h_vect, wp):
    vect21={}
    vect22={}
    o21={}
    w1=w['w1']
    w2=w['w2']
    for i in range(len(h_vect)):
        temp1=0.0
        # print w_size + i
        for j in range(len(h_vect[i])):
            if h_vect[i][j] < w_size:
                # print '\tv->h1\t',str(h_vect[i][j])+'->'+str(w_size+i), wp[i][j]
                temp1 += np.dot(w1[wp[i][j]],vect[h_vect[i][j]])
            else:
                # print '\th2->h1\t',str(h_vect[i][j]),'w2`'
                temp11 = np.dot(w2.transpose(),vect[h_vect[i][j]])
                o21[h_vect[i][j]] = temp11
                temp12 = sigmoid(temp11)
                vect22[h_vect[i][j]] = temp12
                # print '\th1->h1\t', str(h_vect[i][j]) + '->' + str(w_size + i), wp[i][j]
                temp1 += np.dot(w1[wp[i][j]],temp12)
        temp12 = sigmoid(temp1)
        vect21[w_size+i] = temp12
        temp2 = np.dot(w2,temp12)
        # print '\th1->h2\t','w2'
        vect[w_size+i] = sigmoid(temp2)
        # print
    return vect21, vect22, o21
def rae_decoding(vect1, w1, w2, w_size, h_vect, wp):
    vect1_ = {}
    vect21_ = {}
    vect22_ = {}
    o_ = {2 * w_size - 1:np.nan}
    o21_ = {}
    vect1_[2 * w_size - 1] = vect1[2 * w_size - 1]
    for i in reversed(range(len(h_vect))):
        to2 = np.dot(w2.transpose(),vect1_[w_size + i])
        # print w_size + i
        o21_[w_size+i] = to2
        # print '\th2->h1\t','w2`'
        t2 = sigmoid(to2)
        vect22_[w_size+i] = t2
        for j in range(len(h_vect[i])):
            if h_vect[i][j] < w_size:
                to1 = np.dot(w1[wp[i][j]].transpose(),t2)
                o_[h_vect[i][j]]=to1
                # print '\th1->v\t',str(w_size+i)+'->'+str(h_vect[i][j]),wp[i][j]
                vect1_[h_vect[i][j]] = sigmoid(to1)
            else:
                to1=np.dot(w1[wp[i][j]].transpose(), t2)
                o_[h_vect[i][j]] = to1
                # print '\th1->h1\t',str(w_size+i)+'->'+str( h_vect[i][j]),wp[i][j]
                t1 = sigmoid(to1)
                vect21_[h_vect[i][j]] = t1
                to2 = np.dot(w2, t1)
                # print '\th1->h2\t', h_vect[i][j],'w2'
                vect1_[h_vect[i][j]] = sigmoid(to2)
    return vect1_, o_, vect21_, vect22_, o21_
示例#3
0
def rae_encoding(vect, w, w_size, h_vect, wp):
    for i in range(len(h_vect)):
        temp1 = 0.0
        # print w_size+i
        for j in range(len(h_vect[i])):
            # print '\t',h_vect[i][j],
            temp1 += np.dot(w[wp[i][j]], vect[h_vect[i][j]])
        # print
        vect[w_size + i] = sigmoid(temp1)
    return
示例#4
0
def rae_decoding(vect, o, w, w_size, h_vect, wp):
    vect_ = {}
    vect_[2 * w_size - 1] = vect[2 * w_size - 1]
    o[2 * w_size - 1] = np.nan
    for i in reversed(range(len(h_vect))):
        # print w_size+i
        for j in range(len(h_vect[i])):
            # print '\t',h_vect[i][j],
            o[h_vect[i][j]] = np.dot(w[wp[i][j]].transpose(),
                                     vect_[w_size + i])
            vect_[h_vect[i][j]] = sigmoid(o[h_vect[i][j]])
        # print
    return vect_
def gradiant2(vect21, vect22, o21, vect21_, vect22_, o21_, w_size, vect1_):
    grad2 = []
    cost2 = []
    v=[]
    vect21_[w_size*2-1]=vect21[w_size*2-1]
    vect22[w_size * 2 - 1] = vect22_[w_size * 2 - 1]
    o21[w_size*2-1] = o21_[w_size*2-1]
    # print "encoding grad"
    for i in vect22:
        # print '\t',i
        tcost = np.power(vect21[i] - vect22[i],2)
        cost2.append(tcost)
        grad2.append(tcost*sigmoid(o21[i]))
        v.append(vect1_[i])
    # print "decoding grad"
    for i in vect22_:
        # print '\t',i
        tcost = np.power(vect21_[i] - vect22_[i],2)
        cost2.append(tcost)
        grad2.append(tcost * sigmoid(o21_[i]))
        v.append(vect1_[i])
    grad2=grad2[:-1]
    cost2=cost2[:-1]
    return grad2, cost2, v