<jupyter_start><jupyter_text><jupyter_code>!pip3 install torch
import torch
import torch.nn as nn
import torchvision 
import torchvision.transforms as transforms
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

input_size=784
hidden_size=500
num_classes=10
num_epochs=5
batch_size=100
learning_rate=0.001
train_dataset=torchvision.datasets.MNIST(root='../../data',train=True,transform=transforms.ToTensor(),download=True)
test_dataset=torchvision.datasets.MNIST(root='../../data',train=False,transform=transforms.ToTensor(),download=True)
train_loader=torch.utils.data.DataLoader(dataset=train_dataset,batch_size=batch_size,shuffle=True)
test_loader=torch.utils.data.DataLoader(dataset=train_dataset,batch_size=batch_size,shuffle=False)

class NeuralNet(nn.Module):
    def __init__(self, input_size, hidden_size, num_classes):
        super(NeuralNet, self).__init__()
        self.fc1 = nn.Linear(input_size, hidden_size) 
        self.relu = nn.ReLU()
        self.fc2 = nn.Linear(hidden_size, num_classes)
 
       
    def forward(self,x):
        out=self.fc1(x)
        out=self.relu(out)
        out=self.fc2(out)
        return out
      


model=NeuralNet(input_size,hidden_size,num_classes).to(device)
creterion=nn.CrossEntropyLoss()
optimizer=torch.optim.Adam(model.parameters(), lr= learning_rate)
total_step=len(train_loader)
for epochs in range(num_epochs):
  for i,(images,labels) in enumerate(train_loader):
    images=images.reshape(-1,28*28).to(device)
    labels=labels.to(device)




    outputs=model(images)
    loss=creterion(outputs, labels)
    optimizer.zero_grad()
    loss.backward()
    optimizer.step()

    if(i+1)%100==0:
      print('Epochs [{}/{}],step[{}/{}],Loss:{:.4f}'.format(epochs+1,num_epochs,i+1,total_step,loss.item()))


with torch.no_grad():
  correct=0
  total=0
  for images,labels in test_loader:
    images = images.reshape(-1, 28*28).to(device)
    labels = labels.to(device)
    outputs = model(images)
    _,predicted=torch.max(outputs.data,1)
    total += labels.size(0)
    correct+= (predicted ==labels).sum().item()


    print(' Accuracy of the network on the 10000 test images: {} %'.format(100*correct/total))



  torch.save(model.state_dict(), ' model.ckpt')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14524647887323944, 'fraction_numerical': 0.02244718309859155, 'mean_word_length': 4.981578947368421, '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': '570416', 'license': 'MIT', 'n_tokens_mistral': 838, 'n_tokens_neox': 797, 'n_words': 114, 'path': 'Feedforward_neural_network.ipynb', 'repo_name': 'wlrma0108/Pytorch', 'size': '2272'}