|
| 1 | +# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import scipy.sparse as sp |
| 16 | +import numpy as np |
| 17 | +from time import time |
| 18 | +import argparse |
| 19 | + |
| 20 | + |
| 21 | +def parse_args(): |
| 22 | + parser = argparse.ArgumentParser(description="Run GMF.") |
| 23 | + parser.add_argument( |
| 24 | + '--path', nargs='?', default='Data/', help='Input data path.') |
| 25 | + parser.add_argument( |
| 26 | + '--dataset', nargs='?', default='ml-1m', help='Choose a dataset.') |
| 27 | + parser.add_argument( |
| 28 | + '--num_neg', |
| 29 | + type=int, |
| 30 | + default=4, |
| 31 | + help='Number of negative instances to pair with a positive instance.') |
| 32 | + parser.add_argument( |
| 33 | + '--train_data_path', |
| 34 | + type=str, |
| 35 | + default="Data/train_data.csv", |
| 36 | + help='train_data_path') |
| 37 | + return parser.parse_args() |
| 38 | + |
| 39 | + |
| 40 | +def get_train_data(filename, write_file, num_negatives): |
| 41 | + ''' |
| 42 | + Read .rating file and Return dok matrix. |
| 43 | + The first line of .rating file is: num_users\t num_items |
| 44 | + ''' |
| 45 | + # Get number of users and items |
| 46 | + num_users, num_items = 0, 0 |
| 47 | + with open(filename, "r") as f: |
| 48 | + line = f.readline() |
| 49 | + while line != None and line != "": |
| 50 | + arr = line.split("\t") |
| 51 | + u, i = int(arr[0]), int(arr[1]) |
| 52 | + num_users = max(num_users, u) |
| 53 | + num_items = max(num_items, i) |
| 54 | + line = f.readline() |
| 55 | + print("users_num:", num_users, "items_num:", num_items) |
| 56 | + # Construct matrix |
| 57 | + mat = sp.dok_matrix((num_users + 1, num_items + 1), dtype=np.float32) |
| 58 | + with open(filename, "r") as f: |
| 59 | + line = f.readline() |
| 60 | + while line != None and line != "": |
| 61 | + arr = line.split("\t") |
| 62 | + user, item, rating = int(arr[0]), int(arr[1]), float(arr[2]) |
| 63 | + if (rating > 0): |
| 64 | + mat[user, item] = 1.0 |
| 65 | + line = f.readline() |
| 66 | + |
| 67 | + file = open(write_file, 'w') |
| 68 | + print("writing " + write_file) |
| 69 | + |
| 70 | + for (u, i) in mat.keys(): |
| 71 | + # positive instance |
| 72 | + user_input = str(u) |
| 73 | + item_input = str(i) |
| 74 | + label = str(1) |
| 75 | + sample = "{0},{1},{2}".format(user_input, item_input, label) + "\n" |
| 76 | + file.write(sample) |
| 77 | + # negative instances |
| 78 | + for t in range(num_negatives): |
| 79 | + j = np.random.randint(num_items) |
| 80 | + while (u, j) in mat.keys(): |
| 81 | + j = np.random.randint(num_items) |
| 82 | + user_input = str(u) |
| 83 | + item_input = str(j) |
| 84 | + label = str(0) |
| 85 | + sample = "{0},{1},{2}".format(user_input, item_input, label) + "\n" |
| 86 | + file.write(sample) |
| 87 | + |
| 88 | + |
| 89 | +if __name__ == "__main__": |
| 90 | + args = parse_args() |
| 91 | + get_train_data(args.path + args.dataset + ".train.rating", |
| 92 | + args.train_data_path, args.num_neg) |
0 commit comments