Code Monkey home page Code Monkey logo

minitorch's Introduction

minitorch

Implement a minimal library from scratch to help understand the Dynamic Computaional Graph of PyTorch.

TODO

  • support CUDA

Requirements

  1. Create virtual environment

    python3 -m venv minitorch-env
  2. Activate virtual environment

    source minitorch-env/bin/activate
  3. Install dependencies

    pip install -r requirements.txt

Quick Start

  1. Clone the codebase

    git clone [email protected]:zhouzaida/minitorch.git
  2. Install or develop

    python setup.py install
    # or
    python setup.py develop

Examples

  • create Tensor

    from minitorch import Tensor
    
    t1 = Tensor(2.0)
    t2 = Tensor(3.0)
    t3 = t1 + t2
    print(t3)  # Tensor(3.0, requires_grad=False)
  • autograd

    from minitorch import Tensor
    
    t1 = Tensor(2.0, requires_grad=True)
    t2 = Tensor(3.0)
    t3 = t1 + t2
    t4 = t1 * t3
    t4.backward()
    print(f"t1 grad: {t1.grad}")  # t1 grad: Tensor(7.0, requires_grad=False)
    print(f"t2 grad: {t2.grad}")  # t2 grad: None
  • gradient for broadcast

    from minitorch import Tensor
    
    t1 = Tensor([1.0, 2.0], requires_grad=True)
    t2 = Tensor(2.0, requires_grad=True)
    t3 = t1 + t2
    t3.backward(Tensor([1.0, 1.0]))
    print(f"t1 grad: {t1.grad}")  # t1 grad: Tensor([1., 1.], requires_grad=False)
    print(f"t2 grad: {t2.grad}")  # t2 grad: Tensor(2.0, requires_grad=False)
  • create neural network

    import minitorch
    import minitorch.nn as nn
    
    input = minitorch.rand(2, 3)
    linear = nn.Linear(3, 5, bias=True)
    output = nn.linear(input)
    print(f"output: {output}")
    
    class Model(nn.Module):
    
        def __init__(self):
            super().__init__()
            self.linear_1 = nn.Linear(3, 5, bias=True)
            self.linear_2 = nn.Linear(5, 6)
    
        def forward(self, input):
            output = self.linear_1(input)
            output = self.linear_2(output)
            return output
    
    input = minitorch.rand(2, 3)
    model = Model()
    output = model(input)
    print(f"output: {output}")
    
    for name, module in model.named_modules(prefix='model'):
        print(f"{name}: {module}")

Tools

References

minitorch's People

Contributors

zhouzaida avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

mhx1203 yhna940

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.