[Tensorflow] 3. Natural Language Processing in TensorFlow (3 week Sequence models) : programming (3)

gunny·2024년 4월 14일
0

[Tensorflow] 3. Natural Language Processing in TensorFlow (3 week Sequence models) : programming (3)

Using Convolutional Neural Networks (컨볼루셔널 신경망 사용)

  • 여기서는 텍스트 분류 모델을 구축하는 또 다른 방법인 컨볼루션 레이어를 사용해본다.
    이미지를 다루는 코스 2에서도 다뤘지만 컨볼루션은 입력에 필터를 적용하여 특징을 추출한다. 여기서 컨볼루션이 텍스트 데이터에서 어떻게 활용되는지 확인해본다.

[1] Download and prepare the dataset

import tensorflow_datasets as tfds

# Download the subword encoded pretokenized dataset
dataset, info = tfds.load('imdb_reviews/subwords8k', with_info=True, as_supervised=True)

# Get the tokenizer
tokenizer = info.features['text'].encoder
BUFFER_SIZE = 10000
BATCH_SIZE = 256

# Get the train and test splits
train_data, test_data = dataset['train'], dataset['test'], 

# Shuffle the training data
train_dataset = train_data.shuffle(BUFFER_SIZE)

# Batch and pad the datasets to the maximum length of the sequences
train_dataset = train_dataset.padded_batch(BATCH_SIZE)
test_dataset = test_data.padded_batch(BATCH_SIZE)

[2] Build the Model

  • 코스 2에서 이미지 분류 모델을 만들 때, 2D 컨볼루션 레이어를 사용했었다. 텍스트 시퀀스와 같은 시간 데이터의 경우 대신 Conv1D를 사용하므로 컨볼루션이 단일 차원에서 발생한다.
  • 또한 컨볼루션 레이어의 출력을 줄이기 위해 풀링 레이어를 추가하는데, 여기서는 GlobalMaxPooling1D를 사용하여 시간 차원 전체에서 최대값을 얻는다. 평균 풀링을 사용할 수도 있다.

먼저, Conv1D와 GlobalMaxPooling1D 레이어에 들어간 후의 형태가 어떻게 변했는지 아래를 참고한다.

import tensorflow as tf
import numpy as np

random_input = np.random.rand(1, 20, 20)
print(random_input.shape)

conv1d = tf.keras.layers.Conv1D(filters=128, kernel_size=5, activation='relu')
result = conv1d(random_input)
print(result.shape)

gmp = tf.keras.layers.GlobalMaxPooling1D()
result = gmp(result)
print(result.shape)

# output
(1, 20, 20)
(1, 16, 128)
(1, 128)

아래는 새로운 모델 아키텍처이다.

model = tf.keras.models.Sequential([
    tf.keras.layers.Embedding(tokenizer.vocab_size, 64),
    tf.keras.layers.Conv1D(filters=5, kernel_size=5, activation='relu'),
    tf.keras.layers.GlobalMaxPooling1D(),
    tf.keras.layers.Dense(64, activation='relu'),
    tf.keras.layers.Dense(1, activation='sigmoid')
])

model.compile(loss='binary_crossentorpy',
              optimizer='adam',
              metrics=['accuracy'])

model.summary()

[3] Train the model

history = model.fit(train_dataset,
                    epochs=10,
                    validation_data = test_dataset)

학습 과정 플로팅 해보기

import matplotlib.pyplot as plt

# Plot utility
def plot_graphs(history, string):
  plt.plot(history.history[string])
  plt.plot(history.history['val_'+string])
  plt.xlabel("Epochs")
  plt.ylabel(string)
  plt.legend([string, 'val_'+string])
  plt.show()

# Plot the accuracy and results 
plot_graphs(history, "accuracy")
plot_graphs(history, "loss")

여기서는 텍스트 분류에 사용할 수 있는 또 다른 모델 아키텍처인 컨볼루션 레이어를 살펴봤다.

profile
꿈꾸는 것도 개발처럼 깊게

0개의 댓글

관련 채용 정보