Giter Site home page Giter Site logo

smaller_micrograd's Introduction

Smaller MicroGrad

Tried to do autograd engine based on Karpathy's micrograd repo (and on the basis of PyTorch)

Examples

Regression (it actually learns something!)

from autograd.tensor import Tensor
from autograd import nn
from autograd.optim import SGD
from sklearn.datasets import load_boston


class Regressor(nn.Module):
    def __init__(self,
                 in_features: int) -> None:
        super().__init__()

        self.l1 = nn.Linear(in_features, 32)
        self.l2 = nn.Linear(32, 1)
    
    def forward(self, x):
        return self.l2(self.l1(x).relu())


x, y = load_boston(return_X_y=True)
x, y = Tensor(x), Tensor(y).reshape(-1, 1)


net = Regressor(x.shape[1])
optimizer = SGD(net.parameters(), lr=1e-6)

errors = []
iters = 1000

for i in range(iters):
    net.zero_grad()

    loss = nn.MSELoss(net(x), y)
    errors.append(loss.data.flatten())

    loss.backward()
    optimizer.step()

print(np.sqrt(loss.data[0]))

Classification (it also learns something!)

from autograd.tensor import Tensor
from autograd import nn
from autograd.optim import SGD
from sklearn.datasets import load_iris


class Classifier(nn.Module):
    def __init__(self,
                 in_features: int,
                 out_features: int) -> None:
        super().__init__()

        self.l1 = nn.Linear(in_features, 32)
        self.l2 = nn.Linear(32, out_features)
    
    def forward(self, x):
        out = self.l1(x).relu()
        return self.l2(out).softmax(axis=1)


x, y = load_iris(return_X_y=True)
x = (x - x.mean(axis=0)) / x.std(axis=0)
x, y = Tensor(x), nn.one_hot(y)


net = Classifier(in_features=x.shape[1], out_features=3)
optimizer = SGD(net.parameters(), lr=3e-4)

errors = []
iters = 10000

for i in range(iters):
    net.zero_grad()

    loss = nn.CrossEntropyLoss(net(x), y)
    errors.append(loss.data.flatten())

    loss.backward()
    optimizer.step()

print(loss)

References

smaller_micrograd's People

Contributors

zzmtsvv avatar

Stargazers

Samuel Ayala avatar Andrey Laptev avatar Max Bernstein avatar Lev Novitskiy avatar astifer avatar

Watchers

 avatar

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.