<jupyter_start><jupyter_text>Define dataset<jupyter_code>torch_dataset = Data.TensorDataset(x, y)
loader = Data.DataLoader(
    dataset = torch_dataset,
    batch_size = BATCH_SIZE,
    shuffle = True, num_workers=2,)<jupyter_output><empty_output><jupyter_text>Define Network<jupyter_code>class Net(torch.nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        self.hidden = torch.nn.Linear(1,20)
        self.predict = torch.nn.Linear(20,1) # output
        
    def forward(self, x):
        x = F.relu(self.hidden(x)) # activation funtion
        x = self.predict(x)           # linear output
        return x<jupyter_output><empty_output><jupyter_text>Different nets<jupyter_code>net_SGD = Net()
net_Momentum = Net()
net_RMSprop = Net()
net_Adam = Net()
nets = [net_SGD,net_Momentum,net_RMSprop, net_Adam]<jupyter_output><empty_output><jupyter_text>Different optimizers<jupyter_code>opt_SGD  = torch.optim.SGD(net_SGD.parameters(), lr=LR)
opt_Momentum = torch.optim.SGD(net_Momentum.parameters(), lr=LR, momentum=0.8)
#같은 방향으로 계속 움직이는 경향이 있습니다
opt_RMSprop = torch.optim.RMSprop(net_RMSprop.parameters(),lr=LR, alpha=0.9)
#과거 그라디언트의 지수 함수 적으로 감쇠 평균 유지 
opt_Adam  = torch.optim.Adam(net_Adam.parameters(),lr=LR, betas=(0.9,0.99))
optimizers = [opt_SGD, opt_Momentum,opt_RMSprop,opt_Adam]
loss_func = torch.nn.MSELoss()
losses_his = [[],[],[],[]]
for epoch in range(EPOCH):
    print('Epoch:',epoch)
    for step, (batch_x, batch_y) in enumerate(loader):
        b_x = Variable(batch_x)
        b_y = Variable(batch_y)
        
        for net,opt, l_his in zip(nets, optimizers, losses_his):
            output = net(b_x)
            loss = loss_func(output, b_y)
            opt.zero_grad()
            loss.backward()
            opt.step()
            l_his.append(loss.data)
            
labels = ['SGD', 'Momentum','RMSprop','Adam']
for i, l_his in enumerate(losses_his):
    plt.plot(l_his, label = labels[i])
plt.legend(loc='best')
plt.xlabel('Steps')
plt.ylabel('Loss')
plt.ylim((0, 0.2))
plt.show()<jupyter_output>Epoch: 0
Epoch: 1
Epoch: 2
Epoch: 3
Epoch: 4
Epoch: 5
Epoch: 6
Epoch: 7
Epoch: 8
Epoch: 9
Epoch: 10
Epoch: 11
{'fraction_non_alphanumeric': 0.14133456904541242, 'fraction_numerical': 0.015291936978683966, 'mean_word_length': 3.829977628635347, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '567897', 'license': 'MIT', 'n_tokens_mistral': 926, 'n_tokens_neox': 906, 'n_words': 152, 'path': 'Tutorials/07_Optimizers.ipynb', 'repo_name': 'NimbleBoxAI/PyTorch', 'size': '2158'}