๋ฐ˜์‘ํ˜•

์ „์ฒด ๊ธ€ 407

์ž์—ฐ์–ด ์ฒ˜๋ฆฌ์—์„œ์˜ ํ•˜์ดํผ ํŒŒ๋ผ๋ฏธํ„ฐ ์ข…๋ฅ˜, ์„ค์ •

ํ•˜์ดํผ ํŒŒ๋ผ๋ฏธํ„ฐ ํ•˜์ดํผ ํŒŒ๋ผ๋ฏธํ„ฐ ๋ž€ ์‚ฌ๋žŒ์ด ์ˆ˜๋™์œผ๋กœ ์กฐ์ •ํ•˜๋Š” ํŒŒ๋ผ๋ฏธํ„ฐ์ด๋‹ค. ํ•˜์ดํผ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์ž˜ ์กฐ์ •ํ•˜๋ฉด ์„ฑ๋Šฅ์ด ๊ฐœ์„ ๋  ์ˆ˜ ์žˆ๋‹ค. ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ข…๋ฅ˜๊ฐ€ ์žˆ๋‹ค. ์ด๋ฒˆ์—๋Š” ๋”ฅ๋Ÿฌ๋‹์—์„œ ์šฐ๋ฆฌ๊ฐ€ ์กฐ์ž‘ํ•  ์ˆ˜ ์žˆ๋Š” ํ•˜์ดํผ ํŒŒ๋ผ๋ฏธํ„ฐ์— ๋Œ€ํ•ด ์•Œ์•„๋ณด๊ณ  ํŠœ๋‹(์กฐ์ •)ํ•˜๋Š” ๋ฐฉ๋ฒ•๋“ค๋„ ์ •๋ฆฌํ•ด๋ณด๊ณ ์ž ํ•œ๋‹ค. Model-free hyperparameters ( ๋ชจ๋ธ๊ณผ ๊ด€๋ จ ์—†๋Š” ํ•˜์ดํผํŒŒ๋ผ๋ฏธํ„ฐ) learning rate pytorch ์—์„œ๋Š” learning rate scheduler๋ฅผ ์ œ๊ณตํ•œ๋‹ค. ์ด ๋‘ ๊ฐ€์ง€๊ฐ€ ์žˆ๋‹ค. 1. LambdaLR 2. stepLR batch size - ํด์ˆ˜๋ก ์ข‹๋‹ค, ์ฃผ์–ด์ง„ GPU์˜ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ๋ณด๊ณ  ์ตœ์žฌ์˜ ๋ฐฐ์น˜์‚ฌ์ด์ฆˆ๋ฅผ ์„ค์ •ํ•˜๋Š” ๊ฒƒ์ด ์„ฑ๋Šฅ์ด ์ข‹๋‹ค๊ณ  ์•Œ๋ ค์ ธ ์žˆ๋‹ค. ๋ฐฐ์น˜ ์‚ฌ์ด์ฆˆ๊ฐ€ ํฌ๋ฉด, ํ•œ๋ฒˆ ํ•™์Šตํ• ๋•Œ ๋ณด๋Š” ์ด๋ฏธ์ง€/ํ…์ŠคํŠธ ..

numpy argsort ์˜ ์˜๋ฏธ์™€ ์‚ฌ์šฉ๋ฒ• ์ •๋ฆฌ

์ฝ”๋“œ๋ฅผ ๋ฆฌ๋ทฐํ•˜๋‹ค๊ฐ€ np.argsort ๋ฅผ ๋ณด๊ณ  ์ •๋ ฌ์€ ์ •๋ ฌ์ธ๋ฐ ์–ด๋–ค์‹์œผ๋กœ ์ •๋ ฌ๋˜๋Š” ๊ฑด์ง€ ๊ถ๊ธˆํ•ด์„œ ์ •๋ฆฌํ•ด๋ณด์•˜๋‹ค. ๊ฐ ์›์†Œ์˜ ์ •๋ ฌ ์ธ๋ฑ์Šค ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค. numpy.argsort(a, axis=-1, kind=None, order=None) b = [0,1,2,3,10,9,8] x=np.argsort(b) print(x) ๊ฒฐ๊ณผ๊ฐ’ [0 1 2 3 6 5 4] x2 = np.argsort(b)[::-1]. #๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌ ๊ฒฐ๊ณผ๊ฐ’ [4 5 6 3 2 1 0] a = [[0,1,2,3],[1,2,3]] print(np.argsort([len(aa) for aa in a])) [1 0] x = np.array([[0, 3], [2, 2]]) np.argsort(x, axis=0) array([[0, 1], [1,..

์ž์—ฐ์–ด์ฒ˜๋ฆฌ ๋ชจ๋ธ์ด ํƒœ์Šคํฌ๋ฅผ ์ˆ˜ํ–‰ํ•˜๋Š” ๋ฐฉ๋ฒ•์€? (์ธ ์ปจํ…์ŠคํŠธ ๋Ÿฌ๋‹, ์ œ๋กœ์ƒท, ์›์ƒท ํ“จ์ƒท ๋Ÿฌ๋‹)

