-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtutorial39_TextClassificationWithRnn.py
126 lines (99 loc) · 4.54 KB
/
tutorial39_TextClassificationWithRnn.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
# https://www.tensorflow.org/tutorials/text/text_classification_rnn
import tensorflow_datasets as tfds
import tensorflow as tf
import matplotlib.pyplot as plt
# 그래프를 플롯하는 helper 함수
def plot_graphs(history, metric):
plt.plot(history.history[metric])
plt.plot(history.history['val_' + metric], '')
plt.xlabel("Epochs")
plt.ylabel(metric)
plt.legend([metric, 'val_' + metric])
plt.show()
# 입력 파이프라인 설정
dataset, info = tfds.load('imdb_reviews/subwords8k', with_info = True, as_supervised = True)
train_dataset, test_dataset = dataset['train'], dataset['test']
encoder = info.features['text'].encoder
print('Vocabulary size: {}'.format(encoder.vocab_size))
sample_string = 'Hello Tensorflow.'
encoded_string = encoder.encode(sample_string)
print('Encoded string is {}'.format(encoded_string))
original_string = encoder.decode(encoded_string)
print('The original string: "{}"'.format(original_string))
assert original_string == sample_string
for index in encoded_string:
print('{} ---> {}'.format(index, encoder.decode([index])))
# 훈련을 위한 데이터 준비
BUFFER_SIZE = 10000
BATCH_SIZE = 64
train_dataset = train_dataset.shuffle(BUFFER_SIZE)
train_dataset = train_dataset.padded_batch(BATCH_SIZE)
test_dataset = test_dataset.padded_batch(BATCH_SIZE)
# 모델 만들기
model = tf.keras.Sequential([
tf.keras.layers.Embedding(encoder.vocab_size, 64),
tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(64)),
tf.keras.layers.Dense(64, activation = 'relu'),
tf.keras.layers.Dense(1)
])
model.compile(loss = tf.keras.losses.BinaryCrossentropy(from_logits = True),
optimizer = tf.keras.optimizers.Adam(1e-4), metrics = ['accuracy'])
# 모델 훈련하기
history = model.fit(train_dataset, epochs = 10, validation_data = test_dataset,
validation_steps = 30)
test_loss, test_acc = model.evaluate(test_dataset)
print("Test Loss: {}".format(test_loss))
print("Test Accuracy: {}".format(test_acc))
def pad_to_size(vec, size):
zeros = [0] * (size - len(vec))
vec.extend(zeros)
return vec
def sample_predict(sample_pred_text, pad):
encoded_sample_pred_text = encoder.encode(sample_pred_text)
if pad:
encoded_sample_pred_text = pad_to_size(encoded_sample_pred_text, 64)
encoded_sample_pred_text = tf.cast(encoded_sample_pred_text, tf.float32)
predictions = model.predict(tf.expand_dims(encoded_sample_pred_text, 0))
return (predictions)
# predict on a sample text without padding.
sample_pred_text = ('The movie was cool. The animation and the graphics were out of this world.'
'I would recommend this movie.')
predictions = sample_predict(sample_pred_text, pad = False)
print(predictions)
# predict on a sample text with padding.
sample_pred_text = ('The movie was cool. The animation and the graphics were out of this world.'
'I would recommend this movie.')
predictions = sample_predict(sample_pred_text, pad = True)
print(predictions)
plot_graphs(history, 'accuracy')
plot_graphs(history, 'loss')
# 두 개 이상의 LSTM 레이어 쌓기
# 두 가지 사용 가능한 모드 : 각 타임스텝에 대한 전체 연속 출력 스퀀스 반환,
# 각 입력 시퀀스에 대한 마지막 출력만 반환
model = tf.keras.Sequential([
tf.keras.layers.Embedding(encoder.vocab_size, 64),
tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(64, return_sequences = True)),
tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(32)),
tf.keras.layers.Dense(64, activation = 'relu'),
tf.keras.layers.Dropout(0.5),
tf.keras.layers.Dense(1)
])
model.compile(loss = tf.keras.losses.BinaryCrossentropy(from_logits = True),
optimizer = tf.keras.optimizers.Adam(1e-4), metrics = ['accuracy'])
history = model.fit(train_dataset, epochs = 10, validation_data = test_dataset,
validation_steps = 30)
test_loss, test_acc = model.evaluate(test_dataset)
print("Test Loss: {}".format(test_loss))
print("Test Accuracy: {}".format(test_acc))
# predict on a sample text without padding.
sample_pred_text = ('The movie was cool. The animation and the graphics were out of this world.'
'I would recommend this movie.')
predictions = sample_predict(sample_pred_text, pad = False)
print(predictions)
# predict on a sample text with padding.
sample_pred_text = ('The movie was cool. The animation and the graphics were out of this world.'
'I would recommend this movie.')
predictions = sample_predict(sample_pred_text, pad = True)
print(predictions)
plot_graphs(history, 'accuracy')
plot_graphs(history, 'loss')