File size: 16,315 Bytes
508de5b
1
{"nbformat":4,"nbformat_minor":0,"metadata":{"colab":{"name":"post embedding.ipynb","provenance":[{"file_id":"1Wd79wkxXF6CyKVFhYCid1jpIsv0HWRH-","timestamp":1625292490447},{"file_id":"1liafFKE2IZeDAgr_AibZfKykYLvHVzD3","timestamp":1624675505095},{"file_id":"https://github.com/GihanAyesh/sinhala_sentiment_anlaysis_tallip/blob/master/cnn_rnn_multiclass.ipynb","timestamp":1621843389117},{"file_id":"153374pBL1OZuAJMWKK_ae0D9_DUq4P0P","timestamp":1590420423516}],"collapsed_sections":[]},"environment":{"name":"tf2-2-2-gpu.2-2.m50","type":"gcloud","uri":"gcr.io/deeplearning-platform-release/tf2-2-2-gpu.2-2:m50"},"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.7.6"}},"cells":[{"cell_type":"markdown","metadata":{"id":"3zR8-aK009p9"},"source":["# User Parameters"]},{"cell_type":"code","metadata":{"id":"dbSiWEihH0zu","executionInfo":{"status":"ok","timestamp":1640863589116,"user_tz":-330,"elapsed":1040,"user":{"displayName":"Gihan Weeraprameshwara","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiCuBWPBFW3AvbkkMkY_XlfhSKS9YjMV2DdXXxL=s64","userId":"07090364678417433377"}}},"source":["i = \"5\" #@param [1,2,3,4,5]\n","embedding_type = \"word2vec\" #@param [\"fastText\",\"word2vec\",\"glove\"]\n","model_type = \"LSTM\" #@param [\"GRU\", \"LSTM\" ] \n","Attention = True #@param {type:\"boolean\"}\n","EMBEDDING_SIZE = 200 #@param [200,300]\n","if(Attention):\n","  decoder = \"attention\"\n","else:\n","  decoder = \"simple\"\n","\n","i=int(i)\n","EMBEDDING_SIZE = int(EMBEDDING_SIZE)\n"],"execution_count":77,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"p7y2rE1vNZWc"},"source":["# Folder Paths"]},{"cell_type":"code","metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"IDq--mTz9k24","executionInfo":{"status":"ok","timestamp":1640863595944,"user_tz":-330,"elapsed":5534,"user":{"displayName":"Gihan Weeraprameshwara","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiCuBWPBFW3AvbkkMkY_XlfhSKS9YjMV2DdXXxL=s64","userId":"07090364678417433377"}},"outputId":"bf13997a-3fdb-4f90-dafc-4adfe8ef8612"},"source":["from google.colab import drive\n","drive.mount('/content/drive')"],"execution_count":78,"outputs":[{"output_type":"stream","name":"stdout","text":["Drive already mounted at /content/drive; to attempt to forcibly remount, call drive.mount(\"/content/drive\", force_remount=True).\n"]}]},{"cell_type":"code","metadata":{"id":"gWPD3dgEqB5d","executionInfo":{"status":"ok","timestamp":1640863595946,"user_tz":-330,"elapsed":16,"user":{"displayName":"Gihan Weeraprameshwara","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiCuBWPBFW3AvbkkMkY_XlfhSKS9YjMV2DdXXxL=s64","userId":"07090364678417433377"}}},"source":["folder_path = '/content/drive/MyDrive/17 Batch FYP - Facebook Sinhala Data/'\n","embedding_path= (folder_path + 'Word embedding/embedded posts/'+embedding_type+'/'+model_type+'_'+decoder+'_'+str(i)+'.csv')\n","word_embedding_path= (folder_path + 'Word embedding/'+embedding_type+'/'+embedding_type+str(EMBEDDING_SIZE))\n","encoder_path = folder_path + 'Word embedding/seq2seq/'+embedding_type+'_'+model_type+'_'+decoder+'encoder.dict'"],"execution_count":79,"outputs":[]},{"cell_type":"code","metadata":{"colab":{"base_uri":"https://localhost:8080/","height":36},"id":"WbxGy6fAHT-Q","executionInfo":{"status":"ok","timestamp":1640863595946,"user_tz":-330,"elapsed":15,"user":{"displayName":"Gihan Weeraprameshwara","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiCuBWPBFW3AvbkkMkY_XlfhSKS9YjMV2DdXXxL=s64","userId":"07090364678417433377"}},"outputId":"d0d18b29-7abb-4b35-9c53-a9dd1e5563ec"},"source":["embedding_path"],"execution_count":80,"outputs":[{"output_type":"execute_result","data":{"application/vnd.google.colaboratory.intrinsic+json":{"type":"string"},"text/plain":["'/content/drive/MyDrive/17 Batch FYP - Facebook Sinhala Data/Word embedding/embedded posts/word2vec/LSTM_attention_5.csv'"]},"metadata":{},"execution_count":80}]},{"cell_type":"code","metadata":{"colab":{"base_uri":"https://localhost:8080/","height":36},"id":"DaQKg26txGs2","executionInfo":{"status":"ok","timestamp":1640863595946,"user_tz":-330,"elapsed":12,"user":{"displayName":"Gihan Weeraprameshwara","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiCuBWPBFW3AvbkkMkY_XlfhSKS9YjMV2DdXXxL=s64","userId":"07090364678417433377"}},"outputId":"3a15587f-30c7-4faf-c6bb-2c589f26a0d8"},"source":["encoder_path"],"execution_count":81,"outputs":[{"output_type":"execute_result","data":{"application/vnd.google.colaboratory.intrinsic+json":{"type":"string"},"text/plain":["'/content/drive/MyDrive/17 Batch FYP - Facebook Sinhala Data/Word embedding/seq2seq/word2vec_LSTM_attentionencoder.dict'"]},"metadata":{},"execution_count":81}]},{"cell_type":"markdown","metadata":{"id":"0BYvE-D5QAc0"},"source":["# Dependencies"]},{"cell_type":"code","metadata":{"id":"Cf2gZocJff8x","executionInfo":{"status":"ok","timestamp":1640863595947,"user_tz":-330,"elapsed":11,"user":{"displayName":"Gihan Weeraprameshwara","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiCuBWPBFW3AvbkkMkY_XlfhSKS9YjMV2DdXXxL=s64","userId":"07090364678417433377"}}},"source":["# !pip uninstall keras-nightly\n","# !pip install tensorflow==1.14.0\n","# !pip install q keras==2.3.1"],"execution_count":82,"outputs":[]},{"cell_type":"code","metadata":{"id":"gZ9YDtD6qn4t","executionInfo":{"status":"ok","timestamp":1640863595947,"user_tz":-330,"elapsed":11,"user":{"displayName":"Gihan Weeraprameshwara","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiCuBWPBFW3AvbkkMkY_XlfhSKS9YjMV2DdXXxL=s64","userId":"07090364678417433377"}}},"source":["from __future__ import print_function\n","\n","import collections\n","import pickle\n","\n","import gensim\n","from gensim.models.fasttext import FastText\n","from gensim.models import word2vec\n","\n","import pandas as pd\n","import numpy as np\n","from numpy import array\n","from numpy import asarray\n","from numpy import zeros\n","from numpy import cumsum\n","\n","import tensorflow as tf\n","import keras\n","\n","import torch\n","import torch.nn as nn\n","from torch import optim\n","import torch.nn.functional as F\n","torch.set_printoptions(precision=8)\n"],"execution_count":83,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"xXiAlHRZ7X_p"},"source":["# Load Data"]},{"cell_type":"code","metadata":{"id":"GqqHP9rwpbdt","executionInfo":{"status":"ok","timestamp":1640863598337,"user_tz":-330,"elapsed":2400,"user":{"displayName":"Gihan Weeraprameshwara","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiCuBWPBFW3AvbkkMkY_XlfhSKS9YjMV2DdXXxL=s64","userId":"07090364678417433377"}}},"source":["facebook_data_path = folder_path + 'Word embedding/preprocessed_set.csv'\n","all_data = pd.read_pickle(facebook_data_path)"],"execution_count":84,"outputs":[]},{"cell_type":"code","metadata":{"id":"UkilaQMGa9jW","executionInfo":{"status":"ok","timestamp":1640863599000,"user_tz":-330,"elapsed":666,"user":{"displayName":"Gihan Weeraprameshwara","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiCuBWPBFW3AvbkkMkY_XlfhSKS9YjMV2DdXXxL=s64","userId":"07090364678417433377"}}},"source":["all_data=all_data.drop('level_0',axis=1)"],"execution_count":85,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"Shq4w6GcqbIq"},"source":["# Create Input"]},{"cell_type":"markdown","metadata":{"id":"LdKgzrbd8Uy8"},"source":["## Load encoder"]},{"cell_type":"code","metadata":{"id":"0YZs52JpBX79","executionInfo":{"status":"ok","timestamp":1640863599001,"user_tz":-330,"elapsed":11,"user":{"displayName":"Gihan Weeraprameshwara","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiCuBWPBFW3AvbkkMkY_XlfhSKS9YjMV2DdXXxL=s64","userId":"07090364678417433377"}}},"source":["class LSTMEncoderRNN(nn.Module):\n","    def __init__(self, hidden_size):\n","        super(LSTMEncoderRNN, self).__init__()\n","        self.hidden_size = hidden_size\n","        self.lstm = nn.LSTM(hidden_size, hidden_size)\n","\n","    def forward(self, inp, hidden,cell):\n","        input=(inp).view(1,1,-1)\n","        output,(hidden,cell)= self.lstm(input, (hidden,cell))\n","        return hidden,cell\n","\n","class GRUEncoderRNN(nn.Module):\n","    def __init__(self, hidden_size):\n","        super(GRUEncoderRNN, self).__init__()\n","        self.hidden_size = hidden_size\n","        self.gru = nn.GRU(hidden_size, hidden_size)\n","\n","    def forward(self, inp, hidden):\n","        input=(inp).view(1,1,-1)\n","        output, hidden = self.gru(input, hidden)\n","        return hidden"],"execution_count":86,"outputs":[]},{"cell_type":"code","metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"zNKwtomuBjep","executionInfo":{"status":"ok","timestamp":1640863599002,"user_tz":-330,"elapsed":11,"user":{"displayName":"Gihan Weeraprameshwara","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiCuBWPBFW3AvbkkMkY_XlfhSKS9YjMV2DdXXxL=s64","userId":"07090364678417433377"}},"outputId":"db61cda0-4863-4ddf-ff1f-cbaac2114389"},"source":["if (model_type=='GRU'):\n","  encoder = GRUEncoderRNN(EMBEDDING_SIZE)\n","else:\n","  encoder = LSTMEncoderRNN(EMBEDDING_SIZE)\n","encoder.load_state_dict(torch.load(encoder_path))"],"execution_count":87,"outputs":[{"output_type":"execute_result","data":{"text/plain":["<All keys matched successfully>"]},"metadata":{},"execution_count":87}]},{"cell_type":"markdown","metadata":{"id":"tvUoARYaRM2c"},"source":["## Embedding functions"]},{"cell_type":"code","metadata":{"id":"2W3owALoR96d","executionInfo":{"status":"ok","timestamp":1640863599002,"user_tz":-330,"elapsed":7,"user":{"displayName":"Gihan Weeraprameshwara","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiCuBWPBFW3AvbkkMkY_XlfhSKS9YjMV2DdXXxL=s64","userId":"07090364678417433377"}}},"source":["window_size = 40\n","min_word = 5\n","down_sampling = 1e-2"],"execution_count":88,"outputs":[]},{"cell_type":"code","metadata":{"id":"Di94yXN5SALw","executionInfo":{"status":"ok","timestamp":1640863599534,"user_tz":-330,"elapsed":539,"user":{"displayName":"Gihan Weeraprameshwara","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiCuBWPBFW3AvbkkMkY_XlfhSKS9YjMV2DdXXxL=s64","userId":"07090364678417433377"}}},"source":["if (embedding_type=='fastText'):\n","  from gensim.models import FastText\n","  embedding_model = FastText(size=EMBEDDING_SIZE,\n","                        window=window_size,\n","                        min_count=min_word,\n","                        sample=down_sampling,\n","                        sg=1)\n","  embedding_model = FastText.load(word_embedding_path)\n","elif (embedding_type=='word2vec'):\n","  from gensim.models import Word2Vec\n","  embedding_model = Word2Vec(size=EMBEDDING_SIZE,\n","                        window=window_size,\n","                        min_count=min_word,\n","                        sample=down_sampling,\n","                        sg=1)\n","  embedding_model = Word2Vec.load(word_embedding_path)\n","elif (embedding_type=='glove'):\n","  ! pip install glove-python-binary\n","  from glove import Glove\n","  EMBEDDING_DIM=EMBEDDING_SIZE\n","  embedding_model = Glove(no_components=EMBEDDING_DIM, learning_rate=0.05)\n","  embedding_model.load(word_embedding_path+'model.bin')"],"execution_count":89,"outputs":[]},{"cell_type":"code","metadata":{"id":"pLyhHFZ9TAfr","executionInfo":{"status":"ok","timestamp":1640863599535,"user_tz":-330,"elapsed":24,"user":{"displayName":"Gihan Weeraprameshwara","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiCuBWPBFW3AvbkkMkY_XlfhSKS9YjMV2DdXXxL=s64","userId":"07090364678417433377"}}},"source":["hidden=torch.zeros(1, 1, EMBEDDING_SIZE)"],"execution_count":90,"outputs":[]},{"cell_type":"code","metadata":{"id":"eLYOoiKqA3aI","executionInfo":{"status":"ok","timestamp":1640863599535,"user_tz":-330,"elapsed":17,"user":{"displayName":"Gihan Weeraprameshwara","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiCuBWPBFW3AvbkkMkY_XlfhSKS9YjMV2DdXXxL=s64","userId":"07090364678417433377"}}},"source":["def sentence_embedding(sentence):\n","  line = []\n","  for i in range(len(sentence)):\n","    try:\n","      line.append(embedding_model.wv[sentence[i]])\n","    except KeyError:\n","      continue\n","  sentence = line\n","  if (len(sentence)==0):\n","    return None\n","  input_tensor = torch.tensor(sentence,dtype=torch.float32)\n","  length = input_tensor.size(0)\n","  encoder_hidden = hidden\n","  if (model_type=='GRU'):\n","    for ei in range(length):\n","        encoder_hidden = encoder(input_tensor[ei],encoder_hidden)\n","  if (model_type=='LSTM'):\n","    encoder_cell = hidden\n","    for ei in range(length):\n","        encoder_hidden,encoder_cell = encoder(\n","              input_tensor[ei], encoder_hidden,encoder_cell)\n","  return encoder_hidden.view(-1)"],"execution_count":91,"outputs":[]},{"cell_type":"code","metadata":{"id":"6FW0tXi2t90O","executionInfo":{"status":"ok","timestamp":1640863599541,"user_tz":-330,"elapsed":22,"user":{"displayName":"Gihan Weeraprameshwara","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiCuBWPBFW3AvbkkMkY_XlfhSKS9YjMV2DdXXxL=s64","userId":"07090364678417433377"}}},"source":["ZEROS = np.zeros(EMBEDDING_SIZE)\n","SoP = ZEROS - 1\n","EoP = SoP + 2"],"execution_count":92,"outputs":[]},{"cell_type":"code","metadata":{"id":"WthWhf3-Aa1a","executionInfo":{"status":"ok","timestamp":1640863599542,"user_tz":-330,"elapsed":19,"user":{"displayName":"Gihan Weeraprameshwara","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiCuBWPBFW3AvbkkMkY_XlfhSKS9YjMV2DdXXxL=s64","userId":"07090364678417433377"}}},"source":["def get_embeddings(sentences):\n","  embeddings=[SoP]\n","  for i in range (len(sentences)):\n","    sen_emb=sentence_embedding(sentences[i])\n","    if (sen_emb!=None):\n","      embeddings.append(sen_emb.detach().cpu().numpy())\n","  if (len(embeddings)==1):\n","    return None\n","  else:\n","    embeddings.append(EoP)\n","    if len(embeddings) < 28:\n","      for x in range(28 - len(embeddings)):\n","        embeddings.append(ZEROS)\n","    return np.array(embeddings)"],"execution_count":93,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"0cvorEy_tc5G"},"source":["### Input embedding"]},{"cell_type":"code","metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"tX3FTQZnTOeH","outputId":"30f98aee-1bd9-4a18-a288-88379144f937","executionInfo":{"status":"ok","timestamp":1640863807112,"user_tz":-330,"elapsed":207589,"user":{"displayName":"Gihan Weeraprameshwara","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiCuBWPBFW3AvbkkMkY_XlfhSKS9YjMV2DdXXxL=s64","userId":"07090364678417433377"}}},"source":["for j in range (4*(i-1),4*i):\n","  print(j)\n","  m = [get_embeddings(post) for post in all_data['posts'][(j)*10000:(j+1)*10000]]\n","  all_data['posts'][j*10000:(j+1)*10000] = m"],"execution_count":94,"outputs":[{"output_type":"stream","name":"stdout","text":["16\n"]},{"output_type":"stream","name":"stderr","text":["/usr/local/lib/python3.7/dist-packages/ipykernel_launcher.py:4: SettingWithCopyWarning: \n","A value is trying to be set on a copy of a slice from a DataFrame\n","\n","See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n","  after removing the cwd from sys.path.\n","/usr/local/lib/python3.7/dist-packages/pandas/core/internals/blocks.py:849: VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray\n","  arr_value = np.array(value)\n"]},{"output_type":"stream","name":"stdout","text":["17\n","18\n","19\n"]}]},{"cell_type":"code","metadata":{"id":"8hqI-LX--Eia","executionInfo":{"status":"ok","timestamp":1640863825020,"user_tz":-330,"elapsed":17911,"user":{"displayName":"Gihan Weeraprameshwara","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiCuBWPBFW3AvbkkMkY_XlfhSKS9YjMV2DdXXxL=s64","userId":"07090364678417433377"}}},"source":["all_data[(i-1)*40000:(i)*40000].to_pickle(embedding_path)"],"execution_count":95,"outputs":[]}]}