In [1]:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn import metrics
from sklearn.preprocessing import LabelEncoder,OneHotEncoder
from tensorflow.keras.models import Model
from tensorflow.keras.layers import LSTM, GRU, Dense, Embedding, Dropout, GlobalAveragePooling1D, Flatten, SpatialDropout1D, Bidirectional
from tensorflow.keras.optimizers import RMSprop
from tensorflow.keras.preprocessing.text import Tokenizer
from tensorflow.keras.preprocessing import sequence
from tensorflow.keras.callbacks import EarlyStopping
In [2]:
# https://heartbeat.comet.ml/text-classification-using-bi-directional-lstm-ca0070df7a81
#Deep Learning Libraries for LSTM
from tensorflow.keras.layers import Embedding
from tensorflow.keras.preprocessing.sequence import pad_sequences
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import LSTM
from tensorflow.keras.layers import Dense
from tensorflow.keras.preprocessing.text import one_hot
from tensorflow.keras.layers import Bidirectional
#Vocabulary Size
vocab_size=5000 #max_words
In [3]:
df1=pd.read_csv('20220201_new_moj_e_all.csv')
In [4]:
train_rate = 0.7
val_rate = 0.1
test_rate =0.2
train_df = df1.iloc[:int(train_rate*len(df1))]
val_df = df1.iloc[int(train_rate*len(df1)):int((train_rate+val_rate)*len(df1))]
test_df = df1.iloc[int((train_rate+val_rate)*len(df1)):]
display(train_df.head())
| Message | ground_true | jieba_seg | emoj | moj_e | moj_c | mer_seg | Message_emoji | Messge_clear | |
|---|---|---|---|---|---|---|---|---|---|
| 0 | 今日本土+321 +400例校正回歸 大家要自主健康管理口罩戴好 沒清潔不要碰小孩 誠心祝福... | P | 今日 本土 + 321 + 400 例 校正回歸 大家 要 自主 健康 管理 口罩 ... | 🙏 | ['folded hands'] | ['雙手合十的人'] | 今日 本土 + 321 + 400 例 校正回歸 大家 要 自主 健康 管理 口罩 ... | 今日本土+321 +400例校正回歸 大家要自主健康管理口罩戴好 沒清潔不要碰小孩 誠心祝福... | 今日本土+321 +400例校正回歸 大家要自主健康管理口罩戴好 沒清潔不要碰小孩 誠心祝福... |
| 1 | 參考,看雅琴的理解對不對~很多人在問:什麼是400例校正回歸上週~我的理解是,今天驗出確診是... | O | 參考 , 看 雅琴 的 理解 對 不 對 ~ 很多 人在問 : 什麼 是 400 例 校正回... | 😊👍 | ['smiling face with smiling eyes', 'thumbs up'] | ['眼睛笑眯眯的表情', '拇指朝上符號'] | 參考 , 看 雅琴 的 理解 對 不 對 很多 人在問 : 什麼 是 400 例 校正回歸... | 參考,看雅琴的理解對不對~很多人在問:什麼是400例校正回歸上週~我的理解是,今天驗出確診是... | 參考,看雅琴的理解對不對~很多人在問:什麼是400例校正回歸上週~我的理解是,今天驗出確診是... |
| 2 | 周末不待在家,跑去陽明山玩!(#01J) ↗下載ETtoday新聞雲App,掌握大小事 ht... | O | 周末 不 待 在家 , 跑 去 陽明山 玩 ! ( # 01J ) ↗ 下載 ETtod... | NaN | NaN | 周末 不 待 在家 , 跑 去 陽明山 玩 ! ( 01J ) ↗ 下載 ETtoda... | 周末不待在家,跑去陽明山玩(01J) 下載ETtoday新聞雲App,掌握大小事 sourc... | 周末不待在家,跑去陽明山玩(01J) 下載ETtoday新聞雲App,掌握大小事 sourc... | |
| 3 | 校正回歸是? | O | 校正回歸 是 ? | NaN | NaN | 校正回歸 是 ? | 校正回歸是 | 校正回歸是 | |
| 4 | 疫情新聞吃重,#校正 人力之後我就 #回歸 加班了,反正哪都不能去,在家也是跟同事討論新聞。... | O | 疫情 新聞 吃 重 , # 校正 人力 之 後 我 就 # 回歸 加班 了 , ... | NaN | NaN | 疫情 新聞 吃 重 , 校正 人力 之 後 我 就 回歸 加班 了 , 反正... | 疫情新聞吃重,校正 人力之後我就 回歸 加班了,反正哪都不能去,在家也是跟同事討論新聞。在假... | 疫情新聞吃重,校正 人力之後我就 回歸 加班了,反正哪都不能去,在家也是跟同事討論新聞。在假... |
In [5]:
## 對dataset label 做編碼
train_y = train_df.ground_true
val_y = val_df.ground_true
test_y = test_df.ground_true
train_y1 = train_df.ground_true
val_y1 = val_df.ground_true
test_y1 = test_df.ground_true
le = LabelEncoder()
train_y = le.fit_transform(train_y).reshape(-1,1)
val_y = le.transform(val_y).reshape(-1,1)
test_y = le.transform(test_y).reshape(-1,1)
## 對dataset label做one-hot encoding
ohe = OneHotEncoder()
train_y = ohe.fit_transform(train_y).toarray()
val_y = ohe.transform(val_y).toarray()
test_y = ohe.transform(test_y).toarray()
## 使用Tokenizer對詞組進行編碼
## 當我們建立一個Tokenizer對象後,使用該對象的fit_on_texts()函数,以空格去便是每個詞,
## 可以將輸入的文本中的每個詞編號,編號是根據詞頻的,詞頻越大,編號越小。
max_words = 5000
max_len = 600
tok = Tokenizer(num_words=max_words) ## 使用的最大詞語數为5000
# tok = Tokenizer(num_words=max_words, filters='!"#$%&()*+,-./:;<=>@[\]~{|}^',lower=True)
# train_df.mer_seg --> only text
#tok.fit_on_texts(train_df.mer_seg)
# train_df.mer_seg --> text and emoji clearly; Message_emoji -->text and emoji
tok.fit_on_texts(train_df.Message_emoji)
In [6]:
print(train_y.shape)
print(val_y.shape)
print(test_y.shape)
(4221, 3) (603, 3) (1207, 3)
In [7]:
# condition1: train_df.mer_seg --> only text
"""
train_seq = tok.texts_to_sequences(train_df.mer_seg)
train_seq_mat = sequence.pad_sequences(train_seq,maxlen=max_len)
print(train_seq_mat)
"""
# condition2: train_df.mer_seg --> text and emoji clearly
train_seq = tok.texts_to_sequences(train_df.Message_emoji)
train_seq_mat = sequence.pad_sequences(train_seq,maxlen=max_len)
print(train_seq_mat)
[[ 0 0 0 ... 2421 4799 69] [ 0 0 0 ... 4814 497 133] [ 0 0 0 ... 52 60 62] ... [ 0 0 0 ... 4686 4687 4688] [ 0 0 0 ... 220 1 43] [ 0 0 0 ... 92 615 546]]
In [8]:
# it's very important
# call sequence function must import tensorflow.keras.preprocessing
# i.e. replaced all import keras.xxxx to import tensorflow.keras.xxxx
## condition1: train_df.mer_seg --> only text
from tensorflow.keras.preprocessing import sequence
train_seq = tok.texts_to_sequences(train_df.mer_seg)
val_seq = tok.texts_to_sequences(val_df.mer_seg)
test_seq = tok.texts_to_sequences(test_df.mer_seg)
## 將每個序列調整相同的長度
train_seq_mat = sequence.pad_sequences(train_seq,maxlen=max_len)
val_seq_mat = sequence.pad_sequences(val_seq,maxlen=max_len)
test_seq_mat = sequence.pad_sequences(test_seq,maxlen=max_len)
print(train_seq_mat.shape)
print(val_seq_mat.shape)
print(test_seq_mat.shape)
(4221, 600) (603, 600) (1207, 600)
In [9]:
# it's very important
# call sequence function must import tensorflow.keras.preprocessing
# i.e. replaced all import keras.xxxx to import tensorflow.keras.xxxx
from tensorflow.keras.preprocessing import sequence
## condition2: train_df.mer_seg --> text and emoji clearly
train_seq = tok.texts_to_sequences(train_df.Message_emoji)
val_seq = tok.texts_to_sequences(val_df.Message_emoji)
test_seq = tok.texts_to_sequences(test_df.Message_emoji)
## 將每個序列調整相同的長度
train_seq_mat = sequence.pad_sequences(train_seq,maxlen=max_len)
val_seq_mat = sequence.pad_sequences(val_seq,maxlen=max_len)
test_seq_mat = sequence.pad_sequences(test_seq,maxlen=max_len)
print(train_seq_mat.shape)
print(val_seq_mat.shape)
print(test_seq_mat.shape)
(4221, 600) (603, 600) (1207, 600)
In [10]:
# LSTM model
# https://blog.csdn.net/sinat_23971513/article/details/120568766
model=Sequential()
model.add(Embedding(max_words,100, input_length=df1.shape[1]))
model.add(SpatialDropout1D(0.2))
model.add(LSTM(100,dropout=0.2, recurrent_dropout=0.2))
model.add(Dense(3,activation='softmax'))
model.compile(loss='categorical_crossentropy',optimizer="adam",metrics=["accuracy"])
print(model.summary())
history=model.fit(train_seq_mat,train_y,epochs=5, batch_size=64,validation_split=0.1,
callbacks=[EarlyStopping(monitor='val_loss',patience=3,min_delta=0.0001)])
Model: "sequential" _________________________________________________________________ Layer (type) Output Shape Param # ================================================================= embedding (Embedding) (None, 9, 100) 500000 _________________________________________________________________ spatial_dropout1d (SpatialDr (None, 9, 100) 0 _________________________________________________________________ lstm (LSTM) (None, 100) 80400 _________________________________________________________________ dense (Dense) (None, 3) 303 ================================================================= Total params: 580,703 Trainable params: 580,703 Non-trainable params: 0 _________________________________________________________________ None Epoch 1/5 WARNING:tensorflow:Model was constructed with shape (None, 9) for input KerasTensor(type_spec=TensorSpec(shape=(None, 9), dtype=tf.float32, name='embedding_input'), name='embedding_input', description="created by layer 'embedding_input'"), but it was called on an input with incompatible shape (None, 600). WARNING:tensorflow:Model was constructed with shape (None, 9) for input KerasTensor(type_spec=TensorSpec(shape=(None, 9), dtype=tf.float32, name='embedding_input'), name='embedding_input', description="created by layer 'embedding_input'"), but it was called on an input with incompatible shape (None, 600). 60/60 [==============================] - ETA: 0s - loss: 1.0461 - accuracy: 0.4778WARNING:tensorflow:Model was constructed with shape (None, 9) for input KerasTensor(type_spec=TensorSpec(shape=(None, 9), dtype=tf.float32, name='embedding_input'), name='embedding_input', description="created by layer 'embedding_input'"), but it was called on an input with incompatible shape (None, 600). 60/60 [==============================] - 245s 4s/step - loss: 1.0455 - accuracy: 0.4781 - val_loss: 1.2674 - val_accuracy: 0.2151 Epoch 2/5 60/60 [==============================] - 260s 4s/step - loss: 0.9344 - accuracy: 0.5329 - val_loss: 1.1841 - val_accuracy: 0.2435 Epoch 3/5 60/60 [==============================] - 355s 6s/step - loss: 0.8240 - accuracy: 0.6256 - val_loss: 1.2290 - val_accuracy: 0.2624 Epoch 4/5 60/60 [==============================] - 1411s 24s/step - loss: 0.7336 - accuracy: 0.6472 - val_loss: 1.2202 - val_accuracy: 0.4823 Epoch 5/5 60/60 [==============================] - 491s 8s/step - loss: 0.6564 - accuracy: 0.7056 - val_loss: 1.3505 - val_accuracy: 0.3239
In [11]:
# modify confusion matrix @ 2023/05/07
import matplotlib.pyplot as plt
import seaborn as sns
# model.predict() is multi-label and multiclass targets
# model.predict_classes() function is binary-label and binary-class targets
test_pre = model.predict(test_seq_mat)
## 評價預測效果,計算混淆矩陣
confm = metrics.confusion_matrix(np.argmax(test_y,axis=1),np.argmax(test_pre,axis=1))
## 混淆矩陣可視化
#Labname = ["体育","娱乐","家居","房产","教育","时尚","时政","游戏","科技","财经"]
Labname = ["N","O","P"]
plt.figure(figsize=(6,6))
sns.set(font_scale=1.5)
sns.heatmap(confm.T, square=True, annot=True,
fmt='d', cbar=False,linewidths=.8,
cmap="YlGnBu")
plt.xlabel('predicted label',size = 14)
plt.ylabel('ground_true label',size = 14)
plt.xticks(np.arange(3)+0.5,Labname,size = 12) #Labname,
plt.yticks(np.arange(3)+0.3,Labname,size = 12)
plt.show()
print(metrics.classification_report(np.argmax(test_y,axis=1),np.argmax(test_pre,axis=1)))
WARNING:tensorflow:Model was constructed with shape (None, 9) for input KerasTensor(type_spec=TensorSpec(shape=(None, 9), dtype=tf.float32, name='embedding_input'), name='embedding_input', description="created by layer 'embedding_input'"), but it was called on an input with incompatible shape (None, 600).
precision recall f1-score support
0 0.24 0.03 0.05 391
1 0.62 0.63 0.62 495
2 0.29 0.59 0.39 321
accuracy 0.42 1207
macro avg 0.38 0.41 0.35 1207
weighted avg 0.41 0.42 0.37 1207
In [12]:
print(metrics.confusion_matrix(np.argmax(test_y,axis=1),np.argmax(test_pre,axis=1)))
[[ 10 87 294] [ 7 312 176] [ 24 108 189]]
In [28]:
# old version (maybe wrong)
import matplotlib.pyplot as plt
import seaborn as sns
# model.predict() is multi-label and multiclass targets
# model.predict_classes() function is binary-label and binary-class targets
test_pre = model.predict(test_seq_mat)
## 評價預測效果,計算混淆矩陣
confm = metrics.confusion_matrix(np.argmax(test_pre,axis=1),np.argmax(test_y,axis=1))
## 混淆矩陣可視化
#Labname = ["体育","娱乐","家居","房产","教育","时尚","时政","游戏","科技","财经"]
Labname = ["N","O","P"]
plt.figure(figsize=(6,6))
sns.set(font_scale=1.5)
sns.heatmap(confm.T, square=True, annot=True,
fmt='d', cbar=False,linewidths=.8,
cmap="YlGnBu")
plt.xlabel('predicted label',size = 14)
plt.ylabel('ground_true label',size = 14)
plt.xticks(np.arange(3)+0.5,Labname,size = 12) #Labname,
plt.yticks(np.arange(3)+0.3,Labname,size = 12)
plt.show()
print(metrics.classification_report(np.argmax(test_pre,axis=1),np.argmax(test_y,axis=1)))
WARNING:tensorflow:Model was constructed with shape (None, 9) for input KerasTensor(type_spec=TensorSpec(shape=(None, 9), dtype=tf.float32, name='embedding_2_input'), name='embedding_2_input', description="created by layer 'embedding_2_input'"), but it was called on an input with incompatible shape (None, 600).
precision recall f1-score support
0 0.07 0.33 0.12 87
1 0.82 0.43 0.56 948
2 0.21 0.39 0.27 172
accuracy 0.42 1207
macro avg 0.37 0.38 0.32 1207
weighted avg 0.68 0.42 0.49 1207
In [13]:
# BiLSTM compared with LSTM
model_1 = Sequential()
model_1.add(Embedding(max_words, 100, input_length=df1.shape[1]))
model_1.add(SpatialDropout1D(0.2))
model_1.add(Bidirectional(LSTM(100,dropout=0.2, recurrent_dropout=0.2)))
model_1.add(Dense(3,activation='softmax')) #sigmoid
model_1.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
print(model_1.summary())
model_1.fit(train_seq_mat, train_y, epochs=5, batch_size=64,validation_split=0.1,
callbacks=[EarlyStopping(monitor='val_loss',patience=3,min_delta=0.0001)])
Model: "sequential_1" _________________________________________________________________ Layer (type) Output Shape Param # ================================================================= embedding_1 (Embedding) (None, 9, 100) 500000 _________________________________________________________________ spatial_dropout1d_1 (Spatial (None, 9, 100) 0 _________________________________________________________________ bidirectional (Bidirectional (None, 200) 160800 _________________________________________________________________ dense_1 (Dense) (None, 3) 603 ================================================================= Total params: 661,403 Trainable params: 661,403 Non-trainable params: 0 _________________________________________________________________ None Epoch 1/5 WARNING:tensorflow:Model was constructed with shape (None, 9) for input KerasTensor(type_spec=TensorSpec(shape=(None, 9), dtype=tf.float32, name='embedding_1_input'), name='embedding_1_input', description="created by layer 'embedding_1_input'"), but it was called on an input with incompatible shape (None, 600). WARNING:tensorflow:Model was constructed with shape (None, 9) for input KerasTensor(type_spec=TensorSpec(shape=(None, 9), dtype=tf.float32, name='embedding_1_input'), name='embedding_1_input', description="created by layer 'embedding_1_input'"), but it was called on an input with incompatible shape (None, 600). 60/60 [==============================] - ETA: 0s - loss: 1.0297 - accuracy: 0.4986 WARNING:tensorflow:Model was constructed with shape (None, 9) for input KerasTensor(type_spec=TensorSpec(shape=(None, 9), dtype=tf.float32, name='embedding_1_input'), name='embedding_1_input', description="created by layer 'embedding_1_input'"), but it was called on an input with incompatible shape (None, 600). 60/60 [==============================] - 40943s 693s/step - loss: 1.0295 - accuracy: 0.4987 - val_loss: 1.2916 - val_accuracy: 0.2222 Epoch 2/5 60/60 [==============================] - 6682s 113s/step - loss: 0.9374 - accuracy: 0.5367 - val_loss: 1.2312 - val_accuracy: 0.2435 Epoch 3/5 60/60 [==============================] - 1388s 23s/step - loss: 0.8281 - accuracy: 0.6167 - val_loss: 1.2593 - val_accuracy: 0.2600 Epoch 4/5 60/60 [==============================] - 9494s 160s/step - loss: 0.7303 - accuracy: 0.6549 - val_loss: 1.2439 - val_accuracy: 0.2742 Epoch 5/5 60/60 [==============================] - 2065s 35s/step - loss: 0.6588 - accuracy: 0.6923 - val_loss: 1.2656 - val_accuracy: 0.3002
Out[13]:
<tensorflow.python.keras.callbacks.History at 0x165b03bb760>
In [14]:
# model.predict() is multi-label and multiclass targets
# model.predict_classes() function is binary-label and binary-class targets
import seaborn as sns
test_pre = model.predict(test_seq_mat)
## 評價預測效果,計算混淆矩陣
#confm = metrics.confusion_matrix(np.argmax(test_pre,axis=1),np.argmax(test_y,axis=1))
confm = metrics.confusion_matrix(np.argmax(test_y,axis=1),np.argmax(test_pre,axis=1))
## 混淆矩陣可視化
#Labname = ["体育","娱乐","家居","房产","教育","时尚","时政","游戏","科技","财经"]
Labname = ["N","O","P"]
plt.figure(figsize=(6,6))
sns.set(font_scale=1.5)
sns.heatmap(confm.T, square=True, annot=True,
fmt='d', cbar=False,linewidths=.8,
cmap="YlGnBu")
plt.xlabel('predicted label',size = 14)
plt.ylabel('ground_true label',size = 14)
plt.xticks(np.arange(3)+0.5,Labname,size = 12) #Labname,
plt.yticks(np.arange(3)+0.3,Labname,size = 12)
plt.show()
print(metrics.classification_report(np.argmax(np.argmax(test_y,axis=1),np.argmax(test_pre,axis=1))
File "<ipython-input-14-117cdfc60b32>", line 23 print(metrics.classification_report(np.argmax(np.argmax(test_y,axis=1),np.argmax(test_pre,axis=1)) ^ SyntaxError: unexpected EOF while parsing
In [4]:
#BiLSTM model using posts with text and emoji
## 對dataset label 做編碼
train_y = train_df.ground_true
val_y = val_df.ground_true
test_y = test_df.ground_true
train_y1 = train_df.ground_true
val_y1 = val_df.ground_true
test_y1 = test_df.ground_true
le = LabelEncoder()
train_y = le.fit_transform(train_y).reshape(-1,1)
val_y = le.transform(val_y).reshape(-1,1)
test_y = le.transform(test_y).reshape(-1,1)
## 對dataset label做one-hot encoding
ohe = OneHotEncoder()
train_y = ohe.fit_transform(train_y).toarray()
val_y = ohe.transform(val_y).toarray()
test_y = ohe.transform(test_y).toarray()
## 使用Tokenizer對詞組進行編碼
## 當我們建立一個Tokenizer對象後,使用該對象的fit_on_texts()函数,以空格去便是每個詞,
## 可以將輸入的文本中的每個詞編號,編號是根據詞頻的,詞頻越大,編號越小。
max_words = 5000
max_len = 600
tok = Tokenizer(num_words=max_words) ## 使用的最大詞語數为5000
# tok = Tokenizer(num_words=max_words, filters='!"#$%&()*+,-./:;<=>@[\]~{|}^',lower=True)
tok.fit_on_texts(train_df.Message_emoji)
In [5]:
train_seq = tok.texts_to_sequences(train_df.Message_emoji)
train_seq_mat = sequence.pad_sequences(train_seq,maxlen=max_len)
print(train_seq_mat)
[[ 0 0 0 ... 2421 4799 69] [ 0 0 0 ... 4814 497 133] [ 0 0 0 ... 52 60 62] ... [ 0 0 0 ... 4686 4687 4688] [ 0 0 0 ... 220 1 43] [ 0 0 0 ... 92 615 546]]
In [8]:
model_1 = Sequential()
model_1.add(Embedding(max_words, 100, input_length=df1.shape[1]))
model_1.add(SpatialDropout1D(0.2))
model_1.add(Bidirectional(LSTM(100,dropout=0.2, recurrent_dropout=0.2)))
model_1.add(Dense(3,activation='softmax')) #sigmoid
model_1.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
print(model_1.summary())
model_1.fit(train_seq_mat, train_y, epochs=5, batch_size=64,validation_split=0.1,
callbacks=[EarlyStopping(monitor='val_loss',patience=3,min_delta=0.0001)])
Model: "sequential" _________________________________________________________________ Layer (type) Output Shape Param # ================================================================= embedding (Embedding) (None, 9, 100) 500000 _________________________________________________________________ spatial_dropout1d (SpatialDr (None, 9, 100) 0 _________________________________________________________________ bidirectional (Bidirectional (None, 200) 160800 _________________________________________________________________ dense (Dense) (None, 3) 603 ================================================================= Total params: 661,403 Trainable params: 661,403 Non-trainable params: 0 _________________________________________________________________ None Epoch 1/5 WARNING:tensorflow:Model was constructed with shape (None, 9) for input KerasTensor(type_spec=TensorSpec(shape=(None, 9), dtype=tf.float32, name='embedding_input'), name='embedding_input', description="created by layer 'embedding_input'"), but it was called on an input with incompatible shape (None, 600). WARNING:tensorflow:Model was constructed with shape (None, 9) for input KerasTensor(type_spec=TensorSpec(shape=(None, 9), dtype=tf.float32, name='embedding_input'), name='embedding_input', description="created by layer 'embedding_input'"), but it was called on an input with incompatible shape (None, 600). 60/60 [==============================] - ETA: 0s - loss: 1.0407 - accuracy: 0.4746 WARNING:tensorflow:Model was constructed with shape (None, 9) for input KerasTensor(type_spec=TensorSpec(shape=(None, 9), dtype=tf.float32, name='embedding_input'), name='embedding_input', description="created by layer 'embedding_input'"), but it was called on an input with incompatible shape (None, 600). 60/60 [==============================] - 1363s 23s/step - loss: 1.0403 - accuracy: 0.4749 - val_loss: 1.2827 - val_accuracy: 0.2175 Epoch 2/5 60/60 [==============================] - 1116s 19s/step - loss: 0.9288 - accuracy: 0.5366 - val_loss: 1.3275 - val_accuracy: 0.1891 Epoch 3/5 60/60 [==============================] - 800s 13s/step - loss: 0.8286 - accuracy: 0.6119 - val_loss: 1.1597 - val_accuracy: 0.4444 Epoch 4/5 60/60 [==============================] - 810s 14s/step - loss: 0.7458 - accuracy: 0.6529 - val_loss: 1.2647 - val_accuracy: 0.4704 Epoch 5/5 60/60 [==============================] - 1914s 32s/step - loss: 0.6313 - accuracy: 0.7067 - val_loss: 1.2237 - val_accuracy: 0.4894
Out[8]:
<tensorflow.python.keras.callbacks.History at 0x1b6a2f153d0>
In [10]:
# it's very important
# call sequence function must import tensorflow.keras.preprocessing
# i.e. replaced all import keras.xxxx to import tensorflow.keras.xxxx
from tensorflow.keras.preprocessing import sequence
train_seq = tok.texts_to_sequences(train_df.Message_emoji)
val_seq = tok.texts_to_sequences(val_df.Message_emoji)
test_seq = tok.texts_to_sequences(test_df.Message_emoji)
## 將每個序列調整相同的長度
train_seq_mat = sequence.pad_sequences(train_seq,maxlen=max_len)
val_seq_mat = sequence.pad_sequences(val_seq,maxlen=max_len)
test_seq_mat = sequence.pad_sequences(test_seq,maxlen=max_len)
print(train_seq_mat.shape)
print(val_seq_mat.shape)
print(test_seq_mat.shape)
(4221, 600) (603, 600) (1207, 600)
In [ ]:
# model.predict() is multi-label and multiclass targets
# model.predict_classes() function is binary-label and binary-class targets
test_pre = model_1.predict(test_seq_mat)
In [12]:
## 評價預測效果,計算混淆矩陣
confm = metrics.confusion_matrix(np.argmax(test_pre,axis=1),np.argmax(test_y,axis=1))
## 混淆矩陣可視化
#Labname = ["体育","娱乐","家居","房产","教育","时尚","时政","游戏","科技","财经"]
Labname = ["N","O","P"]
plt.figure(figsize=(6,6))
sns.set(font_scale=1.5)
sns.heatmap(confm.T, square=True, annot=True,
fmt='d', cbar=False,linewidths=.8,
cmap="YlGnBu")
plt.xlabel('predicted label',size = 14)
plt.ylabel('ground_true label',size = 14)
plt.xticks(np.arange(3)+0.5,Labname,size = 12) #Labname,
plt.yticks(np.arange(3)+0.3,Labname,size = 12)
plt.show()
print(metrics.classification_report(np.argmax(test_pre,axis=1),np.argmax(test_y,axis=1)))
precision recall f1-score support
0 0.69 0.51 0.59 530
1 0.62 0.63 0.63 490
2 0.22 0.37 0.28 187
accuracy 0.54 1207
macro avg 0.51 0.50 0.50 1207
weighted avg 0.59 0.54 0.55 1207
In [ ]: