-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_no_content.lua
262 lines (221 loc) · 7.89 KB
/
main_no_content.lua
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
require 'nn'
require 'nngraph'
--- Count the number of pairs in a table
function table_size(t)
local a=0
for k,_ in pairs(t) do
a=a+1
end
return a
end
--- Compute an index of the users that appear at least one time in the training and testing cascades
function buildIndex(train, test)
local count={}
local nb=0
print("\tReading "..train)
for line in io.lines(train) do
local tokens=string.gmatch(line,"[^%s]+")
for token in tokens do
iter=string.gmatch(token,"[^,]+")
local user=iter()
local timestamp=tonumber(iter())
if (count[user]==nil) then count[user]=0 end
count[user]=count[user]+1
end
end
print("\tReading "..test)
local countt={}
for line in io.lines(test) do
local tokens=string.gmatch(line,"[^%s]+")
for token in tokens do
iter=string.gmatch(token,"[^,]+")
local user=iter()
local timestamp=tonumber(iter())
if (countt[user]==nil) then countt[user]=0 end
countt[user]=countt[user]+1
end
end
local index={}
local pos=1
for u,n in pairs(count) do
if (countt[u]~=nil) then index[u]=pos; pos=pos+1; end
end
print("\t"..(pos-1).." different users.")
return index
end
----- Read a set of cascade from a file given a index of users
function readFromFile(filename,index_users)
assert(index_users~=nil)
local nb_users=0
nb_users=table_size(index_users)
local cascades_users={}
local cascades_timestamps={}
local nb_cascades=1
for line in io.lines(filename) do
local sequence_users={}
local sequence_timestamps={}
local pos=1
local tokens=string.gmatch(line,"[^%s]+")
for token in tokens do
iter=string.gmatch(token,"[^,]+")
local user=iter()
local timestamp=tonumber(iter())
if (index_users[user]~=nil) then
sequence_users[pos]=index_users[user]
sequence_timestamps[pos]=timestamp
pos=pos+1
end
end
if (#sequence_users>1) then
cascades_users[nb_cascades]=sequence_users
cascades_timestamps[nb_cascades]=sequence_timestamps
nb_cascades=nb_cascades+1
end
end
print("\tNb cascades = "..(nb_cascades-1).." for nb_users="..nb_users)
local retour={cascades=cascades_users,timestamps=cascades_timestamps,index=index_users,nb_users=nb_users,nb_cascades=nb_cascades-1}
retour.size_cascades={}
for i=1,#retour.cascades do retour.size_cascades[i]=#(retour.cascades[i]) end
return(retour)
end
function computeDistanceMatrix(zs,nb_users)
print("Computing distance matrix of "..nb_users.." users.")
local matrix=torch.Tensor(nb_users,nb_users):fill(0)
local is=torch.Tensor(1):fill(1)
local dist=nn.PairwiseDistance(2)
for u=1,nb_users do
local z1=zs[u]:forward(is)
for u2=u,nb_users do
local z2=zs[u2]:forward(is)
matrix[u][u2]=dist:forward({z1,z2})[1]
matrix[u2][u]=matrix[u][u2]
end
end
return(matrix)
end
--- Compute the average precision over a cascade given a distanceMatrix
function computeAveragePrecision(cascade,size_cascade,distanceMatrix)
local idx_source=cascade[1]
local liste={}
for u=1,distanceMatrix:size(1) do
liste[u]={}
liste[u].user=u
liste[u].distance=distanceMatrix[idx_source][u]
end
for i=1,size_cascade do
liste[cascade[i]].relevant=true
end
function compare(a,b)
return(a.distance<b.distance)
end
table.sort(liste,compare)
local nb_positive=0
local rank=1
local avgp=0
while(nb_positive<size_cascade) do
local elt=liste[rank]
if(elt.relevant) then nb_positive=nb_positive+1; local pre=nb_positive/rank; avgp=avgp+pre end
rank=rank+1
end
avgp=avgp/size_cascade
return(avgp)
end
-- Compute the average precision over a cascade given a distanceMatrix
function computeMAP(cascades,size_cascades,distanceMatrix)
local map=0
for i=1,#size_cascades do
map=map+computeAveragePrecision(cascades[i],size_cascades[i],distanceMatrix)
end
map=map/#size_cascades
return map
end
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
-----------------------------------------------------------------------------------
cmd=torch.CmdLine()
cmd:text()
cmd:option('--training_cascades', "", 'training_cascades file')
cmd:option('--testing_cascades', "", 'testing_cascades file')
cmd:option('--outputFile', "", 'The outputfile where to store the embeddings of users')
cmd:option('--learningRate', 0.01, 'learning rate')
cmd:option('--maxEpoch', 1000, 'maximum number of epochs to run')
cmd:option('--evaluationEpoch', 10, 'Number of steps where evaluation is made')
cmd:option('--uniform', 0.1, 'initialize parameters using a gaussian distribution')
cmd:option('--N', 10, 'Dimension of the latent space')
cmd:text()
local opt = cmd:parse(arg or {})
print(opt)
print("Building Index of users (users that appear at least one time in both train and test files")
index_users=buildIndex(opt.training_cascades,opt.testing_cascades)
print("Reading training and testing cascades")
train_cascades=readFromFile(opt.training_cascades,index_users)
test_cascades=readFromFile(opt.testing_cascades,index_users)
print("Initalisation of the embeddings...")
-- print(train_cascades.index)
local zs={}
for u=1,train_cascades.nb_users do
zs[u]=nn.Linear(1,opt.N)
zs[u]:reset(opt.uniform)
end
local is=torch.Tensor(1):fill(1)
local criterion=nn.MarginRankingCriterion(1)
for iteration=1,opt.maxEpoch do
-- Evaluaton
if ((iteration-1)%opt.evaluationEpoch==0) then
local distance_matrix=computeDistanceMatrix(zs,train_cascades.nb_users)
local train_map=computeMAP(train_cascades.cascades,train_cascades.size_cascades,distance_matrix)
print("Training MAP = "..train_map)
local testing_map=computeMAP(test_cascades.cascades,test_cascades.size_cascades,distance_matrix)
print("Testing MAP = "..testing_map)
end
-- SGD
local total_loss=0
for i=1,train_cascades.nb_cascades do
local idx_cascade=math.random(train_cascades.nb_cascades)
local cascade=train_cascades.cascades[idx_cascade]
local size_cascade=train_cascades.size_cascades[idx_cascade]
local user_source=cascade[1] -- source of the cascade
local idx_contaminated=math.random(size_cascade-1)+1
local user_contaminated=cascade[idx_contaminated] -- one user in the cascade, but not the source
local user_further=math.random(train_cascades.nb_users) -- another user which is not between user_source and user_contaminated in the cascade
local flag=true
while(flag) do
flag=false
for i=1,idx_contaminated do if (cascade[i]==user_further) then flag=true end end
if (flag) then user_further=math.random(train_cascades.nb_users) end
end
-- Building the modele
local input=nn.Identity()()
local z_source=zs[user_source](input)
local z_contaminated=zs[user_contaminated](input)
local z_further=zs[user_further](input)
local d1=nn.PairwiseDistance(2)({z_source,z_contaminated})
local d2=nn.PairwiseDistance(2)({z_source,z_further})
local model=nn.gModule({input},{d1,d2})
-- forward/backward
model:zeroGradParameters()
local out=model:forward(is)
local loss=criterion:forward(out,-1)
total_loss=total_loss+loss
local delta=criterion:backward(out,-1)
model:backward(is,delta)
model:updateParameters(opt.learningRate)
end
total_loss=total_loss/train_cascades.nb_cascades
print("Average loss at iteration "..iteration .." is "..total_loss)
end
--- Save embeddings in a file
if (opt.outputFile~="") then
print("Saving embeddings in "..opt.outputFile)
io.output(opt.outputFile)
for u,idx in pairs(index_users) do
local emb=zs[idx]:forward(is)
io.write(u)
for j=1,emb:size(1) do
io.write(" "..emb[j])
end
io.write("\n")
end
end