Skip to content

A simple parser that turns a formulae written in unicode into an ExprGraph

License

Notifications You must be signed in to change notification settings

gorgonia/parser

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Report card

About

The goal of this project is to parse a mathematical formulae written in unicode and to transpile is into an ExprGraph of the Gorgonia project.

Principle

This parser is generated from a yacc file from the subdirectory src. If you want to contribute or add some new functionalities, you may need the goyacc tool and then run go generate from the src dubdirectory. For a more complete explanation, you can refer to this blog post

Available operations

As of today, the parser understands the following opreations on node objects (tensor based):

Operation Gorgonia Operation Symbol Unicode character
Multiplication Mul · U+00B7
Hadamard Product HadamardProd *
Addition Add +
Substraction Sub -
Pointwise Negation Neg -
Sigmoid Sigmoid σ U+03C3
Tanh Tanh tanh
Softmax Softmax softmax

Example

import (
	G "gorgonia.org/gorgonia"
	"github.com/gorgonia/parser"
	"gorgonia.org/tensor"
)

func main(){
	g := G.NewGraph()
	wfT := tensor.New(tensor.WithShape(2, 2), tensor.WithBacking([]float32{1, 1, 1, 1}))
	wf := G.NewMatrix(g, tensor.Float32, G.WithName("wf"), G.WithShape(2, 2), G.WithValue(wfT))
	htprevT := tensor.New(tensor.WithBacking([]float32{1, 1}), tensor.WithShape(2))
	htprev := G.NewVector(g, tensor.Float32, G.WithName("ht-1"), G.WithShape(2), G.WithValue(htprevT))
	xtT := tensor.New(tensor.WithBacking([]float32{1, 1}), tensor.WithShape(2))
	xt := G.NewVector(g, tensor.Float32, G.WithName("xt"), G.WithShape(2), G.WithValue(xtT))
	bfT := tensor.New(tensor.WithBacking([]float32{1, 1}), tensor.WithShape(2))
	bf := G.NewVector(g, tensor.Float32, G.WithName("bf"), G.WithShape(2), G.WithValue(bfT))

	p := parser.NewParser(g)
	p.Set(`Wf`, wf)
	p.Set(`h`, htprev)
	p.Set(`x`, xt)
	p.Set(`bf`, bf)
        result, _ := p.Parse(`σ(1*Wf·h+ Wf·x+ bf)`)
        machine := G.NewLispMachine(g, G.ExecuteFwdOnly())
        if err := machine.RunAll(); err != nil {
              t.Fatal(err)
        }
        res := result.Value().Data().([]float32)
}

Caution

  • The parser is internally using a map and is not concurrent safe.
  • The errors are not handle correctly

About

A simple parser that turns a formulae written in unicode into an ExprGraph

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published