Skip to content

๐ŸŒฑCNN, GAN, RNN, DQN, Autoencoder, ResNet, Seq2Seq, Adversarial Attack IN PYTORCH

Notifications You must be signed in to change notification settings

dnwjddl/pytorch-in-DeepLearning

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

68 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

ALGORITHM IN PYTORCH

๋ชฉ์ฐจ

[1] ANN

  1. ANN-tensor
  2. ANN-Autograd
  3. ANN-NeuralNetwork

[2] DNN

data augmentation๊ณผ dropout์„ ์ด์šฉํ•œ ์„ฑ๋Šฅ ๋†’์ด๊ธฐ(๊ณผ์ ํ•ฉ ์ค„์ด๊ธฐ)

  1. DNN-FashionMNIST

[3] CNN

convolution filter์„ ์‚ฌ์šฉํ•œ ์ด๋ฏธ์ง€ ์ฒ˜๋ฆฌ

  1. CNN-FashionMNIST
  • convolution Layer : ์ด๋ฏธ์ง€ ํŠน์ง• ์ถ”์ถœ
  • pooling Layer : ํ•„ํ„ฐ๋ฅผ ๊ฑฐ์นœ ์—ฌ๋Ÿฌ ํŠน์ง• ์ค‘ ๊ฐ€์žฅ ์ค‘์š”ํ•œ ํŠน์ง• ํ•˜๋‚˜๋ฅผ ๊ณ ๋ฅด๊ธฐ (๋œ ์ค‘์š”ํ•œ ํŠน์ง•์„ ๋ฒ„๋ฆฌ๊ธฐ ๋•Œ๋ฌธ์— ์ด๋ฏธ์ง€ ์ฐจ์›์ด ์ถ•์†Œ)

[4] ResNet(CNN)

์ปจ๋ณผ๋ฃจ์…˜ ์ปค๋„์„ ์—ฌ๋Ÿฌ๊ฒน ๊ฒน์น˜๋ฉฐ ๋ณต์žกํ•œ ๋ฐ์ดํ„ฐ์—๋„ ์‚ฌ์šฉ๊ฐ€๋Šฅ

  1. ResNet-CIFAR10
  • shortcut ๋ชจ๋“ˆ์€ ์ฆํญํ• ๋•Œ๋งŒ ๋”ฐ๋กœ ๊ฐ–๊ฒŒ ๋จ
class BasicBlock(nn.Module):
    ...
    def forward(self,x):
        out = F.relu(self.bn1(self.conv1(x)))
        out = self.bn2(self.conv2(out))
        
        out += self.shortcut(x)
        out = F.relu(out)
        return out
        
class ResNet(nn.Module):
    ...
    def _make_layer(self, planes, num_blocks, stride):
        strides = [stride] + [1]*(num_blocks - 1)
        layers = []
        for stride in strides:
            layers.append(BasicBlock(self.in_planes, planes, stride))
            self.in_planes = planes
        return nn.Sequential(*layers)
     ...

[5] Autoencoder

์‚ฌ๋žŒ์˜ ์ง€๋„ ์—†์ด ํ•™์Šต(encoder + decoder), ์ž ์žฌ๋ณ€์ˆ˜ ์‹œ๊ฐํ™”, ์žก์Œ์„ ํ†ตํ•˜์—ฌ ํŠน์ง• ์ถ”์ถœ ์šฐ์„ ์ˆœ์œ„ ํ™•์ธ

  1. AutoEncoder