์šฐ๋ฆฌ๋Š” BERT, GPT ๋“ฑ ๋‹ค์–‘ํ•œ ํ”„๋ฆฌํŠธ๋ ˆ์ธ ๋œ ๋ชจ๋ธ๋“ค์„ ๊ฐ€์ง€๊ณ  ์–ด๋–ค ํ…Œ์Šคํฌ๋ฅผ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค๋ฉด, ๋ฌธ์„œ ๋ถ„๋ฅ˜, ๊ฐ์ •๋ถ„์„, ์งˆ์˜ ์‘๋‹ต , ๊ฐœ์ฒด๋ช…์ธ์‹,,, ๊ณผ ๊ฐ™์€ ์ผ๋“ค์„ ๋ง์ด์ฃ . ์ด๋•Œ ์ด๋Ÿฌํ•œ ํ…Œ์Šคํฌ๋“ค์€ ๋‹ค์šด์ŠคํŠธ๋ฆผ ํƒœ์Šคํฌ (downstream task)๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๋ฐ˜๋Œ€๋กœ upstream task๋Š” ํ”„๋ฆฌํŠธ๋ ˆ์ธ ์‹œํ‚ค๋Š” ํ•™์Šต ๋ฐฉ์‹์ด๋ผ๊ณ  ์ดํ•ดํ•˜๋ฉด ๋  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. data1 -> model -> upstream task (ex- MLM, ๋‹ค์Œ ๋‹จ์–ด ๋งž์ถ”๊ธฐ) (pretrain) data2 -> model(์œ„๋ž‘ ๊ฐ™์€ ๋ชจ๋ธ, ์ด๋ฅผ ์ „์ดํ•™์Šต transfer learning ์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค.) -> downstream task (NER,QA,text-classification) downstream task๋ฅผ ํ•™์Šตํ•˜๋Š”..

์–ธ์–ด๋ชจ๋ธ GPT

BERT ๊ฐ€ ํŠธ๋žœ์Šคํฌ๋จธ์˜ ์ธ์ฝ”๋”๋ฅผ ํ™œ์šฉํ–ˆ๋‹ค๋ฉด, GPT๋Š” ํŠธ๋žœ์Šคํฌ๋จธ์˜ ๋””์ฝ”๋”๋งŒ ํ™œ์šฉํ•ฉ๋‹ˆ๋‹ค. ๋””์ฝ”๋” ์ค‘์—์„œ๋„ encoder-decoder attention์ด ๋น ์ง„ ๋””์ฝ”๋”๋งŒ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. Masked Multi-Head Attention์—์„œ ์ผ์–ด๋‚˜๋Š” ์ผ์„ ๋ณด๋ฉด, ์ œ๊ฐ€ ๊ณ„์† ์˜ˆ์‹œ๋กœ ๋“œ๋Š” ๋ฌธ์žฅ์„ ๊ฐ€์ ธ์™€ ์ ์šฉํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. '๋‚˜๋Š” ํ† ๋ผ๋ฅผ ํ‚ค์›Œ. ๋ชจ๋“  ์‚ฌ๋žŒ์ด ๊ทธ๋ฅผ ์ข‹์•„ํ•ด'๋ผ๋Š” ๋ฌธ์žฅ์—์„œ ์ฒ˜์Œ์—๋Š” ๋‚˜๋Š”์„ ๋บด๊ณ  ๋ชจ๋‘ ๋งˆ์Šคํ‚น์ฒ˜๋ฆฌํ•ด์ค๋‹ˆ๋‹ค. ๋‚˜๋Š” ๋งŒ๋ณด๊ณ  ํ† ๋ผ๋ฅผ ์„ ์˜ˆ์ธกํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•˜๊ธฐ ์œ„ํ•ด์„œ ํ† ๋ผ๋ฅผ์— ํ™•๋ฅ ์„ ๋†’์ด๋Š” ์‹์œผ๋กœ ์—…๋ฐ์ดํ„ฐํ•˜๋ฉฐ ํ•™์Šต์ด ์ง„ํ–‰๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋‚˜๋Š” ํ† ๋ผ๋ฅผ ๋งŒ์œผ๋กœ ํ‚ค์›Œ๋ฅผ ์˜ˆ์ธกํ•  ์ˆ˜ ์žˆ๊ฒŒ , ํ‚ค์›Œ์— ํ™•๋ฅ ์„ ๋†’์ด๋Š” ๋ฐฉ์‹์œผ๋กœ ํ•™์Šต์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. BERT์—์„œ๋Š” ๊ฐ€์šด๋ฐ ๋‹จ์–ด๋ฅผ [MASK]๋กœ ์ฒ˜๋ฆฌํ•˜๊ณ  ์•ž๊ณผ ๋’ค ๋‹จ์–ด๋“ค์„ ๋ณด๊ณ ..

pytorch DataLoader ํŒŒ์ดํ† ์น˜ ๋ฐ์ดํ„ฐ๋กœ๋” ์‚ฌ์šฉ๋ฒ•

