Potato
์•ˆ๋…•ํ•˜์„ธ์š”, ๊ฐ์žก๋‹ˆ๋‹ค?๐Ÿฅ” ^___^ ๐Ÿ˜บ github ๋ฐ”๋กœ๊ฐ€๊ธฐ ๐Ÿ‘‰๐Ÿป

AI study/๋”ฅ๋Ÿฌ๋‹ ํ”„๋ ˆ์ž„์›Œํฌ

[tensorflow] ํ…์ŠคํŠธ ๋ถ„๋ฅ˜ (Text Classification) - IMDB

๊ฐ์ž ๐Ÿฅ” 2021. 3. 2. 18:02
๋ฐ˜์‘ํ˜•

TF ํ™ˆํŽ˜์ด์ง€

์‹œ์ž‘ํ•˜๋ฉฐ

TF certification ์„ ์ทจ๋“ํ•˜๊ธฐ ์œ„ํ•ด ํŠœํ† ๋ฆฌ์–ผ๋ถ€ํ„ฐ ๊ณต๋ถ€์ค‘์ด๋‹ค. ์˜ˆ์ƒ๋ณด๋‹ค ์˜ค๋žœ ์‹œ๊ฐ„์ด ๊ฑธ๋ฆด ๊ฒƒ๊ฐ™๋‹ค ..
๊ทธ๋ž˜๋„ ์ตœ๋Œ€ํ•œ ๋‹จ๊ธฐ๊ฐ„์— ์ทจ๋“ํ•˜๋Š”๊ฒƒ์„ ๋ชฉํ‘œ๋กœ ํ•œ๋‹ค!!!!!! ํ™”์ดํŒ…

๋ณธ ํฌ์ŠคํŒ…์—์„œ๋Š” ํ…์ŠคํŠธ ๋ถ„๋ฅ˜๋ฅผ ์œ„ํ•ด ๋‰ด๋Ÿด ๋„คํŠธ์›Œํฌ ํ›ˆ๋ จ์‹œํ‚ค๋Š” ์˜ˆ์ œ๋ฅผ ์‚ดํŽด๋ณธ๋‹ค.

ํ…์„œํ”Œ๋กœ์šฐ ๊ณต์‹ ๋ฌธ์„œ ํŠœํ† ๋ฆฌ์–ผ์„ ์ฐธ๊ณ ํ•˜์—ฌ ๋ฆฌ๋ทฐํ•˜๋Š” ํ˜•์‹์˜ ๊ธ€์ด ๋˜๊ฒ ๋‹ค!
www.tensorflow.org/tutorials/keras/text_classification

 

์˜ํ™” ๋ฆฌ๋ทฐ๋ฅผ ์‚ฌ์šฉํ•œ ํ…์ŠคํŠธ ๋ถ„๋ฅ˜  |  TensorFlow Core

Note: ์ด ๋ฌธ์„œ๋Š” ํ…์„œํ”Œ๋กœ ์ปค๋ฎค๋‹ˆํ‹ฐ์—์„œ ๋ฒˆ์—ญํ–ˆ์Šต๋‹ˆ๋‹ค. ์ปค๋ฎค๋‹ˆํ‹ฐ ๋ฒˆ์—ญ ํ™œ๋™์˜ ํŠน์„ฑ์ƒ ์ •ํ™•ํ•œ ๋ฒˆ์—ญ๊ณผ ์ตœ์‹  ๋‚ด์šฉ์„ ๋ฐ˜์˜ํ•˜๊ธฐ ์œ„ํ•ด ๋…ธ๋ ฅํ•จ์—๋„ ๋ถˆ๊ตฌํ•˜๊ณ  ๊ณต์‹ ์˜๋ฌธ ๋ฌธ์„œ์˜ ๋‚ด์šฉ๊ณผ ์ผ์น˜ํ•˜์ง€ ์•Š์„ ์ˆ˜

www.tensorflow.org

 

Library Import

ํ•„์š”ํ•œ lirary๋ฅผ ๋ถˆ๋Ÿฌ์˜จ๋‹ค

# library import
import tensorflow as tf
from tensorflow import keras
import numpy as np

 

๋ฐ์ดํ„ฐ์…‹ ๋‹ค์šด๋กœ๋“œ

ํ•ด๋‹น ํ”„๋กœ์ ํŠธ์—์„œ๋Š” keras์— ๋‚ด์žฅ๋œ IMDB ๋ฐ์ดํ„ฐ์…‹์„ ์‚ฌ์šฉํ•œ๋‹ค.
IMDB ๋ฐ์ดํ„ฐ๋Š” 5๋งŒ๊ฐœ์˜ ์˜ํ™” ๋ฆฌ๋ทฐ ๋ฐ์ดํ„ฐ๋กœ, ๋‹จ์–ด๋กœ ๊ตฌ์„ฑ๋˜์–ด ์žˆ๋‹ค.
๋‹จ์–ด๊ฐ€ ๊ธ์ •์ธ์ง€ (1) , ๋ถ€์ •์ธ์ง€ (0) ์˜ˆ์ธกํ•˜๋Š” ํ”„๋กœ์ ํŠธ์ด๋‹ค.

