[DL Basic] 딥러닝 기본 및 최적화

gromit·2022년 2월 9일
0
post-thumbnail

1. Deep Learning

딥러닝의 구분

  1. AI (인공지능)
    • Mimic human intelligence, 사람의 지능을 모방
  1. ML (기계학습)
    • Data-driven approach, 데이터를 기반으로 무언가를 (기계)학습
  1. DL (딥러닝)
    • Neural Networks, 그 안에서 뉴럴 네트워크를 사용하여 데이터를 가지고 무언가를 학습하는 세부 분야


딥러닝의 Key Components

  1. data
    • the model can learn from
  2. model
    • how to transform the data
  3. loss function
    • quantifies the bbadness of the model
  4. algorithm
    • to adjust the parameters to minimize the loss



기타

  • loss function을 줄이기만 하는 것이 목적이 아니라, 모델이 학습하지 않았던 테스트 데이터/실제 사용 환경에서 잘 동작하는 것이 목적..! -> 해당 parameter 값(weight, bias)을 찾는 것이 중요할 것
  • 모델의 layer를 깊게 쌓으면 문제가 생기는 이유 또한, train error는 적게 나오지만, test data에서 성능이 잘 안 나오기 때문 (+ 이는 ResNet 등장 후 보완이 됨)



Neural Networks

  • 인간의 뇌 구조를 모방한 구조
  • 뉴럴 네트워크는 함수를 근사하는 모델( Function Approximator )
  • 비선형 변환의 연속

Neural networks are function approximators that stack affine transformations followed by nonlinear transformations.



MLP(다층퍼셉트론) PyTorch로 구현해보기

- Evaluate Function 정의

  • Eval 과정은 with torch.no_grad(): 설정해줘야 함
  • 전체 Dataset에서 정의한 iterator통해, 돌면서 Batch Size만큼 뽑아내며 eval과정 처리
  • .to(device) 이용해 Batch Size만큼의 X 데이터/y 데이터로부터 예측 값/정답 값 정의
  • view() 함수 : 필요 시, reshape
def func_eval(model,data_iter,device):
    with torch.no_grad():
        model.eval() # evaluate (affects DropOut and BN)
        n_total,n_correct = 0,0

        for batch_in,batch_out in data_iter:
            y_trgt = batch_out.to(device)
            model_pred = model(batch_in.view(-1, 28*28).to(device))
            _,y_pred = torch.max(model_pred.data,1)
            n_correct += (y_pred == y_trgt).sum().item()
            n_total += batch_in.size(0)
        val_accr = (n_correct/n_total)
        model.train() # back to train mode 
    return val_accr
print ("Done")

- Train 코드

  • 전체 Dataset에서 정의한 iterator통해, 돌면서 Batch Size만큼 뽑아내며 train과정 처리

  • [1] 포워드 과정

    • (1)위에서 정의한 CustomModelforward(self, x) 함수와 (2)선택한loss 함수(크로스엔트로피 등)에 배치를 돌면서 피드포워드

  • [2] 옵티마이저 및 로스 업데이트 과정 (backpropagation)

    • (1)옵티마이저.zero_grad()로 값 리셋, (2)출력로스.backward()로 미분값 백워드, (3)옵티마이저.step()로 값 업데이트

- code

print ("Start training.")
M.init_param() # initialize parameters
M.train()
EPOCHS,print_every = 10,1

for epoch in range(EPOCHS):
    loss_val_sum = 0

    for batch_in,batch_out in train_iter:
        # Forward path
        y_pred = M.forward(batch_in.view(-1, 28*28).to(device))
        loss_out = loss(y_pred,batch_out.to(device))

        # Update
        optm.zero_grad()      # reset gradient 
        loss_out.backward()      # backpropagate
        optm.step()      # optimizer update
        
        loss_val_sum += loss_out
        
    loss_val_avg = loss_val_sum/len(train_iter)
    # Print
    if ((epoch%print_every)==0) or (epoch==(EPOCHS-1)):
        train_accr = func_eval(M,train_iter,device)
        test_accr = func_eval(M,test_iter,device)
        print ("epoch:[%d] loss:[%.3f] train_accr:[%.3f] test_accr:[%.3f]."%
               (epoch,loss_val_avg,train_accr,test_accr))
print ("Done")        