[6] RNN : ์ˆœ์ฐจ์  ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ(์˜ํ™” ๋ฆฌ๋ทฐ ๊ฐ์ • ๋ถ„์„ & ๊ธฐ๊ณ„ ๋ฒˆ์—ญ)

  1. GRU-TetClassification
  • tokenizing, word dictionary, word embedding
  • RNN์˜ gradient vanishing์„ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•˜์—ฌ GRU ์‚ฌ์šฉ
    • update gate ์ด์ „ ์€๋‹‰ ๋ฒกํ„ฐ๊ฐ€ ์ง€๋‹Œ ์ •๋ณด๋ฅผ ์ƒˆ๋กœ์šด ์€๋‹‰ ๋ฒกํ„ฐ๊ฐ€ ์–ผ๋งˆ๋‚˜ ์œ ์ง€ํ• ์ง€
    • reset gate ์ƒˆ๋กœ์šด ์ž…๋ ฅ์ด ์ด์ „ ์€๋‹‰ ๋ฒกํ„ฐ์™€ ์–ด๋–ป๊ฒŒ ์กฐํ•ฉํ•˜๋Š”์ง€ ๊ฒฐ์ •
  1. RNN-Seq2Seq
  • ์ธ์ฝ”๋” RNN + ๋””์ฝ”๋” RNN

โœ” Teacher Forcing

  • ๋งŽ์€ ๋ฐ์ดํ„ฐ์—์„œ๋Š” ๋””์ฝ”๋”๊ฐ€ ์˜ˆ์ธกํ•œ ํ† ํฐ์„ ๋‹ค์Œ ๋ฐ˜๋ณต์—์„œ ์ž…๋ ฅ๋  ํ† ํฐ์œผ๋กœ ๊ฐฑ์‹ ํ•ด์ฃผ๋Š” ๊ฒƒ์ด ์ •์„
  • ํ•˜์ง€๋งŒ ํ•™์Šต์ด ์•„์ง ๋˜์ง€ ์•Š์€ ์ƒํƒœ์˜ ๋ชจ๋ธ์€ ์ž˜๋ชป๋œ ์˜ˆ์ธก ํ† ํฐ์„ ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉ๋  ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ, Teacher Forcing ์‚ฌ์šฉ
  • ๋””์ฝ”๋” ํ•™์Šต ์‹œ ์‹ค์ œ ๋ฒˆ์—ญ๋ฌธ์˜ ํ† ํฐ์„ ๋””์ฝ”๋”์˜ ์ „ ์ถœ๋ ฅ ๊ฐ’ ๋Œ€์‹  ์ž…๋ ฅ์œผ๋กœ ์‚ฌ์šฉํ•ด ํ•™์Šต์„ ๊ฐ€์†ํ•˜๋Š” ๋ฐฉ๋ฒ•
  • ๋ฒˆ์—ญ๋ฌธ์˜ i๋ฒˆ์งธ ํ† ํฐ์— ํ•ด๋‹นํ•˜๋Š” ๊ฐ’ targets[i] ๋ฅผ ๋””์ฝ”๋”์˜ ์ž…๋ ฅ๊ฐ’์œผ๋กœ ์„ค์ •
def __init__(self, vocab_size, hidden_size):
    ...
    self.embedding = nn.Embedding(vocab_size, hidden_size)
    self.encoder = nn.GRU(hidden_size, hidden_size)
    self.decoder = nn.GRU(hidden_size, hidden_size)
    self.project = nn.Linear(hidden_size, vocab_size)
    
def forward(self, inputs, targets):
    ...
    embedding = self.embedding(inputs).unsqueeze(1) #์ž„๋ฒ ๋”ฉ
    encoder_output, encoder_state = self.encoder(embedding, initial_state)  
    # encoder_state: ๋ฌธ๋งฅ๋ฒกํ„ฐ >> decoder์—์„œ ์ฒซ๋ฒˆ์งธ ์€๋‹‰๋ฒกํ„ฐ๋กœ ์“ฐ์ž„
    # encoder_output: y >> ์‹ค์ œ output ๊ฐ’
    decoder_state = encoder_state
    ...
    decoder_output, decoder_state = self.decoder(decoder_input, decoder_state)
    
    # ๋””์ฝ”๋”์˜ ์ถœ๋ ฅ๊ฐ’์œผ๋กœ ๋‹ค์Œ ๊ธ€์ž ์˜ˆ์ธกํ•˜๊ธฐ
    projection = self.project(decoder_output)
    outputs.append(projection)
    
    #ํ”ผ์ฒ˜ ํฌ์‹ฑ์„ ์ด์šฉํ•œ ๋””์ฝ”๋” ์ž…๋ ฅ ๊ฐฑ์‹ 
    decoder_input = torch.LongTensor([targets[i]])
    
    ...

