TarunSinghal commited on
Commit
690fcfe
1 Parent(s): 698a1d8

Upload FineTunnedBertBaseModel_Use.ipynb

Browse files
Files changed (1) hide show
  1. FineTunnedBertBaseModel_Use.ipynb +1 -0
FineTunnedBertBaseModel_Use.ipynb ADDED
@@ -0,0 +1 @@
 
 
1
+ {"nbformat":4,"nbformat_minor":0,"metadata":{"colab":{"provenance":[{"file_id":"1nfj0BGOW4MR1IfJDPB114qVrd9-mktFa","timestamp":1686635069405}],"gpuType":"T4","mount_file_id":"1nfj0BGOW4MR1IfJDPB114qVrd9-mktFa","authorship_tag":"ABX9TyMjLN/IArHMH/nsH3VCZkZ/"},"kernelspec":{"name":"python3","display_name":"Python 3"},"language_info":{"name":"python"},"accelerator":"GPU"},"cells":[{"cell_type":"code","execution_count":1,"metadata":{"id":"spC92L4kM8Nn","executionInfo":{"status":"ok","timestamp":1686634160153,"user_tz":-330,"elapsed":33187,"user":{"displayName":"Tarun Singhal","userId":"01673926791331684615"}}},"outputs":[],"source":["%%capture\n","!pip install transformers\n","!pip install datasets"]},{"cell_type":"code","source":["import tensorflow as tf\n","from transformers import TFAutoModel, AutoTokenizer\n","from datasets import load_dataset"],"metadata":{"id":"IrEeGWnmNYae","executionInfo":{"status":"ok","timestamp":1686634163014,"user_tz":-330,"elapsed":2884,"user":{"displayName":"Tarun Singhal","userId":"01673926791331684615"}}},"execution_count":2,"outputs":[]},{"cell_type":"code","source":["%%capture\n","model = TFAutoModel.from_pretrained(\"bert-base-uncased\")\n","tokenizer = AutoTokenizer.from_pretrained(\"bert-base-uncased\")"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"8GDKrFESNcsi","executionInfo":{"status":"ok","timestamp":1686634540676,"user_tz":-330,"elapsed":5116,"user":{"displayName":"Tarun Singhal","userId":"01673926791331684615"}},"outputId":"3dfc76e1-cde2-4c20-898b-5a256a037a12"},"execution_count":11,"outputs":[{"output_type":"stream","name":"stderr","text":["Some weights of the PyTorch model were not used when initializing the TF 2.0 model TFBertModel: ['cls.seq_relationship.weight', 'cls.predictions.transform.dense.bias', 'cls.predictions.transform.LayerNorm.bias', 'cls.seq_relationship.bias', 'cls.predictions.transform.dense.weight', 'cls.predictions.transform.LayerNorm.weight', 'cls.predictions.bias']\n","- This IS expected if you are initializing TFBertModel from a PyTorch model trained on another task or with another architecture (e.g. initializing a TFBertForSequenceClassification model from a BertForPreTraining model).\n","- This IS NOT expected if you are initializing TFBertModel from a PyTorch model that you expect to be exactly identical (e.g. initializing a TFBertForSequenceClassification model from a BertForSequenceClassification model).\n","All the weights of TFBertModel were initialized from the PyTorch model.\n","If your task is similar to the task the model of the checkpoint was trained on, you can already use TFBertModel for predictions without further training.\n"]}]},{"cell_type":"code","source":["class BERTForClassification(tf.keras.Model):\n","\n"," def __init__(self, bert_model, num_classes):\n"," super().__init__()\n"," self.bert = bert_model\n"," self.fc = tf.keras.layers.Dense(num_classes, activation='softmax')\n","\n"," def call(self, inputs):\n"," x = self.bert(inputs)[1]\n"," return self.fc(x)"],"metadata":{"id":"s6kqjQqaPLHN","executionInfo":{"status":"ok","timestamp":1686634553656,"user_tz":-330,"elapsed":4,"user":{"displayName":"Tarun Singhal","userId":"01673926791331684615"}}},"execution_count":12,"outputs":[]},{"cell_type":"code","source":["classifier_2 = BERTForClassification(bert_model=model, num_classes=6)"],"metadata":{"id":"a0TF6NY7ONXd","executionInfo":{"status":"ok","timestamp":1686634554206,"user_tz":-330,"elapsed":4,"user":{"displayName":"Tarun Singhal","userId":"01673926791331684615"}}},"execution_count":13,"outputs":[]},{"cell_type":"markdown","source":["##In the following path '/content/drive/MyDrive/FineTunning2/model2.1/model'\n","##model2.1 is the folder and in which there are three files -\n","1. model.index\n","2. checkpoint\n","3. model.data-00000-of-00001"],"metadata":{"id":"rlYg5H8rQsX7"}},{"cell_type":"code","source":["classifier_2.load_weights('/content/drive/MyDrive/FineTunning2/model2.1/model')"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"dS2BFve6ORnp","executionInfo":{"status":"ok","timestamp":1686634555522,"user_tz":-330,"elapsed":1320,"user":{"displayName":"Tarun Singhal","userId":"01673926791331684615"}},"outputId":"55a22c97-b7d0-426c-a34c-c1eeb82299cb"},"execution_count":14,"outputs":[{"output_type":"execute_result","data":{"text/plain":["<tensorflow.python.checkpoint.checkpoint.CheckpointLoadStatus at 0x7f2039e60df0>"]},"metadata":{},"execution_count":14}]},{"cell_type":"code","source":["example_input = \"Your work is very very bad\"\n","\n","encoded_example = tokenizer.encode(example_input, padding=True, truncation=True, return_tensors=\"tf\")\n","\n","\n","\n","p = classifier_2(encoded_example)"],"metadata":{"id":"onO80kHwPl3T","executionInfo":{"status":"ok","timestamp":1686634558975,"user_tz":-330,"elapsed":2325,"user":{"displayName":"Tarun Singhal","userId":"01673926791331684615"}}},"execution_count":15,"outputs":[]},{"cell_type":"code","source":["p"],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"k3LvGAcvQOjC","executionInfo":{"status":"ok","timestamp":1686634586050,"user_tz":-330,"elapsed":568,"user":{"displayName":"Tarun Singhal","userId":"01673926791331684615"}},"outputId":"3266e4bf-3e52-45bc-c01c-7b160046adeb"},"execution_count":17,"outputs":[{"output_type":"execute_result","data":{"text/plain":["<tf.Tensor: shape=(1, 6), dtype=float32, numpy=\n","array([[0.55223215, 0.09461907, 0.01764729, 0.2872823 , 0.03648205,\n"," 0.0117372 ]], dtype=float32)>"]},"metadata":{},"execution_count":17}]},{"cell_type":"markdown","source":["\n","#label: a classification label, with possible values including sadness (0), joy (1), love (2), anger (3), fear (4), surprise (5)"],"metadata":{"id":"Q3X490C9QmAi"}}]}