train data ์™€ test data , train labels ์™€ test labels๋กœ ๋ถˆ๋Ÿฌ์˜จ๋‹ค. 
(๋ณธ ๋ฐ์ดํ„ฐ์…‹์€ 25000๊ฐœ์˜ train data, 25000๊ฐœ์˜ test data ๋กœ ๊ธ์ •, ๋ถ€์ •์ธ ๋ฆฌ๋ทฐ์˜ ์ˆ˜๊ฐ€ ๋™์ผํ•˜๊ฒŒ ๊ท ํ˜•์ด ๋งž์ถฐ์ ธ ์žˆ๋‹ค.)

์—ฌ๊ธฐ์„œ๋Š” labels(๊ธ์ •/๋ถ€์ •์„ ๋‚˜ํƒ€๋‚ด๋Š” ๋ผ๋ฒจ)๊ฐ€ y๊ฐ’ (ํƒ€๊ฒŸ)์ด ๋œ๋‹ค.

imdb = keras.datasets.imdb
(train_data, train_labels), (test_data, test_labels) = imdb.load_data(num_words=10000)
  • ๋งค๊ฐœ๋ณ€์ˆ˜์„ค๋ช…
    • num_words 
      • ์ˆœ์œ„๋ฅผ ์˜๋ฏธํ•œ๋‹ค. ์ฆ‰, ๋‹จ์–ด ์ถœ์—ฐ ๋นˆ๋„์ˆ˜๊ฐ€ 1~10000์œ„์ธ ๋‹จ์–ด๋“ค๋งŒ ๊ฐ€์ ธ์˜ค๊ฒ ๋‹ค๋Š” ๋œป์ด๋‹ค.
      • ์ƒ์œ„ ๋ช‡๊ฐœ์˜ ๋‹จ์–ด๋ฅผ ๋ถˆ๋Ÿฌ์˜ฌ ๊ฒƒ์ธ์ง€์— ๋Œ€ํ•œ ๋งค๊ฐœ๋ณ€์ˆ˜์ด๋‹ค.
      • ๋ฐ์ดํ„ฐ์˜ ํฌ๊ธฐ๋ฅผ ์ ๋‹นํ•˜๊ฒŒ ์œ ์ง€ํ•˜๊ธฐ ์œ„ํ•ด์„œ ์‚ฌ์šฉ๋œ๋‹ค.
  • load_data์—์„œ pickle ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค๋ฉด, ์•„๋ž˜ ํฌ์ŠคํŒ… ํ™•์ธ
  • didu-story.tistory.com/24
 

[tensorflow] imdb.load_data ์˜ค๋ฅ˜ ํ•ด๊ฒฐ - Object arrays cannot be loaded when allow_pickle=False

didu-story.tistory.com/25?category=903386 [tensorflow] ํ…์ŠคํŠธ ๋ถ„๋ฅ˜ (Text Classification) - IMDB ์‹œ์ž‘ํ•˜๋ฉฐ TF certification ์„ ์ทจ๋“ํ•˜๊ธฐ ์œ„ํ•ด ํŠœํ† ๋ฆฌ์–ผ๋ถ€ํ„ฐ ๊ณต๋ถ€์ค‘์ด๋‹ค. ์˜ˆ์ƒ๋ณด๋‹ค ์˜ค๋žœ ์‹œ๊ฐ„์ด ๊ฑธ๋ฆด ๊ฒƒ๊ฐ™๋‹ค ...

didu-story.tistory.com

๋ฐ์ดํ„ฐ ํƒ์ƒ‰

1. train data์™€ test data์˜ ๊ฐœ์ˆ˜ ํ™•์ธ

print("ํ›ˆ๋ จ ์ƒ˜ํ”Œ: {}, ๋ ˆ์ด๋ธ” : {}".format(len(train_data), len(train_labels)))

# ์ถœ๋ ฅ ๊ฒฐ๊ณผ - ํ›ˆ๋ จ ์ƒ˜ํ”Œ: 25000, ๋ ˆ์ด๋ธ” : 25000

 

2. train data ์˜ ํ˜•ํƒœ ์‚ดํŽด๋ณด๊ธฐ

print(train_data[0])