[7] ์ ๋Œ€์  ๊ณต๊ฒฉ

FGSM ๊ณต๊ฒฉ
1.AdversialAttack

[8] GAN

์ƒˆ๋กœ์šด ์ด๋ฏธ์ง€ ์ƒ์„ฑ

  1. cGAN

cGAN์— ๋ ˆ์ด๋ธ” ์ •๋ณด ์ถ”๊ฐ€

# '์ง„์งœ'์™€ '๊ฐ€์งœ' ๋ ˆ์ด๋ธ” ์ƒ์„ฑ
real_labels = torch.ones(BATCH_SIZE, 1)
fake_labels = torch.zeros(BATCH_SIZE, 1)
#์ง„์งœ์™€ ๊ฐ€์งœ ์ด๋ฏธ์ง€๋ฅผ ๊ฐ–๊ณ  ๋‚ธ ์˜ค์ฐจ๋ฅผ ๋”ํ•ด์„œ ํŒ๋ณ„์ž์˜ ์˜ค์ฐจ ๊ณ„์‚ฐํ•œ๋‹ค
d_loss = criterion(D(images), real_labels) + criterion(D(G(z)), fake_labels)

[9] DQN

๊ฒŒ์ž„ํ™˜๊ฒฝ์—์„œ ์Šค์Šค๋กœ ์„ฑ์žฅ

  1. DQN-cartpole
    memory๋ฅผ deque์— ๋„ฃ์–ด์„œ ์˜ค๋ž˜๋œ ๊ฒฝํ—˜(๋ฉ์ฒญํ• ๋•Œ์˜ ๊ฒฝํ—˜)์€ ์‚ญ์ œ๋จ
    act() ํ•จ์ˆ˜๋Š” epsilon๊ฐ’์„ ๋„ฃ์–ด์ฃผ์–ด ๋ฌด์ž‘์œ„ ํ–‰๋™์„ ํ•˜๋„๋ก ํ•จ
    self.model(state)- ํ–‰๋™๋“ค์— ๋Œ€ํ•œ ๊ฐ€์น˜๊ฐ’
    action์€ epsilon ํ•ฉ์ณ์ ธ์„œ 1,0 ๋‘˜ ์ค‘ ํ•˜๋‚˜ action ๊ณ ๋ฆ„
    loss๋Š” ํ˜„์žฌ ๊ฐ€์น˜๊ฐ’(์ •๋‹ต)๊ณผ ์˜ˆ์ธก๋œ ๊ฐ€์น˜๊ฐ’(์˜ˆ์ธก, ํ• ์ธํ•ด์คŒ)์˜ ์ฐจ์ด๋กœ ๊ตฌํ•ด์คŒ
env= gym.make('CartPole-v0')

agent = DQNAgent()
for e in range(1, EPISODES+1):
    state= env.reset()
    ...
    while True:
        env.render() # ๊ฒŒ์ž„ ํ™”๋ฉด ๋„์šฐ๊ธฐ

ํ…์„œ ์ƒ์„ฑ

torch.Tensor([[1,2,3],[4,5,6]], dtype = torch.int32, device = device)

# torch์—์„œ dtype ๋‚˜ device ๋ฐ”๊พธ๋Š” ๋ฐฉ๋ฒ•
y = torch.as_tensor(x, dtype = torch.half, device = 'cpu')
y = x.to(device, dtype = torch.float.64)

# start๋ถ€ํ„ฐ end๊นŒ์ง€ step๋ณ„๋กœ tensor
torch.arange(start = 0, end, step = 1, dtype = None, requires_grad = True)

