{"id":61060,"date":"2024-03-29T13:18:32","date_gmt":"2024-03-29T13:18:32","guid":{"rendered":"https:\/\/www.askpython.com\/?p=61060"},"modified":"2025-04-10T20:33:57","modified_gmt":"2025-04-10T20:33:57","slug":"torch-nn-pytorch","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python-modules\/torch-nn-pytorch","title":{"rendered":"Building Neural Networks with torch.nn in PyTorch"},"content":{"rendered":"\n<p>PyTorch is a popular Python library that helps all deep learning enthusiasts. The torch.nn module is a very important component of PyTorch which helps with the building and training of neural networks. <\/p>\n\n\n\n<p>In this article, we will take a deep dive into the torch.nn module, its key components, and the implementation of the module in the Python programming language. <\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><em>The torch.nn module in PyTorch is essential for building and training neural networks. It provides a wide range of pre-defined layers, loss functions, and classes that facilitate the creation and optimization of neural network models.<\/em><\/p>\n<\/blockquote>\n\n\n\n<p><strong><em>Recommended: <a href=\"https:\/\/www.askpython.com\/python\/examples\/deep-learning-pytorch-7-steps\">Deep Learning Using PyTorch In 7 Steps<\/a><\/em><\/strong><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Overview of torch.nn<\/h2>\n\n\n\n<p>The torch.nn module in the PyTorch library is used to build and train neural networks. It also provides classes and functions for defining various layers, loss functions, and optimization algorithms. torch.nn is also designed to facilitate the creation of neural networks. Let us look at the key components of torch.nn.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Key Components of torch.nn<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Modules<\/strong>: The torch.nn module is the base class of all neural network models in PyTorch. It also provides a feasible way to encapsulate parameters.<\/li>\n\n\n\n<li><strong>Layers<\/strong>: The torch.nn module has a variety of pre-defined layers like &#8216;Linear&#8217;, &#8216;ReLu&#8221; etc. These layers can easily be used in other neural network models.<\/li>\n\n\n\n<li><strong>Loss Functions<\/strong>: The module also has a variety of loss functions such as &#8216;CrossEntrpyLoss&#8217; to predict the difference between predicted and actual values during training.<\/li>\n\n\n\n<li><strong>Optimizers<\/strong>: PyTorch offers a variety of optimization algorithms like the torch.optim module. Optimizers like SGD, Adam, and Adagrad can also be easily used in other neural network modules as well.<\/li>\n<\/ul>\n\n\n\n<p><strong><em>Recommended: <a href=\"https:\/\/www.askpython.com\/python\/examples\/pytorch-loss-functions\">A Quick Guide to Pytorch Loss Functions<\/a><\/em><\/strong><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example 1: Simple Neural Network<\/h3>\n\n\n\n<p>Let us now observe a simple Python code that uses torch.nn module.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport torch\nimport torch.nn as nn\nimport torch.optim as optim\n\n# Dummy dataset\nX = torch.rand((100, 10))\ny = torch.randint(0, 2, (100,))\n\n# Define the model\nclass SimpleModel(nn.Module):\n    def __init__(self):\n        super(SimpleModel, self).__init__()\n        self.fc1 = nn.Linear(10, 5)\n        self.relu = nn.ReLU()\n        self.fc2 = nn.Linear(5, 2)\n\n    def forward(self, x):\n        x = self.fc1(x)\n        x = self.relu(x)\n        x = self.fc2(x)\n        return x\n\n# Instantiate the model, loss function, and optimizer\nmodel = SimpleModel()\ncriterion = nn.CrossEntropyLoss()\noptimizer = optim.SGD(model.parameters(), lr=0.01)\n\n# Training loop\nepochs = 100\nfor epoch in range(epochs):\n    # Forward pass\n    outputs = model(X)\n    loss = criterion(outputs, y)\n\n    # Backward pass and optimization\n    optimizer.zero_grad()\n    loss.backward()\n    optimizer.step()\n\n    # Print the loss every 10 epochs\n    if (epoch + 1) % 10 == 0:\n        print(f&#039;Epoch &#x5B;{epoch + 1}\/{epochs}], Loss: {loss.item():.4f}&#039;)\n\n# Test the trained model\nwith torch.no_grad():\n    test_outputs = model(X)\n    predicted_classes = torch.argmax(test_outputs, dim=1)\n\n    accuracy = torch.sum(predicted_classes == y).item() \/ len(y)\n    print(f&#039;Test Accuracy: {accuracy * 100:.2f}%&#039;)\n\n<\/pre><\/div>\n\n\n<p>In the above code, the torch.nn module in PyTorch helps in the process of creation, training and testing a simple neural network using PyTorch&#8217;s &#8216;torch.nn&#8217; module. This code can be modified into more complex neural networks. Let us look at the output.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"761\" height=\"313\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2024\/03\/torch.nn-output.png\" alt=\"Torch Nn Output\" class=\"wp-image-61061\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2024\/03\/torch.nn-output.png 761w, https:\/\/www.askpython.com\/wp-content\/uploads\/2024\/03\/torch.nn-output-300x123.png 300w\" sizes=\"auto, (max-width: 761px) 100vw, 761px\" \/><figcaption class=\"wp-element-caption\"><strong><em>torch.nn Output<\/em><\/strong><\/figcaption><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Example 2: Convolutional Neural Network (CNN)<\/h3>\n\n\n\n<p>Let us look at another Python code regarding torch.nn module of Python programming language.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport torch\nimport torch.nn as nn\nimport torch.optim as optim\nimport torchvision\nimport torchvision.transforms as transforms\n\n# Define a simple Convolutional Neural Network (CNN)\nclass CNN(nn.Module):\n    def __init__(self):\n        super(CNN, self).__init__()\n        self.conv1 = nn.Conv2d(1, 32, kernel_size=3, stride=1, padding=1)\n        self.relu = nn.ReLU()\n        self.pool = nn.MaxPool2d(kernel_size=2, stride=2)\n        self.conv2 = nn.Conv2d(32, 64, kernel_size=3, stride=1, padding=1)\n        self.fc1 = nn.Linear(64 * 7 * 7, 128)\n        self.fc2 = nn.Linear(128, 10)\n\n    def forward(self, x):\n        x = self.conv1(x)\n        x = self.relu(x)\n        x = self.pool(x)\n        x = self.conv2(x)\n        x = self.relu(x)\n        x = self.pool(x)\n        x = x.view(-1, 64 * 7 * 7)\n        x = self.fc1(x)\n        x = self.relu(x)\n        x = self.fc2(x)\n        return x\n\n# Load the MNIST dataset and apply transformations\ntransform = transforms.Compose(&#x5B;transforms.ToTensor(), transforms.Normalize((0.5,), (0.5,))])\ntrain_dataset = torchvision.datasets.MNIST(root=&#039;.\/data&#039;, train=True, transform=transform, download=True)\ntrain_loader = torch.utils.data.DataLoader(dataset=train_dataset, batch_size=64, shuffle=True)\n\n# Instantiate the CNN, define loss function, and choose an optimizer\nmodel = CNN()\ncriterion = nn.CrossEntropyLoss()\noptimizer = optim.Adam(model.parameters(), lr=0.001)\n\n# Training loop\nepochs = 5\nfor epoch in range(epochs):\n    total_loss = 0.0\n    for images, labels in train_loader:\n        optimizer.zero_grad()  # Zero the gradients\n        outputs = model(images)  # Forward pass\n        loss = criterion(outputs, labels)  # Compute the loss\n        loss.backward()  # Backward pass\n        optimizer.step()  # Update the weights\n\n        total_loss += loss.item() * images.size(0)\n\n    average_loss = total_loss \/ len(train_loader.dataset)\n    print(f&#039;Epoch &#x5B;{epoch + 1}\/{epochs}], Loss: {average_loss:.4f}&#039;)\n\nprint(&#039;Training finished.&#039;)\n\n# Save the trained model (optional)\ntorch.save(model.state_dict(), &#039;mnist_cnn_model.pth&#039;)\n<\/pre><\/div>\n\n\n<p>In the example above, we have constructed a CNN (Convolutional neural network) and train it as well. Let us look at the output.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"528\" height=\"176\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2024\/03\/CNN-using-torch.nn_.png\" alt=\"CNN Using Torch Nn \" class=\"wp-image-61062\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2024\/03\/CNN-using-torch.nn_.png 528w, https:\/\/www.askpython.com\/wp-content\/uploads\/2024\/03\/CNN-using-torch.nn_-300x100.png 300w\" sizes=\"auto, (max-width: 528px) 100vw, 528px\" \/><figcaption class=\"wp-element-caption\"><strong><em>CNN Using Torch.nn <\/em><\/strong><\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>torch.nn is a fundamental module in PyTorch that empowers developers to build and train neural networks efficiently. With a rich set of pre-defined layers, loss functions, and optimization algorithms, it simplifies the process of creating complex models. Whether you&#8217;re building a simple feedforward network or a sophisticated CNN, torch.nn has you covered. <\/p>\n\n\n\n<p>So, go ahead and experiment with different architectures and hyperparameters to unleash the full potential of your neural networks. The possibilities are endless with torch.nn!<\/p>\n\n\n\n<p><strong><em>Recommended: <a href=\"https:\/\/www.askpython.com\/python-modules\/numpy\/cross-entropy-in-python\">What Is Cross Entropy In Python?<\/a><\/em><\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>PyTorch is a popular Python library that helps all deep learning enthusiasts. The torch.nn module is a very important component of PyTorch which helps with the building and training of neural networks. In this article, we will take a deep dive into the torch.nn module, its key components, and the implementation of the module in [&hellip;]<\/p>\n","protected":false},"author":80,"featured_media":63907,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-61060","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python-modules"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/61060","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/users\/80"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=61060"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/61060\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/63907"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=61060"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=61060"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=61060"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}