back to home

jcjohnson / pytorch-examples

Simple examples to introduce PyTorch

4,882 stars
919 forks
10 issues
Python

AI Architecture Analysis

This repository is indexed by RepoMind. By analyzing jcjohnson/pytorch-examples in our AI interface, you can instantly generate complete architecture diagrams, visualize control flows, and perform automated security audits across the entire codebase.

Our Agentic Context Augmented Generation (Agentic CAG) engine loads full source files into context on-demand, avoiding the fragmentation of traditional RAG systems. Ask questions about the architecture, dependencies, or specific features to see it in action.

Source files are only loaded when you start an analysis to optimize performance.

Embed this Badge

Showcase RepoMind's analysis directly in your repository's README.

[![Analyzed by RepoMind](https://img.shields.io/badge/Analyzed%20by-RepoMind-4F46E5?style=for-the-badge)](https://repomind.in/repo/jcjohnson/pytorch-examples)
Preview:Analyzed by RepoMind

Repository Overview (README excerpt)

Crawler view

This repository introduces the fundamental concepts of PyTorch through self-contained examples. At its core, PyTorch provides two main features: • An n-dimensional Tensor, similar to numpy but can run on GPUs • Automatic differentiation for building and training neural networks We will use a fully-connected ReLU network as our running example. The network will have a single hidden layer, and will be trained with gradient descent to fit random data by minimizing the Euclidean distance between the network output and the true output. **NOTE:** These examples have been update for PyTorch 0.4, which made several major changes to the core PyTorch API. Most notably, prior to 0.4 Tensors had to be wrapped in Variable objects to use autograd; this functionality has now been added directly to Tensors, and Variables are now deprecated. Table of Contents • Warm-up: numpy • PyTorch: Tensors • PyTorch: Autograd • PyTorch: Defining new autograd functions • TensorFlow: Static Graphs • PyTorch: nn • PyTorch: optim • PyTorch: Custom nn Modules • PyTorch: Control Flow and Weight Sharing Warm-up: numpy Before introducing PyTorch, we will first implement the network using numpy. Numpy provides an n-dimensional array object, and many functions for manipulating these arrays. Numpy is a generic framework for scientific computing; it does not know anything about computation graphs, or deep learning, or gradients. However we can easily use numpy to fit a two-layer network to random data by manually implementing the forward and backward passes through the network using numpy operations: PyTorch: Tensors Numpy is a great framework, but it cannot utilize GPUs to accelerate its numerical computations. For modern deep neural networks, GPUs often provide speedups of 50x or greater, so unfortunately numpy won't be enough for modern deep learning. Here we introduce the most fundamental PyTorch concept: the **Tensor**. A PyTorch Tensor is conceptually identical to a numpy array: a Tensor is an n-dimensional array, and PyTorch provides many functions for operating on these Tensors. Any computation you might want to perform with numpy can also be accomplished with PyTorch Tensors; you should think of them as a generic tool for scientific computing. However unlike numpy, PyTorch Tensors can utilize GPUs to accelerate their numeric computations. To run a PyTorch Tensor on GPU, you use the argument when constructing a Tensor to place the Tensor on a GPU. Here we use PyTorch Tensors to fit a two-layer network to random data. Like the numpy example above we manually implement the forward and backward passes through the network, using operations on PyTorch Tensors: PyTorch: Autograd In the above examples, we had to manually implement both the forward and backward passes of our neural network. Manually implementing the backward pass is not a big deal for a small two-layer network, but can quickly get very hairy for large complex networks. Thankfully, we can use automatic differentiation to automate the computation of backward passes in neural networks. The **autograd** package in PyTorch provides exactly this functionality. When using autograd, the forward pass of your network will define a **computational graph**; nodes in the graph will be Tensors, and edges will be functions that produce output Tensors from input Tensors. Backpropagating through this graph then allows you to easily compute gradients. This sounds complicated, it's pretty simple to use in practice. If we want to compute gradients with respect to some Tensor, then we set when constructing that Tensor. Any PyTorch operations on that Tensor will cause a computational graph to be constructed, allowing us to later perform backpropagation through the graph. If is a Tensor with , then after backpropagation will be another Tensor holding the gradient of with respect to some scalar value. Sometimes you may wish to prevent PyTorch from building computational graphs when performing certain operations on Tensors with ; for example we usually don't want to backpropagate through the weight update steps when training a neural network. In such scenarios we can use the context manager to prevent the construction of a computational graph. Here we use PyTorch Tensors and autograd to implement our two-layer network; now we no longer need to manually implement the backward pass through the network: PyTorch: Defining new autograd functions Under the hood, each primitive autograd operator is really two functions that operate on Tensors. The **forward** function computes output Tensors from input Tensors. The **backward** function receives the gradient of the output Tensors with respect to some scalar value, and computes the gradient of the input Tensors with respect to that same scalar value. In PyTorch we can easily define our own autograd operator by defining a subclass of and implementing the and functions. We can then use our new autograd operator by constructing an instance and calling it like a function, passing Tensors containing input data. In this example we define our own custom autograd function for performing the ReLU nonlinearity, and use it to implement our two-layer network: TensorFlow: Static Graphs PyTorch autograd looks a lot like TensorFlow: in both frameworks we define a computational graph, and use automatic differentiation to compute gradients. The biggest difference between the two is that TensorFlow's computational graphs are **static** and PyTorch uses **dynamic** computational graphs. In TensorFlow, we define the computational graph once and then execute the same graph over and over again, possibly feeding different input data to the graph. In PyTorch, each forward pass defines a new computational graph. Static graphs are nice because you can optimize the graph up front; for example a framework might decide to fuse some graph operations for efficiency, or to come up with a strategy for distributing the graph across many GPUs or many mac…