Path: blob/master/48_tf_serving/BERT_email_classification.ipynb
1141 views
Kernel: Python 3
BERT tutorial: Classify spam vs no spam emails
In [1]:
Import the dataset (Dataset is taken from kaggle)
In [2]:
Out[2]:
In [3]:
Out[3]:
In [4]:
Out[4]:
Split it into training and test data set
In [5]:
In [6]:
Out[6]:
4870 1. Tension face 2. Smiling face 3. Waste face ...
1281 Oh ho. Is this the first time u use these type...
1666 Dunno cos i was v late n when i reach they ins...
1254 Mum say we wan to go then go... Then she can s...
Name: Message, dtype: object
Now lets import BERT model and get embeding vectors for few sample statements
In [7]:
In [8]:
Out[8]:
<tf.Tensor: shape=(2, 768), dtype=float32, numpy=
array([[-0.8435169 , -0.51327276, -0.8884574 , ..., -0.74748874,
-0.75314736, 0.91964483],
[-0.87208366, -0.50543964, -0.94446677, ..., -0.858475 ,
-0.7174535 , 0.8808298 ]], dtype=float32)>
Get embeding vectors for few sample words. Compare them using cosine similarity
In [9]:
In [10]:
Out[10]:
array([[0.9911089]], dtype=float32)
Values near to 1 means they are similar. 0 means they are very different. Above you can use comparing "banana" vs "grapes" you get 0.99 similarity as they both are fruits
In [11]:
Out[11]:
array([[0.8470385]], dtype=float32)
Comparing banana with jeff bezos you still get 0.84 but it is not as close as 0.99 that we got with grapes
In [12]:
Out[12]:
array([[0.98720354]], dtype=float32)
Jeff bezos and Elon musk are more similar then Jeff bezos and banana as indicated above
Build Model
There are two types of models you can build in tensorflow.
(1) Sequential (2) Functional
So far we have built sequential model. But below we will build functional model. More information on these two is here: https://becominghuman.ai/sequential-vs-functional-model-in-keras-20684f766057
In [13]:
In [14]:
Out[14]:
Model: "model"
__________________________________________________________________________________________________
Layer (type) Output Shape Param # Connected to
==================================================================================================
text (InputLayer) [(None,)] 0
__________________________________________________________________________________________________
keras_layer (KerasLayer) {'input_mask': (None 0 text[0][0]
__________________________________________________________________________________________________
keras_layer_1 (KerasLayer) {'default': (None, 7 109482241 keras_layer[0][0]
keras_layer[0][1]
keras_layer[0][2]
__________________________________________________________________________________________________
dropout (Dropout) (None, 768) 0 keras_layer_1[0][13]
__________________________________________________________________________________________________
output (Dense) (None, 1) 769 dropout[0][0]
==================================================================================================
Total params: 109,483,010
Trainable params: 769
Non-trainable params: 109,482,241
__________________________________________________________________________________________________
In [15]:
Out[15]:
4179
In [16]:
Train the model
In [17]:
Out[17]:
Epoch 1/5
131/131 [==============================] - 31s 188ms/step - loss: 0.3172 - accuracy: 0.8686
Epoch 2/5
131/131 [==============================] - 26s 197ms/step - loss: 0.2286 - accuracy: 0.8995
Epoch 3/5
131/131 [==============================] - 26s 199ms/step - loss: 0.1925 - accuracy: 0.9244
Epoch 4/5
131/131 [==============================] - 27s 204ms/step - loss: 0.1703 - accuracy: 0.9354
Epoch 5/5
131/131 [==============================] - 25s 189ms/step - loss: 0.1550 - accuracy: 0.9457
<tensorflow.python.keras.callbacks.History at 0x2add6f58c70>
In [18]:
Out[18]:
44/44 [==============================] - 9s 187ms/step - loss: 0.1490 - accuracy: 0.9476
[0.1490102857351303, 0.9475951194763184]
Inference
In [19]:
Out[19]:
array([[0.5297462 ],
[0.58646685],
[0.40190795],
[0.03496679],
[0.01311444]], dtype=float32)
In [22]:
Out[22]:
WARNING:absl:Found untraced functions such as restored_function_body, restored_function_body, restored_function_body, restored_function_body, restored_function_body while saving (showing 5 of 915). These functions will not be directly callable after loading.
INFO:tensorflow:Assets written to: saved_models/1/assets
INFO:tensorflow:Assets written to: saved_models/1/assets
In [23]:
Out[23]:
WARNING:absl:Found untraced functions such as restored_function_body, restored_function_body, restored_function_body, restored_function_body, restored_function_body while saving (showing 5 of 915). These functions will not be directly callable after loading.
INFO:tensorflow:Assets written to: saved_models/2/assets
INFO:tensorflow:Assets written to: saved_models/2/assets
In [24]:
Out[24]:
WARNING:absl:Found untraced functions such as restored_function_body, restored_function_body, restored_function_body, restored_function_body, restored_function_body while saving (showing 5 of 915). These functions will not be directly callable after loading.
INFO:tensorflow:Assets written to: saved_models/3/assets
INFO:tensorflow:Assets written to: saved_models/3/assets