0
$\begingroup$

Does anyone know what does the num_negatives mean? What is it for?

def get_train_samples(train_mat, num_negatives):
    user_input, item_input, labels = [], [], []
    num_user, num_item = train_mat.shape
    for (u, i) in train_mat.keys():
        user_input.append(u)
        item_input.append(i)
        labels.append(1)
        # negative instances
        for t in range(num_negatives):
            j = np.random.randint(num_item)
            while (u, j) in train_mat.keys():
                j = np.random.randint(num_item)
            user_input.append(u)
            item_input.append(j)
            labels.append(0)
    return user_input, item_input, labels

This occurs here:

user_input, item_input, labels = get_train_samples(train_mat, NUM_NEGATIVES)

And here:

hist = model.fit([np.array(user_input), np.array(item_input)], np.array(labels), 
                 epochs=EPOCHS, verbose=VERBOSE, shuffle=True, batch_size = BATCH_SIZE,
                 validation_data=([np.array(val_user_input), np.array(val_item_input)]),
                  callbacks=CALLBACKS)
$\endgroup$
1
  • 1
    $\begingroup$ More context is likely needed to provide a good answer. What is this model trying to do? $\endgroup$
    – zachdj
    Commented Nov 12, 2020 at 14:36

2 Answers 2

1
$\begingroup$

From the comment specified by the programmer it is clear that he intends to generate negative samples

def get_train_samples(train_mat, num_negatives):
    user_input, item_input, labels = [], [], []
    num_user, num_item = train_mat.shape
    for (u, i) in train_mat.keys():
        user_input.append(u)
        item_input.append(i)
        labels.append(1)
        # negative instances   <============================  FROM THIS COMMENT LINE
        for t in range(num_negatives):
            j = np.random.randint(num_item)
            while (u, j) in train_mat.keys():
                j = np.random.randint(num_item)
            user_input.append(u)
            item_input.append(j)
            labels.append(0)
    return user_input, item_input, labels
$\endgroup$
0
$\begingroup$

Context would be helpful here, but the "train" and "mat" in train_mat probably indicate the intention of the programmer that this be a "training matrix", and thus "num_negatives" appears to be a parameter specifying the number of negative samples (i.e., label=0).

$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.