code/ENMF.py
def dev_step(test_set, train_m, test_m,args):
    """
    Evaluates model on a dev set

    """
    user_te = np.array(test_set.keys())
    user_te2 = user_te[:, np.newaxis]

    ll = int(len(user_te) / 128) + 1

    recall50 = []
    recall100 = []
    recall200 = []
    ndcg50 = []
    ndcg100 = []
    ndcg200 = []

    for batch_num in range(ll):

        start_index = batch_num * 128
        end_index = min((batch_num + 1) * 128, len(user_te))
        u_batch = user_te2[start_index:end_index]

        batch_users = end_index - start_index

        feed_dict = {
            deep.input_u: u_batch,
            deep.dropout_keep_prob: 1.0,
        }

        pre = sess.run(
            deep.pre, feed_dict)

        u_b = user_te[start_index:end_index]

        pre = np.array(pre)
        pre = np.delete(pre, -1, axis=1)

        idx = np.zeros_like(pre, dtype=bool)
        idx[train_m[u_b].nonzero()] = True
        pre[idx] = -np.inf

        # recall

        recall = []

        for kj in args.topK:
            idx_topk_part = np.argpartition(-pre, kj, 1)

            # print pre[np.arange(batch_users)[:, np.newaxis], idx_topk_part[:, :kj]]

            # print idx_topk_part
            pre_bin = np.zeros_like(pre, dtype=bool)
            pre_bin[np.arange(batch_users)[:, np.newaxis], idx_topk_part[:, :kj]] = True

            # print pre_bin

            true_bin = np.zeros_like(pre, dtype=bool)
            true_bin[test_m[u_b].nonzero()] = True

            tmp = (np.logical_and(true_bin, pre_bin).sum(axis=1)).astype(np.float32)
            recall.append(tmp / true_bin.sum(axis=1))
            # print tmp

        # ndcg10
        ndcg = []

        for kj in args.topK:
            idx_topk_part = np.argpartition(-pre, kj, 1)

            topk_part = pre[np.arange(batch_users)[:, np.newaxis], idx_topk_part[:, :kj]]
            idx_part = np.argsort(-topk_part, axis=1)
            idx_topk = idx_topk_part[np.arange(end_index - start_index)[:, np.newaxis], idx_part]

            tp = np.log(2) / np.log(np.arange(2, kj + 2))

            test_batch = test_m[u_b]
            DCG = (test_batch[np.arange(batch_users)[:, np.newaxis],
                              idx_topk].toarray() * tp).sum(axis=1)

            IDCG = np.array([(tp[:min(n, kj)]).sum()
                             for n in test_batch.getnnz(axis=1)])
            ndcg.append(DCG / IDCG)

        recall50.append(recall[0])
        recall100.append(recall[1])
        recall200.append(recall[2])
        ndcg50.append(ndcg[0])
        ndcg100.append(ndcg[1])
        ndcg200.append(ndcg[2])

    recall50 = np.hstack(recall50)
    recall100 = np.hstack(recall100)
    recall200 = np.hstack(recall200)
    ndcg50 = np.hstack(ndcg50)
    ndcg100 = np.hstack(ndcg100)
    ndcg200 = np.hstack(ndcg200)

    print np.mean(recall50), np.mean(ndcg50)
    print np.mean(recall100), np.mean(ndcg100)
    print np.mean(recall200), np.mean(ndcg200)
    f1.write(str(np.mean(recall100)) + ' ' + str(np.mean(ndcg100)) + '\n')
    f1.flush()

    return loss

    Evaluates model on a dev set

    
Evaluates model on a dev set
Evaluates model on a dev set
def dev_step(test_set, train_m, test_m,args):
    user_te = np.array(test_set.keys())
    user_te2 = user_te[:, np.newaxis]
    ll = int(len(user_te) / 128) + 1
    recall50 = []
    recall100 = []
    recall200 = []
    ndcg50 = []
    ndcg100 = []
    ndcg200 = []
    for batch_num in range(ll):
        start_index = batch_num * 128
        end_index = min((batch_num + 1) * 128, len(user_te))
        u_batch = user_te2[start_index:end_index]
        batch_users = end_index - start_index
        feed_dict = {
            deep.input_u: u_batch,
            deep.dropout_keep_prob: 1.0,
        }
        pre = sess.run(
            deep.pre, feed_dict)
        u_b = user_te[start_index:end_index]
        pre = np.array(pre)
        pre = np.delete(pre, -1, axis=1)
        idx = np.zeros_like(pre, dtype=bool)
        idx[train_m[u_b].nonzero()] = True
        pre[idx] = -np.inf
        recall = []
        for kj in args.topK:
            idx_topk_part = np.argpartition(-pre, kj, 1)
            pre_bin = np.zeros_like(pre, dtype=bool)
            pre_bin[np.arange(batch_users)[:, np.newaxis], idx_topk_part[:, :kj]] = True
            true_bin = np.zeros_like(pre, dtype=bool)
            true_bin[test_m[u_b].nonzero()] = True
            tmp = (np.logical_and(true_bin, pre_bin).sum(axis=1)).astype(np.float32)
            recall.append(tmp / true_bin.sum(axis=1))
        ndcg = []
        for kj in args.topK:
            idx_topk_part = np.argpartition(-pre, kj, 1)
            topk_part = pre[np.arange(batch_users)[:, np.newaxis], idx_topk_part[:, :kj]]
            idx_part = np.argsort(-topk_part, axis=1)
            idx_topk = idx_topk_part[np.arange(end_index - start_index)[:, np.newaxis], idx_part]
            tp = np.log(2) / np.log(np.arange(2, kj + 2))
            test_batch = test_m[u_b]
            DCG = (test_batch[np.arange(batch_users)[:, np.newaxis],
                              idx_topk].toarray() * tp).sum(axis=1)
            IDCG = np.array([(tp[:min(n, kj)]).sum()
                             for n in test_batch.getnnz(axis=1)])
            ndcg.append(DCG / IDCG)
        recall50.append(recall[0])
        recall100.append(recall[1])
        recall200.append(recall[2])
        ndcg50.append(ndcg[0])
        ndcg100.append(ndcg[1])
        ndcg200.append(ndcg[2])
    recall50 = np.hstack(recall50)
    recall100 = np.hstack(recall100)
    recall200 = np.hstack(recall200)
    ndcg50 = np.hstack(ndcg50)
    ndcg100 = np.hstack(ndcg100)
    ndcg200 = np.hstack(ndcg200)
    print np.mean(recall50), np.mean(ndcg50)
    print np.mean(recall100), np.mean(ndcg100)
    print np.mean(recall200), np.mean(ndcg200)
    f1.write(str(np.mean(recall100)) + ' ' + str(np.mean(ndcg100)) + '\n')
    f1.flush()
    return loss
