- Notifications
You must be signed in to change notification settings - Fork2
A simple parser that turns a formulae written in unicode into an ExprGraph
License
NotificationsYou must be signed in to change notification settings
gorgonia/parser
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
The goal of this project is to parse a mathematical formulae written in unicode and to transpile is into anExprGraph
of the Gorgonia project.
This parser is generated from ayacc
file from the subdirectorysrc
.If you want to contribute or add some new functionalities, you may need thegoyacc
tool and then rungo generate
from thesrc
dubdirectory.For a more complete explanation, you can refer to thisblog post
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 |
import (G"gorgonia.org/gorgonia""github.com/gorgonia/parser""gorgonia.org/tensor")funcmain(){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())iferr:=machine.RunAll();err!=nil {t.Fatal(err) }res:=result.Value().Data().([]float32)}
- The parser is internally using a
map
and is not concurrent safe. - The errors are not handle correctly