ํŒŒ์ดํ† ์น˜๋กœ ๋”ฅ๋Ÿฌ๋‹ ๋ชจ๋ธ์„ ๋งŒ๋“œ๋ ค๋ฉด ๋ฐ์ดํ„ฐ๋กœ๋”๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค. ์ด๋ฅผ ์ •์˜ ํ•˜๋Š” ๋ฒ•์„ ์ •๋ฆฌํ•ด๋ณด๋ ค ํ•œ๋‹ค , ์ด์ „์— https://getacherryontop.tistory.com/144 ์—์„œ ๋ฒ„ํŠธ๋กœ ์˜ํ™”๋ฆฌ๋ทฐ ๊ฐ์ •๋ถ„์„์„ ํ•˜๋ฉฐ ๋‘ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์„ ์–ธ๊ธ‰ํ–ˆ์—ˆ๋‹ค. 1. transformers ์˜ Trainer๋ฅผ ํ™œ์šฉํ•œ๋‹ค. 2. pytorch๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. 2๋ฒˆ์˜ ๊ฒฝ์šฐ์— ์šฐ๋ฆฌ๋Š” ๋ฐ์ดํ„ฐ๋กœ๋”๋ฅผ ์ •์˜ํ•ด์•ผ ํ•œ๋‹ค. ์œ„ ๊ธ€์—์„œ ๋ฐ์ดํ„ฐ๋กœ๋”๋งŒ ๋ณด๋ฉด from torch.utils.data import DataLoader train_dataloader = DataLoader(small_train_dataset, shuffle=True, batch_size=8) eval_dataloader = DataLoader(small_eval_datas..

๋ฒ„ํŠธ๋ฅผ ํ™œ์šฉํ•œ ์˜ํ™”๋ฆฌ๋ทฐ ๋ถ„๋ฅ˜

Pre-trained BERT๋ฅผ ํ™œ์šฉํ•˜์—ฌ ์˜ํ™”๋ฆฌ๋ทฐ ๊ฐ์ •๋ถ„๋ฅ˜ ๋‘๊ฐ€์ง€ ๋ฐฉ๋ฒ•: 1. transformers ์˜ Trainer๋ฅผ ํ™œ์šฉํ•œ๋‹ค. 2. pytorch๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. ์ด ๋‘๊ฐ€์ง€ ๋ฐฉ๋ฒ•์„ ๋‹ค ์ตํ˜€๋‘๋Š” ๊ฒƒ์ด ์ข‹์œผ๋ฉฐ, 1๋ฒˆ์€ ์ˆ˜์ • ํ˜น์€ ๋ฏธ์„ธ์กฐ์ •ํ•˜๊ธฐ๊ฐ€ ์กฐ๊ธˆ ๊นŒ๋‹ค๋กœ์›Œ์„œ 2๋ฒˆ์œผ๋กœ ํ•˜๋Š” ๊ฒƒ์„ ์„ ํ˜ธ ํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ๋‹ค๊ณ  ํ•œ๋‹ค. from transformers import AutoConfig, AutoTokenizer, AutoModelForSequenceClassification config = AutoConfig.from_pretrained('bert-base-uncased') tokenizer = AutoTokenizer.from_pretrained('bert-base-uncased') model = AutoModelF..

ํ—ˆ๊น…ํŽ˜์ด์Šค์˜ ํŠธ๋žœ์Šคํฌ๋จธ ๐Ÿค— Huggingface's Transformers

์ด๋ฒˆ์—๋Š” Huggingface์—์„œ ์ œ๊ณตํ•˜๋Š” Transformers์— ๋Œ€ํ•˜์—ฌ ์•Œ์•„๋ณด๊ณ ์ž ํ•ฉ๋‹ˆ๋‹ค. https://huggingface.co/docs/transformers/index ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ ๊ด€๋ จ ์—ฌ๋Ÿฌ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ ์žˆ์ง€๋งŒ Transformer๋ฅผ ํ™œ์šฉํ•œ ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ task์—์„œ ๊ฐ€์žฅ ๋งŽ์ด ํ™œ์šฉ๋˜๊ณ  ์žˆ๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋Š” transformers์ž…๋‹ˆ๋‹ค. pytorch version์˜ BERT๋ฅผ ๊ฐ€์žฅ ๋จผ์ € ๊ตฌํ˜„ํ•˜๋ฉฐ ์ฃผ๋ชฉ๋ฐ›์•˜๋˜ huggingface๋Š” ํ˜„์žฌ transformer๊ธฐ๋ฐ˜์˜ ๋‹ค์–‘ํ•œ ๋ชจ๋ธ๋“ค์€ ๊ตฌํ˜„ ๋ฐ ๊ณต๊ฐœํ•˜๋ฉฐ ๋งŽ์€ ์ฃผ๋ชฉ์„ ๋ฐ›๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.(์•„๋ž˜ ์ฃผ์†Œ์—์„œ ๋‹ค์–‘ํ•œ ๋ชจ๋ธ๋“ค์„ ํ™•์ธ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค) https://huggingface.co/models ์ œ์‹œ๋œ ๋ชจ๋ธ ์ด์™ธ์—๋„ custom model์„ ์—…๋กœ๋“œํ•˜์—ฌ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค..

[python] import logging ๋กœ๊น…์— ๋Œ€ํ•ด

