Categorygithub.com/gorgonia/parser
modulepackage
0.0.0-20221008081532-b5eba6ff0655
Repository: https://github.com/gorgonia/parser.git
Documentation: pkg.go.dev

# README

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):

OperationGorgonia OperationSymbolUnicode character
MultiplicationMul·U+00B7
Hadamard ProductHadamardProd*
AdditionAdd+
SubstractionSub-
Pointwise NegationNeg-
SigmoidSigmoidσU+03C3
TanhTanhtanh
SoftmaxSoftmaxsoftmax

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

# Packages

Expr is a simple expression evaluator that serves as a working example of how to use Go's yacc implementation.

# Functions

NewParser ...

# Constants

No description provided by the author

# Structs

Parser is a structure that parses an expression and returns the corresponding gorgonia node.