# ์ถœ๋ ฅ ๊ฒฐ๊ณผ
# [1, 14, 22, 16, 43, 530, 973, 1622, 1385, 65, 458, 4468, 66, 3941, 4, 173, 36, 256, 5, 25, 100, 43, 838, 112, 50, 670, 2, 9, 35, 480, 284, 5, 150, 4, 172, 112, 167, 2, 336, 385, 39, 4, 172, 4536, 1111, 17, 546, 38, 13, 447, 4, 192, 50, 16, 6, 147, 2025, 19, 14, 22, 4, 1920, 4613, 469, 4, 22, 71, 87, 12, 16, 43, 530, 38, 76, 15, 13, 1247, 4, 22, 17, 515, 17, 12, 16, 626, 18, 2, 5, 62, 386, 12, 8, 316, 8, 106, 5, 4, 2223, 5244, 16, 480, 66, 3785, 33, 4, 130, 12, 16, 38, 619, 5, 25, 124, 51, 36, 135, 48, 25, 1415, 33, 6, 22, 12, 215, 28, 77, 52, 5, 14, 407, 16, 82, 2, 8, 4, 107, 117, 5952, 15, 256, 4, 2, 7, 3766, 5, 723, 36, 71, 43, 530, 476, 26, 400, 317, 46, 7, 4, 2, 1029, 13, 104, 88, 4, 381, 15, 297, 98, 32, 2071, 56, 26, 141, 6, 194, 7486, 18, 4, 226, 22, 21, 134, 476, 26, 480, 5, 144, 30, 5535, 18, 51, 36, 28, 224, 92, 25, 104, 4, 226, 65, 16, 38, 1334, 88, 12, 16, 283, 5, 16, 4472, 113, 103, 32, 15, 16, 5345, 19, 178, 32]
  • ํŠน์ • ๋‹จ์–ด๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ์ •์ˆ˜๋กœ ๋ณ€ํ™˜๋˜์–ด ๋‚˜ํƒ€๋‚˜์ ธ ์žˆ๋‹ค.

3. ๊ฐ ๋ฐ์ดํ„ฐ์˜ ๊ธธ์ด ํ™•์ธ

print(len(train_data[0]), len(train_data[1]))

# ์ถœ๋ ฅ๊ฒฐ๊ณผ - 218 189
  • ์ฒซ๋ฒˆ์งธ ๋ฆฌ๋ทฐ์˜ ๊ธธ์ด๋Š” 218, ๋‘๋ฒˆ์งธ ๋ฆฌ๋ทฐ์˜ ๊ธธ์ด๋Š” 189 ์ž„์„ ์•Œ ์ˆ˜ ์žˆ๋‹ค.
  • ์‹ ๊ฒฝ๋ง์€ ๋ชจ๋“  input์˜ ๊ธธ์ด๊ฐ€ ๊ฐ™์•„์•ผํ•จ์„ ๋ช…์‹œํ•˜์ž.
  • ์ฆ‰, ์ถ”ํ›„ ํ•ด๋‹น ๋ฐ์ดํ„ฐ๋“ค์˜ ๋ชจ๋“  ๊ธธ์ด๋ฅผ ๋™์ผํ•˜๊ฒŒ ๋งŒ๋“ค์–ด์ฃผ๋Š” ์ž‘์—…์ด ํ•„์š”ํ•˜๋‹ค.

์ •์ˆ˜๋ฅผ ๋‹จ์–ด๋กœ ๋ณ€ํ™˜ํ•ด๋ณด์ž.

๋ฐ์ดํ„ฐ ํƒ์ƒ‰ ๊ณผ์ •์—์„œ ๋ณด์•˜๋“ฏ, ํ•ด๋‹น ๋ฐ์ดํ„ฐ์…‹์€ ๋‹จ์–ด๋“ค์ด ์ •์ˆ˜์— ๋งคํ•‘๋˜์–ด ์žˆ๋‹ค.
ํ…์ŠคํŠธ ๋ถ„์„์„ ์œ„ํ•ด ํ•ด๋‹น ์ •์ˆ˜๋“ค์„ ๋งคํ•‘๋œ ๋‹จ์–ด๋กœ ์ฒ˜๋ฆฌํ•  ๊ฒƒ์ด๋‹ค.

1. get word index ๋กœ ๋”•์…”๋„ˆ๋ฆฌ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ

# ๋‹จ์–ด์™€ ์ •์ˆ˜ ์ธ๋ฑ์Šค๋ฅผ ๋งคํ•‘ํ•œ ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ๋ถˆ๋Ÿฌ์˜ด
word_index = imdb.get_word_index()
print(word_index)
  • get_word_index() ๋ฅผ ํ™œ์šฉํ•˜๋ฉด ์ •์ˆ˜๋ฅผ word๋กœ ์–ป์„ ์ˆ˜ ์žˆ๋‹ค.
  • ์•„๋ž˜์™€ ๊ฐ™์€ ๋”•์…”๋„ˆ๋ฆฌ๊ฐ€ word_index์— ์ €์žฅ๋˜๋Š” ๊ฒƒ์ด๋‹ค.
  • word_index['fawn'] ์„ ์กฐํšŒํ•˜๋ฉด 34710 ์ด ์ถœ๋ ฅ๋œ๋‹ค.

