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

gunny·2024년 4월 14일
0

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

Training a Sarcasm Detection Model using a Conveolution Layer(컨볼루션 레이어를 사용하여 풍자 감지 모델 학습)

  • 여기서는 Sarcasm data를 활용해서 풍자 감지 모델을 학습할 때 컨볼루션 레이어를 사용한다. 여러가지 매개변수를 조정하고 결과에 어떤 영향을 미치는지 살펴본다.

[1] Download the dataset

import requests

url = 'https://storage.googleapis.com/tensorflow-1-public/course3/sarcasm.json'
filename = 'sarcasm.json'

response = requests.get(url)

with open(filename, 'wb') as f:
    f.write(response.content)
    
import json

with open('./sarcasm.json', 'r') as f:
    dataset = json.load(f)
    
sentences = []
labels = []

for item in dataset:
    sentences.append(item['headline'])
    labels.append(item['is_sarcastic'])

[2] Split the dataset

training_size = 20000

training_sentences = sentences[0:training_size]
testing_sentences = sentences[training_size:]

training_labels = labels[0:training_size]
testing_labels = labels[training_size:]

[3] Data Preprocessing

import numpy as np
from tensorflow.keras.preprocessing.text import Tokenizer
from tensorflow.keras.preprocessing.sequence import pad_sequences

tokenizer = Tokenizer(num_words=10000, oov_token='<OOV>')

tokenizer.fit_on_texts(training_sentences)
word_index= tokenizer.word_index

train_sequences = tokenizer.texts_to_sequences(training_sentences)
padded_train = pad_sequences(train_sequences, padding= 'post', truncating='post', maxlen=120)

test_sequences = tokenizer.texts_to_sequences(testing_sentences)
padded_test = pad_sequences(test_sequences, padding= 'post', truncating='post', maxlen=120)

train_labels = np.array(training_labels)
test_labels = np.array(testing_labels)

[4] Build and Compile the model

import tensorflow as tf

model = tf.keras.models.Sequential([
    tf.keras.layers.Embedding(10000, 16, input_length=120),
    tf.keras.layers.Conv1D(filters=128, kernel_size=5, activation='relu'),
    tf.keras.layers.GlobalMaxPooling1D(),
    tf.keras.layers.Dense(6, activation='relu'),
    tf.keras.layers.Dense(1, activation='sigmoid')
])

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

model.summary()

[5] Train the model

history = model.fit(padded_train, train_labels,
          epochs=10,
          validation_data = (padded_test, test_labels))

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 loss history
plot_graphs(history, 'accuracy')
plot_graphs(history, 'loss')

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

0개의 댓글

관련 채용 정보