KoGPT2-FineTuning
KoGPT2-FineTuning copied to clipboard
๐ฅ Korean GPT-2, KoGPT2 FineTuning cased. ํ๊ตญ์ด ๊ฐ์ฌ ๋ฐ์ดํฐ ํ์ต ๐ฅ
KoGPT2-FineTuning
SKT-AI์์ ์ฝ 20GB์ ํ๊ตญ์ด ๋ฐ์ดํฐ๋ฅผ Pre-Training ์ํจ KoGPT2๋ฅผ ์ฌ์ฉํ์ต๋๋ค. ์ฒซ ๋ฒ์งธ๋ก ๊ฐ์ฌ ์์ฌ๋ฅผ ์ํด์, ์ ์๊ถ์ด ๋ง๋ฃ๋ ์ ์ ๋ ๊ฐ์ฌ ๋ฐ์ดํฐ, ์์ค, ๊ธฐ์ฌ ๋ฑ์ Data๋ณ๋ก weight๋ฅผ ๋ค๋ฅด๊ฒ ์ฃผ๋ฉฐ Fine-tuning ํ์์ต๋๋ค. ๋ํ ์ฅ๋ฅด๋ ๋ฐ์์ ์์ ์ฅ๋ฅด๋ณ ๊ฐ์ฌ ํ์ต ๊ฒฐ๊ณผ๋ฅผ ๋ณผ ์ ์์ต๋๋ค.
๋ํ Colab์์๋ ์ํํ ํ์ต์ ์ํด์ Google Drive์ Dropbbox์ ์ฐ๋ํ์ต๋๋ค. ํ์ตํ ์ค๊ฐ ๊ฒฐ๊ณผ๋ฅผ Google Drive์์ Dropbbox๋ก ์ด๋์ํจ ํ, Google Drive์์ ํด๋น ๊ฒฐ๊ณผ๋ฅผ ์ญ์ ํ๊ฒ ํฉ๋๋ค. ์ด์ ๊ด๋ จ๋ Code
์์ ์ฅ๋ฅด๋ณ๋ก, CSV ํ์์ Dataset์ ๋ฐ๋ ๋ฐ๋ Version 2์ Code๋ก KoGPT2-FineTuning ์์ ์ ํ๊ธฐ ์ด๋ ต๋ค๋ฉด, Version 1.1์ ์ด์ฉํ๊ธธ ๋ฐ๋๋๋ค.
์๋์์, ๋ค์ํ ํ๊ตญ์ด ๊ฐ์ฌ๋ฅผ ํ์ตํ ๊ฒฐ๊ณผ๋ฅผ ํ์ธ ํ ์ ์์ต๋๋ค. ์ฐ๋ฆฌ๋ ์ด์ธ์๋ ๋ค์ํ ํ๋ก์ ํธ๋ฅผ ์งํํ ๊ฒ์ ๋๋ค.
Sample
Data structure
weight | Genre | lyrics |
---|---|---|
1100.0 | ๋ฐ๋ผ๋ | '๋ด ๋ง์ ์์์์\n\n\n๋ฐ๋ก์ฒ๋ผ ๋ฉํ๋ ์ ์๋ ๋ชจ์ต๋ง\n\n\n๋ฐ๋ผ๋ณด๋ค\n\n\nํฌ๊ธฐํ ์ ๋ฐ์ ์์ด์...' |
... |
3x200000
Fine Tuning
python main.py --epoch=200 --data_file_path=./dataset/lyrics_dataset.csv --save_path=./checkpoint/ --load_path=./checkpoint/genre/KoGPT2_checkpoint_296000.tar --batch_size=1
parser
parser.add_argument('--epoch', type=int, default=200,
help="epoch ๋ฅผ ํตํด์ ํ์ต ๋ฒ์๋ฅผ ์กฐ์ ํฉ๋๋ค.")
parser.add_argument('--save_path', type=str, default='./checkpoint/',
help="ํ์ต ๊ฒฐ๊ณผ๋ฅผ ์ ์ฅํ๋ ๊ฒฝ๋ก์
๋๋ค.")
parser.add_argument('--load_path', type=str, default='./checkpoint/Alls/KoGPT2_checkpoint_296000.tar',
help="ํ์ต๋ ๊ฒฐ๊ณผ๋ฅผ ๋ถ๋ฌ์ค๋ ๊ฒฝ๋ก์
๋๋ค.")
parser.add_argument('--samples', type=str, default="samples/",
help="์์ฑ ๊ฒฐ๊ณผ๋ฅผ ์ ์ฅํ ๊ฒฝ๋ก์
๋๋ค.")
parser.add_argument('--data_file_path', type=str, default='dataset/lyrics_dataset.txt',
help="ํ์ตํ ๋ฐ์ดํฐ๋ฅผ ๋ถ๋ฌ์ค๋ ๊ฒฝ๋ก์
๋๋ค.")
parser.add_argument('--batch_size', type=int, default=8,
help="batch_size ๋ฅผ ์ง์ ํฉ๋๋ค.")
Use Colab
Colab์ ์ด์ฉํด์ Fine-tuning Code๋ฅผ ์คํํ ์ ์์ต๋๋ค.
Runtime Disconnection Prevention
function ClickConnect() {
// ๋ฐฑ์๋๋ฅผ ํ ๋นํ์ง ๋ชปํ์ต๋๋ค.
// GPU์ด(๊ฐ) ์๋ ๋ฐฑ์๋๋ฅผ ์ฌ์ฉํ ์ ์์ต๋๋ค. ๊ฐ์๊ธฐ๊ฐ ์๋ ๋ฐํ์์ ์ฌ์ฉํ์๊ฒ ์ต๋๊น?
// ์ทจ์ ๋ฒํผ์ ์ฐพ์์ ํด๋ฆญ
var buttons = document.querySelectorAll("colab-dialog.yes-no-dialog paper-button#cancel");
buttons.forEach(function(btn) {
btn.click();
});
console.log("1๋ถ ๋ง๋ค ๋ค์ ์ฐ๊ฒฐ");
document.querySelector("#top-toolbar > colab-connect-button").click();
}
setInterval(ClickConnect,1000*60);
Clear the screen every 10 minutes
function CleanCurrentOutput(){
var btn = document.querySelector(".output-icon.clear_outputs_enabled.output-icon-selected[title$='ํ์ฌ ์คํ ์ค...'] iron-icon[command=clear-focused-or-selected-outputs]");
if(btn) {
console.log("10๋ถ ๋ง๋ค ์ถ๋ ฅ ์ง์ฐ๊ธฐ");
btn.click();
}
}
setInterval(CleanCurrentOutput,1000*60*10);
GPU Memory Check
nvidia-smi.exe
generator
python generator.py --temperature=1.0 --text_size=1000 --tmp_sent=""
ํ์ ์์
python generator.py --temperature=5.0 --text_size=500 --tmp_sent=""
parser
parser.add_argument('--temperature', type=float, default=0.7,
help="temperature ๋ฅผ ํตํด์ ๊ธ์ ์ฐฝ์์ฑ์ ์กฐ์ ํฉ๋๋ค.")
parser.add_argument('--top_p', type=float, default=0.9,
help="top_p ๋ฅผ ํตํด์ ๊ธ์ ํํ ๋ฒ์๋ฅผ ์กฐ์ ํฉ๋๋ค.")
parser.add_argument('--top_k', type=int, default=40,
help="top_k ๋ฅผ ํตํด์ ๊ธ์ ํํ ๋ฒ์๋ฅผ ์กฐ์ ํฉ๋๋ค.")
parser.add_argument('--text_size', type=int, default=250,
help="๊ฒฐ๊ณผ๋ฌผ์ ๊ธธ์ด๋ฅผ ์กฐ์ ํฉ๋๋ค.")
parser.add_argument('--loops', type=int, default=-1,
help="๊ธ์ ๋ช ๋ฒ ๋ฐ๋ณตํ ์ง ์ง์ ํฉ๋๋ค. -1์ ๋ฌดํ๋ฐ๋ณต์
๋๋ค.")
parser.add_argument('--tmp_sent', type=str, default="์ฌ๋",
help="๊ธ์ ์์ ๋ฌธ์ฅ์
๋๋ค.")
parser.add_argument('--load_path', type=str, default="./checkpoint/Alls/KoGPT2_checkpoint_296000.tar",
help="ํ์ต๋ ๊ฒฐ๊ณผ๋ฌผ์ ์ ์ฅํ๋ ๊ฒฝ๋ก์
๋๋ค.")
Use Colab
Colab์ ์ด์ฉํด์ generator๋ฅผ ์คํํ ์ ์์ต๋๋ค.
tensorboard
ํ์ต์ ๋ฐ๋ฅธ ๋ณํ๋ฅผ ํ์ธํ๊ธฐ ์ํด์, tensorboard๋ก ์ ๊ทผํ์ฌ loss์ text๋ฅผ ํ์ธํฉ๋๋ค.
tensorboard --logdir=runs
loss
text
Citation
@misc{KoGPT2-FineTuning,
author = {gyung},
title = {KoGPT2-FineTuning},
year = {2020},
publisher = {GitHub},
journal = {GitHub repository},
howpublished = {\url{https://github.com/gyunggyung/KoGPT2-FineTuning}},
}
Output
์์ธํ ๊ฒฐ๊ณผ๋ฌผ์ samples์์ ํ์ธ ํ ์ ์์ต๋๋ค. ํ์ต์ ๋ํด์๋ ๊ด๋ จ ํฌ์คํ ์์ ํ์ธํ ์ ์์ต๋๋ค.
Reference
https://github.com/openai/gpt-2
https://github.com/nshepperd/gpt-2
https://github.com/SKT-AI/KoGPT2
https://github.com/asyml/texar-pytorch/tree/master/examples/gpt-2
https://github.com/graykode/gpt-2-Pytorch
https://gist.github.com/thomwolf/1a5a29f6962089e871b94cbd09daf317
https://github.com/shbictai/narrativeKoGPT2
https://github.com/ssut/py-hanspell
https://github.com/likejazz/korean-sentence-splitter