奋战生活(一)

当现在睡眼朦松的时候,我才意识到最近已经好久没好好的休息了。我不清楚是哪个早上4点没有醒来,是哪个夜晚在12点之前入眠,如果按照这样的计算,。。。我不是每天就睡不到4个小时!!!真的好困阿。。。现在我最想做的事情就是好好睡上一觉,什么事情都不想,做个好梦。可是潜意识告诉我不能那样做,因为我还有很多事情没有完成。现在才领悟到什么事情都应该提前有个详细的规划,如果让其自由发展,结果可能是难以控制的。就好比熵值一样,不人为的去控制,只会越来越大。

不过好在有一点,这几天辛苦研读paper和熬夜敲代码,终于有了一丝的回报,大致的transfer learning算法已经实现了,剩下的就是调试工作了。在经过了一番龙争虎斗之后,有一部分已经被解决,不过离最终的结果好像还有一定的差距,唉,debug什么的最讨厌了。就拿今天的一个实例来说吧,程序员最讨厌的就是细节了,错把j写成了i,让我付出了接近一个小时的时间,各种跟踪定位,当揪出bug的时候,我都无奈的笑了,这种事情是要靠人品的,求不来的。。。不过好在解决了,还是要小小的欢喜一下的。。。

顺便小小的抱怨一下,图书馆的网速真的很让人蛋疼唉,VPN肯定是没希望连上了,文章是无法上传了,域名阿,我一定要重新搞一个。。。等回宿舍了,再把代码贴上,希望能对那些做transfer learning的人有所tips吧,呵呵,那样也不枉费我含辛茹苦的代码阿 ^o^

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
'''Here what I want to do is to realize the algorithm of the 
transfer learning which was designed for the cross-domain collaborative filtering
for sparsity reduction'''
 
from random import *
from math import sqrt
 
#basic function
 
#initialize a matrix that each row has only a nonnegative integer 
#and the others are all 0
def _matrix_init(row, column):
    matrix = []
    for i in range(0, row):
        matrix.append([])
        for j in range(0, column):
            matrix[i].append(0)
    return matrix
 
def _matrix_init_para(row, column, para):
     matrix = []
     for i in range(0, row):
         matrix.append([])
         for j in range(0, column):
             matrix[i].append(para)
     return matrix
 
def _init(row, column):
    matrix = _matrix_init(row, column)
    for k in range(0, row):
        location = randrange(0, column)
        num = randrange(1, 100)
        matrix[k][location] = num
    return matrix
 
def _init_binary(row, column):
    matrix = _matrix_init(row, column)
    for k in range(0, row):
        location = randrange(0, column)
        matrix[k][location] = 1
    return matrix
 
def _multiplication(U, V):
    n = len(U)
    m = len(U[0])
    k = len(V[0])
    result = _matrix_init(n, k)
    for i in range(0, n):
        for j in range(0, k):
            sum = 0
            for p in range(0, m):
                sum += U[i][p] * V[p][j]
            result[i][j] = sum
    return result
 
def _transpose(U):
    n = len(U)
    m = len(U[0])
    V = _matrix_init(m, n)
    for i in range(0, m):
        for j in range(0, n):
            V[i][j] = U[j][i]
    return V
 
#codebook construction
 
#what one iterative round does is to update the U, V and S that meet the needs  
#U[n][k], V[m][l], S[k][l], X[n][m]
def _update(U, V, S, X):
    n = len(U)
    k = len(U[0])
    m = len(V)
    l = len(V[0])
    k = len(S)
    uA = _multiplication(_multiplication(X, V), _transpose(S))
    uB = _multiplication(_multiplication(_multiplication
             (_multiplication(U, _transpose(U)), X), V), _transpose(S))
    vA = _multiplication(_multiplication(_transpose(X), U), S)
    vB = _multiplication(_multiplication(_multiplication
            (_multiplication(V, _transpose(V)), _transpose(X)), U), S)
    sA = _multiplication(_multiplication(_transpose(U), X), V)
    sB = _multiplication(_multiplication(_multiplication
            (_multiplication(_transpose(U), U), S), _transpose(V)), V)
    for ui in range(0, n):
        for uj in range(0, k):
            if uB[ui][uj] != 0:
                U[ui][uj] = U[ui][uj] * sqrt(float(uA[ui][uj])/(uB[ui][uj])) 
            else:
                U[ui][uj] = 0
    for vi in range(0, m):
        for vj in range(0, l):
            if vB[vi][vj] != 0:
                V[vi][vj] = V[vi][vj] * sqrt(float(vA[vi][vj])/(vB[vi][vj]))
            else:
                V[vi][vj] = 0
    for si in range(0, k):
        for sj in range(0, l):
            if sB[si][sj] != 0:
                S[si][sj] = S[si][sj] * sqrt(float(sA[si][sj])/(sB[si][sj]))
            else:
                S[si][sj] = 0
    result = []
    result.append(U)
    result.append(V)
    result.append(S)
    return result
 
