train.py 4.3 KB
Newer Older
Y
Yu Yang 已提交
1 2 3
import paddle.v2 as paddle
import cPickle
import copy
D
dzhwinter 已提交
4
import os
Y
Yu Yang 已提交
5

D
dzhwinter 已提交
6
with_gpu = os.getenv('WITH_GPU', '0') != '0'
Y
Yu Yang 已提交
7

H
Helin Wang 已提交
8

Q
qijun 已提交
9
def get_usr_combined_features():
Y
Yu Yang 已提交
10 11 12 13 14
    uid = paddle.layer.data(
        name='user_id',
        type=paddle.data_type.integer_value(
            paddle.dataset.movielens.max_user_id() + 1))
    usr_emb = paddle.layer.embedding(input=uid, size=32)
L
livc 已提交
15
    usr_fc = paddle.layer.fc(input=usr_emb, size=32)
Y
Yu Yang 已提交
16 17 18 19

    usr_gender_id = paddle.layer.data(
        name='gender_id', type=paddle.data_type.integer_value(2))
    usr_gender_emb = paddle.layer.embedding(input=usr_gender_id, size=16)
L
livc 已提交
20
    usr_gender_fc = paddle.layer.fc(input=usr_gender_emb, size=16)
Y
Yu Yang 已提交
21 22 23 24 25 26

    usr_age_id = paddle.layer.data(
        name='age_id',
        type=paddle.data_type.integer_value(
            len(paddle.dataset.movielens.age_table)))
    usr_age_emb = paddle.layer.embedding(input=usr_age_id, size=16)
L
livc 已提交
27
    usr_age_fc = paddle.layer.fc(input=usr_age_emb, size=16)
Y
Yu Yang 已提交
28 29 30 31 32 33

    usr_job_id = paddle.layer.data(
        name='job_id',
        type=paddle.data_type.integer_value(
            paddle.dataset.movielens.max_job_id() + 1))
    usr_job_emb = paddle.layer.embedding(input=usr_job_id, size=16)
L
livc 已提交
34
    usr_job_fc = paddle.layer.fc(input=usr_job_emb, size=16)
Y
Yu Yang 已提交
35 36

    usr_combined_features = paddle.layer.fc(
L
livc 已提交
37
        input=[usr_fc, usr_gender_fc, usr_age_fc, usr_job_fc],
Y
Yu Yang 已提交
38 39
        size=200,
        act=paddle.activation.Tanh())
Q
qijun 已提交
40
    return usr_combined_features
Y
Yu Yang 已提交
41

Q
qijun 已提交
42

Q
qijun 已提交
43 44
def get_mov_combined_features():
    movie_title_dict = paddle.dataset.movielens.get_movie_title_dict()
Y
Yu Yang 已提交
45 46 47 48 49
    mov_id = paddle.layer.data(
        name='movie_id',
        type=paddle.data_type.integer_value(
            paddle.dataset.movielens.max_movie_id() + 1))
    mov_emb = paddle.layer.embedding(input=mov_id, size=32)
L
livc 已提交
50
    mov_fc = paddle.layer.fc(input=mov_emb, size=32)
Y
Yu Yang 已提交
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65

    mov_categories = paddle.layer.data(
        name='category_id',
        type=paddle.data_type.sparse_binary_vector(
            len(paddle.dataset.movielens.movie_categories())))
    mov_categories_hidden = paddle.layer.fc(input=mov_categories, size=32)

    mov_title_id = paddle.layer.data(
        name='movie_title',
        type=paddle.data_type.integer_value_sequence(len(movie_title_dict)))
    mov_title_emb = paddle.layer.embedding(input=mov_title_id, size=32)
    mov_title_conv = paddle.networks.sequence_conv_pool(
        input=mov_title_emb, hidden_size=32, context_len=3)

    mov_combined_features = paddle.layer.fc(
L
livc 已提交
66
        input=[mov_fc, mov_categories_hidden, mov_title_conv],
Y
Yu Yang 已提交
67 68
        size=200,
        act=paddle.activation.Tanh())
Q
qijun 已提交
69
    return mov_combined_features
Q
qijun 已提交
70

Y
Yu Yang 已提交
71

Q
qijun 已提交
72
def main():
D
dzhwinter 已提交
73
    paddle.init(use_gpu=with_gpu)
Q
qijun 已提交
74 75
    usr_combined_features = get_usr_combined_features()
    mov_combined_features = get_mov_combined_features()
Y
Yu Yang 已提交
76 77
    inference = paddle.layer.cos_sim(
        a=usr_combined_features, b=mov_combined_features, size=1, scale=5)
78
    cost = paddle.layer.square_error_cost(
Y
Yu Yang 已提交
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
        input=inference,
        label=paddle.layer.data(
            name='score', type=paddle.data_type.dense_vector(1)))

    parameters = paddle.parameters.create(cost)

    trainer = paddle.trainer.SGD(
        cost=cost,
        parameters=parameters,
        update_equation=paddle.optimizer.Adam(learning_rate=1e-4))
    feeding = {
        'user_id': 0,
        'gender_id': 1,
        'age_id': 2,
        'job_id': 3,
        'movie_id': 4,
        'category_id': 5,
        'movie_title': 6,
        'score': 7
    }

    def event_handler(event):
        if isinstance(event, paddle.event.EndIteration):
            if event.batch_id % 100 == 0:
                print "Pass %d Batch %d Cost %.2f" % (
                    event.pass_id, event.batch_id, event.cost)

    trainer.train(
        reader=paddle.batch(
            paddle.reader.shuffle(
                paddle.dataset.movielens.train(), buf_size=8192),
            batch_size=256),
        event_handler=event_handler,
        feeding=feeding,
        num_passes=1)

    user_id = 234
    movie_id = 345

    user = paddle.dataset.movielens.user_info()[user_id]
    movie = paddle.dataset.movielens.movie_info()[movie_id]

    feature = user.value() + movie.value()

    infer_dict = copy.copy(feeding)
    del infer_dict['score']

    prediction = paddle.infer(
        output_layer=inference,
        parameters=parameters,
        input=[feature],
        feeding=infer_dict)
    print(prediction + 5) / 2


if __name__ == '__main__':
    main()