2. ๋”•์…”๋„ˆ๋ฆฌ์— 4๊ฐœ์˜ ๋‹จ์–ด๋ฅผ ์ถ”๊ฐ€ํ•ด์ค„ ๊ฒƒ์ด๋‹ค. ์ด ๋‹จ์–ด๋“ค์€ 0,1,2,3 index๋ฅผ ๋ถ€์—ฌํ•œ๋‹ค.
๋•Œ๋ฌธ์— ์ธ๋ฑ์Šค๋ฅผ +3 ๋ฏธ๋ค„์ฃผ๊ณ , ์ง์ ‘ 0,1,2,3 ์— ์ง€์ •ํ•  ๋‹จ์–ด๋ฅผ ์ •ํ•ด์ค€๋‹ค.

word_index = {k:(v+3) for k,v in word_index.items()}

word_index["<PAD>"] = 0
word_index["<START>"] = 1
word_index["<UNK>"] = 2  # unknown
word_index["<UNUSED>"] = 3
  • word_index๋ฅผ ์ถœ๋ ฅํ•ด๋ณด๋ฉด fawn์˜ ์ธ๋ฑ์Šค๊ฐ€ 3 ๋ฐ€๋ฆฐ ๊ฒƒ์„ ํ™•์ธ ๊ฐ€๋Šฅ

3. ๋”•์…”๋„ˆ๋ฆฌ์˜ key ์™€ value๊ฐ’์„ list๋กœ ๋ฐ˜ํ™˜์‹œํ‚ค๊ณ , ๋’ค์ง‘์–ด์ค€๋‹ค.
๊ทธ๋ž˜์•ผ ์ •์ˆ˜๋ฅด ๋ถˆ๋ €์„๋•Œ, ๋‹จ์–ด๊ฐ€ ์ถœ๋ ฅ๋˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

reverse_word_index = dict([(value, key) for (key, value) in word_index.items()])

# ์ถœ๋ ฅ ๊ฒฐ๊ณผ- {25184: 'bettina', 70227: "'cannes'", 87565: 'karel', 30490: 'heorot', 4112: 'karen', 34683: "1992's", 34586: 'snorer', [์ƒ๋žต] }

4. ๋งˆ์ง€๋ง‰์œผ๋กœ ์ˆซ์ž๋ฅผ ๋ฌธ์ž๋กœ ๋ฐ”๊พธ๋Š” decode ํ—ฌํผ ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•œ๋‹ค.

def decode_review(text):
    return ' '.join([reverse_word_index.get(i, '?') for i in text])

5. ์ œ๋Œ€๋กœ decode ๋˜๋Š”์ง€ ํ™•์ธํ•ด๋ณด์ž.

print(decode_review(train_data[0]))

์›๋ž˜ train_data[0]์˜ ํ˜•ํƒœ๋Š” ์•„๋ž˜์ฒ˜๋Ÿผ ์ •์ˆ˜๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ๋‹ค.

[1, 14, 22, 16, 43, 530, 973, 1622, 1385, 65, 458, 4468, 66, 3941, 4, 173, 36, 256, 5, 25, 100, 43, 838, 112, 50, 670, 2, 9, 35, 480, 284, 5, 150, 4, 172, 112, 167, 2, 336, 385, 39, 4, 172, 4536, 1111, 17, 546, 38, 13, 447, 4, 192, 50, 16, 6, 147, 2025, 19, 14, 22, 4, 1920, 4613, 469, 4, 22, 71, 87, 12, 16, 43, 530, 38, 76, 15, 13, 1247, 4, 22, 17, 515, 17, 12, 16, 626, 18, 2, 5, 62, 386, 12, 8, 316, 8, 106, 5, 4, 2223, 5244, 16, 480, 66, 3785, 33, 4, 130, 12, 16, 38, 619, 5, 25, 124, 51, 36, 135, 48, 25, 1415, 33, 6, 22, 12, 215, 28, 77, 52, 5, 14, 407, 16, 82, 2, 8, 4, 107, 117, 5952, 15, 256, 4, 2, 7, 3766, 5, 723, 36, 71, 43, 530, 476, 26, 400, 317, 46, 7, 4, 2, 1029, 13, 104, 88, 4, 381, 15, 297, 98, 32, 2071, 56, 26, 141, 6, 194, 7486, 18, 4, 226, 22, 21, 134, 476, 26, 480, 5, 144, 30, 5535, 18, 51, 36, 28, 224, 92, 25, 104, 4, 226, 65, 16, 38, 1334, 88, 12, 16, 283, 5, 16, 4472, 113, 103, 32, 15, 16, 5345, 19, 178, 32]

decode๋ฅผ ์ ์šฉํ•œ ํ›„์˜ ๋ชจ์Šต์„ ์‚ดํŽด๋ณด๋ฉด

<START> this film was just brilliant casting location scenery story direction everyone's really suited the part they played and you could just imagine being there robert <UNK> is an amazing actor and now the same being director <UNK> father came from the same scottish island as myself so i loved the fact there was a real connection with this film the witty remarks throughout the film were great it was just brilliant so much that i bought the film as soon as it was released for <UNK> and would recommend it to everyone to watch and the fly fishing was amazing really cried at the end it was so sad and you know what they say if you cry at a film it must have been good and this definitely was also <UNK> to the two little boy's that played the <UNK> of norman and paul they were just brilliant children are often left out of the <UNK> list i think because the stars that play them all grown up are such a big profile for the whole film but these children are amazing and should be praised for what they have done don't you think the whole story was so lovely because it was true and was someone's life after all that was shared with us all

