티스토리 뷰

08 강 ) Pytorch DataLoader

 

 

#training cycle 

for epoch in range ( training_epochs):    // 한 batch size 만큼 epoch 이 돌아감

  #loop over all batches

   for i in range(total_batch):

 

설명 : 

one epoch = one forward pass and one backward pass of all the training example

( 모든 training examples 가 학습이 한번 다 된 것 , 학습 = forward + backward ) 

batch size = the number of training examples in one forward/backward pass. Thr higer the batch size, the more memory space you'll need.  ( 한번에 메모리에 load 되어서 계산되는 data 의 갯수)

number of iteration = number of passes, each pass using [batch size] number of examples  ( batch size 만큼 몇번을 가져와야 하는지만 고려라면 됨, pass 한번 할 때 batch size 만큼의 number of example 을 같이 학습 하는 거 같음 ) To be clear, one pass = one forward pass + one backward pass ( we do not count the forward pass and backward pass as two different passes )

 

예) if you have 1000 training examples, and your batch size is 500, then it will take two iterations to complete 1 epoch.

 

그런데 data 에서 bach szie 를 나누는 과정 은 pytorch 에서 iterable 변수를 사용하면 따로 생각해 줄 필요가없다.

알아서 batch 를 나눠주고 결정해준다.  ( 출처 : youtu.be/zN49HdDxHi8

 

for i, data in enumerate (train_loader, 0):  // train loader is iterable, index 가 필요할 경우에 enumerate 사용.

     # get the inputs

     inputs, labels = data

 

      #wrap them in Varable

       imputs, lables = Variable( inputs ), Variable( labels )   // using these as the training cycle.

   

      # Run your training process

 

       print( epoch, i, "inputs", input.data, "labels", labels.data ) 

 

 

      

custom dataloader  -> 그러니까 이걸 통해서 하나의 batch 를 만들어주는 것

 

class DiabetedDataset(Dataset):

      """ Diabetes dataset. """

 

        #initializ your data, download, etc

        def __init__(self):   //1. download, read adata , etc

              xy = np.loadtxt('data-diabetes.csv', delimiter=',' , dtype=np.float32)    // numpy 에서 data파일과, 이런 다양한 parameer 들을 정해줄 수 있는 것.

              self.len = xy.shape[0]  // row 는 0 , column 은 1

              self.x_data = torch.from_numpy(xy[ : , 0:-1])

              self.y_data = torch.from_numpy(xy[ : , [-1]])   column 은 -1 하나

 

        def __getitem__(self, index)  // 2.  return one item on the index

              return self.x_data[index], self.y_data[index]

 

        def __len__(self):  // 3.  return the data length

              return self.len

 

// 여기까지가 한 data 만큼의 정보 ( 예를 들어 사진 한 장, 인것 )

 

dataset = DiabetesDataset( )  // data 를 load 하고 읽을 수 있는 class 로 만든 dataset 이다.

train_loader = DataLoader (dataset =dataset,   //이만큼의 data 를 32 만큼 가져온다는 건가?

                                      batch_size =32,   // 32 개의 data 를 batch 로 정해서 이만큼을 data load 해온다는 말

                                      shuffle = True  // data 를 섞는걸 말하는것 같고

                                      num_workers=2 ) // cpu 나 gpu 의 갯수를 말하는 것 

 

 

  // 여기에서의 train_loader 는, inside of the train for loop 에서 사용된다.

 

class Model (torch.nn.Module ) : // 이 Module 은 사용자 정의를 위한 class

     def __init__(self) :

           """ 

              In the constructor we instantiate two nn.Linear module

           """  // perceptron 이니까 첨에는 linear 로 하고 ( weight 를 수정해 나가는 것 ) + 시그모이드 함수 써서 결과값을 만든다.

            super( Model, self ).__init__()   // 인자로 자기자신 class 혹은, 부모나 조상 class 가 들어갈 수 있음.

            self.l1 = torch.nn.Linear(8, 6)   // 첫번째 인자 : input , 두번째 인자 : output

            sellf.l2 = torch.nn.Linear (6, 4)  // applies linear transformation to incomming data

            self.l3 = torch.nn.Linear (4,1여기는 왜 들어가는거지? 

 

            self.sigmoid = torch.nn.Sigmiod()  // sigmoid 함수 설정

 

 

    def forward (self, x ) :

             """

             In the forward function we accept a Variable of input data and  we must retrun a Variable of output                    data. We can use Modules defined in the constructor as well as arbitrary opeerator on variables.

               """

             out1 = self.sigmoid(self.l1(x))    // 과정이 , linear model -> sigmoid , output 이 다시 linear model -> sigmoid 이런 식으로  번의 out 이 존재 하니까 2 layer 라고 표현한것 아닐까?

             out2 = self.sigmoid(self .l2(out1))

             y_pred = self.sigmoid(self.13(out2)) 

             retrun y_perd   // 그렇게 예측 값을 return 한다.

 

// 여기까지가 모델 만들 수 있는 class 라고 보면 되나?

 

 

 

 

# our model   optimizer 와 loss 를 정하는구간, optimizer 는 오차의 최저점을 찾아가는 역할을 한다. -> 보통 Gradient descentdent 방법을 사용한다.  loss 는 

model = Model()

 

#construct our loss function and an Optimizer. The call to model. parameters( )

#in the SGD constructor will contain the learnable parameters of the two

#nn.Linear modules which are member of the model.

criterion = torch.nn.BCELoss(size_average=True)

optimizer = torch.optim.SGD(model.parameters( ), lr=0.1)   ( w+ = w -  n*dE/dw )  -> n 은 learning rate 

dE/dw 는 어떤 방향으로 학습할지  -> 러닝rate 정해주면은 이거대로 optimize 된.... 값을.....구해준다는건가....

 

#Training loop

for epoch in range(2):

   for i, data in enumerate (train_loader, 0):  //parateters : iterable, start   한 batch size 만큼 돌린다는 얘기?

         # get the inputs

          inputs, labels = data  // batch size 안에서의 data 하나의 inputs 과 labels 를 넣는 것.

 

         # wrap them in Variable

          inputs, labels = Variable(inputs), Variable(labels)  // 이것들을 variable 로 만드는 것.

 

          #forward pass : compute predicted y by passing x to model

            y_pred = model (inputs)   // 모델에 데이터를 넣어서 예측값을 만든다. 

 

           #compute and print loss

            loss = criterion (y_pred, labels)  // 모델을 통해서 만들어진 예측값과, data 에서의 label ( 이두개는어떻게 data 넣으면 알아서 split 해주지?)  을 criterion 함수에 넣고,  loss 를 구한다.

            print(epoch, i , loss.data[0] )

 

            # zero gradients, perform a backward pass, and update the weights .

             optimizer.zero_grad()  // In PyTorch, we need to set the gradients to zero before starting to do backpropragation because PyTorch accumulates the gradients on subsequent backward passes. back 할 때 gradient 가 누적된다는 말

             loss.backward()  // 왜해

             optimizer.step()   // update 를 해줌

 

 

 

Super 설명: ( 인자로 자기자신이나, parents 나 조상이 들어갈 수 있다. ) 

class A:

    def f(self): print('A')

class B(A):

    def f(self): print('B')

class C(B):

     def f(self): print('C')

 

c = C()

c.f()

super(C, c).f()

super(B, c).f()

 

// 결과

C

B

A

 

weight ( 가중치 ) 가 뭔지 또 생각이 안나서...

https://sacko.tistory.com/10

 

문과생도 이해하는 딥러닝 (1) - 퍼셉트론 Perceptron

퍼셉트론 Perceptron 문과생도 이해하는 딥러닝 (1) 딥러닝이라는 말이 학계, 업계 어디든 할 것 없이 엄청난 화두이다. 그래도 아직까지는 기계학습이면 충분하지만 점점 더 인공지능과 관련된 신�

sacko.tistory.com

 

 

criterion 이란 어떤 loss 를 줄일것인지에 대해서 명시한다.

pytorch 는 이미 있는 loss 중에서 선택할 수 있다.

 

criterion = torch.nn.MSELoss(size_average=False) # MSELoss는 Mean Squared Error Loss의 줄임말로, 원본과 예측의 차이의 제곱의 평균을 구해준다는 의미를 가진다. # size_average를 false로 하면 Loss를 발생시키는 것들의 평균으로 나누지 않는다.

 

optimizer = torch.optim.SGD(model.parameters(),lr=0.01) #이전 글에서는 optimizer 없이 직접 그냥 뺀 값을 적용 해 주었는데, #이번에는 그 과정 대신 optimizer을 사용한다. #model.parameters()를 통해서 #우리가 만든 모델의 파라미터들을 옵티마이저에 전달해주면, #우리가 이전에 gradient를 사용해서 업데이트하던 #w = w - grad * learning rate 식 같은 것을 자동으로 해 준다. #단순히 빼기만 해서 하는게 아닌 SGD(stochastic gradient descent) 라는 #방법을 써서 optimizing을 진행한다. 자세한건 구글링해보자. #lr = 0.01로 learning rate를 정해줄 수 있다.

출처: https://wingnim.tistory.com/30 [jinyo의 뇌]

 

옵티마이저가 잘 이해가 안돼서 다시,  -> 학습 속도를 빠르게 하고 안정적이게 한다.  -> optimizer 들이 오차의 최저점을 찾는다.  

   

댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/04   »
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30
글 보관함