<jupyter_start><jupyter_text><jupyter_code>import torch
import random
import numpy as np

random.seed(0)
np.random.seed(0)
torch.manual_seed(0)
torch.cuda.manual_seed(0)
torch.backends.cudnn.deterministic = True
import torchvision.datasets
MNIST_train = torchvision.datasets.MNIST('./', download=True, train=True)
MNIST_test = torchvision.datasets.MNIST('./', download=True, train=False)
X_train = MNIST_train.train_data
y_train = MNIST_train.train_labels
X_test = MNIST_test.test_data
y_test = MNIST_test.test_labels
X_train.dtype, y_train.dtype
X_train = X_train.float()
X_test = X_test.float()

X_train.shape, X_test.shape
print(
x.reshape(-1,1,1).shape,
x.reshape(-1,6000).shape,
x.shape,
x.reshape(-1).shape,
x.reshape(-1,14,32,7).shape,sep='\n')
y_train.shape, y_test.shape
import matplotlib.pyplot as plt
plt.imshow(X_train[7, :])
plt.show()
print(y_train[7])
X_train = X_train.reshape([-1, 28 * 28])
X_test = X_test.reshape([-1, 28 * 28])

class MNISTNet(torch.nn.Module):
    def __init__(self, n_hidden_neurons):
        super(MNISTNet, self).__init__()
        self.fc1 = torch.nn.Linear(28 * 28, n_hidden_neurons)
        self.ac1 = torch.nn.Sigmoid()
        self.fc2 = torch.nn.Linear(n_hidden_neurons, 10) 
    
    def forward(self, x):
        x = self.fc1(x)
        x = self.ac1(x)
        x = self.fc2(x)
        return x
    
mnist_net = MNISTNet(100)
torch.cuda.is_available()
!nvidia-smi
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
mnist_net = mnist_net.to(device)
list(mnist_net.parameters())
order = np.random.permutation(len(X_train))
len(X_train)
loss = torch.nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(mnist_net.parameters(), lr=1.0e-3)
batch_size = 100

test_accuracy_history = []
test_loss_history = []

X_test = X_test.to(device)
y_test = y_test.to(device)

for epoch in range(10000):
    order = np.random.permutation(len(X_train))
    
    for start_index in range(0, len(X_train), batch_size):
        optimizer.zero_grad()
        
        batch_indexes = order[start_index:start_index+batch_size]
        
        X_batch = X_train[batch_indexes].to(device)
        y_batch = y_train[batch_indexes].to(device)
        
        preds = mnist_net.forward(X_batch) 
        
        loss_value = loss(preds, y_batch)
        loss_value.backward()
        
        optimizer.step()

    test_preds = mnist_net.forward(X_test)
    test_loss_history.append(loss(test_preds, y_test))
    
    accuracy = (test_preds.argmax(dim=1) == y_test).float().mean()
    test_accuracy_history.append(accuracy)
    print(accuracy)
test_loss_history
#plt.plot(test_accuracy_history)
plt.plot(test_loss_history);
f, ax = plt.subplots(1, 2, figsize=(20, 6))

 

ax[0].plot(test_accuracy_history)

ax[0].set_title('Accuracy Score Plot')

ax[0].set_xlabel('Epoch')

ax[0].set_ylabel('Accuracy')

 

ax[1].plot(test_loss_history);

ax[1].set_title('Loss Plot')

ax[1].set_xlabel('Epoch')

ax[1].set_ylabel('Loss Function Value')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14242928452579035, 'fraction_numerical': 0.024292845257903494, 'mean_word_length': 4.164948453608248, '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': '161507', 'license': 'MIT', 'n_tokens_mistral': 1248, 'n_tokens_neox': 1192, 'n_words': 176, 'path': 'classification_handwritten_digit.ipynb', 'repo_name': 'Stanislav-Fadeev/Neural_Networks_and_CV', 'size': '3005'}