์ด๋ ‡๊ฒŒ ๋ณ€ํ•œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. ๋‚ด๊ฐ€ ์ง€์ •ํ•ด์ค€ ์ธ๋ฑ์Šค 1 ๋„ <START> ๋กœ ์ž˜ ๋ณ€ํ™˜ ๋˜์—ˆ๋‹ค.

ํ›ˆ๋ จ์„ ์œ„ํ•œ ๋ฐ์ดํ„ฐ ์ค€๋น„

'์ •์ˆ˜ ๋ฐฐ์—ด'์€ ์‹ ๊ฒฝ๋ง์— ์ฃผ์ž…ํ•˜๊ธฐ์ „์— ํ…์„œํ”Œ๋กœ์šฐ๊ฐ€ ์•Œ์•„๋จน์„ ์ˆ˜ ์žˆ๋Š” ํ˜•ํƒœ๋กœ ๋ณ€ํ™˜ํ•ด ์ฃผ์–ด์•ผ ํ•œ๋‹ค.
๋ณ€ํ™˜ ๋ฐฉ๋ฒ•์€ ๋‘ ๊ฐ€์ง€๊ฐ€ ์žˆ๋‹ค.

1. one-hot encoding ๋ฐฉ์‹

  • ๋งŒ์•ฝ data ๊ฐ€ [3,5] ์ด๋ฉด ์ธ๋ฑ์Šค 3,5 ์—๋งŒ 1์ด ๋“ค์–ด๊ฐ€๊ณ  ๋‚˜๋จธ์ง€ ์ธ๋ฑ์Šค์—๋Š” ์ „๋ถ€ 0์œผ๋กœ ์ฑ„์›Œ์ง„๋‹ค.
  • num_words * num_reviews ํฌ๊ธฐ์˜ ํ–‰๋ ฌ์ด ํ•„์š”ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ๋„ˆ๋ฌด ํฌ๋‹ค๋Š” ๊ฒƒ์ด ๋‹จ์ ์ด๋‹ค.
  • ์ด ๋ฐฉ์‹์„ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด, ์‹ค์ˆ˜ ๋ฒกํ„ฐ๋ฅผ ๋‹ค๋ฃฐ ์ˆ˜ ์žˆ๋Š” ์ธต์ธ Dense์ธต์„ ํ™œ์šฉํ•˜์—ฌ ์‹ ๊ฒฝ๋ง์„ ๊ตฌ์„ฑํ•ด์•ผํ•œ๋‹ค.

2. padding ํ•˜๋Š” ๋ฐฉ์‹

  • ์ •์ˆ˜ ๋ฐฐ์—ด์˜ ๊ธธ์ด๊ฐ€ ๋ชจ๋‘ ๊ฐ™๋„๋ก padding ์‹œ์ผœ์ฃผ๋Š” ๊ฒƒ
  • max_length * num_reviews ํฌ๊ธฐ์˜ ํ–‰๋ ฌ์ด ํ•„์š”ํ•˜๋‹ค
  • ์ด ๋ฐฉ์‹์„ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด, ์‹ ๊ฒฝ๋ง์˜ ์ฒซ๋ฒˆ์งธ ์ธต์œผ๋กœ embading์ธต์„ ํ™œ์šฉํ•ด์•ผ ํ•œ๋‹ค.

๋ณธ ํ”„๋กœ์ ํŠธ๋Š” padding๋ฐฉ์‹์„ ์‚ฌ์šฉํ–ˆ๋‹ค.

train_data = keras.preprocessing.sequence.pad_sequences(train_data,
                                                        value=word_index['<PAD>'],
                                                        padding="post",
                                                        maxlen=256)
test_data = keras.preprocessing.sequence.pad_sequences(test_data,
                                                       value=word_index['<PAD>'],
                                                       padding='post',
                                                       maxlen=256)
  • ๋งค๊ฐœ๋ณ€์ˆ˜ 
    • value : ํŒจ๋”ฉ๋œ(๋Š˜์–ด๋‚œ) ์ž๋ฆฌ์— ๋“ค์–ด๊ฐˆ ๋‹จ์–ด๋ฅผ ์ง€์ •ํ•ด์ค€๋‹ค. 
      • ์•ž์˜ dic์— ๋”ฐ๋ฅด๋ฉด, <PAD>์˜ ์ธ๋ฑ์Šค๋Š” 0 ์ด๋‹ค.
    • padding : ํŒจ๋”ฉ์„ ์–ด๋Š ๋ฐฉํ–ฅ์— ์„ค์ •ํ•  ๊ฒƒ์ธ์ง€์— ๋Œ€ํ•œ ๋งค๊ฐœ๋ณ€์ˆ˜์ด๋‹ค. 
      • 'post' ๋Š” ์‹œํ€€์Šค์˜ ๋’ท์ชฝ์— ํŒจ๋”ฉ์„ ํ˜•์„ฑํ•˜๊ณ , 'pre'๋Š” ์•ž์ชฝ์— ํ˜•์„ฑํ•œ๋‹ค๋Š” ๋œป์ด๋‹ค.
        • ๋ณธ ํ”„๋กœ์ ํŠธ์—์„œ๋Š” post ๋ฐฉ์‹์„ ์„ ํƒํ–ˆ์œผ๋ฏ€๋กœ, [3,5,0,0,0,0,0,0,0,0,0] ์ด๋ ‡๊ฒŒ ๋’ท์ชฝ์— ํŒจ๋”ฉ์ด ํ˜•์„ฑ๋  ๊ฒƒ์ด๋‹ค.
