-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0803_mecab_train_10e_20batch_size.py
More file actions
227 lines (155 loc) · 7.08 KB
/
Copy path0803_mecab_train_10e_20batch_size.py
File metadata and controls
227 lines (155 loc) · 7.08 KB
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import re
import json
import os
import tqdm
from konlpy.tag import Okt
import tensorflow as tf
from tensorflow.keras.preprocessing.sequence import pad_sequences
from tensorflow.keras.preprocessing.text import Tokenizer
from tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint
from transformers import BertTokenizer, BertModel, TFBertModel
gpus = tf.config.experimental.list_physical_devices('GPU')
if gpus:
# 텐서플로가 첫 번째 GPU만 사용하도록 제한
try:
tf.config.experimental.set_visible_devices(gpus[0], 'GPU')
except RuntimeError as e:
# 프로그램 시작시에 접근 가능한 장치가 설정되어야만 합니다
print(e)
train=pd.read_csv('mecab0803_alltrain.csv')
test=pd.read_csv('mecab0803_alltest.csv')
sample_submission=pd.read_csv('../sample_submission.csv')
print(f'train.shape:{train.shape}')
print(f'test.shape:{test.shape}')
print(f'train label 개수: {train.label.nunique()}')
os.environ["CUDA_DEVICE_ORDER"]="PCI_BUS_ID"
os.environ["CUDA_VISIBLE_DEVICES"]="1"
train=train[['data','label']]
test=test[['data']]
#3. 모델링
#random seed 고정
tf.random.set_seed(1234) #매번 랜덤성을 유지시켜주는 randomSeed
np.random.seed(1234)
BATCH_SIZE = 32
NUM_EPOCHS = 3
VALID_SPLIT = 0.2
MAX_LEN=400
# from transformers import *
tokenizer=BertTokenizer.from_pretrained('bert-base-multilingual-cased', cache_dir='bert_ckpt', do_lower_case=False)
#tokenizer=BertTokenizer.from_pretrained('bert-base-multilingual-cased', do_lower_case=False)
def bert_tokenizer(sent, MAX_LEN):
encoded_dict=tokenizer.encode_plus(
text = sent,
add_special_tokens=True,
max_length=MAX_LEN,
pad_to_max_length=True,
return_attention_mask=True,
truncation = True)
input_id=encoded_dict['input_ids']
attention_mask=encoded_dict['attention_mask']
token_type_id = encoded_dict['token_type_ids']
return input_id, attention_mask, token_type_id
input_ids =[]
attention_masks =[]
token_type_ids =[]
train_data_labels = []
def clean_text(sent):
sent_clean=re.sub("[^가-힣ㄱ-하-ㅣ]", " ", sent)
return sent_clean
###
for train_sent, train_label in zip(train['data'], train['label']):
try:
input_id, attention_mask, token_type_id = bert_tokenizer(clean_text(train_sent), MAX_LEN=MAX_LEN)
input_ids.append(input_id)
attention_masks.append(attention_mask)
token_type_ids.append(token_type_id)
#########################################
train_data_labels.append(train_label)
except Exception as e:
print(e)
print(train_sent)
pass
train_input_ids=np.array(input_ids, dtype=int)
train_attention_masks=np.array(attention_masks, dtype=int)
train_token_type_ids=np.array(token_type_ids, dtype=int)
###########################################################
train_inputs=(train_input_ids, train_attention_masks, train_token_type_ids)
train_labels=np.asarray(train_data_labels, dtype=np.int32)
print(train_input_ids[1])
print(train_attention_masks[1])
print(train_token_type_ids[1])
print(tokenizer.decode(train_input_ids[1]))
class TFBertClassifier(tf.keras.Model):
def __init__(self, model_name, dir_path, num_class):
super(TFBertClassifier, self).__init__()
self.bert = TFBertModel.from_pretrained(model_name, cache_dir=dir_path)
self.dropout = tf.keras.layers.Dropout(self.bert.config.hidden_dropout_prob)
self.classifier = tf.keras.layers.Dense(num_class,
kernel_initializer=tf.keras.initializers.TruncatedNormal(self.bert.config.initializer_range),
name="classifier")
def call(self, inputs, attention_mask=None, token_type_ids=None, training=False):
#outputs 값: # sequence_output, pooled_output, (hidden_states), (attentions)
outputs = self.bert(inputs, attention_mask=attention_mask, token_type_ids=token_type_ids)
pooled_output = outputs[1]
pooled_output = self.dropout(pooled_output, training=training)
logits = self.classifier(pooled_output)
return logits
cls_model = TFBertClassifier(model_name='bert-base-multilingual-cased',
dir_path='bert_ckpt',
num_class=46)
# 학습 준비하기
optimizer = tf.keras.optimizers.Adam(3e-5)
'''loss = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True)'''
loss = loss = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True)
metric = tf.keras.metrics.SparseCategoricalAccuracy('accuracy')
cls_model.compile(optimizer=optimizer, loss=loss, metrics=[metric])
model_name = "tf2_bert_classifier"
# overfitting을 막기 위한 ealrystop 추가
earlystop_callback = EarlyStopping(monitor='val_accuracy', min_delta=0.0001, patience=5)
# min_delta: the threshold that triggers the termination (acc should at least improve 0.0001)
# patience: no improvment epochs (patience = 1, 1번 이상 상승이 없으면 종료)\
checkpoint_path = os.path.join(model_name, 'weights.h5')
checkpoint_dir = os.path.dirname(checkpoint_path)
# Create path if exists
if os.path.exists(checkpoint_dir):
print("{} -- Folder already exists \n".format(checkpoint_dir))
else:
os.makedirs(checkpoint_dir, exist_ok=True)
print("{} -- Folder create complete \n".format(checkpoint_dir))
cp_callback = ModelCheckpoint(
checkpoint_path, monitor='val_accuracy', verbose=1, save_best_only=True, save_weights_only=True)
# 학습과 eval 시작
history = cls_model.fit(train_inputs, train_labels, epochs=1, batch_size=8,
validation_split = VALID_SPLIT, callbacks=[earlystop_callback, cp_callback])
input_ids =[]
attention_masks =[]
token_type_ids =[]
train_data_labels = []
def clean_text(sent):
sent_clean=re.sub("[^가-힣ㄱ-하-ㅣ]", " ", sent)
return sent_clean
for test_sent in test['data']:
try:
input_id, attention_mask, token_type_id = bert_tokenizer(clean_text(test_sent), MAX_LEN=40)
input_ids.append(input_id)
attention_masks.append(attention_mask)
token_type_ids.append(token_type_id)
#########################################
except Exception as e:
print(e)
print(test_sent)
pass
test_input_ids=np.array(input_ids, dtype=int)
test_attention_masks=np.array(attention_masks, dtype=int)
test_token_type_ids=np.array(token_type_ids, dtype=int)
###########################################################
test_inputs=(test_input_ids, test_attention_masks, test_token_type_ids)
results = cls_model.predict(test_inputs)
results=tf.argmax(results, axis=1)
results = results.numpy()
results = np.array(results)
sample_submission['label']=results
sample_submission.to_csv('bert_baseline0803.csv', index=False)