์—ฌ๋Ÿฌ ์˜คํ”ˆ์†Œ์Šค๋“ค์„ ๊ณต๋ถ€ํ•˜๋ฉด์„œ logging์— ๋Œ€ํ•ด ์ฒ˜์Œ ์ ‘ํ•˜๊ฒŒ ๋˜์—ˆ๋‹ค. ์ฝ”๋“œ๊ฐ€ ์–ด๋””๊นŒ์ง€ ์ง„ํ–‰์ด ๋˜์—ˆ๊ณ , ์–ด๋””์„œ ์—๋Ÿฌ๊ฐ€ ๋‚˜๋Š”์ง€ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•œ ๋กœ๊ทธ๋ถ„์„์„ ํ•˜๋Š”๋ฐ ์ด๋Ÿฌํ•œ ์ž‘์—…์ด ๋ฐ”๋กœ logging์—์„œ ์ด๋ฃจ์–ด์ง€๋Š” ๊ฒƒ์ด๋‹ค. ์•„๋ž˜ ํŒŒ์ด์ฌ ๊ณต์‹ ๋ฌธ์„œ๋ฅผ ์ฐธ์กฐํ•˜๋ฉด ๋กœ๊น…์— ๋Œ€ํ•ด ์ฝ์–ด๋ณผ ์ˆ˜ ์žˆ๋‹ค. https://docs.python.org/ko/3/howto/logging.html ์ด ๋ฌธ์„œ์— ๋”ฐ๋ฅด๋ฉด : ๋กœ๊น…์€ ์–ด๋–ค ์†Œํ”„ํŠธ์›จ์–ด๊ฐ€ ์‹คํ–‰๋  ๋•Œ ๋ฐœ์ƒํ•˜๋Š” ์ด๋ฒคํŠธ๋ฅผ ์ถ”์ ํ•˜๋Š” ์ˆ˜๋‹จ์ž…๋‹ˆ๋‹ค. ์†Œํ”„ํŠธ์›จ์–ด ๊ฐœ๋ฐœ์ž๋Š” ์ฝ”๋“œ์— ๋กœ๊น… ํ˜ธ์ถœ์„ ์ถ”๊ฐ€ํ•˜์—ฌ ํŠน์ • ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ–ˆ์Œ์„ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. ์ด๋ฒคํŠธ๋Š” ์„ ํƒ์ ์œผ๋กœ ๊ฐ€๋ณ€ ๋ฐ์ดํ„ฐ (์ฆ‰, ์ด๋ฒคํŠธ ๋ฐœ์ƒ๋งˆ๋‹ค ์ž ์žฌ์ ์œผ๋กœ ๋‹ค๋ฅธ ๋ฐ์ดํ„ฐ)๋ฅผ ํฌํ•จํ•  ์ˆ˜ ์žˆ๋Š” ์„ค๋ช… ๋ฉ”์‹œ์ง€๋กœ ๊ธฐ์ˆ ๋ฉ๋‹ˆ๋‹ค. ์ด๋ฒคํŠธ๋Š” ๋˜ํ•œ ๊ฐœ๋ฐœ์ž๊ฐ€ ์ด๋ฒคํŠธ์— ๋ถ€์—ฌํ•œ ..

Python 2023.01.16

ํŠธ๋žœ์Šคํฌ๋จธ์™€ ์–ดํƒ ์…˜ ์ •๋ฆฌ --2

https://getacherryontop.tistory.com/140 ํŠธ๋žœ์Šคํฌ๋จธ์™€ ์–ดํƒ ์…˜ ์ •๋ฆฌ --1 ๋“œ๋””์–ด ๋‚˜๋ฅผ ๊ดด๋กญํžˆ๋˜ ์–ดํƒ ์…˜์— ๊ด€ํ•ด ์ดํ•ดํ•œ ํ›„, ์Šค์Šค๋กœ ์ฒ˜์Œ๋ถ€ํ„ฐ ์ •๋ฆฌํ•ด๋ณด์•˜๋‹ค!!! ํŠธ๋žœ์Šคํฌ๋จธ ๋“ฑ์žฅ ์ „ ํŠธ๋žœ์Šคํฌ๋จธ ์ „์—๋Š” RNN, CNN์„ ํ™œ์šฉํ•œ ์ž์—ฐ์–ด์ฒ˜๋ฆฌ๊ฐ€ ์ด๋ฃจ์–ด์กŒ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์ด๋Ÿฌํ•œ ๋ฐฉ๋ฒ•์€ ์‹œ getacherryontop.tistory.com ์ง€๋‚œ๋ฒˆ๊นŒ์ง€ ํŠธ๋žœ์Šคํฌ๋จธ์˜ ์ž…๋ ฅ๊ณผ ์ถœ๋ ฅ, ์–ดํƒ ์…˜ํ•จ์ˆ˜์— ๋Œ€ํ•ด ์•Œ์•„๋ณด์•˜๋‹ค. ์ธ์ฝ”๋”์™€ ๋””์ฝ”๋”์— ๋Œ€ํ•ด ์ •๋ฆฌํ•˜๋˜ ์ค‘ ์ฑ…์—์„œ ์ข€ ์ด์ƒํ•œ ๋ถ€๋ถ„์ด ์žˆ์–ด ๋ช…ํ™•ํ•˜๊ฒŒ ์ดํ•ด๋˜์ง€ ์•Š์€ ๋ถ€๋ถ„์— ๋Œ€ํ•ด ์งˆ๋ฌธ์„ ํ†ตํ•ด ํ•ด๊ฒฐํ–ˆ๋‹ค! ์•„๋ฌดํŠผ ์ด๋ฒˆ์—๋Š” ํŠธ๋žœ์Šคํฌ๋จธ์˜ ์ธ์ฝ”๋”์™€ ๋””์ฝ”๋”์— ๋Œ€ํ•ด ์ •๋ฆฌํ•˜๊ฒ ๋‹ค. ์™ผ์ชฝ ์‚ฌ์ง„์ธ ํŠธ๋žœ์Šคํฌ๋จธ ์ „์ฒด ๊ตฌ์กฐ์—์„œ ์ดˆ๋ก์ƒ‰์€ ์ธ์ฝ”๋”, ๋นจ๊ฐ„์ƒ‰์€ ๋””์ฝ”๋”์ด๋‹ค. ์˜ค๋ฅธ์ชฝ ์‚ฌ์ง„์€ ์ธ์ฝ”๋”์™€ ๋””์ฝ”๋”..