def dev_step ( test_set , train_m , test_m , args ) : user_te = np . array ( test_set . keys ( ) ) user_te2 = user_te [ : , np . newaxis ] ll = int ( len ( user_te ) / 128 ) + 1 recall50 = [ ] recall100 = [ ] recall200 = [ ] ndcg50 = [ ] ndcg100 = [ ] ndcg200 = [ ] for batch_num in range ( ll ) : start_index = batch_num * 128 end_index = min ( ( batch_num + 1 ) * 128 , len ( user_te ) ) u_batch = user_te2 [ start_index : end_index ] batch_users = end_index - start_index feed_dict = { deep . input_u : u_batch , deep . dropout_keep_prob : 1.0 , } pre = sess . run ( deep . pre , feed_dict ) u_b = user_te [ start_index : end_index ] pre = np . array ( pre ) pre = np . delete ( pre , - 1 , axis = 1 ) idx = np . zeros_like ( pre , dtype = bool ) idx [ train_m [ u_b ] . nonzero ( ) ] = True pre [ idx ] = - np . inf recall = [ ] for kj in args . topK : idx_topk_part = np . argpartition ( - pre , kj , 1 ) pre_bin = np . zeros_like ( pre , dtype = bool ) pre_bin [ np . arange ( batch_users ) [ : , np . newaxis ] , idx_topk_part [ : , : kj ] ] = True true_bin = np . zeros_like ( pre , dtype = bool ) true_bin [ test_m [ u_b ] . nonzero ( ) ] = True tmp = ( np . logical_and ( true_bin , pre_bin ) . sum ( axis = 1 ) ) . astype ( np . float32 ) recall . append ( tmp / true_bin . sum ( axis = 1 ) ) ndcg = [ ] for kj in args . topK : idx_topk_part = np . argpartition ( - pre , kj , 1 ) topk_part = pre [ np . arange ( batch_users ) [ : , np . newaxis ] , idx_topk_part [ : , : kj ] ] idx_part = np . argsort ( - topk_part , axis = 1 ) idx_topk = idx_topk_part [ np . arange ( end_index - start_index ) [ : , np . newaxis ] , idx_part ] tp = np . log ( 2 ) / np . log ( np . arange ( 2 , kj + 2 ) ) test_batch = test_m [ u_b ] DCG = ( test_batch [ np . arange ( batch_users ) [ : , np . newaxis ] , idx_topk ] . toarray ( ) * tp ) . sum ( axis = 1 ) IDCG = np . array ( [ ( tp [ : min ( n , kj ) ] ) . sum ( ) for n in test_batch . getnnz ( axis = 1 ) ] ) ndcg . append ( DCG / IDCG ) recall50 . append ( recall [ 0 ] ) recall100 . append ( recall [ 1 ] ) recall200 . append ( recall [ 2 ] ) ndcg50 . append ( ndcg [ 0 ] ) ndcg100 . append ( ndcg [ 1 ] ) ndcg200 . append ( ndcg [ 2 ] ) recall50 = np . hstack ( recall50 ) recall100 = np . hstack ( recall100 ) recall200 = np . hstack ( recall200 ) ndcg50 = np . hstack ( ndcg50 ) ndcg100 = np . hstack ( ndcg100 ) ndcg200 = np . hstack ( ndcg200 ) print np . mean ( recall50 ) , np . mean ( ndcg50 ) print np . mean ( recall100 ) , np . mean ( ndcg100 ) print np . mean ( recall200 ) , np . mean ( ndcg200 ) f1 . write ( str ( np . mean ( recall100 ) ) + ' ' + str ( np . mean ( ndcg100 ) ) + '\n' ) f1 . flush ( ) return loss
Evaluates model on a dev set
Evaluates model on a dev set
"""
    Evaluates model on a dev set

    """ # recall # print pre[np.arange(batch_users)[:, np.newaxis], idx_topk_part[:, :kj]] # print idx_topk_part # print pre_bin # print tmp # ndcg10
test_set
train_m
test_m
args