print(train_data[0])
# ์ถœ๋ ฅ ๊ฒฐ๊ณผ - [ 1 14 22 16 43 530 973 1622 1385 65 458 4468 66 3941 4 173 36 256 5 25 100 43 838 112 50 670 2 9 35 480 284 5 150 4 172 112 167 2 336 385 39 4 172 4536 1111 17 546 38 13 447 4 192 50 16 6 147 2025 19 14 22 4 1920 4613 469 4 22 71 87 12 16 43 530 38 76 15 13 1247 4 22 17 515 17 12 16 626 18 2 5 62 386 12 8 316 8 106 5 4 2223 5244 16 480 66 3785 33 4 130 12 16 38 619 5 25 124 51 36 135 48 25 1415 33 6 22 12 215 28 77 52 5 14 407 16 82 2 8 4 107 117 5952 15 256 4 2 7 3766 5 723 36 71 43 530 476 26 400 317 46 7 4 2 1029 13 104 88 4 381 15 297 98 32 2071 56 26 141 6 194 7486 18 4 226 22 21 134 476 26 480 5 144 30 5535 18 51 36 28 224 92 25 104 4 226 65 16 38 1334 88 12 16 283 5 16 4472 113 103 32 15 16 5345 19 178 32 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]

print(len(train_data[0]))
# ์ถœ๋ ฅ ๊ฒฐ๊ณผ - 256

 

๋ชจ๋ธ ๊ตฌ์„ฑ

์ž…๋ ฅ๊ณผ ์ถœ๋ ฅ ์‚ฌ์ด์— ๋‘ ๊ฐœ์˜ ์€๋‹‰์ธต์„ ๋งŒ๋“ค์–ด์„œ ๋ชจ๋ธ์„ ๋งŒ๋“ค์–ด ๋ณด์ž.

vocab_size=10000

model = keras.Sequential()
model.add(keras.layers.Embedding(vocab_size, 16, input_shape=(None,)))
model.add(keras.layers.GlobalAvgPool1D()) # layer 1 output (25000, 16)
model.add(keras.layers.Dense(16, activation='relu')) # layer 2 output (25000, 16)
model.add(keras.layers.Dense(1, activation='sigmoid')) # layer 3 output (25000, 1)

model.summary()