ํŠธ๋žœ์Šคํฌ๋จธ์™€ ์–ดํƒ ์…˜ ์ •๋ฆฌ --1

๋“œ๋””์–ด ๋‚˜๋ฅผ ๊ดด๋กญํžˆ๋˜ ์–ดํƒ ์…˜์— ๊ด€ํ•ด ์ดํ•ดํ•œ ํ›„, ์Šค์Šค๋กœ ์ฒ˜์Œ๋ถ€ํ„ฐ ์ •๋ฆฌํ•ด๋ณด์•˜๋‹ค!!! ํŠธ๋žœ์Šคํฌ๋จธ ๋“ฑ์žฅ ์ „ ํŠธ๋žœ์Šคํฌ๋จธ ์ „์—๋Š” RNN, CNN์„ ํ™œ์šฉํ•œ ์ž์—ฐ์–ด์ฒ˜๋ฆฌ๊ฐ€ ์ด๋ฃจ์–ด์กŒ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์ด๋Ÿฌํ•œ ๋ฐฉ๋ฒ•์€ ์‹œํ€€์Šค๊ฐ€ ๊ธธ์–ด์ง€๋ฉด ์•ž์˜ ์ •๋ณด๊ฐ€ ์žŠํ˜€์ง€๊ณ (๊ธฐ์–ต๋ ฅ์ด ์งง์Œ), ๊ฐ™์€ ๋ชจ๋“ˆ์„ ๊ฑฐ์น˜๋ฉฐ ์—ฐ์‚ฐ์„ ๋ฐ˜๋ณตํ•˜์—ฌ ์ •๋ณด์˜ ์œ ์‹ค์ด ๋งŽ์ด ์ผ์–ด๋‚ฌ๋‹ค. ํŠธ๋žœ์Šคํฌ๋จธ์˜ ๋“ฑ์žฅ -> NO MORE RNN,CNN!! ํŠธ๋žœ์Šคํฌ๋จธ์˜ ๊ตฌ์กฐ ์ดˆ๋ก์ƒ‰์€ ์ธ์ฝ”๋”, ๋นจ๊ฐ„์ƒ‰์€ ๋””์ฝ”๋” ์ด๋‹ค. ์ฆ‰, ํŠธ๋žœ์Šคํฌ๋จธ๋Š” ์ธ์ฝ”๋”์™€ ๋””์ฝ”๋”๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋งŽ์ด ๋“ฑ์žฅํ•˜๋Š” ๋‹จ์–ด๊ฐ€ Attention....Attention์ด ๋ญ˜๊นŒ???? (์—ฌ๊ธฐ์„œ ๋งŽ์ด ์–ด๋ ค์›Œํ•˜๊ณ  ํฌ๊ธฐํ•˜๊ธฐ ์‰ฝ๋‹ค..๊ทธ์น˜๋งŒ ์ตœ๋Œ€ํ•œ ์‰ฝ๊ฒŒ ์ •๋ฆฌํ•ด๋ณด๊ฒ ๋‹ค!) ์–ดํƒ ์…˜์€ ์ฃผ๋ชฉ์ด๋‹ค. Looking for attention ๋„ˆ์•ผ๊ฒ ์–ด ํ™•..

end-to-end ๋ชจ๋ธ์ด๋ž€

