|
119 | 119 | { |
120 | 120 | "cell_type": "code", |
121 | 121 | "execution_count": null, |
122 | | - "metadata": {}, |
| 122 | + "metadata": { |
| 123 | + "collapsed": true |
| 124 | + }, |
123 | 125 | "outputs": [], |
124 | 126 | "source": [ |
125 | 127 | "!mkdir /tmp/recsys/\n", |
|
379 | 381 | }, |
380 | 382 | "outputs": [], |
381 | 383 | "source": [ |
382 | | - "class MFBlock(gluon.Block):\n", |
| 384 | + "class MFBlock(gluon.HybridBlock):\n", |
383 | 385 | " def __init__(self, max_users, max_items, num_emb, dropout_p=0.5):\n", |
384 | 386 | " super(MFBlock, self).__init__()\n", |
385 | 387 | " \n", |
|
394 | 396 | " self.dropout = gluon.nn.Dropout(dropout_p)\n", |
395 | 397 | " self.dense = gluon.nn.Dense(num_emb, activation='relu')\n", |
396 | 398 | " \n", |
397 | | - " def forward(self, users, items):\n", |
| 399 | + " def hybrid_forward(self, F, users, items):\n", |
398 | 400 | " a = self.user_embeddings(users)\n", |
399 | 401 | " a = self.dense(a)\n", |
400 | 402 | " \n", |
401 | 403 | " b = self.item_embeddings(items)\n", |
402 | 404 | " b = self.dense(b)\n", |
403 | 405 | "\n", |
404 | 406 | " predictions = self.dropout(a) * self.dropout(b) \n", |
405 | | - " predictions = nd.sum(predictions, axis=1)\n", |
| 407 | + " predictions = F.sum(predictions, axis=1)\n", |
406 | 408 | " return predictions" |
407 | 409 | ] |
408 | 410 | }, |
|
443 | 445 | "net.collect_params().initialize(mx.init.Xavier(magnitude=2.24),\n", |
444 | 446 | " ctx=ctx,\n", |
445 | 447 | " force_reinit=True)\n", |
| 448 | + "net.hybridize()\n", |
446 | 449 | "\n", |
447 | 450 | "# Set optimization parameters\n", |
448 | 451 | "opt = 'sgd'\n", |
|
486 | 489 | " with mx.autograd.record():\n", |
487 | 490 | " output = net(user, item) \n", |
488 | 491 | " loss = loss_function(output, label)\n", |
489 | | - " loss.backward()\n", |
490 | | - " net.collect_params().values()\n", |
| 492 | + " loss.backward()\n", |
491 | 493 | " trainer.step(batch_size)\n", |
492 | 494 | " except:\n", |
493 | 495 | " pass\n", |
|
825 | 827 | "cell_type": "markdown", |
826 | 828 | "metadata": {}, |
827 | 829 | "source": [ |
828 | | - "We can see that our neural network and embedding model produces substantially better results (1.28 vs 1.65 on mean square error).\n", |
| 830 | + "We can see that our neural network and embedding model produces substantially better results (~1.27 vs 1.65 on mean square error).\n", |
829 | 831 | "\n", |
830 | 832 | "For recommender systems, subjective accuracy also matters. Let's get some recommendations for a random user to see if they make intuitive sense." |
831 | 833 | ] |
|
931 | 933 | { |
932 | 934 | "cell_type": "code", |
933 | 935 | "execution_count": null, |
934 | | - "metadata": {}, |
| 936 | + "metadata": { |
| 937 | + "collapsed": true |
| 938 | + }, |
935 | 939 | "outputs": [], |
936 | 940 | "source": [ |
937 | 941 | "sagemaker.Session().delete_endpoint(predictor.endpoint)" |
|
0 commit comments