|
| 1 | +# Copyright (c) 2018 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 unittest |
| 16 | +import numpy as np |
| 17 | +import random |
| 18 | +import sys |
| 19 | + |
| 20 | +import paddle |
| 21 | +import paddle.fluid as fluid |
| 22 | +import paddle.fluid.core as core |
| 23 | +from test_imperative_base import new_program_scope |
| 24 | +from paddle.fluid.imperative.base import to_variable |
| 25 | + |
| 26 | +NUM_USERS = 100 |
| 27 | +NUM_ITEMS = 1000 |
| 28 | + |
| 29 | +BATCH_SIZE = 32 |
| 30 | +NUM_BATCHES = 2 |
| 31 | + |
| 32 | + |
| 33 | +class MLP(fluid.imperative.Layer): |
| 34 | + def __init__(self, name_scope): |
| 35 | + super(MLP, self).__init__(name_scope) |
| 36 | + self._user_latent = fluid.imperative.FC(self.full_name(), 256) |
| 37 | + self._item_latent = fluid.imperative.FC(self.full_name(), 256) |
| 38 | + |
| 39 | + self._user_layers = [] |
| 40 | + self._item_layers = [] |
| 41 | + self._hid_sizes = [128, 64] |
| 42 | + for i in range(len(self._hid_sizes)): |
| 43 | + self._user_layers.append( |
| 44 | + self.add_sublayer( |
| 45 | + 'user_layer_%d' % i, |
| 46 | + fluid.imperative.FC( |
| 47 | + self.full_name(), self._hid_sizes[i], act='relu'))) |
| 48 | + self._item_layers.append( |
| 49 | + self.add_sublayer( |
| 50 | + 'item_layer_%d' % i, |
| 51 | + fluid.imperative.FC( |
| 52 | + self.full_name(), self._hid_sizes[i], act='relu'))) |
| 53 | + |
| 54 | + def forward(self, users, items): |
| 55 | + users = self._user_latent(users) |
| 56 | + items = self._item_latent(items) |
| 57 | + |
| 58 | + for ul, il in zip(self._user_layers, self._item_layers): |
| 59 | + users = ul(users) |
| 60 | + items = il(items) |
| 61 | + return fluid.layers.elementwise_mul(users, items) |
| 62 | + |
| 63 | + |
| 64 | +class DMF(fluid.imperative.Layer): |
| 65 | + def __init__(self, name_scope): |
| 66 | + super(DMF, self).__init__(name_scope) |
| 67 | + self._user_latent = fluid.imperative.FC(self.full_name(), 256) |
| 68 | + self._item_latent = fluid.imperative.FC(self.full_name(), 256) |
| 69 | + self._match_layers = [] |
| 70 | + self._hid_sizes = [128, 64] |
| 71 | + for i in range(len(self._hid_sizes)): |
| 72 | + self._match_layers.append( |
| 73 | + self.add_sublayer( |
| 74 | + 'match_layer_%d' % i, |
| 75 | + fluid.imperative.FC( |
| 76 | + self.full_name(), self._hid_sizes[i], act='relu'))) |
| 77 | + self._mat |
| 78 | + |
| 79 | + def forward(self, users, items): |
| 80 | + users = self._user_latent(users) |
| 81 | + items = self._item_latent(items) |
| 82 | + match_vec = fluid.layers.concat( |
| 83 | + [users, items], axis=len(users.shape) - 1) |
| 84 | + for l in self._match_layers: |
| 85 | + match_vec = l(match_vec) |
| 86 | + return match_vec |
| 87 | + |
| 88 | + |
| 89 | +class DeepCF(fluid.imperative.Layer): |
| 90 | + def __init__(self, name_scope): |
| 91 | + super(DeepCF, self).__init__(name_scope) |
| 92 | + |
| 93 | + self._user_emb = fluid.imperative.Embedding(self.full_name(), |
| 94 | + [NUM_USERS, 256]) |
| 95 | + self._item_emb = fluid.imperative.Embedding(self.full_name(), |
| 96 | + [NUM_ITEMS, 256]) |
| 97 | + |
| 98 | + self._mlp = MLP(self.full_name()) |
| 99 | + self._dmf = DMF(self.full_name()) |
| 100 | + self._match_fc = fluid.imperative.FC(self.full_name(), 1, act='sigmoid') |
| 101 | + |
| 102 | + def forward(self, users, items): |
| 103 | + users_emb = self._user_emb(users) |
| 104 | + items_emb = self._item_emb(items) |
| 105 | + |
| 106 | + mlp_predictive = self._mlp(users_emb, items_emb) |
| 107 | + dmf_predictive = self._dmf(users_emb, items_emb) |
| 108 | + predictive = fluid.layers.concat( |
| 109 | + [mlp_predictive, dmf_predictive], |
| 110 | + axis=len(mlp_predictive.shape) - 1) |
| 111 | + prediction = self._match_fc(predictive) |
| 112 | + return prediction |
| 113 | + |
| 114 | + |
| 115 | +def get_data(): |
| 116 | + user_ids = [] |
| 117 | + item_ids = [] |
| 118 | + labels = [] |
| 119 | + for uid in range(NUM_USERS): |
| 120 | + for iid in range(NUM_ITEMS): |
| 121 | + # 10% positive |
| 122 | + label = float(random.randint(1, 10) == 1) |
| 123 | + user_ids.append(uid) |
| 124 | + item_ids.append(iid) |
| 125 | + labels.append(label) |
| 126 | + indices = np.arange(NUM_USERS * NUM_ITEMS) |
| 127 | + np.random.shuffle(indices) |
| 128 | + users_np = np.array(user_ids, dtype=np.int64)[indices] |
| 129 | + items_np = np.array(item_ids, dtype=np.int64)[indices] |
| 130 | + labels_np = np.array(labels, dtype=np.float32)[indices] |
| 131 | + return np.expand_dims(users_np, -1), \ |
| 132 | + np.expand_dims(items_np, -1), \ |
| 133 | + np.expand_dims(labels_np, -1) |
| 134 | + |
| 135 | + |
| 136 | +class TestImperativeDeepCF(unittest.TestCase): |
| 137 | + def test_gan_float32(self): |
| 138 | + seed = 90 |
| 139 | + users_np, items_np, labels_np = get_data() |
| 140 | + |
| 141 | + startup = fluid.Program() |
| 142 | + startup.random_seed = seed |
| 143 | + main = fluid.Program() |
| 144 | + main.random_seed = seed |
| 145 | + |
| 146 | + scope = fluid.core.Scope() |
| 147 | + with new_program_scope(main=main, startup=startup, scope=scope): |
| 148 | + users = fluid.layers.data('users', [1], dtype='int64') |
| 149 | + items = fluid.layers.data('items', [1], dtype='int64') |
| 150 | + labels = fluid.layers.data('labels', [1], dtype='float32') |
| 151 | + |
| 152 | + deepcf = DeepCF('deepcf') |
| 153 | + prediction = deepcf(users, items) |
| 154 | + loss = fluid.layers.reduce_sum( |
| 155 | + fluid.layers.log_loss(prediction, labels)) |
| 156 | + adam = fluid.optimizer.AdamOptimizer(0.01) |
| 157 | + adam.minimize(loss) |
| 158 | + |
| 159 | + exe = fluid.Executor(fluid.CPUPlace( |
| 160 | + ) if not core.is_compiled_with_cuda() else fluid.CUDAPlace(0)) |
| 161 | + exe.run(startup) |
| 162 | + for slice in range(0, BATCH_SIZE * NUM_BATCHES, BATCH_SIZE): |
| 163 | + static_loss = exe.run( |
| 164 | + main, |
| 165 | + feed={ |
| 166 | + users.name: users_np[slice:slice + BATCH_SIZE], |
| 167 | + items.name: items_np[slice:slice + BATCH_SIZE], |
| 168 | + labels.name: labels_np[slice:slice + BATCH_SIZE] |
| 169 | + }, |
| 170 | + fetch_list=[loss])[0] |
| 171 | + sys.stderr.write('static loss %s\n' % static_loss) |
| 172 | + |
| 173 | + with fluid.imperative.guard(): |
| 174 | + fluid.default_startup_program().random_seed = seed |
| 175 | + fluid.default_main_program().random_seed = seed |
| 176 | + |
| 177 | + deepcf = DeepCF('deepcf') |
| 178 | + for slice in range(0, BATCH_SIZE * NUM_BATCHES, BATCH_SIZE): |
| 179 | + prediction = deepcf( |
| 180 | + to_variable(users_np[slice:slice + BATCH_SIZE]), |
| 181 | + to_variable(items_np[slice:slice + BATCH_SIZE])) |
| 182 | + loss = fluid.layers.reduce_sum( |
| 183 | + fluid.layers.log_loss(prediction, |
| 184 | + to_variable(labels_np[slice:slice + |
| 185 | + BATCH_SIZE]))) |
| 186 | + loss._backward() |
| 187 | + adam = fluid.optimizer.AdamOptimizer(0.01) |
| 188 | + adam.minimize(loss) |
| 189 | + deepcf.clear_gradients() |
| 190 | + dy_loss = loss._numpy() |
| 191 | + |
| 192 | + self.assertEqual(static_loss, dy_loss) |
| 193 | + |
| 194 | + |
| 195 | +if __name__ == '__main__': |
| 196 | + unittest.main() |
0 commit comments