๋”ฅ๋Ÿฌ๋‹ ์ฑ…์„ ์ฝ๋‹ค๋ณด๋ฉด, ์—”๋“œํˆฌ์—”๋“œ ๋ชจ๋ธ์ด๋ผ๋Š” ์šฉ์–ด๊ฐ€ ๋งŽ์ด ๋‚˜์˜จ๋‹ค. end-to-end ๋ชจ๋ธ ๋Œ€๋ถ€๋ถ„์˜ ๋”ฅ๋Ÿฌ๋‹ ๋ชจ๋ธ์€ x๋ฅผ ๋„ฃ์œผ๋ฉด y๊ฐ€ ๋‚˜์˜ค๋„๋ก ํ•œ๋‹ค. (์˜ˆ๋ฅผ ๋“ค๋ฉด, ๊ฐœ์‚ฌ์ง„ ์ž…๋ ฅ-> ๊ฐœ๋ฅผ๋ฑ‰๋Š” ์ด๋ฏธ์ง€ ๋ถ„๋ฅ˜ ํ…Œ์Šคํฌ) ๊ทธ ๊ณผ์ •์€ ์ด๋ฏธ์ง€์—์„œ ํŠน์ง•์„ ์ถ”์ถœํ•˜๊ณ , ํŠน์ง•์— ๋งž๋Š” ๋‹จ์–ด์™€ ๋งค์นญ์‹œํ‚ค๊ณ  ์ด๋Ÿฌํ•œ ์ž‘์—…๋“ค์„ ๊ฑฐ์น˜๋Š” ํŒŒ์ดํ”„๋ผ์ธ์œผ๋กœ ์ด๋ฃจ์–ด์ ธ์žˆ๋‹ค.end-to-end ๋ชจ๋ธ์€ ์ด๋Ÿฌํ•œ ์ค‘๊ฐ„ ๊ณผ์ •๋“ค์„ ํ•˜๋‚˜์˜ ์‹ ๊ฒฝ๋ง(ํ•˜๋‚˜์˜ ํŒŒ์ดํ”„๋ผ์ธ)์œผ๋กœ ๋Œ€์ฒด/์••์ถ•ํ•˜๋Š” ๊ฒƒ์ด๋ผ ์ƒ๊ฐํ•˜๋ฉด ๋œ๋‹ค. ๋‹ค๋งŒ ์ด๋Š” ๋งŽ์€ ์–‘์˜ ๋ฐ์ดํ„ฐ๊ฐ€ ์žˆ์–ด์•ผ๋งŒ ๊ฐ€๋Šฅํ•˜๋‹ค. ์Œ์„ฑ์ธ์‹์— end-to-end ๋ฅผ ์ ์šฉํ•œ ์˜ˆ ์‚ฌ์‹ค ์ข€ ์ถ”์ƒ์ ์ธ ๊ฐœ๋…์ด๊ธฐ๋Š” ํ•œ๋ฐ์š” “ํŠน์ •ํ•œ task๋ฅผ ํ•˜๋‚˜์˜ ๋ชจ๋ธ์— ๋Œ€ํ•œ ์ถ”๋ก  ๊ฒฐ๊ณผ๋กœ ์ˆ˜ํ–‰ํ•œ๋‹ค” ์ด์ •๋„์˜ ์˜๋ฏธ์ธ๋ฐ ์—ฌ๊ธฐ์„œ “ํŠน์ •ํ•œ task” ๋Š” language m..

transfomers ๋ฅผ ์‚ฌ์šฉํ•œ ๊ฐ„๋‹จํ•œ ๋ถ„๋ฅ˜ ์˜ˆ์ œ(BertForSequenceClassification)

https://huggingface.co/docs/transformers/model_doc/bert#transformers.BertForSequenceClassification BERT call ( input_ids: typing.Union[typing.List[tensorflow.python.framework.ops.Tensor], typing.List[numpy.ndarray], typing.List[tensorflow.python.keras.engine.keras_tensor.KerasTensor], typing.Dict[str, tensorflow.python.framework.ops.Tensor], typin huggingface.co ๊ฑฐ์˜ ๋ชจ๋“  ์ž์—ฐ์–ด์ฒ˜๋ฆฌ ํ…Œ์Šคํฌ์—๋Š” ํŠธ๋žœ์Šคํฌ..

Word Embedding

์›Œ๋“œ ์ž„๋ฒ ๋”ฉ(Word Embedding)์€ ๋‹จ์–ด๋ฅผ ๋ฒกํ„ฐ๋กœ ํ‘œํ˜„ํ•˜๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. 1. Sparse Representation ์•ž์„œ ์›-ํ•ซ ์ธ์ฝ”๋”ฉ์„ ํ†ตํ•ด์„œ ๋‚˜์˜จ ์›-ํ•ซ ๋ฒกํ„ฐ๋“ค์€ ํ‘œํ˜„ํ•˜๊ณ ์ž ํ•˜๋Š” ๋‹จ์–ด์˜ ์ธ๋ฑ์Šค์˜ ๊ฐ’๋งŒ 1์ด๊ณ , ๋‚˜๋จธ์ง€ ์ธ๋ฑ์Šค์—๋Š” ์ „๋ถ€ 0์œผ๋กœ ํ‘œํ˜„๋˜๋Š” ๋ฒกํ„ฐ ํ‘œํ˜„ ๋ฐฉ๋ฒ•์ด์—ˆ์Šต๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ๋ฒกํ„ฐ ๋˜๋Š” ํ–‰๋ ฌ(matrix)์˜ ๊ฐ’์ด ๋Œ€๋ถ€๋ถ„์ด 0์œผ๋กœ ํ‘œํ˜„๋˜๋Š” ๋ฐฉ๋ฒ•์„ ํฌ์†Œ ํ‘œํ˜„(sparse representation)์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์›-ํ•ซ ๋ฒกํ„ฐ๋Š” ํฌ์†Œ ๋ฒกํ„ฐ(sparse vector)์ž…๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ํฌ์†Œ ๋ฒกํ„ฐ์˜ ๋ฌธ์ œ์ ์€ ๋‹จ์–ด์˜ ๊ฐœ์ˆ˜๊ฐ€ ๋Š˜์–ด๋‚˜๋ฉด ๋ฒกํ„ฐ์˜ ์ฐจ์›์ด ํ•œ์—†์ด ์ปค์ง„๋‹ค๋Š” ์ ์ž…๋‹ˆ๋‹ค. ๋‹จ์–ด์˜ ์ธ๋ฑ์Šค์— ํ•ด๋‹น๋˜๋Š” ๋ถ€๋ถ„๋งŒ 1์ด๊ณ  ๋‚˜๋จธ์ง€๋Š” 0์˜ ๊ฐ’์„ ๊ฐ€์ ธ์•ผ๋งŒ ํ•˜๋ฏ€๋กœ ๋‹จ์–ด ์ง‘ํ•ฉ์ด ํด์ˆ˜๋ก ๊ณ ์ฐจ์›์˜ ๋ฒกํ„ฐ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ๋ฒกํ„ฐ..