torch.from_numpy() #numpy array์ธ ndarray๋กœ๋ถ€ํ„ฐ ํ…์„œ๋ฅผ ๋งŒ๋“ฆ

# N(0,1) ์ •๊ทœ๋ถ„ํฌ๋ฅผ ๋”ฐ๋ฅด๋Š” random ํ•จ์ˆ˜ ์ถ”์ถœ
torch.randn(*sizes, dtype = , device = , requires_grad = )

torch.Tensor์˜ AutogradํŒจํ‚ค์ง€๋Š” ๋ชจ๋“  ์—ฐ์‚ฐ์— ๋Œ€ํ•ด ์ž๋™๋ฏธ๋ถ„์„ ์ œ๊ณต
ํ…์„œ์˜ ์†์„ฑ ์ค‘ ํ•˜๋‚˜์ธ x.requires_grad = True๋กœ ํ•˜๋ฉด ํ…์„œ์˜ ๋ชจ๋“  ์—ฐ์‚ฐ์— ๋Œ€ํ•ด์„œ ์ถ”์ ์„ ์‹œ์ž‘
๊ณ„์‚ฐ ์ž‘์—… ํ›„ x.backward()๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด ๋ชจ๋“  ๊ทธ๋ ˆ์ด๋””์–ธํŠธ๋ฅผ ์ž๋™์œผ๋กœ ๊ณ„์‚ฐํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•จ

ํ…์„œ์— ๋Œ€ํ•œ ๊ธฐ๋ก(history) ์ถ”์  ์ค‘์ง€ํ•˜๋ ค๋ฉด x.detach() ํ˜ธ์ถœ
: ํ˜„์žฌ ๊ณ„์‚ฐ ๊ธฐ๋ก์œผ๋กœ๋ถ€ํ„ฐ ๋ถ„๋ฆฌ์‹œํ‚ค๊ณ  ์ดํ›„ ์ผ์–ด๋‚˜๋Š” ๊ณ„์‚ฐ๋“ค์€ ์ถ”์ ๋˜์ง€ ์•Š๋Š”๋‹ค.


๊ธฐ๋ก ์ถ”์ (๋ฐ ๋ฉ”๋กœ๋ฆฌ ์‚ฌ์šฉ)์— ๋Œ€ํ•ด ๋ฐฉ์ง€๋ฅผ ํ•˜๋ ค๋ฉด with.no_grad()๋ฅผ wrap ๊ฐ€๋Šฅ ์ด ํ…์„œ์˜ ๋ณ€ํ™”๋„๋Š” x.grad์†์„ฑ์— ๋ˆ„์ ๋œ๋‹ค
: with.no_grad()๋Š” ๋ณ€ํ™”๋„(gradient)๋Š” ํ•„์š”์—†์ง€๋งŒ requires_grad = True ๊ฐ€ ์„ค์ •๋˜์–ด ํ•™์Šต๊ฐ€๋Šฅํ•œ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ๊ฐ–๋Š” ๋ชจ๋ธ์€ ํ‰๊ฐ€ํ•  ๋•Œ ์œ ์šฉ

x.data๋„ x.detach()์™€ ๋น„์Šทํ•œ ๊ธฐ๋Šฅ

x.detach(): ๊ธฐ์กด ํ…์„œ์—์„œ gradient๊ฐ€ ์ „ํŒŒ๊ฐ€ ์•ˆ๋˜๋Š” ํ…์„œ
x.clone(): ๊ธฐ์กด ํ…์„œ์™€ ๋‚ด์šฉ ๋ณต์‚ฌํ•œ tensor ์ƒ์„ฑ

torch.new_tensor(x, requires_grad=True)์™€ x.clone().detach().requires_grad(True)๋Š” ๊ฐ™์€ ์˜๋ฏธ