ํ•œ ์ค„์”ฉ ์„ค๋ช…ํ•ด๋ณด๊ฒ ๋‹ค.

  • vocab_size = 10000
    • input์˜ ์ฒซ๋ฒˆ์งธ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ input ํฌ๊ธฐ๋ฅผ ์ ์–ด์ฃผ๊ธฐ ์œ„ํ•ด ๋ณ€์ˆ˜๋กœ ์ง€์ •ํ–ˆ๋‹ค.
    • ์ฒ˜์Œ num_words ๋ฅผ 10000๊ฐœ๋กœ ์ง€์ •ํ–ˆ๋˜๊ฒƒ์„ ๊ธฐ์–ตํ•˜์ž.
  • model.add(keras.layers.Embedding(vocab_size, 16, input_shape=(None,)))
    • layer 0 ๋ฒˆ 
    • ํ•ด๋‹น ์ธต์„ ๊ฑฐ์น˜๊ณ  ๋‚˜์„œ์˜ ์ถœ๋ ฅ๋˜๋Š” train_data์˜ shape์€  (25000, 256, 16) ์ด๋‹ค.
    • 25000์€ train_data์˜ ๊ฐœ์ˆ˜ / 256์€ padding ํ•œ ์ตœ๋Œ€ ๊ธธ์ด / 16์€ ์ƒ์„ฑ๋˜๋Š” embading์˜ ํฌ๊ธฐ
    • ์ฆ‰, 25000๊ฐœ์˜ ๊ฐ ๋ฆฌ๋ทฐ๋Š” 256๊ฐœ์˜ ๋‹จ์–ด๋กœ ๊ตฌ์„ฑ๋˜๊ณ , ๊ฐ ๋‹จ์–ด๋Š” 16๊ฐœ์˜ ์›์†Œ๋กœ ๊ตฌ์„ฑ๋œ๋‹ค.
      • ํ•˜๋‚˜์˜ ๋ฆฌ๋ทฐ ์†์— 16์ฐจ์›์งœ๋ฆฌ 256๊ฐœ๊ฐ€ ์กด์žฌํ•œ๋‹ค.
  • model.add(keras.layers.GlobalAvgPool1D())
    • layer 1์˜ output : (25000, 16)
    • GlobalAvgPool1D() ๋ ˆ์ด์–ด๋Š” ๊ณ ์ •๋œ ํฌ๊ธฐ (16)์˜ ์ถœ๋ ฅ ๋ฒกํ„ฐ๋ฅผ ๋ฆฌํ„ดํ•ด์ฃผ๋Š” ํ•จ์ˆ˜
    • (25000, 256, 16) --> 256์˜ ํ‰๊ท ์„ ๊ตฌํ•˜์—ฌ (25000, 16)์œผ๋กœ ๋งŒ๋“ค์–ด์ค€๋‹ค.
  • model.add(keras.layers.Dense(16, activation='relu'))
    • layer 2์˜ output : (25000, 16)
    • ๋‰ด๋Ÿฐ์ด 16๊ฐœ์ธ fully-connected layer (Dense)์— ์ž…๋ ฅ๋ฒกํ„ฐ๋ฅผ ํ†ต๊ณผใ…ฃํ‚ค๊ณ , ์ถœ๋ ฅ์ฐจ์›์€ 16์œผ๋กœ ์ง€์ •
    • ํ™œ์„ฑํ™”ํ•จ์ˆ˜ relu ์‚ฌ์šฉ
  • model.add(keras.layers.Dense(1, activation='sigmoid'))
    • layer 3์˜ output : (25000, 1)
    • ๋งˆ์ง€๋ง‰ ์ธต์œผ๋กœ, ํ•˜๋‚˜์˜ ๋…ธ๋“œ๋กœ ๊ตฌ์„ฑ๋œ fully-connected layer(Dense)์— ์—ฐ๊ฒฐ
    • Binary (์ด์ง„๋ถ„๋ฅ˜) ์ด๋ฏ€๋กœ, ๋งˆ์ง€๋ง‰ ํ™œ์„ฑํ™” ํ•จ์ˆ˜๋ฅผ sigmoid ํ•จ์ˆ˜๋กœ ์ง€์ •
      • sigmoid : ์ถœ๋ ฅ๊ฐ’์ด 0~1 ์‚ฌ์ด์ด๋ฉฐ, ํ™•๋ฅ ๋กœ ์ถœ๋ ฅํ•˜๊ณ  ์‹ถ์„ ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค.

 

์†์‹คํ•จ์ˆ˜ ๋ฐ ์˜ตํ‹ฐ๋งˆ์ด์ € ์„ค์ •

- ๋ชจ๋ธ์„ ํ•™์Šต์‹œํ‚ค๋ ค๋ฉด ์†์‹คํ•จ์ˆ˜ ๋ฐ ์˜ตํ‹ฐ๋งˆ์ด์ € ํ•„์š”
- ์˜ตํ‹ฐ๋งˆ์ด์ €๋Š” Adam ์‚ฌ์šฉ
- ์†์‹คํ•จ์ˆ˜๋กœ Binary_crossentropy (ํ™•๋ฅ  ๋ถ„ํฌ๊ฐ„์˜ ๊ฑฐ๋ฆฌ๋ฅผ ์ธก์ •ํ•˜๋Š” ์†์‹คํ•จ์ˆ˜
----- ์ผ๋ฐ˜์ ์œผ๋กœ binary_crossentropy๊ฐ€ ํ™•๋ฅ ์„ ๋‹ค๋ฃจ๋Š”๋ฐ ์ ํ•ฉ
----- ์—ฌ๊ธฐ์„œ๋Š” ์˜ˆ์ธก๋œ ๋ถ„ํฌ์™€ ์ •๋‹ต (= ๋ผ๋ฒจ) ๋ถ„ํฌ๊ฐ„์˜ ๊ฑฐ๋ฆฌ ์ธก์ •
- ๋ชจ๋ธ ์„ฑ๋Šฅ ํ‰๊ฐ€ : Accuracy ์‚ฌ์šฉ

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

 

๊ฒ€์ฆ ๋ฐ์ดํ„ฐ ์ƒ์„ฑ 

ํ›ˆ๋ จ๋ฐ์ดํ„ฐ๋ฅผ ๋‚˜๋ˆ„์–ด์„œ ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ๋ฅผ ์ƒ์„ฑํ•ด์ค€๋‹ค. 
๋ณธ ํ”„๋กœ์ ํŠธ์—์„œ๋Š” 10000๊ฐœ๋ฅผ ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ๋กœ ์‚ฌ์šฉํ•ด์ค„ ์˜ˆ์ •์ด๋‹ค.

x_val = train_data[:10000]
partial_x_train = train_data[10000:]

y_val = train_labels[:10000]
partial_y_train = train_labels[10000:]

 

๋ชจ๋ธ ํ›ˆ๋ จ

- train ๋ฐ์ดํ„ฐ๋กœ ๋ชจ๋ธ ํ›ˆ๋ จ์„ ์ง„ํ–‰
- 40๋ฒˆ์˜ ์—ํฌํฌ
- 512๊ฐœ์˜ ์ƒ˜ํ”Œ๋กœ ์ด๋ฃจ์–ด์ง„ ๋ฏธ๋‹ˆ ๋ฐฐ์น˜์—์„œ
------ ์ฆ‰, x,y train ํ…์„œ์— ์žˆ๋Š” ๋ชจ๋“  ์ƒ˜ํ”Œ์— ๋Œ€ํ•ด 40๋ฒˆ ๋ฐ˜๋ณตํ•ด์„œ ํ›ˆ๋ จํ•œ๋‹ค๋Š” ๋œป
- ํ›ˆ๋ จํ•˜๋Š” ๋™์•ˆ 10000๊ฐœ์˜ ๊ฒ€์ฆ๋ฐ์ดํ„ฐ๋กœ ๋ชจ๋ธ์˜ ์†์‹ค๊ณผ ์ •ํ™•๋„๋ฅผ ๋ชจ๋‹ˆํ„ฐ๋ง

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_data, test_labels, verbose=2)
print(results)