[์ž์—ฐ์–ด ์ฒ˜๋ฆฌ] ํŒŒ์ดํ† ์น˜ LSTM ๊ตฌํ˜„

์ด๋ฒˆ์—๋Š” ์‹ค์ œ๋กœ LSTM์„ ๊ตฌํ˜„ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. import torch import torch.nn as nn import torch.nn.functional as F import torch.optim as optim torch.manual_seed(1) `torch.nn` ์„ ํ™œ์šฉํ•˜์—ฌ LSTM cell ์„ ์ƒ์„ฑํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. * `input_size` : The number of expected features in the input x * `hidden_size` : The number of features in the hidden state h lstm = nn.LSTM(input_size, hidden_size) # input_size: 3, hidden_size: 3 ์œผ๋กœ ์„ค์ •ํ•˜์—ฌ L..

[์ž์—ฐ์–ด ์ฒ˜๋ฆฌ] RNN์„ ๋ณด์™„ํ•˜๋Š” LSTM๊ณผ GRU

https://getacherryontop.tistory.com/125 RNN ์ž์—ฐ์–ด์ฒ˜๋ฆฌ์—์„œ ํ† ํฐ์˜ ์ˆœ์„œ๊ฐ€ ๋งค์šฐ ์ค‘์š”ํ•ฉ๋‹ˆ๋‹ค. ๋‚˜๋Š” ์ง‘์— ๊ฐ„๋‹ค. (o) ๋‚˜๋Š” ๊ฐ„๋‹ค ์ง‘์— (x) ์ด๋Ÿฌํ•œ ํ† ํฐ์˜ ์ˆœ์„œ๋ฅผ ๊ณ ๋ คํ•˜๊ธฐ ์œ„ํ•ด RNN ํ˜•ํƒœ์˜ ๋ชจ๋ธ์ด ๊ฐœ๋ฐœ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. RNN์˜ ์˜๋ฏธ์™€ ๊ตฌ์กฐ ๋˜‘๊ฐ™์€ weight getacherryontop.tistory.com RNN์€ ์‹œํ€€์…œํ•œ ๋ฐ์ดํ„ฐ ์ž…๋ ฅ์— ํ›Œ๋ฅญํ•˜๊ฒŒ ๋™์ž‘ํ•˜์ง€๋งŒ, ๊ธฐ์–ต๋ ฅ์ด ์•ˆ์ข‹์Šต๋‹ˆ๋‹ค. time-step์ด ๊ธธ์–ด์งˆ์ˆ˜๋ก ์•ž์˜ ๋‚ด์šฉ์„ ๊นŒ๋จน๊ฒŒ ๋˜๋Š” ๊ฒƒ์ด์ฃ . ์ด๋ฅผ ๋ณด์™„ํ•˜๊ธฐ ์œ„ํ•ด LSTM์ด๋ผ๋Š” ๊ตฌ์กฐ๊ฐ€ ์ œ์‹œ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. LSTM(Long short-term memory) - ๋ณ„๋„์˜ Cell state ๋ณ€์ˆ˜๋ฅผ ํ†ตํ•ด ๊ธฐ์–ต์„ ํ•  ์ˆ˜ ์žˆ๋„๋กํ•ฉ๋‹ˆ๋‹ค. ์—ฌ๋Ÿฌ๊ฐ€์ง€ gate๋ฅผ ๋‘์–ด ๊ธฐ์–ต, ์žŠ์–ด๋ฒ„๋ฆฌ๊ธฐ, ์ถœ๋ ฅํ•  ๋ฐ..

[์ž์—ฐ์–ด ์ฒ˜๋ฆฌ] ํ† ํฐํ™”์™€ ํ† ํฌ๋‚˜์ด์ € ์ข…๋ฅ˜