โœ” GRU ๋‚ด torch ์—ฐ์‚ฐ [iterator ์—ฐ์‚ฐ]

  • parameters() ํ•จ์ˆ˜๋Š” ๊ทธ ์‹ ๊ฒฝ๋ง ๋ชจ๋“ˆ์˜ ๊ฐ€์ค‘์น˜ ์ •๋ณด๋“ค์„ iterator ํ˜•ํƒœ๋กœ ๋ฐ˜ํ™˜ํ•˜๊ฒŒ ๋œ๋‹ค
  • next(self.parameters() (๊ทธ ์™ธ, iter(~), new()๋“ฑ)
# nn.GRU ๋ชจ๋“ˆ์˜ ์ฒซ ๋ฒˆ์งธ ๊ฐ€์ค‘์น˜ ํ…์„œ๋ฅผ ์ถ”์ถœ > ์ด ํ…์„œ๋Š” ๋ชจ๋ธ์˜ ๊ฐ€์ค‘์น˜ํ…์„œ์™€ ๊ฐ™์€ ๋ฐ์ดํ„ฐ ํƒ€์ž…
# new๋ฅผ ํ†ตํ•˜์—ฌ ๋ชจ๋ธ์˜ ๊ฐ€์ค‘์น˜์™€ ๊ฐ™์€ ๋ชจ์–‘์ธ (n_layers, batch_size, hidden_dum) ๋ชจ์–‘์˜ ํ…์„œ ๋ณ€ํ™˜
def _init_state(self, batch_size = 1):
    weight = next(self.parameter()).data
    return weight.new(self.n_layers, batch_size, self.hidden_dim).zero_()

ํ…์„œ ์—ฐ์‚ฐ

๊ฐ„๋‹จ ์—ฐ์‚ฐ

  • squeeze() & unsqueeze() : 1์ธ ์ฐจ์›์„ ์ƒ์„ฑ or ์ œ๊ฑฐ squeeze()ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ํ•ด๋‹น ์ธ๋ฑ์Šค์˜ ๊ฐ’ squeeze
    unsqueeze()ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ํ•ด๋‹น ์ธ๋ฑ์Šค์˜ ๊ฐ’์— 1์ฐจ์› ์ถ”๊ฐ€
  • contiguous() ์—ฐ์ƒ๊ณผ์ •์—์„œ Tensor๊ฐ€ ๋ฉ”๋ชจ๋ฆฌ์— ์˜ฌ๋ ค์ง„ ์ˆœ์„œ ์œ ์ง€ํ•˜๋ ค๋ฉด congiguous()์‚ฌ์šฉ
  • transpose(), permute() transpose()์›๋ณธ tensor์™€ data๋ฅผ ๊ณต์œ ํ•˜๋ฉด์„œ ์ƒˆ๋กœ์šด tensor ๋ฐ˜ํ™˜ permute()๋Š” ๋ชจ๋“  ์ฐจ์›์—์„œ ๋งž๊ตํ™˜ํ•  ์ˆ˜ ์žˆ์Œ(transpose()๋Š” ๋‘ ๊ฐœ์˜ ์ฐจ์›์„ ๋งž๊ตํ™˜)
  • reshape(), view() reshape()์€ ์›๋ณธ tensor์˜ ๋ณต์‚ฌ๋ณธ ํ˜น์€ view๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค

transpose()์™€ view()์˜ ์ฐจ์ด์ ์€ viewํ•จ์ˆ˜๋Š” ์˜ค์ง contiguous tensor์—์„œ๋งŒ ์ž‘๋™, ๋ฐ˜ํ™˜ํ•˜๋Š” tensor ์—ญ์‹œ contiguousํ•˜๋‹ค
transpose()๋Š” non-contiguous์™€ contiguous tensor ๋‘˜๋‹ค์—์„œ ์ž‘๋™ ๊ฐ€๋Šฅ
contiguous ํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด transpose().contiguous()ํ•ด์ฃผ์–ด์•ผํ•จ(permute๋„ ๋™์ผ)

๋”ฅ๋Ÿฌ๋‹ Dataset

### ๋ฐ์ดํ„ฐ ๋กœ๋“œ ###
trainset = datasets.FashionMNIST( root = './.data/', train = True,download = True, transform = transform)
## transform: ํ…์„œ๋กœ ๋ณ€ํ™˜, ํฌ๊ธฐ ์กฐ์ ˆ(resize), ํฌ๋กญ(crop), ๋ฐ๊ธฐ(brightness), ๋Œ€๋น„(contrast)๋“ฑ ์‚ฌ์šฉ ๊ฐ€๋Šฅ
transform = transforms.Compose([
    transforms.ToTensor(),
    transforms.Normalize((0.1307,),(0.3081,))
])
### DATALOADER ###
## DataLoader๋Š” ํ•™์Šต ์ด๋ฏธ์ง€์™€ ๋ ˆ์ด๋ธ”์„ (์ด๋ฏธ์ง€, ๋ ˆ์ด๋ธ”) ํŠœํ”Œ ํ˜•ํƒœ๋กœ ๋ฐ˜ํ™˜ํ•ด์คŒ
# Dataloader > ํ…์„œํ™” & ์ •๊ทœํ™”
train_loader = data.DataLoader(
    dataset= trainset,
    batch_size = batch_size )

โœ” iter(), next()

  • for๋ฌธ์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  iter() ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐ˜๋ณต๋ฌธ ์•ˆ์—์„œ ์ด์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•จ
  • next() ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•˜์—ฌ ๋ฐฐ์น˜ 1๊ฐœ๋ฅผ ๊ฐ€์ง€๊ณ  ์˜ด
dataiter = iter(train_loader)
images, labels = next(dataiter)

โœ” ์‹œ๊ฐํ™” (make_grid())

  • img = utils.make_grid(images, padding=0) : ์—ฌ๋Ÿฌ ์ด๋ฏธ์ง€๋ฅผ ๋ชจ์•„ ํ•˜๋‚˜์˜ ์ด๋ฏธ์ง€๋กœ ๋งŒ๋“ค ์ˆ˜ ์žˆ์Œ
  • ํŒŒ์ดํ† ์น˜์˜ ํ…์„œ์—์„œ numpy๋กœ ๋ฐ”๊พธ์–ด์•ผ ์‹œ๊ฐํ™” ๊ฐ€๋Šฅ

๋”ฅ๋Ÿฌ๋‹ ํ…์„œ ๊ณผ์ •

### TRAIN ###
optimizer = torch.optim.SGD(model.parameters(), lr = 0.02..)
## weight_decay ๊ฐ’์ด ์ปค์งˆ์ˆ˜๋ก ๊ฐ€์ค‘์น˜ ๊ฐ’์ด ์ž‘์•„์ง€๊ฒŒ ๋˜๊ณ , Overfitting ํ˜„์ƒ์€ ํ•ด์†Œ but underfitting ์œ„ํ—˜
# optimizer = optim.SGD(model.parameters(), lr = 0.1, momentum = 0.9, weight_decay = 0.0005)
# scheduler = optim.lr_scheduler.StepLR(optimizer, step_size = 50, gamma = 0.1)

criterion = nn.BCELoss()
loss = criterion(model(x_test).squeeze(), y_test)
# loss = F.cross_entropy(output, target)

optimizer.zero_grad() #optimizer ์ดˆ๊ธฐํ™”

train_loss.backward() #๊ทธ๋ ˆ์ด๋””์–ธํŠธ ๊ณ„์‚ฐ
optimizer.step() #step๋ณ„๋กœ ๊ณ„์‚ฐ

### EVALUATE ###
with torch.no_grad():
  for data, target in test_loader:
    output = model(data)
    test_loss += F.cross_entropy(output, target, reduction='sum').item()
    
    pred = output.max(1, keepdim = True)[1]
    correct += pred.eq(target.view_as(pred)).sum().item()

โœ” torch.max(1) -> 1์ฐจ์›์„ ๊ธฐ์ค€์œผ๋กœ max ๊ฐ’์„ ์ •ํ•จ
ex) x = tensor(2,40,8)์ฐจ์› -> x.max(1) -> 40 x 2(ํ•˜๋‚˜๋Š” ์ธ๋ฑ์Šค๊ฐ’, ํ•˜๋‚˜๋Š” ๊ฐ’)

