Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
CD-BPR
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Wiki
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Arthur Batel
CD-BPR
Commits
093807db
Commit
093807db
authored
1 year ago
by
Céline Robardet
Browse files
Options
Downloads
Patches
Plain Diff
accuracy
parent
d5e68713
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
code/nary_model/BPR_model.py
+12
-1
12 additions, 1 deletion
code/nary_model/BPR_model.py
code/nary_model/main_nary.py
+42
-41
42 additions, 41 deletions
code/nary_model/main_nary.py
with
54 additions
and
42 deletions
code/nary_model/BPR_model.py
+
12
−
1
View file @
093807db
...
...
@@ -137,6 +137,7 @@ class BPRModel(nn.Module):
all_labels
=
None
all_decisions
=
None
first
=
True
all_preferences
=
None
for
k
in
range
(
num_kc
):
trips
=
test_triplets
[
k
]
y
=
y_test
[
k
]
...
...
@@ -162,6 +163,11 @@ class BPRModel(nn.Module):
comp
=
negative_scores
<
positive_scores
comp
=
comp
.
cpu
()
all_decisions
=
comp
positive_scores
=
positive_scores
.
cpu
()
negative_scores
=
negative_scores
.
cpu
()
preferences
=
np
.
concatenate
((
users_batch
.
cpu
().
reshape
(
-
1
,
1
),
items_batch
.
cpu
().
reshape
(
-
1
,
1
),
positive_scores
.
detach
().
numpy
().
reshape
(
-
1
,
1
)),
axis
=
1
)
pref_neg
=
np
.
concatenate
((
users_batch
.
cpu
().
reshape
(
-
1
,
1
),
negatives_batch
.
cpu
().
reshape
(
-
1
,
1
),
negative_scores
.
detach
().
numpy
().
reshape
(
-
1
,
1
)),
axis
=
1
)
all_preferences
=
np
.
concatenate
((
preferences
,
pref_neg
),
axis
=
0
)
first
=
False
else
:
all_labels
=
np
.
concatenate
((
all_labels
,
y_true
),
axis
=
0
)
...
...
@@ -169,9 +175,14 @@ class BPRModel(nn.Module):
comp
=
negative_scores
<
positive_scores
comp
=
comp
.
cpu
()
all_decisions
=
np
.
concatenate
((
all_decisions
,
comp
),
axis
=
0
)
preferences
=
np
.
concatenate
((
users_batch
.
cpu
().
reshape
(
-
1
,
1
),
items_batch
.
cpu
().
reshape
(
-
1
,
1
),
positive_scores
.
detach
().
numpy
().
reshape
(
-
1
,
1
)),
axis
=
1
)
pref_neg
=
np
.
concatenate
((
users_batch
.
cpu
().
reshape
(
-
1
,
1
),
negatives_batch
.
cpu
().
reshape
(
-
1
,
1
),
negative_scores
.
detach
().
numpy
().
reshape
(
-
1
,
1
)),
axis
=
1
)
preferences
=
np
.
concatenate
((
preferences
,
pref_neg
),
axis
=
0
)
#print(preferences.shape)
all_preferences
=
np
.
concatenate
((
all_preferences
,
preferences
),
axis
=
0
)
correct_ranking
=
sum
(
negative_scores
<
positive_scores
)
# for score in negative_scores)
mse1
=
mean_squared_error
(
all_labels
,
all_predictions
)
auc
=
roc_auc_score
(
all_labels
,
all_predictions
)
return
accuracy_score
(
all_labels
,
all_decisions
),
precision_score
(
all_labels
,
all_decisions
),
recall_score
(
all_labels
,
all_decisions
),
all_decisions
return
accuracy_score
(
all_labels
,
all_decisions
),
precision_score
(
all_labels
,
all_decisions
),
recall_score
(
all_labels
,
all_decisions
),
all_decisions
,
all_preferences
This diff is collapsed.
Click to expand it.
code/nary_model/main_nary.py
+
42
−
41
View file @
093807db
...
...
@@ -127,41 +127,49 @@ def parse_dataframe(data, dico_kc, dico_users, dico_items, d_quest_val, nb_item_
else
:
return
dico_items
,
triplets
,
y_true
def
compute_
pred
(
all_predictions
,
test
,
y_t
es
t
,
dico_users
,
dico_items
):
r
=
0
all_test
=
test
[
0
]
for
i
in
range
(
1
,
len
(
test
)):
all_test
=
np
.
concatenate
((
test
[
i
],
all_test
),
axis
=
0
)
def
compute_
accuracy_multi_mod
(
all_preferenc
es
,
dico_users
,
dico_items
,
dataTest
):
# Remove duplicate
new_array
=
[
tuple
(
row
)
for
row
in
all_preferences
]
all_preferences
=
np
.
unique
(
new_array
,
axis
=
0
)
# Revert dictionaries
l
=
list
(
dico_items
)
lu
=
list
(
dico_users
)
# Extract the questions
list_quest
=
[]
for
i
in
range
(
len
(
all_
t
es
t
)):
q
,
r
=
parse_it
(
l
[
all_t
es
t
[
i
,
1
]])
for
i
in
range
(
len
(
all_
preferenc
es
)):
q
,
r
=
parse_it
(
l
[
int
(
all_preferenc
es
[
i
,
1
]
)
])
list_quest
.
append
(
int
(
q
))
all_
t
es
t
=
np
.
concatenate
((
all_
test
,
np
.
array
(
list_quest
).
reshape
(
-
1
,
1
)),
axis
=
1
)
all_
preferenc
es
=
np
.
concatenate
((
all_
preferences
,
np
.
array
(
list_quest
).
reshape
(
-
1
,
1
)),
axis
=
1
)
# Compute the predicted value
responses
=
[]
list_user
=
flattern_array
(
all_
t
es
t
[:,
0
])
list_user
=
flattern_array
(
all_
preferenc
es
[:,
0
])
for
u
in
list_user
:
my_users
=
all_
t
es
t
[:,
0
]
==
u
list_quest
=
flattern_array
(
all_
t
es
t
[
my_users
,
4
])
my_users
=
all_
preferenc
es
[:,
0
]
==
u
list_quest
=
flattern_array
(
all_
preferenc
es
[
my_users
,
3
])
for
quest
in
list_quest
:
rows
=
all_
t
es
t
[:,
4
]
==
quest
rows
=
all_
preferenc
es
[:,
3
]
==
quest
my_rows
=
np
.
logical_and
(
rows
,
my_users
)
# we go the rows with all the modalities
modality
=
-
1
for
t
in
range
(
len
(
all_test
[
my_rows
])):
ione
=
all_test
[
my_rows
][
t
][
1
]
itwo
=
all_test
[
my_rows
][
t
][
2
]
q1
,
r1
=
parse_it
(
l
[
ione
])
q2
,
r2
=
parse_it
(
l
[
itwo
])
if
(
all_predictions
[
my_rows
][
t
]
==
1
):
modality
=
r1
else
:
modality
=
r2
responses
.
append
([
dico_users
[
u
],
quest
,
modality
])
return
responses
# we got the rows corresponding to a user and a question
# and take the row with the maximum predicted values
m
=
np
.
argmax
(
all_preferences
[
my_rows
,
2
])
# Get the modality
item
=
int
(
all_preferences
[
my_rows
][
m
][
1
])
q1
,
modality
=
parse_it
(
l
[
item
])
responses
.
append
([
lu
[
int
(
u
)],
int
(
quest
),
int
(
modality
)])
# Sort responses
dfPred
=
pd
.
DataFrame
(
responses
,
columns
=
[
'
user_id
'
,
'
question
'
,
'
modality
'
])
dfPred
=
dfPred
.
sort_values
(
by
=
[
'
user_id
'
,
'
question
'
])
pred
=
dfPred
[
'
modality
'
].
values
# True data
dfTrue
=
pd
.
read_csv
(
dataTest
,
names
=
[
'
user_id
'
,
'
item_id
'
,
'
correct
'
,
'
knowledge
'
,
"
question
"
])
for
row_index
,
row
in
dfTrue
.
iterrows
():
col
=
row
[
'
item_id
'
]
q
,
r
=
parse_it
(
col
)
dfTrue
.
at
[
row_index
,
'
question
'
]
=
int
(
q
)
dfTrue
=
dfTrue
.
drop_duplicates
(
subset
=
[
'
user_id
'
,
'
question
'
])
dfTrue
=
dfTrue
.
sort_values
(
by
=
[
'
user_id
'
,
'
question
'
])
print
(
"
Accuracy multi-modal
"
,
accuracy_score
(
dfTrue
[
'
correct
'
].
values
,
pred
),
"
RMSE
"
,
np
.
sqrt
(
mean_squared_error
(
dfTrue
[
'
correct
'
].
values
,
pred
)))
...
...
@@ -201,19 +209,12 @@ if __name__ == '__main__':
new_embedding_value
=
bpr_model
.
user_embeddings
.
weight
.
clone
().
detach
().
cpu
().
numpy
()
write_file_doa
(
filename
,
new_embedding_value
,
train
,
dico_kc
,
dico_users
,
dico_items
)
doa
,
rdoa
=
compute_doa
(
filename
)
# write embed items
#new_embedding_items = bpr_model.item_embeddings.weight.clone().detach().cpu().numpy()
#write_file(filename+"embedding_items.csv", new_embedding_items[0:nb_item_train])
#write_file_std(FileName1+"kc_emb.txt", dico_items)
#print("Accuracy and Doa on train dataset:", acc, doa)
# Test
acc
,
precision
,
rappel
,
all_predictions
=
bpr_model
.
evaluate_model
(
test
,
len
(
dico_kc
),
y_test
)
acc
,
precision
,
rappel
,
all_predictions
,
all_preferences
=
bpr_model
.
evaluate_model
(
test
,
len
(
dico_kc
),
y_test
)
s
=
str
(
acc
)
+
"
,
"
+
str
(
precision
)
+
"
,
"
+
str
(
rappel
)
+
"
,
"
+
str
(
doa
)
#for i in range(len(rdoa)):
# s = s + ','+ str(rdoa[i])
print
(
s
)
compute_accuracy_multi_mod
(
all_preferences
,
dico_users
,
dico_items
,
dataTest
)
# coding users and kc from user_label.csv
'''
os.getcwd()
...
...
@@ -232,6 +233,6 @@ if __name__ == '__main__':
questEval[ind, kc] = val
write_file(filename+
"
_user_quest_label.csv
"
, questEval)
'''
the_predictions
=
compute_pred
(
all_predictions
,
test
,
y_test
,
dico_users
,
dico_items
)
file
=
"
_test_predictions_
"
+
str
(
alpha
)
+
"
.csv
"
write_file
(
filename
+
file
,
np
.
array
(
the_predictions
))
\ No newline at end of file
#the_predictions = compute_pred(all_predictions, test, y_test, dico_users, dico_items)
#file = "_test_predictions_"+str(alpha)+".csv"
#write_file(filename + file, np.array(the_predictions))
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment