๐ ๋ชฉ์ฐจ
1. preview
2. Naïve Bayes ํ์ฉํ๊ธฐ
3. ํํ ์คํด 2
4. RNN ํ์ฉํ๊ธฐ
5. CNN ํ์ฉํ๊ธฐ
6. ์ฌ์ด๊ฐ๊ธฐ) Multi-Label Classification
๐ ๊ธ์ ๋ง์น๋ฉฐ...
1. Preview
Text Classification์ด๋, ํ ์คํธโ๋ฌธ์ฅโ๋ฌธ์๋ฅผ ์ ๋ ฅ์ผ๋ก ๋ฐ์ ์ฌ์ ์ ์ ์๋ ํด๋์ค์ค ์ด๋์ ์ํ๋์ง ๋ถ๋ฅ(classification)ํ๋ ๊ณผ์ ์ผ๋ก ์๋์ ๊ฐ์ด ์์ฉ๋ถ์ผ๊ฐ ๋ค์ํ๋ค.
๋ฌธ์ ํด๋์ค ์์ ๊ฐ์ฑ๋ถ์ (Sentiment Analysis) ๊ธ์ / ์ค๋ฆฝ / ๋ถ์ ์คํธ๋ฉ์ผ ํ์ง (Spam Detection) ์ ์ / ์คํธ ์ฌ์ฉ์ ์๋ ๋ถ๋ฅ (Intent Classification) ๋ช ๋ น / ์ง๋ฌธ / ์ก๋ด ๋ฑ ์ฃผ์ ๋ถ๋ฅ (Topic Classification) ๊ฐ ์ฃผ์ ์นดํ ๊ณ ๋ฆฌ ๋ถ๋ฅ (Category Classification) ๊ฐ ์นดํ ์ฝ๋ฆฌ
๋ฅ๋ฌ๋ ์ด์ ์๋ Naïve Bayes Classification, Support Vector Machine ๋ฑ ๋ค์ํ ๋ฐฉ๋ฒ์ผ๋ก Text Classification์ ์งํํ์๋ค.
์ด๋ฒ์๊ฐ์๋ ๋ฅ๋ฌ๋ ์ด์ ์ ๊ฐ์ฅ ๊ฐ๋จํ ๋ถ๋ฅ๋ฐฉ์์ธ Naïve Bayes๋ฐฉ์์ ๋น๋กฏ, ์ฌ๋ฌ ๋ฅ๋ฌ๋ ๋ฐฉ์์ ์์๋ณด์.
2. Naïve Bayes ํ์ฉํ๊ธฐ
Naïve Bayes๋ ์์ฃผ ๊ฐ๋ ฅํ("๊ฐ feature๋ independentํ๋ค!"๋ผ๋ ๊ฐ๋ ฅํ ๊ฐ์ ์ ๊ฐ์ง) ๋ถ๋ฅ๋ฐฉ์์ผ๋ก
์ฑ๋ฅ์ ์ค์ํ์ง๋ง ๋จ์ด๋ผ๋ ๋ถ์ฐ์์ ์ธ symbol์ ๋ค๋ฃจ๋ NLP์์๋ ์์ฌ์ด ๋ฉด์ด ์กด์ฌํ๋ค.
2.1 MAP (Maximum A Posterior)
โ๏ธBayes Theorem
์ด๋, ๋๋ถ๋ถ์ ๋ฌธ์ ์์ evidence, P(D)๋ ๊ตฌํ๊ธฐ ์ด๋ ต๊ธฐ์
P(c | D) ∝ P(D | c)โP(c) ์์ผ๋ก ์ ๊ทผํ๊ธฐ๋ ํ๋ค.
์์ ์ฑ์ง์ ์ด์ฉํ๋ฉด, ์ฃผ์ด์ง data D์ ๋ํด ํ๋ฅ ์ ์ต๋๋ก ํ๋ ํด๋์ค c๋ฅผ ๊ตฌํ ์ ์๋๋ฐ,
โ๏ธMAP
์ด์ฒ๋ผ ์ฌํํ๋ฅ ์ ์ต๋ํํ๋ ํด๋์ค c๋ฅผ ๊ตฌํ๋ ๊ฒ์ MAP(์ฌํํ๋ฅ ์ต๋ํ)๋ผ ํ๋ค.
โ๏ธMLE
์ด์ ๋ง์ฐฌ๊ฐ์ง๋ก ๊ฐ๋ฅ๋๋ฅผ ์ต๋ํํ๋ ํด๋์ค c๋ฅผ ๊ตฌํ๋ ๊ฒ์ MLE(์ต๋๊ฐ๋ฅ๋์ถ์ )์ด๋ผ ํ๋ค.
MLE๋ ์ฃผ์ด์ง data D์ label C์ ๋ํด ํ๋ฅ ๋ถํฌ๋ฅผ ๊ทผ์ฌํ๊ธฐ ์ํ
parameter θ๋ฅผ ํ๋ จํ๊ธฐ์ํ ๋ฐฉ๋ฒ์ผ๋ก ์ฌ์ฉ๋๋ค.
MLE. vs. MAP
MAP๊ฐ ๊ฒฝ์ฐ์ ๋ฐ๋ผ MLE๋ณด๋ค ๋ ์ ํํ ์ ์๋ค. (โต ์ฌ์ ํ๋ฅ ์ด ํฌํจ๋์ด์์ด์)
2.2 Naïve Bayes
Naïve Bayes๋ MAP๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ์๋ํ๋ค.
๊ฐ์ : ๊ฐ feature๋ independentํ๋ค! ๋ผ๋ ๊ฐ๋ ฅํ ๊ฐ์ ์ ๋ฐํ์ผ๋ก ์งํ๋๋ค.
๋๋ถ๋ถ์ ๊ฒฝ์ฐ, ์ฌํํ๋ฅ ์ ๊ตฌํ๊ธฐ ์ด๋ ต๊ธฐ์ ๊ฐ๋ฅ๋์ ์ฌ์ ํ๋ฅ ์ ๊ณฑ์ผ๋ก ํด๋์ค๋ฅผ ์์ธกํ๋ค.
๋ง์ฝ ๋ค์ํ ํน์ง์ผ๋ก ์ด๋ฃจ์ด์ง data์ ๊ฒฝ์ฐ, feature๊ฐ ํฌ๋ฐํ๊ธฐ์ ๊ฐ๋ฅ๋๋ฅผ ๊ตฌํ๊ธฐ ๋ํ ์ด๋ ต๋ค.
์ด๋, Naïve Bayes๊ฐ ๋งค์ฐ ๊ฐ๋ ฅํ ํ์ ๋ฐํํ๋๋ฐ, ๊ฐ ํน์ง์ด ๋ ๋ฆฝ์ ์ด๋ผ๋ ๊ฐ์ ์ ํตํด ์ฌ์ ํ๋ฅ ์ด ์ค์ data corpus์์ ์ถํํ ๋น๋๋ฅผ ํตํด ์ถ์ ์ด ๊ฐ๋ฅํด์ง๋ ๊ฒ์ด๋ค.
์ด์ฒ๋ผ ๊ฐ๋จํ ๊ฐ์ ์ผ๋ก ๋ฐ์ดํฐ์ ํฌ์์ฑ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๋ ์ฝ๊ณ ๊ฐ๋ ฅํ ๋ฐฉ๋ฒ์ผ๋ก MAP์ ์ ๋ตํด๋์ค๋ผ๋ฒจ์์ธก์ด ๊ฐ๋ฅํด์ง๋ ๊ฒ์ด๋ค.
์์ธ์์ ๋ฐ ์์ ์๋ 2.3์ ์ฐธ๊ณ
2.3 Sentiment Analysis ์์
์์ ๊ฐ์ด class์ data๊ฐ ๊ธ์ /๋ถ์ ๊ณผ document๋ก ์ฃผ์ด์ง ๋,
'I am happy to see this movie' ๋ผ๋ ๋ฌธ์ฅ์ด ์ฃผ์ด์ง๋ค๋ฉด, ์ด ๋ฌธ์ฅ์ด ๊ธ์ ์ธ์ง ๋ถ์ ์ธ์ง ํ๋จํด๋ณด์!
Naïve Bayes๋ฅผ ํ์ฉํด ๋จ์ด์ ์กฐํฉ์ ๋ํ ํ๋ฅ ์ ๊ฐ๊ฐ ๋ถํดํ ์ ์๋ค.
์ฆ, ๊ฐ ๋จ์ด์ ์ถํํ๋ฅ ์ ๋ ๋ฆฝ์ ์ด๋ผ ๊ฐ์ ํ, ๊ฒฐํฉ๊ฐ๋ฅ๋ํ๋ฅ ์ ๋ชจ๋ ๊ฐ๊ฐ์ ๊ฐ๋ฅ๋ํ๋ฅ ๋ก ๋ถํดํ๋ค.
๊ทธ๋ ๊ฒ ๋๋ฉด ๋ฐ์ดํฐ D์์์ ์ถํ๋น๋๋ฅผ ๊ตฌํ ์ ์๋ค.
์ด์ฒ๋ผ corpus์์ ๋จ์ํ ๊ฐ ๋จ์ด์ class๋น ์ถํ๋น๋๋ฅผ ๊ณ์ฐํ๋ ๊ฒ๋ง์ผ๋ก๋ ๊ฐ๋จํ sentiment analysis๊ฐ ๊ฐ๋ฅํ๋ค.
2.4 Add-One Smoothing
Naïve Bayes๊ฐ์ ์ ํตํด corpus์์ ์ถํํ๋ฅ ์ ๋ ๋ฆฝ์ผ๋ก ๋ง๋ค์ด ์ถํํ์๋ฅผ ์ ๊ทน์ ์ผ๋ก ํ์ฉํ ์ ์๊ฒ ๋์๋ค.
์ฌ๊ธฐ์ ๋ฌธ์ ์ ์ด ๋ฐ์ํ๋๋ฐ, ๋ง์ฝ Count(happy, neg)=0์ด๋ผ๋ฉด? P(happy | neg)=0์ด ๋์ด๋ฒ๋ฆฐ๋ค.
์๋ฌด๋ฆฌ data corpus์ ์กด์ฌํ์ง ์๋๋ผ๋ ๊ทธ๋ฐ ์ด์ ๋ก ํด๋น sample์ ์ถํํ๋ฅ ์ 0์ผ๋ก ์ถ์ ํด๋ฒ๋ฆฌ๋ ๊ฒ์ ๋งค์ฐ ์ํํ ์ผ์ด ๋์ด๋ฒ๋ฆฌ๊ธฐ์ ์๋์ฒ๋ผ ๋ถ์(์ถํํ์)์ 1์ ๋ํด์ฃผ๋ฉด ์ฝ๊ฒ ๋ฌธ์ ํด๊ฒฐ์ด ๊ฐ๋ฅํ๋ค.(๋ฌผ๋ก ์๋ฒฝํ ํด๊ฒฐ๋ฒ์ ์๋)
2.5 ์ฅ์ ๋ฐ ํ๊ณ
์ฅ์ : ๋จ์ํ ์ถํ๋น๋๋ฅผ ์ธ๋ ๊ฒ์ฒ๋ผ ์ฝ๊ณ ๊ฐ๋จํ์ง๋ง ๊ฐ๋ ฅ!!
๋ฅ๋ฌ๋์ ํ์ฉํ๊ธฐ์ label๋ ๋ฌธ์ฅ ์๊ฐ ๋งค์ฐ ์ ์ ๊ฒฝ์ฐ, ์คํ๋ ค ๋ณต์กํ ๋ฅ๋ฌ๋๋ฐฉ์๋ณด๋ค ๋ ๋์ ๋์์ด ๋ ์ ์๋ค.
ํ๊ณ: 'I am not happy'์ 'I am happy'์์ not์ ์ถ๊ฐ๋ก ๋ฌธ์ฅ์ ์ ๋ฐ๋๋ป์ด ๋๋ค.
์์์ผ๋ก๋ P(not, happy) ≠ P(not)โP(happy)๊ฐ ๋๋ค.
๋จ์ด๊ฐ ์์๋ก ์ธํด ์๊ธฐ๋ ์ ๋ณด๋ ๋ฌด์ํ ์ ์๋๋ฐ, "๊ฐ ํน์ง์ ์๋ก ๋ ๋ฆฝ์ ์ด๋ค."๋ผ๋ Naïve Bayes์ ๊ธฐ๋ณธ๊ฐ์ ์ ์ธ์ด์ ์ด๋ฐ ํน์ง์ ๋จ์ํํด ์ ๊ทผํด ํ๊ณ๊ฐ ์กด์ฌํ๋ค.
3. ํํ ์คํด 2
ํ์ ์ด์ถ์ถ(lemmatization), ์ด๊ฐ์ถ์ถ(stemming)์ ์ํํด ์ ์ฌ๋ฑ์ ์ ๊ฑฐํ ์ดํ Text Classification์ ์งํํด์ผํ๋๊ฐ??
์๋ฅผ๋ค์ด, "๋๋ ํ๊ต์ ๊ฐ์"๋ผ๋ ์๋ฌธ์ด ์๋ค๋ฉด, [๋ ํ๊ต ๊ฐ] ์ฒ๋ผ ์ด๊ฐ์ถ์ถ์ด ์งํ๋๋ค.
์ด๋ ์ ์ corpus์์ ํจ๊ณผ๋ฅผ ๋ฐํํ์ฌ ํฌ์์ฑ๋ฌธ์ ์์ ์ด๋์ ๋์ ํํ์ ์ด ์กด์ฌํ ์ ์๊ฒ๋๋ค.
ํนํ, DNN์ด์ ์ ํต์ ๊ธฐ๊ณํ์ต๋ฐฉ๋ฒ์์ ๋ถ์ฐ์์ ์กด์ฌ์ธ ์์ฐ์ด์ ์ข์ ๋ํ๊ตฌ๋ฅผ ๋ง๋ จํด์ฃผ์๋ค.
ํ์ง๋ง, DNN์๋์์๋ ์ฑ๊ณต์ ์ผ๋ก ์ฐจ์์ถ์(https://chan4im.tistory.com/197#n2)๋ฅผ ์ํํ ์ ์๊ฒ ๋๋ฉด์ ํฌ์์ฑ๋ฌธ์ ๋ ๋์ด์ ํฐ ์ฅ์ ๋ฌผ์ด ๋์ง๋ ์๊ธฐ์ lemmazation, stemming๋ฑ์ ๋ฐ๋์ ์ ์์ด๋ผ ํ๊ธด ์ด๋ ต๋ค.
๋ํ, "๋๋ ํ๊ต์ ๊ฐ์" / "๋๋ง ํ๊ต์ ๊ฐ์" ๋ผ๋ ๋ ๋ฌธ์ฅ์ ์๋ก ๊ธ์ / ๋ถ์ ์ด๋ผ๋ ๋ค๋ฅธ class๋ฅผ ๊ฐ๊ธฐ์ lemmazation์ด๋ stemming์ ํ ํ Text Classification์ ์ ๊ทผํ๋ ๊ฒ์ ๋ฐ๋์งํ์ง ๋ชปํ ๋ฐฉ๋ฒ์ผ ์๋ ์๋ค.
๋ฐ๋ผ์ ์ดํ ์ค๋ช ๋ ์ ๊ฒฝ๋ง๋ชจ๋ธ์ ์ฌ์ฉํด text classification์ ์๋ํ๋๊ฒ์ด ํจ์ฌ ๋ฐ๋์งํ๋ค.
๋ง์ฝ, ์ฑ๋ฅํฅ์์ ์ํด tuning ๋ฐ ์ฌ๋ฌ ์๋์์ corpus์ ๋ถ์กฑ์ด ์ฑ๋ฅ์ ํ์ ์์ธ์ด๋ผ ์๊ฐ๋ ๋, ์ถ๊ฐ์ ์ธ ์คํ์ผ๋ก๋ ๊ด์ฐฎ์ ์๋๊ฐ ๋ ์ ์๋ค.
4. RNN ํ์ฉํ๊ธฐ
์ด์ DNN์ ํตํ text classification๋ฌธ์ ๋ฅผ ์ดํด๋ณด์.
๊ฐ์ฅ ๊ฐ๋จํ ๋ฐฉ๋ฒ์ RNN์ ํ์ฉํ๋ ๊ฒ์ผ๋ก sequential data๋ผ๋ ๋ฌธ์ฅ์ ํน์ง์ ๊ฐ์ฅ ์ ํ์ฉ๊ฐ๋ฅํ ์ ๊ฒฝ๋ง ๊ตฌ์กฐ์ด๋ค.
n๊ฐ์ ๋จ์ด๋ก ์ด๋ฃจ์ด์ง ๋ฌธ์ฅ x์ ๋ํด RNN์ด ์์ ํ ์, n๊ฐ์ hidden_state๋ฅผ ์ป๋๋ค.
์ด๋, ๊ฐ์ฅ ๋ง์ง๋ง ์๋์ธต์ผ๋ก text classification์ด ๊ฐ๋ฅํ๋ฉฐ RNN์ ์ ๋ ฅ์ผ๋ก ์ฃผ์ด์ง ๋ฌธ์ฅ์ ๋ถ๋ฅ๋ฌธ์ ์ ๋ง๊ฒ encodingํ ์ ์๋ค.
์ฆ, RNN์ ์ถ๋ ฅ๊ฐ์ ๋ฌธ์ฅ์๋ฒ ๋ฉ๋ฒกํฐ(sentence embedding vector)๋ผ ํ ์ ์๋ค.
4.1 Architecture
์๋ค์ํผ, text์์ ๋จ์ด๋ ๋ถ์ฐ์์ ๊ฐ์ด๊ธฐ์ ์ด๋ค์ด ๋ชจ์ธ ๋ฌธ์ฅ ๋ํ, ๋ถ์ฐ์์ ๊ฐ์ด๋ค.
์ฆ, ์ด์ฐํ๋ฅ ๋ถํฌ์์ ๋ฌธ์ฅ์ samplingํ ๊ฒ์ด๋ฏ๋ก ์ ๋ ฅ์ผ๋ก๋ one-hot๋ฒกํฐ๋ค์ด ์ฌ๋ฌ time-step์ผ๋ก ์ฃผ์ด์ง๋ค.
mini-batch๊น์ง ๊ณ ๋ คํ๋ค๋ฉด, ์ ๋ ฅ์ 3์ฐจ์ tensor (n×m×|V|)๊ฐ ๋ ๊ฒ์ด๋ค.
โ n : mini_batch size (= ํ๋ฒ์ ์ฒ๋ฆฌํ ๋ฌธ์์ ๊ฐ์)
โ m : sentence length (= feature vector์ ์ฐจ์์ = ํ ์คํธ ๋ฌธ์์ ๋จ์ด์ ๊ฐ์)
โ|V| : Vocabulary size (= Dataset๋ด์ ๊ณ ์ ํ ๋จ์ด/ํ ํฐ์ ์ด ์)
ํ์ง๋ง ์ํซ๋ฒกํฐ๋ ์ฃผ์ด์ง |V| ์ฐจ์์ ๋จ ํ๋์ 1๊ณผ |V|-1๊ฐ์ 0์ผ๋ก ์ด๋ฃจ์ด์ง๋ค.
ํจ์จ์ ์ ์ฅ์ ์ํด ๊ตณ์ด ์ํซ๋ฒกํฐ ์ ์ฒด๋ฅผ ๊ฐ์ง๊ณ ์์ ํ์๋ ์๊ธฐ์
์ํซ๋ฒกํฐ๋ฅผ 0 ~ |V|-1 ์ฌ์ด ์ ์๋ก ๋ํ๋ผ ์ ์๊ฒ ๋๋ค๋ฉด, 2์ฐจ์ matrix (n×m)์ผ๋ก ์ถฉ๋ถํ ๋ํ๋ผ ์ ์๋ค.
์ด๋ ๊ฒ ์ํซ์ธ์ฝ๋ฉ๋ (n×m) tensor๋ฅผ embedding์ธต์ ํต๊ณผ์ํค๋ฉด,
word embedding tensor๋ฅผ ์ป์ ์ ์๋ค.
์ดํ word_embedding tensor๋ฅผ RNN์ ํต๊ณผ์ํค๋ฉด ๋๋ค.
์ด๋, ์ฐ๋ฆฐ RNN์ ๋ํด ๊ฐ time-step๋ณ, ๊ณ์ธต๋ณ๋ก ๊ตฌ๋ถํด word_embedding tensor๋ hidden_state๋ฅผ ๋ฃ์ด์ค ํ์๊ฐ ์๋ค.
์ต์ข ์ ์ผ๋ก ์ ์ผ ๋ง์ง๋ง time-step๋ง ์ ํํด softmax์ธต์ ํต๊ณผ์์ผ ์ด์ฐํ๋ฅ ๋ถํฌ P(y | x;θ)๋ก ๋ํ๋ผ ์ ์๋ค.
์ด๋ ์ ์ผ ๋ง์ง๋ง time-step์ H[:, -1]๊ณผ ๊ฐ์ ๋ฐฉ์์ผ๋ก index slicing์ ํตํด ๋์ถํ ์ ์๋ค.
๋ชจ๋ธ๊ตฌ์กฐ๋ก ๋ณด๋ฉด ์๋์ ๊ฐ๋ค.
๋ง์ง๋ง์ผ๋ก ์ํซ๋ฒกํฐ y์ด๊ธฐ์ ์ธ๋ฑ์ค์ ๋ก๊ทธํ๋ฅ ๊ฐ๋ง ์ต๋ํํ๋ฉด ๋๋ฏ๋ก
CE Loss ์์์ NLL(์์ ๋ก๊ทธ๊ฐ๋ฅ๋)๋ฅผ ์ต์ํํ๋ ๊ฒ๊ณผ ๋์น์ด๋ค.
Pytorch ๊ตฌํ์์
์์ ์์์ pytorch๋ก ๊ตฌํํ ์์ ์ฝ๋๋ก ์ฌ๋ฌ๊ณ์ธต์ผ๋ก ์ด๋ค์ง LSTM์ ์ฌ์ฉํ๋ค.
โ LSTM์๋ ๊ฐ ์ธต๋ง๋ค Dropout์ด ์ฌ์ฉ๋๋ฉฐ
โ NLL(์์ ๋ก๊ทธ๊ฐ๋ฅ๋)์์คํจ์๋ก ์ต์ ํํ๊ธฐ ์ํด logsoftmax๋ก ๋ก๊ทธํ๋ฅ ์ ๋ฐํํ๋ค.
import torch.nn as nn class RNNClassifier(nn.Module): def __init__( self, input_size, word_vec_size, hidden_size, n_classes, n_layers=4, dropout_p=.3, ): self.input_size = input_size # vocabulary_size self.word_vec_size = word_vec_size self.hidden_size = hidden_size self.n_classes = n_classes self.n_layers = n_layers self.dropout_p = dropout_p super().__init__() self.emb = nn.Embedding(input_size, word_vec_size) self.rnn = nn.LSTM( input_size=word_vec_size, hidden_size=hidden_size, num_layers=n_layers, dropout=dropout_p, batch_first=True, bidirectional=True, ) self.generator = nn.Linear(hidden_size * 2, n_classes) # We use LogSoftmax + NLLLoss instead of Softmax + CrossEntropy self.activation = nn.LogSoftmax(dim=-1) def forward(self, x): # |x| = (batch_size, length) x = self.emb(x) # |x| = (batch_size, length, word_vec_size) x, _ = self.rnn(x) # |x| = (batch_size, length, hidden_size * 2) y = self.activation(self.generator(x[:, -1])) # |y| = (batch_size, n_classes) return yโ
5. CNN ํ์ฉํ๊ธฐ
5.1 Convolution Operation
5.2 Convolution Layer
์์ธํ ์ค๋ช ์ ์๋ ๋งํฌ ์ฐธ๊ณ (https://chan4im.tistory.com/133)
5.3 Text Classification with CNN
CNN์ RNN๊ณผ ๋ฌ๋ฆฌ ์์ฐจ์ ์ ๋ณด๋ณด๋ค๋ ํจํด์ธ์ ๋ฐ ํ์ ์ ์ค์ ์ ๋๋ ๊ตฌ์กฐ๋ฅผ ๊ฐ๋๋ค.
CNN์ classification์ ์ค์ํ ๋จ์ด๋ค์ ์กฐํฉ์ ๋ํ ํจํด์ ๊ฐ์งํ๊ธฐ๋ ํ๋๋ฐ,
ํด๋น ํด๋์ค๋ฅผ ๋ํ๋ด๋ ๋จ์ด์กฐํฉ์ ๋ํ pattern์ ์ ๋ฌด๋ฅผ ๊ฐ์ฅ ์ค์ํ๋ค.
์๋ฅผ๋ค์ด, 'good'์ด๋ผ๋ ๋จ์ด๋ ๊ธ์ /๋ถ์ ๋ถ๋ฅ์ ํต์ฌ์ด ๋๋ ์ค์ํ signal๋ก ์๋ํ๋ค.
๊ทธ๋ ๋ค๋ฉด, 'good'์ ํด๋นํ๋ embedding vector์ pattern์ ๊ฐ์งํ๋ filter๋ฅผ ๋ชจ๋ธ์ด ํ์ตํ๋ค๋ฉด?
→ 'better', 'best', 'great'๋ฑ์ ๋จ์ด๋ค๋ 'good'๊ณผ ๋น์ทํ ๋ฒกํฐ๊ฐ์ ๊ฐ๊ฒ ๋ ๊ฒ์ด๋ค.
→ ๋ ๋์๊ฐ ๋จ์ด๋ค์ ์กฐํฉ ํจํด(word sequence pattern)์ ๊ฐ์งํ๋ filter๋ ํ์ต์ด ๊ฐ๋ฅํ ๊ฒ์ด๋ค.
๋ชจ๋ธ ๊ตฌ์กฐ์ ๋ํด ๊ฐ๋จํ ์ค๋ช ํ์๋ฉด, ์๋์ ๊ฐ๋ค.
๋จผ์ one-hot๋ฒกํฐ๋ฅผ ํํํ๋ ์ธ๋ฑ์ค๊ฐ์ ๋จ์ด์๋ฒ ๋ฉ๋ฒกํฐ(1์ฐจ์)๋ก ๋ณํํ๋ค.
๊ทธ ํ ๋ฌธ์ฅ ๋ด ๋ชจ๋ time-step์ ๋จ์ด์๋ฒ ๋ฉ๋ฒกํฐ๋ฅผ ํฉ์น๋ฉด 2์ฐจ์ ํ๋ ฌ์ด ๋๋ค.
๊ทธ ํ Convolution Operation์ ์ํํ๋ฉด CNN์ด ํจ๊ณผ๋ฅผ ๋ฐํํ๋ค.
Pytorch ๊ตฌํ์์
RNN์ text classification์ฒ๋ผ NLL(์์ ๋ก๊ทธ๊ฐ๋ฅ๋)์์คํจ์๋ก ์ต์ ํํ๊ธฐ ์ํด logsoftmax๋ก ๋ก๊ทธํ๋ฅ ์ ๋ฐํํ๋ค.
import torch import torch.nn as nn class CNNClassifier(nn.Module): def __init__( self, input_size, word_vec_size, n_classes, use_batch_norm=False, dropout_p=.5, window_sizes=[3, 4, 5], n_filters=[100, 100, 100], ): self.input_size = input_size # vocabulary size self.word_vec_size = word_vec_size self.n_classes = n_classes self.use_batch_norm = use_batch_norm self.dropout_p = dropout_p # window_size means that how many words a pattern covers. self.window_sizes = window_sizes # n_filters means that how many patterns to cover. self.n_filters = n_filters super().__init__() self.emb = nn.Embedding(input_size, word_vec_size) # Use nn.ModuleList to register each sub-modules. self.feature_extractors = nn.ModuleList() for window_size, n_filter in zip(window_sizes, n_filters): self.feature_extractors.append( nn.Sequential( nn.Conv2d( in_channels=1, # We only use one embedding layer. out_channels=n_filter, kernel_size=(window_size, word_vec_size), ), nn.ReLU(), nn.BatchNorm2d(n_filter) if use_batch_norm else nn.Dropout(dropout_p), ) ) # An input of generator layer is max values from each filter. self.generator = nn.Linear(sum(n_filters), n_classes) # We use LogSoftmax + NLLLoss instead of Softmax + CrossEntropy self.activation = nn.LogSoftmax(dim=-1) def forward(self, x): # |x| = (batch_size, length) x = self.emb(x) # |x| = (batch_size, length, word_vec_size) min_length = max(self.window_sizes) if min_length > x.size(1): # Because some input does not long enough for maximum length of window size, # we add zero tensor for padding. pad = x.new(x.size(0), min_length - x.size(1), self.word_vec_size).zero_() # |pad| = (batch_size, min_length - length, word_vec_size) x = torch.cat([x, pad], dim=1) # |x| = (batch_size, min_length, word_vec_size) # In ordinary case of vision task, you may have 3 channels on tensor, # but in this case, you would have just 1 channel, # which is added by 'unsqueeze' method in below: x = x.unsqueeze(1) # |x| = (batch_size, 1, length, word_vec_size) cnn_outs = [] for block in self.feature_extractors: cnn_out = block(x) # |cnn_out| = (batch_size, n_filter, length - window_size + 1, 1) # In case of max pooling, we does not know the pooling size, # because it depends on the length of the sentence. # Therefore, we use instant function using 'nn.functional' package. # This is the beauty of PyTorch. :) cnn_out = nn.functional.max_pool1d( input=cnn_out.squeeze(-1), kernel_size=cnn_out.size(-2) ).squeeze(-1) # |cnn_out| = (batch_size, n_filter) cnn_outs += [cnn_out] # Merge output tensors from each convolution layer. cnn_outs = torch.cat(cnn_outs, dim=-1) # |cnn_outs| = (batch_size, sum(n_filters)) y = self.activation(self.generator(cnn_outs)) # |y| = (batch_size, n_classes) return yโ
6. ์ฌ์ด๊ฐ๊ธฐ) Multi-Label Classification
Mutli-Label Classification: ๊ธฐ์กด softmax ๋ถ๋ฅ์ ๋ฌ๋ฆฌ ์ฌ๋ฌ ํด๋์ค๊ฐ ๋์์ ์ ๋ต์ด ๋ ์ ์๋๊ฒ
6.1 Binary-Classification
sigmoid. &. BCELoss๋ฅผ ์ฌ์ฉํ๋ค. (์ด์ง๋ถ๋ฅ์ํฉ์ Bernoulli Distribution์ด๊ธฐ ๋๋ฌธ)
์์์ ์๋์ ๊ฐ์๋ฐ, BCE Loss๋ ์ด์ง๋ถ๋ฅ์ ํนํ๋ ๊ธฐ์กด CE Loss์ ํ ์ข ๋ฅ์ด๋ค.
์ด ์์์์ y๋ 0๋๋ 1์ ๊ฐ๋ ๋ถ์ฐ์์ ์ธ ๊ฐ์ด๊ณ
y_hat์ sigmoid๋ฅผ ํต๊ณผํ 0~1์ฌ์ด์ ์ฐ์์ ์ธ ์ถ๋ ฅ๊ฐ์ด๋ค.
6.2 Multi-Binary Classification
๊ทธ๋ ๋ค๋ฉด, Multi-Label๋ฌธ์ ์์ Binary Classification๋ฅผ ์ด๋ป๊ฒ ์ ์ฉํ ๊น?
n๊ฐ์ ํญ๋ชฉ์ ๊ฐ๋ ๋ถ๋ฅ์ ๋ํด ์ ๊ฒฝ๋ง์ ๋ง์ง๋ง ๊ณ์ธต์ n๊ฐ์ ๋ ธ๋๋ฅผ ์ฃผ๊ณ , ๋ชจ๋ sigmoidํจ์๋ฅผ ์ ์ฉํ๋ค.
์ฆ, ํ๋์ ๋ชจ๋ธ๋ก ์ฌ๋ฌ ์ด์ง๋ถ๋ฅ์์ ์ด ๊ฐ๋ฅํ๋ค.
๊ทธ๋ ๋ค๋ฉด ์ต์ข ์์คํจ์๋? ๋ค์๊ณผ ๊ฐ๋ค.
6.3 ETC
์ด์ง๋ถ๋ฅ๊ฐ ์๋ ๋๋, sigmoid๊ฐ ์๋ softmax๋ฅผ ์ฌ์ฉํ๊ณ , Loss๋ Cross-Entropy๋ก ๋ฐ๊พธ๋ฉด ๋๋ค.
๋ง์น๋ฉฐ...
์ด๋ฒ์๊ฐ์๋ text classification์ ๋ํด ๋ค๋ฃจ์๋ค.
text classification์ ๋ชจ๋ธ์ ๊ตฌ์กฐ์ ๋ณต์ก๋๋ ์ฝ๋์์ฑ๋๋์ ๋นํด ํ์ฉ๋๊ฐ ๋งค์ฐ ๋์ ๋ถ์ผ์ด๋ค.
๋ค๋ง, ์ ๊ฒฝ๋ง์ฌ์ฉ์ด์ , ๋ถ์ฐ์์ ๊ฐ์ ๋ํ ํฌ์์ฑ๋ฌธ์ ํด๊ฒฐ์ ํ์ง ๋ชปํ ์ฑ,
Naïve Bayes๋ฐฉ์๊ณผ ๊ฐ์ด ๋งค์ฐ ๊ฐ๋จํ๊ณ ์ง๊ด์ ์ธ ๋ฐฉ๋ฒ์ ์ฌ์ฉํ๋ค.
๋ค๋ง, Naïve Bayes๋ฐฉ์์ "๊ฐ feature๋ independentํ๋ค!"๋ผ๋ ๊ฐ๋ ฅํ ๊ฐ์ ์ผ๋ก์ธํด classification์ ์ ํ๋๊ฐ ๋จ์ด์ง ์ ๋ฐ์ ์์๋ค.
ํ์ง๋ง ๋ฅ๋ฌ๋์ ๋์ ์ผ๋ก ๋งค์ฐ ํจ์จ์ ์ด๊ณ ์ ํํ๊ฒ text classification์ด ๊ฐ๋ฅํด์ก๋๋ฐ,
RNN์ ๋จ์ด๋ค์ ์์ฐจ์ ์ผ๋ก ๋ฐ์ ๊ฐ์ฅ ๋ง์ง๋ง time-step์์ classification์ ์์ธกํ๊ณ
CNN์ classification์ ์ค์ํ ๋จ์ด๋ค์ ์กฐํฉ์ ๋ํ ํจํด์ ๊ฐ์งํ๊ธฐ๋ ํ๋ค.
โRNN์ ๊ฒฝ์ฐ, ๋ฌธ์ฅ์ ์ฒด์ ๋งฅ๋ฝ๊ณผ ์๋ฏธ์ ๋ ์ง์คํด classification์ ์ํํ๋ฉฐ
โCNN์ ๊ฒฝ์ฐ, ํด๋น ํด๋์ค๋ฅผ ๋ํ๋ด๋ ๋จ์ด์กฐํฉ์ ๋ํ pattern์ ์ ๋ฌด๋ฅผ ๊ฐ์ฅ ์ค์ํ๋ค.
๋ฐ๋ผ์ RNN๊ณผ CNN์ ์กฐํฉํด Ensemble Model๋ก ๊ตฌํํ๋ค๋ฉด ๋ ์ข์ ๊ฒฐ๊ณผ๋ฅผ ์ป์์๋ ์๋ค.
์ด๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ๋ค๋ฅธ ๋ชจ๋ธ๋ค์ ์ฐธ๊ณ ํ๋ค๋ฉด, ๊ธด๋ฌธ์ฅ์ด๋ ์ด๋ ค์ด ํ ์คํธ์์๋ ๋ ๋์ ์ฑ๋ฅ์ ๋ผ ์ ์์ ๊ฒ์ด๋ค.