โœ” F.nll_loss(output, torch.tensor([263]))

  • Negative log likelihood_loss
  • F.logsoftmax + F.null_loss = F.crossEntropy

โœ” torch.clamp(input, min, max)

  • ํ•ด๋‹น ๋ฒ”์ฃผ ๊ฐ’ ์ด์ƒ ์ดํ•˜๊ฐ€ ๋˜์ง€ ์•Š๋„๋ก ์žก๋Š” ๋ช…๋ น์–ด

โœ” weight decay

  • L2 regularization์€ ๊ฐ€์žฅ ์ผ๋ฐ˜์ ์œผ๋กœ ์‚ฌ์šฉ๋˜๋Š” regularization๊ธฐ๋ฒ•
  • ์˜ค๋ฒ„ํ”ผํŒ…์€ ๊ฐ€์ค‘์น˜ ๋งค๊ฐœ๋ณ€์ˆ˜์˜ ๊ฐ’์ด ์ปค์„œ ๋ฐœ์ƒํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ€์ค‘์น˜๊ฐ€ ํด ์ˆ˜๋ก ํฐ ํŒจ๋„ํ‹ฐ๋ฅผ ๋ถ€๊ณผ
  • L1 regularization๋„ ๋™์‹œ์— ์‚ฌ์šฉ๊ฐ€๋Šฅ

