Sejong-Kaggle-Challengers/MAIN

[데이콘 박막 두께 경진대회 - pytorch custom 에 대해서]

Opened this issue · 0 comments

# custom batchsampler
class BatchSampler(Sampler):
    # sampler batch_size drop_last 필요.  __iter__과 __len__ 수정.
    def __init__(self, sampler, batch_size, drop_last):
        self.sampler = sampler
        self.batch_size = batch_size
        self.drop_last = drop_last

    def __iter__(self):
        batch = []
        for _, idx in enumerate(iter(self.sampler)):
            batch = idx
            yield batch

        if len(batch) > 0 and not self.drop_last:
            yield batch

    def __len__(self):
        return len(self.sampler) // self.batch_size

# custom model
class net(nn.Module):
    # __init__ 과 forward 정의 
    def __init__(self):
        super.__init__()
        self.avg=nn.AvgPool1d(kernel_size=3,stride=3,padding=1)
        self.linear=nn.Sequential(
            nn.Linear(76, 768),
            nn.BatchNorm1d(768),
            nn.ReLU(),
            nn.Linear(768, 768),
            ...
            nn.BatchNorm1d(768),
            nn.ReLU(),
            nn.Linear(768, 4)
        )
    def forward(self,x):
          x=x.view(x.shape[0],1,-1)
          x=self.avg(x)
          x=x.view(x.shape[0],-1)
          x=self.linear(x)
          return x

# custom dataset 
class PandasDataset(Dataset):
    # __init__ 과 __len__ __getitem__ 보면 꼭 줘야하는 parameter가 있다.
    def __init__(self, path):
        super(PandasDataset, self).__init__()
        train = pd.read_csv(path).iloc[:,1:]
        self.train_X, self.train_Y = train.iloc[:,4:], train.iloc[:,0:4]
        self.tmp_x , self.tmp_y = self.train_X.values, self.train_Y.values
    
    def __len__(self):
        return len(self.train_X)

    def __getitem__(self, idx):
        return {
            'X':torch.from_numpy(self.tmp_x)[idx],
            'Y':torch.from_numpy(self.tmp_y)[idx]
        }

# seed 고정
def set_seed(seed,mode=None):
    torch.manual_seed(seed)
    torch.backends.cudnn.deterministic=True
    torch.backends.cudnn.benchmark=False
    np.random.seed(seed)
    random.seed(seed)
    if mode=='reproductibility':
        torch.cuda.manual_seed(seed)
        torch.cuda.manual_seed_all(seed)
        
set_seed(71,mode='reproductibility')

loss 에 따른 custom sampler와 전체적인 pytorch 의 class 구현으로 편하게 훈련 진행하는 방법
링크