# ์ถœ๋ ฅ ๊ฒฐ๊ณผ -  loss: 0.3361 - accuracy: 0.8702

์ •ํ™•๋„ 0.87์ •๋„ ๋‚˜์™”๋‹ค. ๋” ๊ณ ์„ฑ๋Šฅ์˜ ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•˜๋ฉด 95๊ฐ€ ๋‚˜์˜จ๋‹ค๊ณ ํ•œ๋‹ค. 
๊ทธ๊ฒƒ์€ ์ถ”ํ›„ ๊ณต๋ถ€ํ•˜๊ณ  ํฌ์ŠคํŒ…ํ•ด๋ณด๊ฒ ๋‹ค.

์˜ˆ์ธกํ•˜๊ธฐ

# ์˜ˆ์ธก๋œ 10๊ฐœ์˜ ํ™•๋ฅ ๊ฐ’ ์ถœ๋ ฅํ•ด๋ณด๊ธฐ
predictions = model.predict(test_data)
print(predictions[0:10].T)

[[0.06341892 0.9973872  0.5436513  0.7175522  0.9909915  0.93047166
  0.928525   0.01527587 0.9466015  0.99780303]]

์ฒ˜์Œ 10๊ฐœ์˜ test_data๋Š” ์œ„์™€ ๊ฐ™์€ ํ™•๋ฅ ๋กœ ์˜ˆ์ธก ๋์Œ์„ ์•Œ ์ˆ˜ ์žˆ๊ณ ,
labels ๋กœ ์ถœ๋ ฅํ•ด๋ณด๊ณ  ์‹ถ์œผ๋ฉด ์•„๋ž˜ ์ฝ”๋“œ๋กœ ํ™•์ธ ๊ฐ€๋Šฅํ•˜๋‹ค.

predict_class=model.predict_classes(test_data)
print(predict_class[0:10].T)

[[0 1 1 1 1 1 1 0 1 1]]

์ด๋ ‡๊ฒŒ ์˜ˆ์ธก๋๋‹ค!

 

๋งˆ์น˜๋ฉฐ

TF ์ž๊ฒฉ์ฆ์„ ๋”ฐ๊ธฐ ์œ„ํ•ด ๊ณต๋ถ€์ค‘์ธ๋ฐ, ์ƒ๊ฐ๋ณด๋‹ค ์˜ค๋ž˜๊ฑธ๋ฆฐ๋‹ค.... ๋ฌธ์ œ๋งŒ ์ฃผ์–ด์ง€๊ณ  ์ธํ„ฐ๋„ท ์—†์ด ๋ชจ๋ธ์„ ๊ตฌ์ถ•ํ•ด์•ผํ•˜๋‹ˆ ๋ชจ๋“  ๊ฒƒ์„ ์™ธ์›Œ์•ผ ํ•œ๋‹ค๋Š” ๋ถ€๋‹ด๊ฐ์ด ์•ž์„ ๋‹ค... ๊ทธ๋ž˜์„œ ๋” ์˜ค๋ž˜๊ฑธ๋ฆฌ๋Š” ๋“ฏ ์‹ถ๋‹คใ… ใ…  ํ•˜๋‚˜ํ•˜๋‚˜ ์ž์„ธํ•˜๊ฒŒ ์งš๊ณ  ๋„˜์–ด๊ฐ€์•ผํ•˜๊ธฐ ๋•Œ๋ฌธ์—. ์•ž์œผ๋กœ ๋” ์—ด์‹ฌํžˆ ํ•ด์„œ ์–ผ๋ฅธ ๋”ฐ์•ผ๊ฒ ๋‹ค!!!!!

๋ฐ˜์‘ํ˜•