Colab ReviewRadar
์๋
ํ์ธ์?
AI์ ๊ด์ฌ์ด ๋ง์์, ์ด๋ฒ์ ์ฒ์์ผ๋ก ์ํ ๋ฆฌ๋ทฐ ๊ฐ์ ๋ถ์ ํ๋ก์ ํธ์ ๋์ ํด๋ดค์ต๋๋ค.
์ฌ์ค ์ธ๊ณต์ง๋ฅ์ด๋ผ๋ ๊ฒ ๋ง์ฐํ๊ฒ๋ง ๋๊ปด์ก๋๋ฐ,
๊ตฌ๊ธ ์ฝ๋ฉ์์ ์ง์ ๋ฐ์ดํฐ๋ฅผ ๋ค๋ฃจ๊ณ ๋ชจ๋ธ์ ๋ง๋ค์ด๋ณด๋๊น
์๊ฐ๋ณด๋ค ์ฌ๋ฏธ์๊ณ ์ ๊ธฐํ ๊ฒฝํ์ด์์ด์.
์ด ๊ธ์์๋ TensorFlow์์ ์ ๊ณตํ๋ IMDB ์ํ ๋ฆฌ๋ทฐ ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ง๊ณ
AI๊ฐ ์ด๋ป๊ฒ ์ฌ๋์ ๊ฐ์ ์ ์ดํดํ๋์ง ํ๋์ฉ ๋ฐ๋ผ๊ฐ๋ดค๋ ๊ณผ์ ์ ์ ๋ฆฌํด๋ณด๋ ค๊ณ ํฉ๋๋ค.
์ ์ฒ๋ผ AI์ ์
๋ฌธํ์๋ ๋ถ๋ค๊ป ์กฐ๊ธ์ด๋๋ง ๋์์ด ๋์์ผ๋ฉด ์ข๊ฒ ์ต๋๋ค.
IMDB ๋ฐ์ดํฐ?
IMDB ๋ฐ์ดํฐ์
์ ์คํ ํฌ๋ ๋ํ์์ ๊ณต๊ฐํ ์ํ ๋ฆฌ๋ทฐ ๋ฐ์ดํฐ๋ก,
๊ฐ ๋ฆฌ๋ทฐ์ ๊ธ์ (1) ๋๋ ๋ถ์ (0) ๋ ์ด๋ธ์ด ๋ถ์ด ์์ต๋๋ค.
์์ฐ์ด ์ฒ๋ฆฌ ๋ถ์ผ์์ ๊ฐ์ ๋ถ์ ์ค์ต์ฉ์ผ๋ก ์์ฃผ ์ฌ์ฉ๋๋ ๋ํ์ ์ธ ๋ฐ์ดํฐ์
์
๋๋ค.
๊ฐ๋ฐ ํ๊ฒฝ ๋ฐ ๋ผ์ด๋ธ๋ฌ๋ฆฌ
Google Colab
Python 3.x
TensorFlow, TensorFlow Hub, TensorFlow Datasets
matplotlib, numpy
* TensorFlow Datasets์ด๋ tfds์ Full Name์ผ๋ก ๋จธ์ ๋ฌ๋, ๋ฅ๋ฌ๋์์ ์์ฃผ ์ฐ์ด๋ ๊ณต๊ฐ ๋ฐ์ดํฐ์
๋ค์
์ฝ๊ฒ ๋ถ๋ฌ์์ ๋ฐ๋ก ์ธ ์ ์๊ฒ ํด์ฃผ๋ ํ์ด์ฌ ๋ผ์ด๋ธ๋ฌ๋ฆฌ์
๋๋ค.
Code
import numpy as np
import tensorflow as tf
import tensorflow_hub as hub
import tensorflow_datasets as tfds
import matplotlib.pyplot as plt
๋ฐ์ดํฐ ๋ค๋ฃจ๋ ๋ฐ ์์ฃผ ์ฐ๋ numpy,
๋ฅ๋ฌ๋ ๋ชจ๋ธ ๋ง๋ค ๋ ํ์์ธ tensorflow,
์ฌ์ ํ์ต๋ ์๋ฒ ๋ฉ ๋ชจ๋ธ ์ธ ๋ ํ์ํ tensorflow_hub,
์ ๋ช
ํ ๋ฐ์ดํฐ์
์ ์ฝ๊ฒ ๋ถ๋ฌ์ค๋ tensorflow_datasets(tfds),
๊ทธ๋ฆฌ๊ณ ๊ทธ๋ํ ๊ทธ๋ฆด ๋ ์ฐ๋ matplotlib๊น์ง ๋ถ๋ฌ์์ด์.
print("Version: ", tf.__version__)
print("Eager mode: ", tf.executing_eagerly())
print("Hub version: ", hub.__version__)
print("GPU is", "available" if tf.config.list_physical_devices('GPU') else "NOT AVAILABLE")
๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ค์ ๋ฒ์ ์ ํ์ธํด์ฃผ๋ ์ฝ๋์ ๋๋ค.
* ๋ฒ์ ์ด ๋ง์ง ์์ผ๋ฉด ์ค๋ฅ๊ฐ ๋น๋ฒํฉ๋๋ค. ํ๋ฃจ ์ฌ์ด์๋ ๋ฒ์ ์ด ๋ฐ๋๋ ์ฌ๋ฌ ๋ฒ์ ์ผ๋ก ํ ์คํธ ํด๋ณด์๊ธฐ ๋ฐ๋๋๋ค.
train_data, test_data = tfds.load(
name="imdb_reviews",
split=["train", "test"],
batch_size=-1,
as_supervised=True
)
tfds.load๋ฅผ ํตํด IMDB ์ํ ๋ฆฌ๋ทฐ ๋ฐ์ดํฐ์
์ ๋ค์ด๋ก๋ ๋ฐ ๋ก๋ํฉ๋๋ค.
split ์ต์
์ผ๋ก ํ์ต(train)๊ณผ ํ
์คํธ(test) ๋ฐ์ดํฐ๋ฅผ ๋ถ๋ฆฌํฉ๋๋ค.
batch_size=-1์ ์ ์ฒด ๋ฐ์ดํฐ๋ฅผ ํ ๋ฒ์ ๋ฉ๋ชจ๋ฆฌ๋ก ๋ถ๋ฌ์ต๋๋ค(๋ฐ์ดํฐ๊ฐ ํฌ์ง ์์ ๋ ์ ์ฉ).
as_supervised=True๋ก (์
๋ ฅ, ๋ ์ด๋ธ) ์์ ํํ๋ก ๋ฐ์ดํฐ๋ฅผ ๋ฐํํฉ๋๋ค.
train_examples, train_labels = tfds.as_numpy(train_data)
test_examples, test_labels = tfds.as_numpy(test_data)
TensorFlow Dataset ๊ฐ์ฒด๋ฅผ numpy ๋ฐฐ์ด๋ก ๋ณํํฉ๋๋ค.
IMDB ๋ฐ์ดํฐ์ ์ ๋ฆฌ๋ทฐ์ ๋ ์ด๋ธ๋ก ๋๋์ด์ ธ์์ต๋๋ค. ๋๋ฌธ์ ํ๋ จ๋ฐ์ดํฐ์ ํ ์คํธ๋ฐ์ดํฐ๋ฅผ ์์ ๊ฐ์ ๋ฐฉ์์ผ๋ก ๋๋์ด์ค๋๋ค.
model_url = "https://tfhub.dev/google/nnlm-en-dim50/2"
hub_layer = hub.KerasLayer(model_url, input_shape=[], dtype=tf.string, trainable=True)
TensorFlow Hub์์ ์ ๊ณตํ๋ ์ฌ์ ํ์ต๋ ์์ด ์๋ฒ ๋ฉ ๋ชจ๋ธ(`nnlm-en-dim50/2`)์ KerasLayer๋ก ๋ถ๋ฌ์ต๋๋ค.
์ด ๋ ์ด์ด๋ ํ
์คํธ ๋ฐ์ดํฐ๋ฅผ ๊ณ ์ ๋ ์ฐจ์์ ์๋ฒ ๋ฉ ๋ฒกํฐ๋ก ๋ณํํ์ฌ, ๋ฅ๋ฌ๋ ๋ชจ๋ธ์ ์
๋ ฅ์ผ๋ก ์ฌ์ฉํ ์ ์๋๋ก ํฉ๋๋ค.
*์๋ฒ ๋ฉ ๋ฒกํฐ๋ n์ฐจ์ ๊ณต๊ฐ์ ์๊ฐํ์๋ฉด ๋ฉ๋๋ค. ๋น์ ํ ๋ฐ์ดํฐ๋ฅผ ์ซ์๋ก ๋ฐ๊พผ ๊ณต๊ฐ์ ๊ฐ๋ ์ ๋๋ค.
model = tf.keras.Sequential()
model.add(hub_layer)
model.add(tf.keras.layers.Dense(16, activation='relu'))
model.add(tf.keras.layers.Dense(1))
๋ชจ๋ธ ์ํคํ ์ณ๋ฅผ ์ค๊ณํฉ๋๋ค.
16๊ฐ์ ๋ด๋ฐ์ ๊ฐ์ง ์๋์ธต๊ณผ ์ถ๋ ฅ์ธต์ ์ถ๊ฐํด์ฃผ์์ต๋๋ค.
model.compile(
optimizer='adam',
loss=tf.losses.BinaryCrossentropy(from_logits=True),
metrics=['accuracy']
)
๋ชจ๋ธ์ ์ปดํ์ผํฉ๋๋ค.
Adam ์ตํฐ๋ง์ด์ ๋ ํ์ต ํจ์จ์ฑ๊ณผ ์์ ์ฑ์ ๋์ฌ์ฃผ๋ ๋ํ์ ์ธ ์ต์ ํ ์๊ณ ๋ฆฌ์ฆ์
๋๋ค.
BinaryCrossentropy ์์ค ํจ์๋ ์ด์ง ๋ถ๋ฅ ๋ฌธ์ ์์ ํ์ค์ ์ผ๋ก ์ฌ์ฉ๋ฉ๋๋ค.
metrics=‘accuracy’๋ก ํ์ต ๋ฐ ํ๊ฐ ์ ์ ํ๋๋ฅผ ํจ๊ป ๋ชจ๋ํฐ๋งํฉ๋๋ค
x_val = train_examples[:10000]
partial_x_train = train_examples[10000:]
y_val = train_labels[:10000]
partial_y_train = train_labels[10000:]
ํ์ต๋ฐ์ดํฐ์ ์ผ๋ถ๋ฅผ ๋ถ๋ฆฌํด์ค๋๋ค. ํ ์คํธ ๋ฐ์ดํฐ๋ ํ ๋ฒ๋ง ์จ ์ ํ๋๋ฅผ ์๋ฒฝํ๊ฒ ์ฒดํฌํ๊ณ ์ ํ๊ธฐ ์ํจ์ ๋๋ค.
history = model.fit(
partial_x_train,
partial_y_train,
epochs=40,
batch_size=512,
validation_data=(x_val, y_val),
verbose=1
)
๋ชจ๋ธ์ ํ์ต์ํต๋๋ค.
์ํญ์ ํ๋ ์ด๋ผ๊ณ ์๊ฐํ์๋ฉด ๋ฉ๋๋ค. 40ํ๋ ์ ์ํจ๊ฒ๋๋ค.
acc = history.history['accuracy']
val_acc = history.history['val_accuracy']
epochs = range(1, len(acc) + 1)
plt.plot(epochs, acc, 'bo', label='Training acc')
plt.plot(epochs, val_acc, 'b', label='Validation acc')
plt.title('Training and validation accuracy')
plt.xlabel('Epochs')
plt.ylabel('Accuracy')
plt.legend()
plt.show()
๊ทธ๋ํ๋ฅผ ๋ง๋ค์ด์ค๋๋ค.
๊ทธ๋ผ ์ ์ฌ์ง๊ณผ ๊ฐ์ด ์ถ๋ ฅ์ด ๋ฉ๋๋ค.
์ด ํ๋ฅผ ๋ณด์๋ฉด 20 ์ํญ ํ๋ก๋ ๊ฒ์ฆ ์ ํ๋๊ฐ ๋ฏธ์ธํ๊ฒ ์ค์ด๋๋ ๊ฑธ ๋ณผ ์ ์์ต๋๋ค.
๊ทธ๋ผ ์ํญ์ ์ค์ด์ ์ ์ฌ์ฉํ๋ฉด๋ฉ๋๋ค.
๋์ ์๋ ๊ธ์ด์ง๋ง ์ฝ์ด์ฃผ์ ์ ๊ฐ์ฌํฉ๋๋ค.
ํ ์ฝ๋๋ ์๋์ ์ฒจ๋ถํ๊ฒ ์ต๋๋ค.
import numpy as np
import tensorflow as tf
import tensorflow_hub as hub
import tensorflow_datasets as tfds
import matplotlib.pyplot as plt
print("Version: ", tf.__version__)
print("Eager mode: ", tf.executing_eagerly())
print("Hub version: ", hub.__version__)
print("GPU is", "available" if tf.config.list_physical_devices('GPU') else "NOT AVAILABLE")
train_data, test_data = tfds.load(name="imdb_reviews", split=["train", "test"],
batch_size=-1, as_supervised=True)
train_examples, train_labels = tfds.as_numpy(train_data)
test_examples, test_labels = tfds.as_numpy(test_data)
print("Training entries: {}, test entries: {}".format(len(train_examples), len(test_examples)))
model = "https://tfhub.dev/google/nnlm-en-dim50/2"
hub_layer = hub.KerasLayer(model, input_shape=[], dtype=tf.string, trainable=True)
hub_layer(train_examples[:3])
model = tf.keras.Sequential()
model.add(hub_layer)
model.add(tf.keras.layers.Dense(16, activation='relu'))
model.add(tf.keras.layers.Dense(1))
model.summary()
model.compile(optimizer='adam',
loss=tf.losses.BinaryCrossentropy(from_logits=True),
metrics=[tf.metrics.BinaryAccuracy(threshold=0.0, name='accuracy')])
x_val = train_examples[:10000]
partial_x_train = train_examples[10000:]
y_val = train_labels[:10000]
partial_y_train = train_labels[10000:]
history = model.fit(partial_x_train,
partial_y_train,
epochs=40,
batch_size=512,
validation_data=(x_val, y_val),
verbose=1)
results = model.evaluate(test_examples, test_labels)
print(results)
model.save('imdb_model')
history_dict = history.history
history_dict.keys()
# history.history๋ ๋์
๋๋ฆฌ๋ก, ๊ฐ metric์ ๊ฐ์ด ๋ฆฌ์คํธ๋ก ์ ์ฅ๋์ด ์์ต๋๋ค.
acc = history.history['accuracy'] # ๋๋ 'binary_accuracy' (metrics ์ด๋ฆ์ ๋ฐ๋ผ ๋ค๋ฆ)
val_acc = history.history['val_accuracy'] # ๋๋ 'val_binary_accuracy'
epochs = range(1, len(acc) + 1)
plt.clf() # clear figure
plt.plot(epochs, acc, 'bo', label='Training acc')
plt.plot(epochs, val_acc, 'b', label='Validation acc')
plt.title('Training and validation accuracy')
plt.xlabel('Epochs')
plt.ylabel('Accuracy')
plt.legend()
plt.show()