Giter Club home page Giter Club logo

micrograd's Introduction

Elixir Micrograd

A little (and slow) neural network library that implements backpropagation using automatic differentiation.

This is based on Micrograd by Andrej Karpathy.

I made this projects as an EXPERIMENT to learn more about Neural Networks and Elixir. The implementation on Elixir was a bit tricky because of the lack of operator overloading and the immutability of the language. There is maybe a better approach to solve this problem, but I learned a lot in the process.

Automatic gradients example

x1 = Value.new(2.0)
w1 = Value.new(-3.0)
x2 = Value.new(0.0)
w2 = Value.new(1.0)

x1w1 = Value.mul(x1, w1)
x2w2 = Value.mul(x2, w2)

x1w1x2w2 = Value.add(x1w1, x2w2)

n = Value.add(x1w1x2w2, 4.0)

o = Value.tanh(n)

# The Value struct keeps track of the operations that were performed

IO.puts "o: #{Value.item(o)}"
# o: -0.9640275800758168

# The backward function will calculate the gradients of the operations
# and store them in the Value struct.

o = Value.backward(o)

# %Value{
#   parents: [
#     %Value{
#       parents: [
#         %Value{
#           parents: [
#             %Value{
#               parents: [
#                 %Value{ data: 2.0, grad: -3.0, },
#                 %Value{ data: -3.0, grad: 2.0, }
#               ],
#               data: -6.0,
#               grad: 1.0,
#             },
#             %Value{
#               parents: [
#                 %Value{ data: 0.0, grad: 1.0, },
#                 %Value{ data: 1.0, grad: 0.0, }
#               ],
#               ...
# }

Create a neural network

epochs = 2000
learning_rate = 0.08

# XOR Inputs
xs = [
  [0.0, 0.0],
  [0.0, 1.0],
  [1.0, 0.0],
  [1.0, 1.0]
]

# XOR Results
ys = [0.0, 1.0, 1.0, 0.0]

# Create a multilayer perceptron
mlp = MLP.new(2, [{4, :tanh}, {4, :sigmoid}, {1, :sigmoid}])

# Train
mlp =
  Enum.reduce(1..epochs, mlp, fn _, mlp ->
  
    # Forward pass
    ypred = Enum.map(xs, &MLP.forward(mlp, &1))

    # Calculate loss
    loss =
      Enum.zip(ys, ypred)
      |> Enum.map(fn {ygt, yout} -> Value.sub(yout, ygt) |> Value.pow(2.0) end)
      |> Value.sum()

    # Backward pass
    loss = Value.backward(loss)

    # Update weights
    MLP.update(mlp, learning_rate, loss)
  end)
  
# Predict
MLP.forward(mlp, [0.0, 1.0]) |> Value.item() # ~= 1.0

micrograd's People

Contributors

luisgabrielroldan avatar

Watchers

 avatar  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.