2. Optimization

  1. Generalization

    • 일반화 성능을 높이자
    • 학습 데이터 - 테스트 데이터 간 성능의 차이(Generalization Gap)가 적을 때를 지향
    • underfitting vs. overfitting 용어와 관련
  2. Cross-validations

    • 학습 데이터 / 테스트 데이터를 k개로 나누어서, (k-1)개로 training, 1개로 validation 일부만 학습에 사용
      • Cross-validations을 이용해 최적의 하이퍼 파라미터 값을 찾고, 이 하이퍼 파라미터 값을 가지고 모델을 학습하는 방법
  3. Bias와 Variance

    • 편향과 분산
    • bias와 variance는 Trade-off 관계를 갖는다.
    • Cost를 최소화하는 문제는, 즉, bias, variance, noise 세 가지를 최소화하는 것
  1. Bootstrapping
    • 학습 데이터가 고정되어 있을 때, 그 안에서 subsampling을 통해서 학습 데이터를 여러 개로 만들고, 이를 통해 만든 여러 개의 모델/metric을 통해, 전체 모델의 일치성/불확실성을 파악하는 접근법


선형회귀문제(음성신호 데이터) PyTorch로 optimizer 비교해보기

  • optimizers 정의
    • torch.optim라이브러리 활용해 선언
    • momentum 옵티마이저는 momentum=(확률값) 옵션 추가

- code

### 임포트 : import torch.optim as optim



LEARNING_RATE = 1e-2
# Instantiate models
model_sgd = Model(name='mlp_sgd',xdim=1,hdims=[64,64],ydim=1).to(device)
model_momentum = Model(name='mlp_momentum',xdim=1,hdims=[64,64],ydim=1).to(device)
model_adam = Model(name='mlp_adam',xdim=1,hdims=[64,64],ydim=1).to(device)

# Optimizers
loss = nn.MSELoss()
optm_sgd = optim.SGD(model_sgd.parameters(), lr=LEARNING_RATE)
optm_momentum = optim.SGD(model_momentum.parameters(), lr=LEARNING_RATE, momentum=0.9)
optm_adam = optim.Adam(model_adam.parameters(), lr=LEARNING_RATE)

print ("Done.")


- 💡 인사이트

  • Adam

    • Adam은 짧은 학습 시간부터 성능(정확도)이 확보됨
    • Adam에는 adaptive learning 개념이 반영된 접근법. (adaptive learning: 어떠한 파라미터에 대해서는 lr을 높이고, 다른 어떠한 파라미터에 대해서는 lr을 줄여나감)
    • 따라서, 똑같은 lr을 선언해주어도 더 빠르게 성능이 확보될 수 있다..! ✨

  • Momentum
    • Momentum은 학습 시간이 조금 길어지면 성능 확보됨. 반면, SGD는 같은 시간까지도 성능이 확보되지 않음
    • Momentum은 이전 배치의 gradient 정보를 활용해 현재 배치 턴에서 사용하겠다는 접근법
    • 이러한 점에서 미니배치일 때, SGD보다 좋다..! ✨

  • SGD
    • SGD는 데이터에서 큰 특징이 되는 파트 위주로 학습이 잘되고, 세부적으로는 잘 놓치는 모양새를 보임
    • 하지만, SGD는 오랜 학습 시간이 흘렀을 때에는 더 좋은 성능을 보일 순 있다.

  • 결론: Adam/rAdam을 초기에 사용하면 어느정도의 성능을 짧은 시간에 효율적으로 확보할 수 있다..! ✨
profile
AI, Big Data, Industrial Engineering

54개의 댓글

comment-user-thumbnail
2025년 5월 20일

i read a lot of stuff and i found that the way of writing to clearifing that exactly want to say was very good so i am impressed and ilike to come again in future..http://wwscc.org/evinfo/pages/1xbet_promo_code_for_registration___sign_up_bonus_india.html

답글 달기
comment-user-thumbnail
2025년 5월 20일

Double your deposit with Linebet’s promo code BNB777! Claim a 100% bonus up to €130. Enjoy free spins, sports bets, and exclusive rewards today.code promo linebet cote d'ivoire

답글 달기
comment-user-thumbnail
2025년 6월 12일

You make so many great points here that I read your article a couple of times. Your views are in accordance with my own for the most part. This is great content for your readers. 五反田 英語 アルバイト

답글 달기
comment-user-thumbnail
2025년 6월 17일

POL88 adalah situs slot online gacor terpercaya dengan RTP tinggi dan game dari provider ternama. Daftar sekarang untuk nikmati jackpot besar serta bonus harian yang banyak. slot gacor

답글 달기
comment-user-thumbnail
2025년 6월 22일

This article was written by a real thinking writer. I agree many of the with the solid points made by the writer. I’ll be back. jay rufer

답글 달기
comment-user-thumbnail
2025년 6월 22일

Keep up the good work , I read few posts on this web site and I conceive that your blog is very interesting and has sets of fantastic information. agenolx

답글 달기
comment-user-thumbnail
2025년 6월 22일

Really a great addition. I have read this marvelous post. Thanks for sharing information about it. I really like that. Thanks so lot for your convene. BATMAN138