ํ…์„œ ์ €์žฅ

ํ•™์Šต๋œ ๋ชจ๋ธ์„ state_dict() ํ•จ์ˆ˜ ํ˜•ํƒœ๋กœ ๋ฐ”๊พธ์–ด์ค€ ํ›„ .pt ํŒŒ์ผ๋กœ ์ €์žฅ
state_dict() ํ•จ์ˆ˜๋Š” ๋ชจ๋ธ ๋‚ด ๊ฐ€์ค‘์น˜๋“ค์ด ๋”•์…”๋„ˆ๋ฆฌ ํ˜•ํƒœ๋กœ {์—ฐ์‚ฐ ์ด๋ฆ„: ๊ฐ€์ค‘์น˜ ํ…์„œ์™€ ํŽธํ–ฅ ํ…์„œ} ์™€ ๊ฐ™์ด ํ‘œํ˜„๋œ ๋ฐ์ดํ„ฐ

# ํ•™์Šต๋œ ๊ฐ€์ค‘์น˜ ์ €์žฅ
torch.save(model.state_dict(), './model.pt')

# ์ดํ›„ ๋กœ๋“œ [์ „์ดํ•™์Šต]
new_model = NeuralNet(2,5)
new_model.load_state_dict(torch.load('./model.pt'))

โœ” sklearn์˜ make_blobs

๋ถ„๋ฅ˜์šฉ ๊ฐ€์ƒ ๋ฐ์ดํ„ฐ ์ƒ์„ฑ : ๋“ฑ๋ฐฉ์„ฑ ๊ฐ€์šฐ์‹œ์•ˆ ์ •๊ทœ๋ถ„ํฌ๋ฅผ ์ด์šฉํ•ด ๊ฐ€์ƒ ๋ฐ์ดํ„ฐ๋ฅผ ์ƒ์„ฑํ•œ๋‹ค (๋“ฑ๋ฐฉ์„ฑ: ๋ชจ๋“  ๋ฐฉํ–ฅ์œผ๋กœ ๊ฐ™์€ ์„ฑ์งˆ์„ ๊ฐ€์ง„๋‹ค๋Š” ๋œป)

from sklearn.datasets import make_blobs
x_train, y_train = make_blobs(n_samples = 80, n_features = 2,
                            centers = [[1,1],[1,-1],[-1,1],[-1,-1]],
                            shuffle = True, cluster_std = 0.3)

