<jupyter_start><jupyter_text><jupyter_code>import torch
from torch import nn
from torchvision import transforms, datasets
transform = transforms.Compose([
    transforms.ToTensor(),
    transforms.Normalize((0.1307,),(0.3081,)),  # mean value = 0.1307, standard deviation value = 0.3081
])
data_path = './MNIST'

training_set = datasets.MNIST(root = data_path, train= True, download=True, transform= transform)
testing_set = datasets.MNIST(root = data_path, train= False, download=True, transform= transform)
training_loader = torch.utils.data.DataLoader(training_set)
testing_loader = torch.utils.data.DataLoader(testing_set)
class classification(nn.Module):
    def __init__(self):
        super(classification, self).__init__()
        
        # construct layers for a neural network
        self.classifier1 = nn.Sequential(
            nn.Linear(in_features=28*28, out_features=20*20),
            nn.Sigmoid(),
        ) 
        self.classifier2 = nn.Sequential(
            nn.Linear(in_features=20*20, out_features=10*10),
            nn.Sigmoid(),
        ) 
        self.classifier3 = nn.Sequential(
            nn.Linear(in_features=10*10, out_features=10),
            nn.LogSoftmax(dim=1),
        ) 
        
        
    def forward(self, inputs):                 # [batchSize, 1, 28, 28]
        x = inputs.view(inputs.size(0), -1)    # [batchSize, 28*28]
        x = self.classifier1(x)                # [batchSize, 20*20]
        x = self.classifier2(x)                # [batchSize, 10*10]
        out = self.classifier3(x)              # [batchSize, 10]
        
        return out

criterion = nn.NLLLoss()
learning_rate_value = 0.001
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
classifier = classification().to(device)
optimizer = torch.optim.SGD(classifier.parameters(), lr=learning_rate_value)
epoch_num = 5
classifier.train()
losses = []
accuracies = []
mini_batch_size = 32
for epoch in range(epoch_num) :
    correct = 0
    for batch_idx, (data, target) in enumerate(training_loader):
        data, target = data.to(device), target.to(device)
        if batch_idx % mini_batch_size == 0 :
            optimizer.zero_grad()
        output = classifier(data)
        loss = criterion(output, target)
        loss.backward()
        if batch_idx % mini_batch_size == mini_batch_size-1 :
            optimizer.step()
        pred = output.argmax(dim=1, keepdim=True)
        correct += pred.eq(target.view_as(pred)).sum().item()
        if batch_idx % 10000 == 0:
            print('Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}'.format(
                epoch, batch_idx * len(data), len(training_loader.dataset),
                100. * batch_idx / len(training_loader), loss.item()))
    losses.append(loss.item())
    accuracies.append(correct)
    print(losses[-1], accuracies[-1])<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1321017776228651, 'fraction_numerical': 0.033112582781456956, 'mean_word_length': 2.761467889908257, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '580870', 'license': 'Apache-2.0', 'n_tokens_mistral': 980, 'n_tokens_neox': 905, 'n_words': 176, 'path': 'exercise_09/assignment_09.ipynb', 'repo_name': 'nh7881/machine_learning_project_class', 'size': '2869'}