์ž์—ฐ์–ด ์ฒ˜๋ฆฌ์—์„œ ํ† ํฐ์€ ์‚ฌ์šฉ์ž ์ง€์ • ๋‹จ์œ„์ž…๋‹ˆ๋‹ค. raw string์„ ํ† ํฐ ๋‹จ์œ„๋กœ ์ชผ๊ฐœ๋Š” ๊ฒƒ์„ ํ† ํฐํ™”๋ผ๊ณ  ํ•˜๊ณ , ์šฐ๋ฆฌ๋Š” ์ด ํ† ํฐ๋“ค๋กœ vocab ์‚ฌ์ „์„ ๋งŒ๋“ค๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ฆ‰, ํ† ํฐํ™”๋Š” ์ฃผ์–ด์ง„ ์ž…๋ ฅ ๋ฐ์ดํ„ฐ๋ฅผ ์ž์—ฐ์–ด์ฒ˜๋ฆฌ ๋ชจ๋ธ์ด ์ธ์‹ํ•  ์ˆ˜ ์žˆ๋Š” ๋‹จ์œ„๋กœ ๋ณ€ํ™˜ํ•ด์ฃผ๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด I am student๋ผ๋Š” ๋ฌธ์žฅ์€ ํ† ํฐํ™”๋ฅผ ๊ฑฐ์น˜๋ฉด I, am, student๋กœ ๋‚˜๋ˆ ์ง€๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. I am student=[I, am, student] ๊นŒ์ง€ ๋˜๋ฉด ์šฐ๋ฆฌ๋Š” ์ด ๋‹จ์–ด๋“ค์ด vocab ์‚ฌ์ „์—์„œ์˜ ์ธ๋ฑ์Šค๊ฐ’์œผ๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ๋งŒ์•ฝ I๊ฐ€ 10๋ฒˆ์งธ, am์ด 12๋ฒˆ์งธ, student๊ฐ€ 100๋ฒˆ์งธ์— ์œ„์น˜ํ•˜๊ณ  ์žˆ๋‹ค๋ฉด [I, am, student] =[10,12,100]์ด ๋˜๊ณ  ํ•ด๋‹น ๋ฒกํ„ฐ๋“ค์ด ๋ชจ๋ธ์˜ ์ธํ’‹์œผ๋กœ ๋“ค์–ด๊ฐ€๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ํ† ํฌ๋‚˜์ด์ € ํ† ํฐ..

[์ž์—ฐ์–ด ์ฒ˜๋ฆฌ] ๋งž์ถค๋ฒ• ์ „์ฒ˜๋ฆฌ ๊ต์ • Py-Hanspell ์˜ˆ์ œ

!pip install git+https://github.com/ssut/py-hanspell.git from hanspell import spell_checker sent = "์–ด์ œ๋Š” ์•Š์žค์–ด. ๊ฐ๊ธฐ ๋นจ๋ฆฌ ๋‚ณ์•„! " spelled_sent = spell_checker.check(sent) hanspell_sent = spelled_sent.checked print(hanspell_sent) ์–ด์ œ๋Š” ์•ˆ ์žค์–ด. ๊ฐ๊ธฐ ๋นจ๋ฆฌ ๋‚˜์•„!

[์ž์—ฐ์–ด ์ฒ˜๋ฆฌ]์ „์ฒ˜๋ฆฌ ๋„์–ด์“ฐ๊ธฐ ๊ต์ • ์ˆ˜์ • PyKoSpacing ์˜ˆ์ œ

!pip install git+https://github.com/haven-jeon/PyKoSpacing.git sentence = '์ฝ”๋”ฉ๊ณผ AI ๊ฐœ๋ฐœ์ด ๋‘˜๋‹ค ๊ฐ€๋Šฅํ•œ ์‚ฌ๋žŒ์€ ๋งŽ์ง€ ์•Š๋‹ค.' new = sentence.replace(" ", '') # ๋„์–ด์“ฐ๊ธฐ๊ฐ€ ์—†๋Š” ๋ฌธ์žฅ ์ž„์˜๋กœ ๋งŒ๋“ค๊ธฐ print(new) from pykospacing import Spacing spacing = Spacing() kospacing_sen = spacing(new) print('๋„์–ด์“ฐ๊ธฐ๊ฐ€ ์—†๋Š” ๋ฌธ์žฅ :\n', new) print('์ •๋‹ต ๋ฌธ์žฅ:\n', sentence) print('๋„์–ด์“ฐ๊ธฐ ๊ต์ • ํ›„:\n', kospacing_sen) ๋„์–ด์“ฐ๊ธฐ๊ฐ€ ์—†๋Š” ๋ฌธ์žฅ : ์ฝ”๋”ฉ๊ณผAI๊ฐœ๋ฐœ์ด๋‘˜๋‹ค๊ฐ€๋Šฅํ•œ์‚ฌ๋žŒ์€๋งŽ์ง€์•Š๋‹ค. ์ •๋‹ต ๋ฌธ์žฅ: ์ฝ”๋”ฉ๊ณผ AI ๊ฐœ๋ฐœ..

[์ž์—ฐ์–ด ์ฒ˜๋ฆฌ] ํ•œ๊ตญ์–ด ํ† ํฐํ™”, ํ’ˆ์‚ฌํƒœ๊น… ๊ตฌํ˜„ KoNLPy (Hannanum,Kkma),Khaiii

์„ค์น˜ !pip install konlpy ํ•œ๋‚˜๋ˆ”(Hannanum) from konlpy.tag import Hannanum hannanum = Hannanum() text = '์•ˆ๋…•ํ•˜์„ธ์š”! ์˜ค๋Š˜ ๋งŽ์ด ์ถ”์›Œ์š”' print(hannanum.morphs(text)) # Parse phrase to morphemes print(hannanum.nouns(text)) # Noun extractors print(hannanum.pos(text)) # POS tagger ['์•ˆ๋…•', 'ํ•˜', '์„ธ', '์š”', '!', '์˜ค๋Š˜', '๋งŽ', '์ด', '์ถฅ', '์–ด์š”'] ['์•ˆ๋…•', '์˜ค๋Š˜'] [('์•ˆ๋…•', 'N'), ('ํ•˜', 'X'), ('์„ธ', 'E'), ('์š”', 'J'), ('!', 'S'), ('์˜ค๋Š˜', 'N'..

๋ฐ˜์‘ํ˜•