๋”ฅ๋Ÿฌ๋‹ ์ฃผ์˜

์„ฑ๋Šฅ ์˜ฌ๋ฆฌ๊ธฐ

์กฐ๊ธฐ ์ข…๋ฃŒ

  • ํ•™์Šต ์ค‘๊ฐ„์ค‘๊ฐ„ ๊ฒ€์ฆ์šฉ ๋ฐ์ดํ„ฐ์…‹์œผ๋กœ ๋ชจ๋ธ์ด ํ•™์Šต ๋ฐ์ดํ„ฐ์—๋งŒ ๊ณผ์ ํ•ฉ๋˜์ง€ ์•Š์•˜๋Š”์ง€ ํ™•์ธ
  • ๊ฒ€์ฆ ๋ฐ์ดํ„ฐ์…‹์— ๋Œ€ํ•œ ์„ฑ๋Šฅ์ด ๋‚˜๋น ์ง€๊ธฐ ์‹œ์ž‘ํ•˜๊ธฐ ์ง์ „์ด ๊ฐ€์žฅ ์ ํ•ฉํ•œ ๋ชจ๋ธ

Data Augmentation

train_loader = torch.utils.data.DataLoader(
    datasets.MNIST('./.data',
                  train = True,
                  download = True,
                  transform = transforms.Compose([
                      transforms.RandomHorizontalFlip(),
                      transforms.ToTensor(),
                      transforms.Normalize((0.1307,),(0.3081,))
                  ])),
    batch_size = BATCH_SIZE, shuffle = True)

dropout

  • ์‚ฌ์‹ค layer๊ฐ€ ์–•์€๊ฑฐ ์•„๋‹ˆ๋ฉด dropout๋ณด๋‹จ batch normalization์ด ๋” ๋‚˜์Œ
## nn.Dropout & F.dropout 
# forward ํ•จ์ˆ˜ ๋‚ด
x = F.dropout(x, training = self.training, p = self.dropout

# main ํ•จ์ˆ˜ ๋‚ด
model = Net(dropout_p = 0.2)

๊ฒ€์ฆ ์˜ค์ฐจ๊ฐ€ ๊ฐ€์žฅ ์ ์€ ์ตœ์ ์˜ ๋ชจ๋ธ

best_val_loss = None
for e in range(1, EPOCHS +1):
    train(model, optimizer, train_iter)
    val_loss, val_accuracy = evaluate(model, val_iter)
    
    # ๊ฒ€์ฆ ์˜ค์ฐจ๊ฐ€ ๊ฐ€์žฅ ์ ์€ ์ตœ์ ์˜ ๋ชจ๋ธ ์ €์žฅ 
    if not best_val_Loss or val_loss < best_val_loss:
        # ๊ฒฝ๋กœ๊ฐ€ ์—†์„๋•Œ ๊ฒฝ๋กœ๋ฅผ ๋งŒ๋“ค์–ด์คŒ
        if not os.path.isdir("snapshot"):
            os.makedirs("snapshot")
        # ์ €์žฅ
        torch.save(model.state_dict(), './snapshot/txtclassification.pt')
        best_val_loss = val_loss

โœ” K-Fold (๊ต์ฐจ ๊ฒ€์ฆ)

  • ๋Œ์•„๊ฐ€๋ฉด์„œ ๋ชจ๋ธ์„ ํ›ˆ๋ จ์‹œ์ผœ ์ตœ์ ์˜ ํ•˜์ดํผํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์ฐพ๋Š”๋‹ค

์ถœ์ฒ˜

ํŽญ๊ท„๋ธŒ๋กœ์˜ 3๋ถ„ ๋”ฅ๋Ÿฌ๋‹ (ํŒŒ์ดํ† ์น˜ ๋ง›)

About

๐ŸŒฑCNN, GAN, RNN, DQN, Autoencoder, ResNet, Seq2Seq, Adversarial Attack IN PYTORCH

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published