답글 달기
comment-user-thumbnail
2025년 6월 23일

Thank you for such a well written article. It’s full of insightful information and entertaining descriptions. Your point of view is the best among many. DeepL

답글 달기
comment-user-thumbnail
2025년 6월 24일

This is my first time i visit here and I found so many interesting stuff in your blog especially it's discussion, thank you. sandibet

답글 달기
comment-user-thumbnail
2025년 6월 25일

A very awesome blog post. We are really grateful for your blog post. You will find a lot of approaches after visiting your post. hptoto alternatif

답글 달기
comment-user-thumbnail
2025년 6월 25일

A very awesome blog post. We are really grateful for your blog post. You will find a lot of approaches after visiting your post. pass prop firm

답글 달기
comment-user-thumbnail
2025년 6월 25일

A very awesome blog post. We are really grateful for your blog post. You will find a lot of approaches after visiting your post. toto login

답글 달기
comment-user-thumbnail
2025년 6월 26일

Thanks for a wonderful share. Your article has proved your hard work and experience you have got in this field. Brilliant .i love it reading. olxtoto

답글 달기
comment-user-thumbnail
2025년 6월 26일

Thanks for a wonderful share. Your article has proved your hard work and experience you have got in this field. Brilliant .i love it reading. situs togel terpercaya

답글 달기
comment-user-thumbnail
2025년 6월 26일

I’m going to read this. I’ll be sure to come back. thanks for sharing. and also This article gives the light in which we can observe the reality. this is very nice one and gives indepth information. thanks for this nice article... hptoto situs slot

답글 달기
comment-user-thumbnail
2025년 6월 26일

Thanks for a wonderful share. Your article has proved your hard work and experience you have got in this field. Brilliant .i love it reading. sandibet

답글 달기
comment-user-thumbnail
2025년 6월 28일

You make so many great points here that I read your article a couple of times. Your views are in accordance with my own for the most part. This is great content for your readers.olxtoto

답글 달기
comment-user-thumbnail
2025년 6월 28일

Excellent article. Very interesting to read. I really love to read such a nice article. Thanks! keep rocking. slot qris

답글 달기
comment-user-thumbnail
2025년 6월 28일

It was wondering if I could use this write-up on my other website, I will link it back to your website though.Great Thanks. Bulenox coupon code

답글 달기
comment-user-thumbnail
2025년 6월 28일

Hey what a brilliant post I have come across and believe me I have been searching out for this similar kind of post for past a week and hardly came across this. Thank you very much and will look for more postings from you.gaspol189

답글 달기
comment-user-thumbnail
2025년 6월 28일

It was wondering if I could use this write-up on my other website, I will link it back to your website though.Great Thanks. bandar toto macau

답글 달기
comment-user-thumbnail
2025년 6월 28일

Hey what a brilliant post I have come across and believe me I have been searching out for this similar kind of post for past a week and hardly came across this. Thank you very much and will look for more postings from you.M88

답글 달기
comment-user-thumbnail
2025년 6월 28일

I admire this article for the well-researched content and excellent wording. I got so involved in this material that I couldn’t stop reading. I am impressed with your work and skill. Thank you so much.situs toto

답글 달기
comment-user-thumbnail
2025년 6월 28일

I admire this article for the well-researched content and excellent wording. I got so involved in this material that I couldn’t stop reading. I am impressed with your work and skill. Thank you so much.situs toto

답글 달기
comment-user-thumbnail
2025년 6월 28일

I admire this article for the well-researched content and excellent wording. I got so involved in this material that I couldn’t stop reading. I am impressed with your work and skill. Thank you so much.situs toto

답글 달기
comment-user-thumbnail
2025년 6월 28일

I admire this article for the well-researched content and excellent wording. I got so involved in this material that I couldn’t stop reading. I am impressed with your work and skill. Thank you so much.situs toto

답글 달기
comment-user-thumbnail
2025년 6월 28일

I think this is an informative post and it is very useful and knowledgeable. therefore, I would like to thank you for the efforts you have made in writing this article.olxtoto

답글 달기
comment-user-thumbnail
2025년 6월 28일

Thanks for a very interesting blog. What else may I get that kind of info written in such a perfect approach? I’ve a undertaking that I am simply now operating on, and I have been at the look out for such info. keluaran toto macau

답글 달기
comment-user-thumbnail
2025년 6월 29일

Hey what a brilliant post I have come across and believe me I have been searching out for this similar kind of post for past a week and hardly came across this. Thank you very much and will look for more postings from you.keytoto

답글 달기
comment-user-thumbnail
2025년 6월 29일

Hey what a brilliant post I have come across and believe me I have been searching out for this similar kind of post for past a week and hardly came across this. Thank you very much and will look for more postings from you.keytoto

