-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
180 lines (157 loc) · 7.92 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import tensorflow as tf
import pandas as pd
import warnings
warnings.filterwarnings('ignore')
data = pd.read_csv('../input/ner-dataset/ner_datasetreference.csv', encoding= 'unicode_escape')
print(data.shape)
data.head()
import matplotlib.pyplot as plt
import seaborn as sns
plt.figure(figsize = (16,8))
sns.set_style('darkgrid')
sns.histplot(data['Tag'])
plt.tight_layout(pad=2)
plt.show()
from itertools import chain
def get_dict_map(data, token_or_tag):
tok2idx = {}
idx2tok = {}
if token_or_tag == 'token':
vocab = list(set(data['Word'].to_list()))
else:
vocab = list(set(data['Tag'].to_list()))
idx2tok = {idx:tok for idx, tok in enumerate(vocab)}
tok2idx = {tok:idx for idx, tok in enumerate(vocab)}
return tok2idx, idx2tok
token2idx, idx2token = get_dict_map(data, 'token')
tag2idx, idx2tag = get_dict_map(data, 'tag')
data['Word_idx'] = data['Word'].map(token2idx)
data['Tag_idx'] = data['Tag'].map(tag2idx)
data_fillna = data.fillna(method='ffill', axis=0)
data_group = data_fillna.groupby(
['Sentence #'],as_index=False
)['Word', 'POS', 'Tag', 'Word_idx', 'Tag_idx'].agg(lambda x: list(x))
import numpy as np
from sklearn.model_selection import train_test_split
from tensorflow.keras.preprocessing.sequence import pad_sequences
from tensorflow.keras.utils import to_categorical
def get_pad_train_test_val(data_group, data):
#get max token and tag length
n_token = len(list(set(data['Word'].to_list())))
n_tag = len(list(set(data['Tag'].to_list())))
#Pad tokens (X var)
tokens = data_group['Word_idx'].tolist()
maxlen = max([len(s) for s in tokens])
pad_tokens = pad_sequences(tokens, maxlen=maxlen, dtype='int32', padding='post', value= n_token - 1)
#Pad Tags (y var) and convert it into one hot encoding
tags = data_group['Tag_idx'].tolist()
pad_tags = pad_sequences(tags, maxlen=maxlen, dtype='int32', padding='post', value= tag2idx["O"])
n_tags = len(tag2idx)
pad_tags = [to_categorical(i, num_classes=n_tags) for i in pad_tags]
#Split train, test and validation set
tokens_, test_tokens, tags_, test_tags = train_test_split(pad_tokens, pad_tags, test_size=0.1, train_size=0.9, random_state=2020)
train_tokens, val_tokens, train_tags, val_tags = train_test_split(tokens_,tags_,test_size = 0.25,train_size =0.75, random_state=2020)
print(
'train_tokens length:', len(train_tokens),
'\ntrain_tokens length:', len(train_tokens),
'\ntest_tokens length:', len(test_tokens),
'\ntest_tags:', len(test_tags),
'\nval_tokens:', len(val_tokens),
'\nval_tags:', len(val_tags),
)
return train_tokens, val_tokens, test_tokens, train_tags, val_tags, test_tags
train_tokens, val_tokens, test_tokens, train_tags, val_tags, test_tags = get_pad_train_test_val(data_group, data)
train_tags = np.array(train_tags)
val_tags = np.array(val_tags)
test_tags = np.array(test_tags)
print('train_tags: ',train_tags.shape,'val_tags: ',val_tags.shape,'test_tags: ',test_tags.shape)
from sklearn.utils import class_weight
train_temp_tags = np.ravel(np.argmax(train_tags, axis=-1))
print(len(train_temp_tags))
class_weights = class_weight.compute_class_weight('balanced',
np.unique(train_temp_tags),
train_temp_tags)
class_weight_dict = dict(enumerate(class_weights))
print(class_weight_dict)
train_label = np.argmax(train_tags, axis=-1)
print(train_tokens.shape)
print(train_label.shape)
import numpy as np
import tensorflow
from tensorflow.keras.models import Sequential, Model
from tensorflow.keras.layers import LSTM, Embedding, Dense, TimeDistributed, Dropout, Bidirectional, Input
from tensorflow.keras.utils import plot_model
from numpy.random import seed
seed(1)
tensorflow.random.set_seed(2)
input_dim = len(list(set(data['Word'].to_list())))+1
output_dim = 64
input_length = max([len(s) for s in data_group['Word_idx'].tolist()])
n_tags = len(tag2idx)
def get_bilstm_lstm_model():
model = Sequential()
model.add(Embedding(input_dim=input_dim, output_dim=output_dim, input_length=input_length))
model.add(Bidirectional(LSTM(units=output_dim, return_sequences=True, dropout=0.2, recurrent_dropout=0.2), merge_mode = 'concat'))
model.add(LSTM(units=output_dim, return_sequences=True, dropout=0.5, recurrent_dropout=0.5))
model.add((Dense(n_tags, activation="softmax")))
model.compile(loss='sparse_categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
model.summary()
return model
maj_index = tag2idx['O']
''' Change it More for Better Fine-Tuning '''
class_weight_dict[maj_index] = 28.5277954105731576
print(class_weight_dict)
sample_weights = np.ones(shape=(len(train_label), train_label.shape[-1]))
for i in range(17):
sample_weights[train_label == i] = class_weight_dict.get(i)
print(sample_weights.shape)
nlp_model = get_bilstm_lstm_model()
plot_model(nlp_model)
his = nlp_model.fit(train_tokens, train_label , batch_size = 32, epochs=1, validation_split=0.2, sample_weight = sample_weights)
tf.keras.models.save_model(nlp_model, filepath = "./nlp_model.h5")
nlp_model = tf.keras.models.load_model('./nlp_model.h5')
y_test = np.argmax(test_tags, axis=-1)
print(test_tokens.shape,y_test.shape)
nlp_model.evaluate(test_tokens, y_test)
def predict(seed):
query = test_tokens[seed]
query_text = []
for i in query.tolist():
query_text.append(idx2token.get(i))
print('Query_Text: ',' '.join(query_text[:10]))
ans = y_test[seed]
ans_text = []
for i in ans.tolist():
ans_text.append(idx2tag.get(i))
print('Tag_Text: ',' '.join(ans_text[:10]))
query = query.reshape(1,-1)
pred = nlp_model.predict(query)
pred = np.ravel(np.argmax(pred, axis=-1))
print('Query: ',query.shape,'Prediction: ',pred.shape)
pred_list = []
for i in pred.tolist():
pred_list.append(idx2tag.get(i))
print('Prediction_Text: ',' '.join(pred_list[:10]))
print()
print('--- Better-Representation---')
print()
rep_qr = []
for q, r_tag in zip(query_text[:10], ans_text[:10]):
rep_qr.append(q)
rep_qr.append('['+r_tag+']')
print('Actual_NER: ',' '.join(rep_qr),'....')
print()
rep_qp = []
for q, r_tag in zip(query_text[:10], pred_list[:10]):
rep_qp.append(q)
rep_qp.append('['+r_tag+']')
print('--'*70)
print()
print('Predicted_NER: ',' '.join(rep_qp),'....')
seed = 1
predict(seed)
import spacy
from spacy import displacy
nlp = spacy.load('en_core_web_sm')
text = nlp('Rectangle movement. Did Elon Musk really stick his company in the middle of this dreck? Then,okay, things start to make more sense when you see one 550,000-square-foot rectangle painted anostentatious hue of “Unity of Body, Soul, and Mind” white. This is the main SpaceX building.It was only after going through the front doors of SpaceX that the grandeur of what this man haddone became apparent. Musk had built an honest-to-God rocket factory in the middle of Los Angeles.And this factory was not making one rocket at a time. No. It was making many rockets—from scratch.The factory was a giant, shared work area. Near the back were massive delivery bays that allowedfor the arrival of hunks of metal, which were transported to two-story-high welding machines. Overto one side were technicians in white coats making motherboards, radios, and other electronics. Otherpeople were in a special, airtight glass chamber, building the capsules that rockets would take to theSpace Station. Tattooed men in bandanas were blasting Van Halen and threading wires around rocketengines. There were completed bodies of rockets lined up one after the other ready to be placed ontrucks. Still more rockets, in another part of the building, awaited coats of white paint. It was difficultto take in the entire factory at once. There were hundreds of bodies in constant motion whirringaround a variety of bizarre machines')
displacy.render(text, style = 'ent', jupyter=True)