{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/usr/local/lib/python3.5/dist-packages/h5py/__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.\n",
      "  from ._conv import register_converters as _register_converters\n"
     ]
    }
   ],
   "source": [
    "import word2vec\n",
    "import numpy as np\n",
    "import tensorflow as tf\n",
    "import json\n",
    "import os\n",
    "os.environ['CUDA_VISIBLE_DEVICES'] = ''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "with open('news-bm.json','r') as fopen:\n",
    "    sentences = json.loads(fopen.read())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "word_array, dictionary, rev_dictionary, num_lines, num_words = word2vec.build_word_array(sentences,vocab_size=50000)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "X, Y = word2vec.build_training_set(word_array)\n",
    "graph_params = {'batch_size': 32,\n",
    "                'vocab_size': np.max(X)+1,\n",
    "                'embed_size': 256,\n",
    "                'hid_size': 256,\n",
    "                'neg_samples': 128,\n",
    "                'learn_rate': 0.01,\n",
    "                'momentum': 0.9,\n",
    "                'embed_noise': 0.1,\n",
    "                'hid_noise': 0.3,\n",
    "                'epoch':500,\n",
    "                'optimizer': 'Momentum'}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "split = round(X.shape[0]*0.9)\n",
    "X_val, Y_val = (X[split:, :], Y[split:, :])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "model built, vocab size 33735, document length 1211339\n"
     ]
    }
   ],
   "source": [
    "model = word2vec.Model(graph_params)\n",
    "print('model built, vocab size %d, document length %d'%(np.max(X)+1, len(word_array)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "epoch 1, total batch 37854, train loss 19.410577, val loss 4.984008\n",
      "epoch 2, total batch 41639, train loss 4.872250, val loss 4.809626\n",
      "epoch 3, total batch 45424, train loss 4.707454, val loss 4.653493\n",
      "epoch 4, total batch 49209, train loss 4.563189, val loss 4.522354\n",
      "epoch 5, total batch 52994, train loss 4.438481, val loss 4.397399\n",
      "epoch 6, total batch 56779, train loss 4.320188, val loss 4.294604\n",
      "epoch 7, total batch 60564, train loss 4.228621, val loss 4.196805\n",
      "epoch 8, total batch 64349, train loss 4.129316, val loss 4.101282\n",
      "epoch 9, total batch 68134, train loss 4.053257, val loss 4.029452\n",
      "epoch 10, total batch 71919, train loss 3.972771, val loss 3.950143\n",
      "epoch 11, total batch 75704, train loss 3.914891, val loss 3.880473\n",
      "epoch 12, total batch 79489, train loss 3.848843, val loss 3.831122\n",
      "epoch 13, total batch 83274, train loss 3.790863, val loss 3.769880\n",
      "epoch 14, total batch 87059, train loss 3.720768, val loss 3.720846\n",
      "epoch 15, total batch 90844, train loss 3.673163, val loss 3.663340\n",
      "epoch 16, total batch 94629, train loss 3.622525, val loss 3.629031\n",
      "epoch 17, total batch 98414, train loss 3.572309, val loss 3.572841\n",
      "epoch 18, total batch 102199, train loss 3.535035, val loss 3.540320\n",
      "epoch 19, total batch 105984, train loss 3.490320, val loss 3.490434\n",
      "epoch 20, total batch 109769, train loss 3.439533, val loss 3.449242\n",
      "epoch 21, total batch 113554, train loss 3.408456, val loss 3.419999\n",
      "epoch 22, total batch 117339, train loss 3.372041, val loss 3.371552\n",
      "epoch 23, total batch 121124, train loss 3.340509, val loss 3.344398\n",
      "epoch 24, total batch 124909, train loss 3.295173, val loss 3.307472\n",
      "epoch 25, total batch 128694, train loss 3.264076, val loss 3.266987\n",
      "epoch 26, total batch 132479, train loss 3.220413, val loss 3.242130\n",
      "epoch 27, total batch 136264, train loss 3.197044, val loss 3.218016\n",
      "epoch 28, total batch 140049, train loss 3.152025, val loss 3.184589\n",
      "epoch 29, total batch 143834, train loss 3.131478, val loss 3.149631\n",
      "epoch 30, total batch 147619, train loss 3.106729, val loss 3.140369\n",
      "epoch 31, total batch 151404, train loss 3.080749, val loss 3.095468\n",
      "epoch 32, total batch 155189, train loss 3.041988, val loss 3.076711\n",
      "epoch 33, total batch 158974, train loss 3.029641, val loss 3.061267\n",
      "epoch 34, total batch 162759, train loss 2.989419, val loss 3.033874\n",
      "epoch 35, total batch 166544, train loss 2.971599, val loss 2.999866\n",
      "epoch 36, total batch 170329, train loss 2.943768, val loss 2.980346\n",
      "epoch 37, total batch 174114, train loss 2.917619, val loss 2.962311\n",
      "epoch 38, total batch 177899, train loss 2.902129, val loss 2.930744\n",
      "epoch 39, total batch 181684, train loss 2.878056, val loss 2.910861\n",
      "epoch 40, total batch 185469, train loss 2.857537, val loss 2.892059\n",
      "epoch 41, total batch 189254, train loss 2.824936, val loss 2.880067\n",
      "epoch 42, total batch 193039, train loss 2.804476, val loss 2.851916\n",
      "epoch 43, total batch 196824, train loss 2.791047, val loss 2.835077\n",
      "epoch 44, total batch 200609, train loss 2.762986, val loss 2.819644\n",
      "epoch 45, total batch 204394, train loss 2.743482, val loss 2.796116\n",
      "epoch 46, total batch 208179, train loss 2.727733, val loss 2.776750\n",
      "epoch 47, total batch 211964, train loss 2.694422, val loss 2.757054\n",
      "epoch 48, total batch 215749, train loss 2.683700, val loss 2.743514\n",
      "epoch 49, total batch 219534, train loss 2.661990, val loss 2.728689\n",
      "epoch 50, total batch 223319, train loss 2.642129, val loss 2.698447\n",
      "epoch 51, total batch 227104, train loss 2.618429, val loss 2.686950\n",
      "epoch 52, total batch 230889, train loss 2.599418, val loss 2.666093\n",
      "epoch 53, total batch 234674, train loss 2.595263, val loss 2.652713\n",
      "epoch 54, total batch 238459, train loss 2.576598, val loss 2.630797\n",
      "epoch 55, total batch 242244, train loss 2.556240, val loss 2.624896\n",
      "epoch 56, total batch 246029, train loss 2.525914, val loss 2.599908\n",
      "epoch 57, total batch 249814, train loss 2.508742, val loss 2.586313\n",
      "epoch 58, total batch 253599, train loss 2.496085, val loss 2.565693\n",
      "epoch 59, total batch 257384, train loss 2.491046, val loss 2.554747\n",
      "epoch 60, total batch 261169, train loss 2.463796, val loss 2.533616\n",
      "epoch 61, total batch 264954, train loss 2.450550, val loss 2.528277\n",
      "epoch 62, total batch 268739, train loss 2.431884, val loss 2.510516\n",
      "epoch 63, total batch 272524, train loss 2.420982, val loss 2.496649\n",
      "epoch 64, total batch 276309, train loss 2.407268, val loss 2.478496\n",
      "epoch 65, total batch 280094, train loss 2.395552, val loss 2.466607\n",
      "epoch 66, total batch 283879, train loss 2.374198, val loss 2.449333\n",
      "epoch 67, total batch 287664, train loss 2.355424, val loss 2.436210\n",
      "epoch 68, total batch 291449, train loss 2.340937, val loss 2.416132\n",
      "epoch 69, total batch 295234, train loss 2.331344, val loss 2.407565\n",
      "epoch 70, total batch 299019, train loss 2.308701, val loss 2.399363\n",
      "epoch 71, total batch 302804, train loss 2.306738, val loss 2.376234\n",
      "epoch 72, total batch 306589, train loss 2.287595, val loss 2.365224\n",
      "epoch 73, total batch 310374, train loss 2.284951, val loss 2.353816\n",
      "epoch 74, total batch 314159, train loss 2.262206, val loss 2.333635\n",
      "epoch 75, total batch 317944, train loss 2.246239, val loss 2.331001\n",
      "epoch 76, total batch 321729, train loss 2.227632, val loss 2.314121\n",
      "epoch 77, total batch 325514, train loss 2.220162, val loss 2.304099\n",
      "epoch 78, total batch 329299, train loss 2.209852, val loss 2.295341\n",
      "epoch 79, total batch 333084, train loss 2.197815, val loss 2.281658\n",
      "epoch 80, total batch 336869, train loss 2.172376, val loss 2.265415\n",
      "epoch 81, total batch 340654, train loss 2.161468, val loss 2.253693\n",
      "epoch 82, total batch 344439, train loss 2.159744, val loss 2.247974\n",
      "epoch 83, total batch 348224, train loss 2.143162, val loss 2.230752\n",
      "epoch 84, total batch 352009, train loss 2.126135, val loss 2.229262\n",
      "epoch 85, total batch 355794, train loss 2.114673, val loss 2.203676\n",
      "epoch 86, total batch 359579, train loss 2.111706, val loss 2.205716\n",
      "epoch 87, total batch 363364, train loss 2.102937, val loss 2.189410\n",
      "epoch 88, total batch 367149, train loss 2.090572, val loss 2.176852\n",
      "epoch 89, total batch 370934, train loss 2.072838, val loss 2.169617\n",
      "epoch 90, total batch 374719, train loss 2.061411, val loss 2.144968\n",
      "epoch 91, total batch 378504, train loss 2.060603, val loss 2.141249\n",
      "epoch 92, total batch 382289, train loss 2.040215, val loss 2.135530\n",
      "epoch 93, total batch 386074, train loss 2.020843, val loss 2.117867\n",
      "epoch 94, total batch 389859, train loss 2.015175, val loss 2.103921\n",
      "epoch 95, total batch 393644, train loss 2.003378, val loss 2.102392\n",
      "epoch 96, total batch 397429, train loss 1.999918, val loss 2.092342\n",
      "epoch 97, total batch 401214, train loss 1.990757, val loss 2.091723\n",
      "epoch 98, total batch 404999, train loss 1.981429, val loss 2.080337\n",
      "epoch 99, total batch 408784, train loss 1.962819, val loss 2.066892\n",
      "epoch 100, total batch 412569, train loss 1.949963, val loss 2.053274\n",
      "epoch 101, total batch 416354, train loss 1.946159, val loss 2.046301\n",
      "epoch 102, total batch 420139, train loss 1.940427, val loss 2.039953\n",
      "epoch 103, total batch 423924, train loss 1.931351, val loss 2.026329\n",
      "epoch 104, total batch 427709, train loss 1.911322, val loss 2.014397\n",
      "epoch 105, total batch 431494, train loss 1.913104, val loss 2.008030\n",
      "epoch 106, total batch 435279, train loss 1.891276, val loss 2.007698\n",
      "epoch 107, total batch 439064, train loss 1.892540, val loss 1.992400\n",
      "epoch 108, total batch 442849, train loss 1.882357, val loss 1.971929\n",
      "epoch 109, total batch 446634, train loss 1.876552, val loss 1.969698\n",
      "epoch 110, total batch 450419, train loss 1.852156, val loss 1.960171\n",
      "epoch 111, total batch 454204, train loss 1.847939, val loss 1.955020\n",
      "epoch 112, total batch 457989, train loss 1.840325, val loss 1.944186\n",
      "epoch 113, total batch 461774, train loss 1.833661, val loss 1.936239\n",
      "epoch 114, total batch 465559, train loss 1.814270, val loss 1.926201\n",
      "epoch 115, total batch 469344, train loss 1.807743, val loss 1.926452\n",
      "epoch 116, total batch 473129, train loss 1.807134, val loss 1.911643\n",
      "epoch 117, total batch 476914, train loss 1.792902, val loss 1.903999\n",
      "epoch 118, total batch 480699, train loss 1.788517, val loss 1.891147\n",
      "epoch 119, total batch 484484, train loss 1.782067, val loss 1.885671\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "epoch 120, total batch 488269, train loss 1.766257, val loss 1.873878\n",
      "epoch 121, total batch 492054, train loss 1.769288, val loss 1.867519\n",
      "epoch 122, total batch 495839, train loss 1.746573, val loss 1.859189\n",
      "epoch 123, total batch 499624, train loss 1.743070, val loss 1.857109\n",
      "epoch 124, total batch 503409, train loss 1.738902, val loss 1.847050\n",
      "epoch 125, total batch 507194, train loss 1.730103, val loss 1.836721\n",
      "epoch 126, total batch 510979, train loss 1.729770, val loss 1.827683\n",
      "epoch 127, total batch 514764, train loss 1.710917, val loss 1.822790\n",
      "epoch 128, total batch 518549, train loss 1.713743, val loss 1.813293\n",
      "epoch 129, total batch 522334, train loss 1.714596, val loss 1.809900\n",
      "epoch 130, total batch 526119, train loss 1.692693, val loss 1.794717\n",
      "epoch 131, total batch 529904, train loss 1.687143, val loss 1.792241\n",
      "epoch 132, total batch 533689, train loss 1.679182, val loss 1.787589\n",
      "epoch 133, total batch 537474, train loss 1.676870, val loss 1.772183\n",
      "epoch 134, total batch 541259, train loss 1.665893, val loss 1.768851\n",
      "epoch 135, total batch 545044, train loss 1.658550, val loss 1.758942\n",
      "epoch 136, total batch 548829, train loss 1.654119, val loss 1.748554\n",
      "epoch 137, total batch 552614, train loss 1.649010, val loss 1.744395\n",
      "epoch 138, total batch 556399, train loss 1.622598, val loss 1.742999\n",
      "epoch 139, total batch 560184, train loss 1.626412, val loss 1.733366\n",
      "epoch 140, total batch 563969, train loss 1.620467, val loss 1.731679\n",
      "epoch 141, total batch 567754, train loss 1.621899, val loss 1.723763\n",
      "epoch 142, total batch 571539, train loss 1.600403, val loss 1.718776\n",
      "epoch 143, total batch 575324, train loss 1.603280, val loss 1.709262\n",
      "epoch 144, total batch 579109, train loss 1.585806, val loss 1.701405\n",
      "epoch 145, total batch 582894, train loss 1.590046, val loss 1.686045\n",
      "epoch 146, total batch 586679, train loss 1.584077, val loss 1.694583\n",
      "epoch 147, total batch 590464, train loss 1.573198, val loss 1.682276\n",
      "epoch 148, total batch 594249, train loss 1.566141, val loss 1.674595\n",
      "epoch 149, total batch 598034, train loss 1.560085, val loss 1.666140\n",
      "epoch 150, total batch 601819, train loss 1.548067, val loss 1.660313\n",
      "epoch 151, total batch 605604, train loss 1.551967, val loss 1.654604\n",
      "epoch 152, total batch 609389, train loss 1.536953, val loss 1.655676\n",
      "epoch 153, total batch 613174, train loss 1.545314, val loss 1.646525\n",
      "epoch 154, total batch 616959, train loss 1.525953, val loss 1.641355\n",
      "epoch 155, total batch 620744, train loss 1.528206, val loss 1.631889\n",
      "epoch 156, total batch 624529, train loss 1.526027, val loss 1.621932\n",
      "epoch 157, total batch 628314, train loss 1.502327, val loss 1.615485\n",
      "epoch 158, total batch 632099, train loss 1.514730, val loss 1.615314\n",
      "epoch 159, total batch 635884, train loss 1.491611, val loss 1.604035\n",
      "epoch 160, total batch 639669, train loss 1.491930, val loss 1.601232\n",
      "epoch 161, total batch 643454, train loss 1.492228, val loss 1.596451\n",
      "epoch 162, total batch 647239, train loss 1.480854, val loss 1.588719\n",
      "epoch 163, total batch 651024, train loss 1.472436, val loss 1.576065\n",
      "epoch 164, total batch 654809, train loss 1.470086, val loss 1.573696\n",
      "epoch 165, total batch 658594, train loss 1.467682, val loss 1.576490\n",
      "epoch 166, total batch 662379, train loss 1.461225, val loss 1.568612\n",
      "epoch 167, total batch 666164, train loss 1.458791, val loss 1.564361\n",
      "epoch 168, total batch 669949, train loss 1.451292, val loss 1.557363\n",
      "epoch 169, total batch 673734, train loss 1.452584, val loss 1.550807\n",
      "epoch 170, total batch 677519, train loss 1.437571, val loss 1.541716\n",
      "epoch 171, total batch 681304, train loss 1.438418, val loss 1.534482\n",
      "epoch 172, total batch 685089, train loss 1.435064, val loss 1.533668\n",
      "epoch 173, total batch 688874, train loss 1.425987, val loss 1.530150\n",
      "epoch 174, total batch 692659, train loss 1.425957, val loss 1.529074\n",
      "epoch 175, total batch 696444, train loss 1.421651, val loss 1.517400\n",
      "epoch 176, total batch 700229, train loss 1.414667, val loss 1.524286\n",
      "epoch 177, total batch 704014, train loss 1.400225, val loss 1.503177\n",
      "epoch 178, total batch 707799, train loss 1.396186, val loss 1.503128\n",
      "epoch 179, total batch 711584, train loss 1.395244, val loss 1.495513\n",
      "epoch 180, total batch 715369, train loss 1.388516, val loss 1.489772\n",
      "epoch 181, total batch 719154, train loss 1.386857, val loss 1.485127\n",
      "epoch 182, total batch 722939, train loss 1.374441, val loss 1.480390\n",
      "epoch 183, total batch 726724, train loss 1.374427, val loss 1.481129\n",
      "epoch 184, total batch 730509, train loss 1.371316, val loss 1.469663\n",
      "epoch 185, total batch 734294, train loss 1.369058, val loss 1.475338\n",
      "epoch 186, total batch 738079, train loss 1.359043, val loss 1.457655\n",
      "epoch 187, total batch 741864, train loss 1.362827, val loss 1.461788\n",
      "epoch 188, total batch 745649, train loss 1.347365, val loss 1.457757\n",
      "epoch 189, total batch 749434, train loss 1.350456, val loss 1.451572\n",
      "epoch 190, total batch 753219, train loss 1.341177, val loss 1.446881\n",
      "epoch 191, total batch 757004, train loss 1.337281, val loss 1.435018\n",
      "epoch 192, total batch 760789, train loss 1.334593, val loss 1.438713\n",
      "epoch 193, total batch 764574, train loss 1.329251, val loss 1.430411\n",
      "epoch 194, total batch 768359, train loss 1.320910, val loss 1.427301\n",
      "epoch 195, total batch 772144, train loss 1.319399, val loss 1.424354\n",
      "epoch 196, total batch 775929, train loss 1.319784, val loss 1.417999\n",
      "epoch 197, total batch 779714, train loss 1.316443, val loss 1.413302\n",
      "epoch 198, total batch 783499, train loss 1.299846, val loss 1.409481\n",
      "epoch 199, total batch 787284, train loss 1.305127, val loss 1.403486\n",
      "epoch 200, total batch 791069, train loss 1.298318, val loss 1.401852\n",
      "epoch 201, total batch 794854, train loss 1.296332, val loss 1.396902\n",
      "epoch 202, total batch 798639, train loss 1.294475, val loss 1.389571\n",
      "epoch 203, total batch 802424, train loss 1.289182, val loss 1.387557\n",
      "epoch 204, total batch 806209, train loss 1.283884, val loss 1.385190\n",
      "epoch 205, total batch 809994, train loss 1.288102, val loss 1.380573\n",
      "epoch 206, total batch 813779, train loss 1.277190, val loss 1.378427\n",
      "epoch 207, total batch 817564, train loss 1.277107, val loss 1.373530\n",
      "epoch 208, total batch 821349, train loss 1.273096, val loss 1.367976\n",
      "epoch 209, total batch 825134, train loss 1.267505, val loss 1.362287\n",
      "epoch 210, total batch 828919, train loss 1.258987, val loss 1.363787\n",
      "epoch 211, total batch 832704, train loss 1.256827, val loss 1.364636\n",
      "epoch 212, total batch 836489, train loss 1.256434, val loss 1.360344\n",
      "epoch 213, total batch 840274, train loss 1.246535, val loss 1.351918\n",
      "epoch 214, total batch 844059, train loss 1.239115, val loss 1.342626\n",
      "epoch 215, total batch 847844, train loss 1.242363, val loss 1.345233\n",
      "epoch 216, total batch 851629, train loss 1.235798, val loss 1.343347\n",
      "epoch 217, total batch 855414, train loss 1.232150, val loss 1.335311\n",
      "epoch 218, total batch 859199, train loss 1.229901, val loss 1.334628\n",
      "epoch 219, total batch 862984, train loss 1.220898, val loss 1.324954\n",
      "epoch 220, total batch 866769, train loss 1.219546, val loss 1.328384\n",
      "epoch 221, total batch 870554, train loss 1.223831, val loss 1.325772\n",
      "epoch 222, total batch 874339, train loss 1.223777, val loss 1.317526\n",
      "epoch 223, total batch 878124, train loss 1.215634, val loss 1.311474\n",
      "epoch 224, total batch 881909, train loss 1.212719, val loss 1.311937\n",
      "epoch 225, total batch 885694, train loss 1.198559, val loss 1.309523\n",
      "epoch 226, total batch 889479, train loss 1.201496, val loss 1.303655\n",
      "epoch 227, total batch 893264, train loss 1.208272, val loss 1.291873\n",
      "epoch 228, total batch 897049, train loss 1.197420, val loss 1.291729\n",
      "epoch 229, total batch 900834, train loss 1.190795, val loss 1.287704\n",
      "epoch 230, total batch 904619, train loss 1.191660, val loss 1.285627\n",
      "epoch 231, total batch 908404, train loss 1.180963, val loss 1.284547\n",
      "epoch 232, total batch 912189, train loss 1.179375, val loss 1.275001\n",
      "epoch 233, total batch 915974, train loss 1.184782, val loss 1.275745\n",
      "epoch 234, total batch 919759, train loss 1.174959, val loss 1.273064\n",
      "epoch 235, total batch 923544, train loss 1.170790, val loss 1.265225\n",
      "epoch 236, total batch 927329, train loss 1.171183, val loss 1.267592\n",
      "epoch 237, total batch 931114, train loss 1.167890, val loss 1.267020\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "epoch 238, total batch 934899, train loss 1.164827, val loss 1.254778\n",
      "epoch 239, total batch 938684, train loss 1.157326, val loss 1.251721\n",
      "epoch 240, total batch 942469, train loss 1.163584, val loss 1.254982\n",
      "epoch 241, total batch 946254, train loss 1.162210, val loss 1.253827\n",
      "epoch 242, total batch 950039, train loss 1.160126, val loss 1.251529\n",
      "epoch 243, total batch 953824, train loss 1.148146, val loss 1.242491\n",
      "epoch 244, total batch 957609, train loss 1.146160, val loss 1.239109\n",
      "epoch 245, total batch 961394, train loss 1.144618, val loss 1.238298\n",
      "epoch 246, total batch 965179, train loss 1.140931, val loss 1.232402\n",
      "epoch 247, total batch 968964, train loss 1.139036, val loss 1.235952\n",
      "epoch 248, total batch 972749, train loss 1.133489, val loss 1.223287\n",
      "epoch 249, total batch 976534, train loss 1.130619, val loss 1.229582\n",
      "epoch 250, total batch 980319, train loss 1.129308, val loss 1.223292\n",
      "epoch 251, total batch 984104, train loss 1.124807, val loss 1.217861\n",
      "epoch 252, total batch 987889, train loss 1.126767, val loss 1.213947\n",
      "epoch 253, total batch 991674, train loss 1.125814, val loss 1.213911\n",
      "epoch 254, total batch 995459, train loss 1.117112, val loss 1.209868\n",
      "epoch 255, total batch 999244, train loss 1.117348, val loss 1.204628\n",
      "epoch 256, total batch 1003029, train loss 1.116719, val loss 1.201160\n",
      "epoch 257, total batch 1006814, train loss 1.108665, val loss 1.198552\n",
      "epoch 258, total batch 1010599, train loss 1.111351, val loss 1.196953\n",
      "epoch 259, total batch 1014384, train loss 1.106654, val loss 1.197125\n",
      "epoch 260, total batch 1018169, train loss 1.102364, val loss 1.192720\n",
      "epoch 261, total batch 1021954, train loss 1.097678, val loss 1.191346\n",
      "epoch 262, total batch 1025739, train loss 1.093568, val loss 1.184916\n",
      "epoch 263, total batch 1029524, train loss 1.092927, val loss 1.180869\n",
      "epoch 264, total batch 1033309, train loss 1.084938, val loss 1.178226\n",
      "epoch 265, total batch 1037094, train loss 1.088424, val loss 1.178022\n",
      "epoch 266, total batch 1040879, train loss 1.085113, val loss 1.174191\n",
      "epoch 267, total batch 1044664, train loss 1.078772, val loss 1.176288\n",
      "epoch 268, total batch 1048449, train loss 1.082504, val loss 1.174695\n",
      "epoch 269, total batch 1052234, train loss 1.076529, val loss 1.163366\n",
      "epoch 270, total batch 1056019, train loss 1.071797, val loss 1.157336\n",
      "epoch 271, total batch 1059804, train loss 1.080096, val loss 1.165144\n",
      "epoch 272, total batch 1063589, train loss 1.072952, val loss 1.160887\n",
      "epoch 273, total batch 1067374, train loss 1.066180, val loss 1.158833\n",
      "epoch 274, total batch 1071159, train loss 1.059008, val loss 1.152040\n",
      "epoch 275, total batch 1074944, train loss 1.064770, val loss 1.153767\n",
      "epoch 276, total batch 1078729, train loss 1.062577, val loss 1.154836\n",
      "epoch 277, total batch 1082514, train loss 1.057061, val loss 1.150962\n",
      "epoch 278, total batch 1086299, train loss 1.045904, val loss 1.145764\n",
      "epoch 279, total batch 1090084, train loss 1.052909, val loss 1.137432\n",
      "epoch 280, total batch 1093869, train loss 1.046006, val loss 1.140966\n",
      "epoch 281, total batch 1097654, train loss 1.057922, val loss 1.142041\n",
      "epoch 282, total batch 1101439, train loss 1.045914, val loss 1.137516\n",
      "epoch 283, total batch 1105224, train loss 1.045948, val loss 1.134221\n",
      "epoch 284, total batch 1109009, train loss 1.051448, val loss 1.133519\n",
      "epoch 285, total batch 1112794, train loss 1.039698, val loss 1.126288\n",
      "epoch 286, total batch 1116579, train loss 1.046507, val loss 1.126105\n",
      "epoch 287, total batch 1120364, train loss 1.041685, val loss 1.124065\n",
      "epoch 288, total batch 1124149, train loss 1.043233, val loss 1.128880\n",
      "epoch 289, total batch 1127934, train loss 1.038636, val loss 1.125195\n",
      "epoch 290, total batch 1131719, train loss 1.038337, val loss 1.119348\n",
      "epoch 291, total batch 1135504, train loss 1.028550, val loss 1.114161\n",
      "epoch 292, total batch 1139289, train loss 1.033392, val loss 1.112427\n",
      "epoch 293, total batch 1143074, train loss 1.022715, val loss 1.106242\n",
      "epoch 294, total batch 1146859, train loss 1.017093, val loss 1.116286\n",
      "epoch 295, total batch 1150644, train loss 1.016144, val loss 1.105221\n",
      "epoch 296, total batch 1154429, train loss 1.023741, val loss 1.108256\n",
      "epoch 297, total batch 1158214, train loss 1.009238, val loss 1.103423\n",
      "epoch 298, total batch 1161999, train loss 1.012006, val loss 1.101178\n",
      "epoch 299, total batch 1165784, train loss 1.009093, val loss 1.096575\n",
      "epoch 300, total batch 1169569, train loss 1.009471, val loss 1.103806\n",
      "epoch 301, total batch 1173354, train loss 1.012880, val loss 1.093122\n",
      "epoch 302, total batch 1177139, train loss 1.007359, val loss 1.094029\n",
      "epoch 303, total batch 1180924, train loss 1.005952, val loss 1.090980\n",
      "epoch 304, total batch 1184709, train loss 1.001180, val loss 1.085502\n",
      "epoch 305, total batch 1188494, train loss 0.999539, val loss 1.082539\n",
      "epoch 306, total batch 1192279, train loss 0.993827, val loss 1.083549\n",
      "epoch 307, total batch 1196064, train loss 0.996282, val loss 1.080412\n",
      "epoch 308, total batch 1199849, train loss 0.993751, val loss 1.078705\n",
      "epoch 309, total batch 1203634, train loss 0.983098, val loss 1.072853\n",
      "epoch 310, total batch 1207419, train loss 0.992406, val loss 1.075330\n",
      "epoch 311, total batch 1211204, train loss 0.987409, val loss 1.076650\n",
      "epoch 312, total batch 1214989, train loss 0.983648, val loss 1.076251\n",
      "epoch 313, total batch 1218774, train loss 0.982525, val loss 1.069029\n",
      "epoch 314, total batch 1222559, train loss 0.979812, val loss 1.062020\n",
      "epoch 315, total batch 1226344, train loss 0.985094, val loss 1.062667\n",
      "epoch 316, total batch 1230129, train loss 0.977494, val loss 1.067626\n",
      "epoch 317, total batch 1233914, train loss 0.975866, val loss 1.064947\n",
      "epoch 318, total batch 1237699, train loss 0.978010, val loss 1.061877\n",
      "epoch 319, total batch 1241484, train loss 0.978879, val loss 1.054859\n",
      "epoch 320, total batch 1245269, train loss 0.968855, val loss 1.055838\n",
      "epoch 321, total batch 1249054, train loss 0.970562, val loss 1.050522\n",
      "epoch 322, total batch 1252839, train loss 0.965554, val loss 1.048172\n",
      "epoch 323, total batch 1256624, train loss 0.966524, val loss 1.048327\n",
      "epoch 324, total batch 1260409, train loss 0.959070, val loss 1.052745\n",
      "epoch 325, total batch 1264194, train loss 0.966605, val loss 1.048556\n",
      "epoch 326, total batch 1267979, train loss 0.955813, val loss 1.046283\n",
      "epoch 327, total batch 1271764, train loss 0.952465, val loss 1.040779\n",
      "epoch 328, total batch 1275549, train loss 0.963297, val loss 1.041494\n",
      "epoch 329, total batch 1279334, train loss 0.954275, val loss 1.037205\n",
      "epoch 330, total batch 1283119, train loss 0.955607, val loss 1.033251\n",
      "epoch 331, total batch 1286904, train loss 0.953351, val loss 1.042027\n",
      "epoch 332, total batch 1290689, train loss 0.948063, val loss 1.030309\n",
      "epoch 333, total batch 1294474, train loss 0.945804, val loss 1.030097\n",
      "epoch 334, total batch 1298259, train loss 0.946385, val loss 1.030796\n",
      "epoch 335, total batch 1302044, train loss 0.941001, val loss 1.029387\n",
      "epoch 336, total batch 1305829, train loss 0.944540, val loss 1.028480\n",
      "epoch 337, total batch 1309614, train loss 0.940753, val loss 1.021849\n",
      "epoch 338, total batch 1313399, train loss 0.936743, val loss 1.021644\n",
      "epoch 339, total batch 1317184, train loss 0.941334, val loss 1.021055\n",
      "epoch 340, total batch 1320969, train loss 0.930213, val loss 1.015259\n",
      "epoch 341, total batch 1324754, train loss 0.934486, val loss 1.018282\n",
      "epoch 342, total batch 1328539, train loss 0.931029, val loss 1.018289\n",
      "epoch 343, total batch 1332324, train loss 0.934420, val loss 1.010116\n",
      "epoch 344, total batch 1336109, train loss 0.933553, val loss 1.009019\n",
      "epoch 345, total batch 1339894, train loss 0.928851, val loss 1.010560\n",
      "epoch 346, total batch 1343679, train loss 0.929040, val loss 1.005449\n",
      "epoch 347, total batch 1347464, train loss 0.920301, val loss 1.010386\n",
      "epoch 348, total batch 1351249, train loss 0.927982, val loss 1.008166\n",
      "epoch 349, total batch 1355034, train loss 0.917557, val loss 1.007869\n",
      "epoch 350, total batch 1358819, train loss 0.911827, val loss 0.999371\n",
      "epoch 351, total batch 1362604, train loss 0.917953, val loss 1.000187\n",
      "epoch 352, total batch 1366389, train loss 0.915028, val loss 1.001394\n",
      "epoch 353, total batch 1370174, train loss 0.915222, val loss 0.989907\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "epoch 354, total batch 1373959, train loss 0.920605, val loss 0.988469\n",
      "epoch 355, total batch 1377744, train loss 0.915099, val loss 0.993207\n",
      "epoch 356, total batch 1381529, train loss 0.908397, val loss 0.992937\n",
      "epoch 357, total batch 1385314, train loss 0.912728, val loss 0.995862\n",
      "epoch 358, total batch 1389099, train loss 0.909446, val loss 0.989608\n",
      "epoch 359, total batch 1392884, train loss 0.909597, val loss 0.986612\n",
      "epoch 360, total batch 1396669, train loss 0.901058, val loss 0.986198\n",
      "epoch 361, total batch 1400454, train loss 0.898088, val loss 0.980800\n",
      "epoch 362, total batch 1404239, train loss 0.899243, val loss 0.981821\n",
      "epoch 363, total batch 1408024, train loss 0.902046, val loss 0.979839\n",
      "epoch 364, total batch 1411809, train loss 0.898806, val loss 0.978010\n",
      "epoch 365, total batch 1415594, train loss 0.904568, val loss 0.974360\n",
      "epoch 366, total batch 1419379, train loss 0.902979, val loss 0.978154\n",
      "epoch 367, total batch 1423164, train loss 0.898163, val loss 0.974523\n",
      "epoch 368, total batch 1426949, train loss 0.892207, val loss 0.976920\n",
      "epoch 369, total batch 1430734, train loss 0.896108, val loss 0.967983\n",
      "epoch 370, total batch 1434519, train loss 0.892695, val loss 0.966426\n",
      "epoch 371, total batch 1438304, train loss 0.895314, val loss 0.969454\n",
      "epoch 372, total batch 1442089, train loss 0.888962, val loss 0.971248\n",
      "epoch 373, total batch 1445874, train loss 0.886583, val loss 0.961682\n",
      "epoch 374, total batch 1449659, train loss 0.885658, val loss 0.965855\n",
      "epoch 375, total batch 1453444, train loss 0.887459, val loss 0.962270\n",
      "epoch 376, total batch 1457229, train loss 0.881119, val loss 0.961709\n",
      "epoch 377, total batch 1461014, train loss 0.880611, val loss 0.960764\n",
      "epoch 378, total batch 1464799, train loss 0.882427, val loss 0.961844\n",
      "epoch 379, total batch 1468584, train loss 0.879389, val loss 0.959671\n",
      "epoch 380, total batch 1472369, train loss 0.879218, val loss 0.960192\n",
      "epoch 381, total batch 1476154, train loss 0.873978, val loss 0.956489\n",
      "epoch 382, total batch 1479939, train loss 0.878361, val loss 0.955495\n",
      "epoch 383, total batch 1483724, train loss 0.867224, val loss 0.957646\n",
      "epoch 384, total batch 1487509, train loss 0.870464, val loss 0.953560\n",
      "epoch 385, total batch 1491294, train loss 0.866737, val loss 0.951135\n",
      "epoch 386, total batch 1495079, train loss 0.874080, val loss 0.951914\n",
      "epoch 387, total batch 1498864, train loss 0.867847, val loss 0.943873\n",
      "epoch 388, total batch 1502649, train loss 0.877246, val loss 0.948994\n",
      "epoch 389, total batch 1506434, train loss 0.864546, val loss 0.946102\n",
      "epoch 390, total batch 1510219, train loss 0.873045, val loss 0.946919\n",
      "epoch 391, total batch 1514004, train loss 0.864301, val loss 0.946671\n",
      "epoch 392, total batch 1517789, train loss 0.865535, val loss 0.937761\n",
      "epoch 393, total batch 1521574, train loss 0.866791, val loss 0.939669\n",
      "epoch 394, total batch 1525359, train loss 0.863491, val loss 0.936254\n",
      "epoch 395, total batch 1529144, train loss 0.857495, val loss 0.942714\n",
      "epoch 396, total batch 1532929, train loss 0.854642, val loss 0.937942\n",
      "epoch 397, total batch 1536714, train loss 0.851331, val loss 0.936239\n",
      "epoch 398, total batch 1540499, train loss 0.855693, val loss 0.935097\n",
      "epoch 399, total batch 1544284, train loss 0.856089, val loss 0.931645\n",
      "epoch 400, total batch 1548069, train loss 0.851334, val loss 0.927072\n",
      "epoch 401, total batch 1551854, train loss 0.851651, val loss 0.929845\n",
      "epoch 402, total batch 1555639, train loss 0.852572, val loss 0.926156\n",
      "epoch 403, total batch 1559424, train loss 0.849914, val loss 0.923316\n",
      "epoch 404, total batch 1563209, train loss 0.843978, val loss 0.924689\n",
      "epoch 405, total batch 1566994, train loss 0.856677, val loss 0.921374\n",
      "epoch 406, total batch 1570779, train loss 0.846619, val loss 0.922367\n",
      "epoch 407, total batch 1574564, train loss 0.839530, val loss 0.923884\n",
      "epoch 408, total batch 1578349, train loss 0.845353, val loss 0.921999\n",
      "epoch 409, total batch 1582134, train loss 0.836335, val loss 0.917033\n",
      "epoch 410, total batch 1585919, train loss 0.836514, val loss 0.918743\n",
      "epoch 411, total batch 1589704, train loss 0.840192, val loss 0.914429\n",
      "epoch 412, total batch 1593489, train loss 0.838968, val loss 0.912886\n",
      "epoch 413, total batch 1597274, train loss 0.832021, val loss 0.917742\n",
      "epoch 414, total batch 1601059, train loss 0.839127, val loss 0.908793\n",
      "epoch 415, total batch 1604844, train loss 0.835054, val loss 0.905462\n",
      "epoch 416, total batch 1608629, train loss 0.828682, val loss 0.902238\n",
      "epoch 417, total batch 1612414, train loss 0.830745, val loss 0.912329\n",
      "epoch 418, total batch 1616199, train loss 0.835233, val loss 0.907834\n",
      "epoch 419, total batch 1619984, train loss 0.833880, val loss 0.903815\n",
      "epoch 420, total batch 1623769, train loss 0.831875, val loss 0.905165\n",
      "epoch 421, total batch 1627554, train loss 0.828495, val loss 0.900427\n",
      "epoch 422, total batch 1631339, train loss 0.829419, val loss 0.899592\n",
      "epoch 423, total batch 1635124, train loss 0.821629, val loss 0.899389\n",
      "epoch 424, total batch 1638909, train loss 0.817935, val loss 0.900895\n",
      "epoch 425, total batch 1642694, train loss 0.821876, val loss 0.895019\n",
      "epoch 426, total batch 1646479, train loss 0.825788, val loss 0.900917\n",
      "epoch 427, total batch 1650264, train loss 0.812862, val loss 0.895858\n",
      "epoch 428, total batch 1654049, train loss 0.821924, val loss 0.896240\n",
      "epoch 429, total batch 1657834, train loss 0.820444, val loss 0.897497\n",
      "epoch 430, total batch 1661619, train loss 0.819521, val loss 0.889087\n",
      "epoch 431, total batch 1665404, train loss 0.812571, val loss 0.891901\n",
      "epoch 432, total batch 1669189, train loss 0.816593, val loss 0.891723\n",
      "epoch 433, total batch 1672974, train loss 0.819318, val loss 0.887772\n",
      "epoch 434, total batch 1676759, train loss 0.816568, val loss 0.884232\n",
      "epoch 435, total batch 1680544, train loss 0.815790, val loss 0.884333\n",
      "epoch 436, total batch 1684329, train loss 0.816909, val loss 0.879587\n",
      "epoch 437, total batch 1688114, train loss 0.815575, val loss 0.888231\n",
      "epoch 438, total batch 1691899, train loss 0.808021, val loss 0.880238\n",
      "epoch 439, total batch 1695684, train loss 0.812628, val loss 0.879178\n",
      "epoch 440, total batch 1699469, train loss 0.807094, val loss 0.884533\n",
      "epoch 441, total batch 1703254, train loss 0.808248, val loss 0.879763\n",
      "epoch 442, total batch 1707039, train loss 0.810731, val loss 0.878450\n",
      "epoch 443, total batch 1710824, train loss 0.803156, val loss 0.878878\n",
      "epoch 444, total batch 1714609, train loss 0.801921, val loss 0.879375\n",
      "epoch 445, total batch 1718394, train loss 0.810759, val loss 0.879400\n",
      "epoch 446, total batch 1722179, train loss 0.802043, val loss 0.879208\n",
      "epoch 447, total batch 1725964, train loss 0.797864, val loss 0.873343\n",
      "epoch 448, total batch 1729749, train loss 0.795805, val loss 0.873536\n",
      "epoch 449, total batch 1733534, train loss 0.800711, val loss 0.873690\n",
      "epoch 450, total batch 1737319, train loss 0.795847, val loss 0.872461\n",
      "epoch 451, total batch 1741104, train loss 0.794181, val loss 0.867983\n",
      "epoch 452, total batch 1744889, train loss 0.796889, val loss 0.867786\n",
      "epoch 453, total batch 1748674, train loss 0.801455, val loss 0.865285\n",
      "epoch 454, total batch 1752459, train loss 0.791635, val loss 0.865739\n",
      "epoch 455, total batch 1756244, train loss 0.791931, val loss 0.868586\n",
      "epoch 456, total batch 1760029, train loss 0.796051, val loss 0.860207\n",
      "epoch 457, total batch 1763814, train loss 0.791152, val loss 0.863858\n",
      "epoch 458, total batch 1767599, train loss 0.788213, val loss 0.862336\n",
      "epoch 459, total batch 1771384, train loss 0.790406, val loss 0.862552\n",
      "epoch 460, total batch 1775169, train loss 0.787622, val loss 0.859394\n",
      "epoch 461, total batch 1778954, train loss 0.794820, val loss 0.860849\n",
      "epoch 462, total batch 1782739, train loss 0.782188, val loss 0.857944\n",
      "epoch 463, total batch 1786524, train loss 0.789244, val loss 0.862068\n",
      "epoch 464, total batch 1790309, train loss 0.785057, val loss 0.853211\n",
      "epoch 465, total batch 1794094, train loss 0.782478, val loss 0.853615\n",
      "epoch 466, total batch 1797879, train loss 0.777750, val loss 0.857590\n",
      "epoch 467, total batch 1801664, train loss 0.786499, val loss 0.855362\n",
      "epoch 468, total batch 1805449, train loss 0.777167, val loss 0.855752\n",
      "epoch 469, total batch 1809234, train loss 0.783522, val loss 0.852686\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "epoch 470, total batch 1813019, train loss 0.785807, val loss 0.849860\n",
      "epoch 471, total batch 1816804, train loss 0.769274, val loss 0.848929\n",
      "epoch 472, total batch 1820589, train loss 0.777090, val loss 0.855131\n",
      "epoch 473, total batch 1824374, train loss 0.781009, val loss 0.852111\n",
      "epoch 474, total batch 1828159, train loss 0.772511, val loss 0.847179\n",
      "epoch 475, total batch 1831944, train loss 0.769631, val loss 0.842901\n",
      "epoch 476, total batch 1835729, train loss 0.773598, val loss 0.843428\n",
      "epoch 477, total batch 1839514, train loss 0.776401, val loss 0.843648\n",
      "epoch 478, total batch 1843299, train loss 0.770020, val loss 0.838017\n",
      "epoch 479, total batch 1847084, train loss 0.772534, val loss 0.842164\n",
      "epoch 480, total batch 1850869, train loss 0.766155, val loss 0.843569\n",
      "epoch 481, total batch 1854654, train loss 0.773751, val loss 0.840331\n",
      "epoch 482, total batch 1858439, train loss 0.770172, val loss 0.839523\n",
      "epoch 483, total batch 1862224, train loss 0.768791, val loss 0.837494\n",
      "epoch 484, total batch 1866009, train loss 0.761213, val loss 0.838806\n",
      "epoch 485, total batch 1869794, train loss 0.768023, val loss 0.835402\n",
      "epoch 486, total batch 1873579, train loss 0.767307, val loss 0.831768\n",
      "epoch 487, total batch 1877364, train loss 0.769245, val loss 0.836926\n",
      "epoch 488, total batch 1881149, train loss 0.770414, val loss 0.836189\n",
      "epoch 489, total batch 1884934, train loss 0.765082, val loss 0.833189\n",
      "epoch 490, total batch 1888719, train loss 0.761938, val loss 0.838747\n",
      "epoch 491, total batch 1892504, train loss 0.757216, val loss 0.826310\n",
      "epoch 492, total batch 1896289, train loss 0.755823, val loss 0.820463\n",
      "epoch 493, total batch 1900074, train loss 0.760366, val loss 0.826154\n",
      "epoch 494, total batch 1903859, train loss 0.751555, val loss 0.824993\n",
      "epoch 495, total batch 1907644, train loss 0.761997, val loss 0.826215\n",
      "epoch 496, total batch 1911429, train loss 0.762228, val loss 0.828290\n",
      "epoch 497, total batch 1915214, train loss 0.747179, val loss 0.824756\n",
      "epoch 498, total batch 1918999, train loss 0.761686, val loss 0.824705\n",
      "epoch 499, total batch 1922784, train loss 0.753307, val loss 0.820460\n",
      "epoch 500, total batch 1926569, train loss 0.750132, val loss 0.824536\n"
     ]
    }
   ],
   "source": [
    "embed_weights, nce_weights = model.train(X, Y, X_val, Y_val,\n",
    "                                         graph_params['epoch'],\n",
    "                                         graph_params['batch_size'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "word_vector_embed = word2vec.Word2Vec(embed_weights, dictionary)\n",
    "word_vector_nce = word2vec.Word2Vec(nce_weights, dictionary)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Embedding layer: 8 closest words to: 'mahathir'\n",
      "['mahahir', 'bn', 'mahatir', 'beliau', 'mim', 'zamzamzairani', 'adnan', 'sembari'] \n",
      "\n",
      "NCE layer: 8 closest words to: 'mahathir'\n",
      "['subramaniam', 'zambry', 'dzulkefly', 'salleh', 'obama', 'mahkota', 'norlela', 'mahahir']\n"
     ]
    }
   ],
   "source": [
    "word = 'mahathir'\n",
    "print(\"Embedding layer: 8 closest words to: '%s'\"%(word))\n",
    "print(word_vector_embed.n_closest(word=word, num_closest=8, metric='cosine'), '\\n')\n",
    "print(\"NCE layer: 8 closest words to: '%s'\"%(word))\n",
    "print(word_vector_nce.n_closest(word=word, num_closest=8, metric='cosine'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['kerajaan', 'anwar', 'pertanian', 'sultan', 'cukai']\n"
     ]
    }
   ],
   "source": [
    "print(word_vector_nce.analogy('mahathir', 'anwar', 'kerajaan', 5))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['anwar', 'kerajaan', 'luarmana', 'dipertanggungjawab', 'perkaumaniv']\n"
     ]
    }
   ],
   "source": [
    "print(word_vector_embed.analogy('mahathir', 'anwar', 'kerajaan', 5))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "passage = [rev_dictionary[i] for i in word_array[12200:12300]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " sangat dominan sedangkan mca ada masalah mic dan gerakan ada masalah ini yang lemahkan bnnamun nature politik memang begitu tapi boleh settle down lepas prucuma bimbang orang malaysia sangat emosi dengan apa yang berlaku maka ia tidak sihatazizudin politik kena rasional jadi kita patut ke arah politik matangnamun hakikatnya melihat pada isu kaum ia berlaku di semua negarakena rasionalkan politik perkauman itu cuma di malaysia kita masih baik dan tidak berperang sesama sendirilihat faktor ekonomi mempengaruhi politik jadi fenomena arab spring tidak akan berlaku di malaysiamoderator adakah bn boleh reformmaszlee kalau kita lihat bn dan umno ada dinamik berevolusi ketika\n"
     ]
    }
   ],
   "source": [
    "readable = ''\n",
    "for word in passage:\n",
    "    if word == '\"':\n",
    "        readable += word\n",
    "    elif word in ['?', '!', '.', ',']:\n",
    "        readable += word + ' '\n",
    "    else: \n",
    "        readable += ' ' + word\n",
    "print(readable)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "x, y = word2vec.build_training_set(word_array[(12200-2):(12300+2)])\n",
    "y_hat = model.sess.run(model.logits,feed_dict={model.X:x})\n",
    "passage_predict = [rev_dictionary[i] for i in y_hat]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " sangat kuat khususnya kita ada masalah besar dan mengakui menyelesaikan teknologi ini yang menyentuh melakukan ahli politik memang banyak tapi boleh cakap tumpang lepas kami ramai orang ini mempunyai terpengaruh dengan perkahwinan yang salah kerana ia tidak bermakna politik yang jawab memberitahu kita hendak ke arah politik pada hakikatnya melihat tiada polarisasi mana yang berlaku di semua tempat parti politik perkauman itu termasuk di selangor tetapi tidak ramai dan tidak bertanggungjawab sesama jenis perkembangan ekonomi faktor politik jadi artis arab saudi tidak akan berlaku di myanmar adakah bn boleh digunakan kalau kita lihat sekarang khususnya umno ada penyokong namun ketika\n"
     ]
    }
   ],
   "source": [
    "readable = ''\n",
    "for word in passage_predict:\n",
    "    if word == '\"':\n",
    "        readable += word\n",
    "    elif word in ['?', '!', '.', ',']:\n",
    "        readable += word + ' '\n",
    "    else: \n",
    "        readable += ' ' + word\n",
    "print(readable)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pickle\n",
    "with open('word2vec-256.p', 'wb') as fopen:\n",
    "    pickle.dump({'dictionary':dictionary,'rev_dictionary':rev_dictionary,\n",
    "                 'embed_weights':embed_weights,'nce_weights':nce_weights}, fopen)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