답글 달기
comment-user-thumbnail
2025년 6월 29일

Hey what a brilliant post I have come across and believe me I have been searching out for this similar kind of post for past a week and hardly came across this. Thank you very much and will look for more postings from you.olxtoto

답글 달기
comment-user-thumbnail
2025년 6월 29일

Thanks for a wonderful share. Your article has proved your hard work and experience you have got in this field. Brilliant .i love it reading. olxtoto link alternatif

답글 달기
comment-user-thumbnail
2025년 6월 29일

Thanks for a wonderful share. Your article has proved your hard work and experience you have got in this field. Brilliant .i love it reading. toto slot 777 login

답글 달기
comment-user-thumbnail
2025년 6월 29일

Hey what a brilliant post I have come across and believe me I have been searching out for this similar kind of post for past a week and hardly came across this. Thank you very much and will look for more postings from you.olxtoto

답글 달기
comment-user-thumbnail
2025년 6월 29일

This is my first time i visit here. I found so many interesting stuff in your blog especially its discussion. From the tons of comments on your articles, I guess I am not the only one having all the enjoyment here keep up the good worktoto togel

답글 달기
comment-user-thumbnail
2025년 6월 29일

This is my first time i visit here and I found so many interesting stuff in your blog especially it's discussion, thank you.toto 4d

답글 달기
comment-user-thumbnail
2025년 6월 29일

Thanks for a wonderful share. Your article has proved your hard work and experience you have got in this field. Brilliant .i love it reading. togel online

답글 달기
comment-user-thumbnail
7일 전

Thanks for a very interesting blog. What else may I get that kind of info written in such a perfect approach? I’ve a undertaking that I am simply now operating on, and I have been at the look out for such info.keluaran toto macau

답글 달기
comment-user-thumbnail
7일 전

I have read all the comments and suggestions posted by the visitors for this article are very fine,We will wait for your next article so only.Thanks! olxtoto

답글 달기

When your website or blog goes live for the first time, it is exciting. That is until you realize no one but you and your.kantorbola slot

답글 달기
comment-user-thumbnail
6일 전

Positive site, where did u come up with the information on this posting? I'm pleased I discovered it though, ill be checking back soon to find out what additional posts you include.situs toto 4d

답글 달기
comment-user-thumbnail
5일 전

It was wondering if I could use this write-up on my other website, I will link it back to your website though.Great Thanks. olxtoto login

답글 달기
comment-user-thumbnail
5일 전

Thanks For sharing this Superb article.I use this Article to show my assignment in college.it is useful For me Great Work.toto macau

답글 달기
comment-user-thumbnail
5일 전

Hey what a brilliant post I have come across and believe me I have been searching out for this similar kind of post for past a week and hardly came across this. Thank you very much and will look for more postings from you. olxtoto

답글 달기
comment-user-thumbnail
5일 전

Hey what a brilliant post I have come across and believe me I have been searching out for this similar kind of post for past a week and hardly came across this. Thank you very much and will look for more postings from you. jpdewa

답글 달기
comment-user-thumbnail
5일 전

It was wondering if I could use this write-up on my other website, I will link it back to your website though.Great Thanks. slot dax69

답글 달기
comment-user-thumbnail
5일 전

I haven’t any word to appreciate this post.....Really i am impressed from this post....the person who create this post it was a great human..thanks for shared this with us. olxtoto link alternatif

답글 달기

Wow, What a Excellent post. I really found this to much informatics. It is what i was searching for.I would like to suggest you that please keep sharing such type of info.Thankssitus slot

답글 달기
comment-user-thumbnail
4일 전

Wow, excellent post. I'd like to draft like this too - taking time and real hard work to make a great article. This post has encouraged me to write some posts that I am going to write soon. SoundCloud MP3

답글 달기
comment-user-thumbnail
4일 전

I wanted to thank you for this great read!! I definitely enjoying every little bit of it I have you bookmarked to check out new stuff you post. olxtoto link

답글 달기
comment-user-thumbnail
4일 전

I wanted to thank you for this great read!! I definitely enjoying every little bit of it I have you bookmarked to check out new stuff you post. toto macau

답글 달기
comment-user-thumbnail
4일 전

Great job for publishing such a beneficial web site. Your web log isn’t only useful but it is additionally really creative too.slot dax69

답글 달기
comment-user-thumbnail
4일 전

Positive site, where did u come up with the information on this posting? I'm pleased I discovered it though, ill be checking back soon to find out what additional posts you include. bandar togel terpercaya

답글 달기
comment-user-thumbnail
3일 전

Thanks for a very interesting blog. What else may I get that kind of info written in such a perfect approach? I’ve a undertaking that I am simply now operating on, and I have been at the look out for such info.olxtoto togel login

답글 달기