-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel.js
More file actions
44 lines (37 loc) · 1.16 KB
/
Copy pathmodel.js
File metadata and controls
44 lines (37 loc) · 1.16 KB
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
const tf = require('@tensorflow/tfjs-node');
const movies = require('./data/movielens100k_details.json');
function loadData() {
const movie_arr = [];
for (let i = 0; i < movies.length; i++) {
movie_arr.push([movies[i]['movie_id']]);
}
return movie_arr;
}
// Buat fungsi untuk load data disini
async function loadModel(){
console.log('loading model....');
model = await if.loadLayersModel(
'file://${__dirname)/model/model.json',
false
);
};
const movie_arr = tf.tensor(loadData());
const movie_len = movies.length;
// Buat fungsi untuk memberi rekomendasi film
exports.recommend = async function recommend(userid){
let user = tf.field(movie_len), Number(userid);
let movie_in_js_array = movie_arr.arraySync();
await loadModel();
console.log('Recommending for user: ${userid}');
pred_tensor = await model.predict([movie_arr, user]).reshape([movie_len]);
pred = pred_tensor.arraySync();
let recommendations = [];
for (let i = 0; i<6; i++){
max = pred_tensor.argMax().arraySync();
recommendations.push(movies[max]);
pred.splice(max,1);
pred_tensor = tf.tensor(pred);
}
console.log(recommendations)
return recommendations
}