def _iteration(U, V, S, X):
    para = _update(U, V, S, X)
    #just take a 20th-iterative round, or you can make a if-ondition 
    #that see wether U, V and S meet the needs
    #there is a dault here
    for i in range(0, 100):
        para = _update(para[0], para[1], para[2], X)
    return para
 
def _auxiliary(U):
    n = len(U)
    k = len(U[0])
    for i in range(0, n):
        for j in range(0, k):
            if U[i][j] != 0:
                U[i][j] = 1
    return U
 
#the main function of codebook construction
def _codebook(U, V, X):
    n = len(X)
    m = len(X[0])
    k = len(U[0])
    l = len(V[0])
    B = _multiplication(_multiplication(_transpose(U), X), V)
    One = _matrix_init_para(n, m, 1)
    C = _multiplication(_multiplication(_transpose(U), One), V)
    for i in range(0, k):
        for j in range(0, l):
            if C[i][j] != 0:
                B[i][j] = B[i][j]/C[i][j]
            else:
                B[i][j] = 0
    return B
 
#the main port of the codebook construction
def codebookconstruction(X, k ,l):
    n = len(X)
    m = len(X[0])
    U = _init(n, k)
    V = _init(m, l)
    S = _init(k, l)
    result = _iteration(U, V, S, X)
    Uaux = _auxiliary(result[0])
    Vaux = _auxiliary(result[1])
    print Uaux
    print Vaux
    codebook = _codebook(Uaux, Vaux, X)
    return codebook
 
 
#codebook transfer
 
def _findrowmin(X, B, V, wi):
    q = len(X[0])
    k = len(B)
    matrix1 = _multiplication(B, _transpose(V))
    matrix2 = _matrix_init(1, q)
    flag = 0 
    min = 0
    for j in range(0, k):
        for i in range(0, q):
            matrix2[0][i] = X[wi][i] - matrix1[j][i]
        data = _weightednorm(matrix2)
        if j == 0:
            min = data
        elif min > data:
            flag = j
            min = data
    return flag
 
def _findcolumnmin(X, B, U, wi):
    p = len(X)
    l = len(B[0])
    matrix1 = _multiplication(U, B)
    matrix2 = _matrix_init(p, 1)
    flag = 0
    min = 0
    for j in range(0, l):
        for i in range(0, p):
            matrix2[i][0] = X[i][wi] - matrix1[i][j]
        data = _weightednorm(matrix2)
        if j == 0:
            min = data
        elif min > data:
            flag = j
            min = data
    return flag
 
def _weightednorm(U):
    n = len(U)
    m = len(U[0])
    sum = 0
    for i in range(0, n):
        for j in range(0, m):
            sum += (U[i][j]) * (U[i][j])
    return sum
 
def _target(U, ui, uj):
    m = len(U[0])
    for j in range(0, m):
        if j == uj:
            U[ui][j] = 1
        else:
            U[ui][j] = 0
    return U
 
def _weightingmatrix(U):
    n = len(U)
    m = len(U[0])
    W = _matrix_init(n, m)
    for i in range(0, n):
        for j in range(0, m):
            if U[i][j] != 0:
                W[i][j] = 1
    return W
 
def _targetmatrix(X, W, U, V, B):
    p = len(X)
    q = len(X[0])
    matrix = _multiplication(_multiplication(U, B), _transpose(V))
    print matrix
    for i in range(0, p):
        for j in range(0, q):
            if W[i][j] == 0:
                X[i][j] = matrix[i][j]
    return X
 
#the main port of codebook transfer
def codebooktransfer(X, B):
    p = len(X)
    q = len(X[0])
    k = len(B)
    l = len(B[0])
    V = _init_binary(q, l)
    U = _matrix_init(p, k)
    #10 iterative rounds
    for t in range(0, 20):
        for i in range(0, p):
            pi = _findrowmin(X, B, V, i)
            U = _target(U, i, pi)
        for j in range(0, q):
            pj = _findcolumnmin(X, B, U, j)
            V = _target(V, j, pj)
    print U
    print V
    W = _weightingmatrix(X)
    X = _targetmatrix(X, W, U, V, B)
    return X
 
#just for some tests
if __name__ == '__main__':
    a=_init(2,3)
    b=_init(3,4)
    c=_multiplication(a, b)
    d=_transpose(a)
    print a
    print b
    e=_update(a, b, b, a)
    print a
    print b
    print e

One thought on “奋战生活(一)

  1. littlion

    你好,不知道你现在还搞不搞迁移学习了,你上面的代码是来自Can Movies and Books Collaborate?Cross-Domain Collaborative Filtering for Sparsity Reduction这篇文章吧,我有些地方不是很懂,希望能和你交流一下

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.