MCPcopy
hub / github.com/nlpodyssey/spago

github.com/nlpodyssey/spago @v1.1.0 sqlite

repository ↗ · DeepWiki ↗ · release v1.1.0 ↗
2,892 symbols 11,106 edges 364 files 1,215 documented · 42%
README
<img src="https://github.com/nlpodyssey/spago/blob/main/assets/spago_logo.png" width="400"/>









<a href="https://github.com/nlpodyssey/spago/actions/workflows/go.yml?query=branch%3Amain">
    <img alt="Build" src="https://github.com/nlpodyssey/spago/actions/workflows/go.yml/badge.svg?branch=main">
</a>
<a href="https://codecov.io/gh/nlpodyssey/spago">
    <img alt="Coverage" src="https://codecov.io/gh/nlpodyssey/spago/branch/main/badge.svg">
</a>
<a href="https://goreportcard.com/report/github.com/nlpodyssey/spago">
    <img alt="Go Report Card" src="https://goreportcard.com/badge/github.com/nlpodyssey/spago">
</a>
<a href="https://codeclimate.com/github/nlpodyssey/spago/maintainability">
    <img alt="Maintainability" src="https://api.codeclimate.com/v1/badges/be7350d3eb1a6a8aa503/maintainability">
</a>
<a href="https://pkg.go.dev/github.com/nlpodyssey/spago/">
    <img alt="Documentation" src="https://pkg.go.dev/badge/github.com/nlpodyssey/spago/.svg">
</a>
<a href="https://opensource.org/licenses/BSD-2-Clause">
    <img alt="License" src="https://img.shields.io/badge/License-BSD%202--Clause-orange.svg">
</a>
<a href="http://makeapullrequest.com">
    <img alt="PRs Welcome" src="https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square">
</a>
<a href="https://github.com/avelino/awesome-go">
    <img alt="Awesome Go" src="https://awesome.re/mentioned-badge.svg">
</a>









<i>If you like the project, please ★ star this repository to show your support! 🤩</i>

If you're interested in NLP-related functionalities, be sure to explore the Cybertron package!

Spago is a Machine Learning library written in pure Go designed to support relevant neural architectures in Natural Language Processing.

Spago is self-contained, in that it uses its own lightweight computational graph both for training and inference, easy to understand from start to finish.

It provides: - Automatic differentiation via dynamic define-by-run execution - Feed-forward layers (Linear, Highway, Convolution...) - Recurrent layers (LSTM, GRU, BiLSTM...) - Attention layers (Self-Attention, Multi-Head Attention...) - Gradient descent optimizers (Adam, RAdam, RMS-Prop, AdaGrad, SGD) - Gob compatible neural models for serialization

Usage

Requirements:

Clone this repo or get the library:

go get -u github.com/nlpodyssey/spago

Getting Started

A good place to start is by looking at the implementation of built-in neural models, such as the LSTM.

Example 1

Here is an example of how to calculate the sum of two variables:

package main

import (
    "fmt"
    "log"

    "github.com/nlpodyssey/spago/ag"
    "github.com/nlpodyssey/spago/mat"
)

func main() {
    // define the type of the elements in the tensors
    type T = float32

    // create a new node of type variable with a scalar
    a := mat.Scalar(T(2.0), mat.WithGrad(true)) // create another node of type variable with a scalar
    b := mat.Scalar(T(5.0), mat.WithGrad(true)) // create an addition operator (the calculation is actually performed here)
    c := ag.Add(a, b)

    // print the result
    fmt.Printf("c = %v (float%d)\n", c.Value(), c.Value().Item().BitSize())

    c.AccGrad(mat.Scalar(T(0.5)))

    if err := ag.Backward(c); err != nil {
        log.Fatalf("error during Backward(): %v", err)
    }

    fmt.Printf("ga = %v\n", a.Grad())
    fmt.Printf("gb = %v\n", b.Grad())
}

Output:

c = [7] (float32)
ga = [0.5]
gb = [0.5]

Example 2

Here is a simple implementation of the perceptron formula:

package main

import (
    "fmt"

    . "github.com/nlpodyssey/spago/ag"
    "github.com/nlpodyssey/spago/mat"
)

func main() {
    x := mat.Scalar(-0.8)
    w := mat.Scalar(0.4)
    b := mat.Scalar(-0.2)

    y := Sigmoid(Add(Mul(w, x), b))

    fmt.Printf("y = %0.3f\n", y.Value().Item())
}

Contributing

If you think something is missing or could be improved, please open issues and pull requests.

To start contributing, check the Contributing Guidelines.

Contact

We highly encourage you to create an issue as it will contribute to the growth of the community. However, if you prefer to communicate with us privately, please feel free to email Matteo Grella with any questions or comments you may have.

Acknowledgments

Spago is part of the open-source NLP Odyssey initiative initiated by members of the EXOP team (now part of Crisis24).

Sponsors

See our Open Collective page if you too are interested in becoming a sponsor.

Extension points exported contracts — how you extend this code

StandardModel (Interface)
StandardModel consists of a model that implements a Forward variadic function that accepts mat.Tensor and returns a slic [32 …
nn/model.go
Source (Interface)
A Source represents a source of uniformly-distributed pseudo-random int64 values in the range [0, 1<<64). [4 implementers]
mat/internal/rand/rand.go
Float (Interface)
Float is implemented by any value that can be resolved to the types constrained by DType. [2 implementers]
mat/float/float.go
ParamsTraverser (Interface)
ParamsTraverser allows you to define a custom procedure to traverse the parameters of a model. If a model implements thi [2 …
nn/traversal.go
Function (Interface)
Function is implemented by any value that has the Decay method. [2 implementers]
optimizers/decay/function.go
T (Interface)
T requires a subset of methods from testing.TB. This interface is primarily useful to simplify the testing of the packag [1 …
mat/mat_test_utils.go
Tensor (Interface)
Tensor represents an interface for a generic tensor. [1 implementers]
mat/tensor.go
OptimizationStrategy (Interface)
OptimizationStrategy is the interface implemented by AdaGrad, Adam, etc. [1 implementers]
optimizers/optimizer.go

Core symbols most depended-on inside this repo

Run
called by 897
ag/operator.go
WithShape
called by 792
mat/dense_new.go
WithBacking
called by 734
mat/dense_new.go
Value
called by 573
mat/tensor.go
Data
called by 495
mat/tensor.go
Grad
called by 341
mat/tensor.go
WithGrad
called by 228
mat/dense_new.go
Errorf
called by 221
mat/mat_test_utils.go

Shape

Function 2,024
Method 635
Struct 203
Interface 16
TypeAlias 11
FuncType 3

Languages

Go100%

Modules by API surface

mat/internal/f64/asm64/bench_other_test.go258 symbols
mat/internal/f32/asm32/bench_test.go169 symbols
mat/dense_test.go169 symbols
mat/matrix.go87 symbols
mat/dense.go87 symbols
ag/operators.go84 symbols
mat/gradfn/misc.go61 symbols
mat/internal/rand/rand_test.go46 symbols
mat/internal/rand/rand.go43 symbols
mat/internal/f64/asm64/dot_test.go42 symbols
mat/mathfunctions_test.go39 symbols
mat/gradfn/misc_test.go30 symbols

Dependencies from manifests, versioned

github.com/davecgh/go-spewv1.1.1 · 1×
github.com/google/flatbuffersv23.5.26+incompatibl · 1×
github.com/kr/prettyv0.3.1 · 1×
github.com/pmezard/go-difflibv1.0.0 · 1×
gopkg.in/check.v1v1.0.0-2020113013444 · 1×
gopkg.in/yaml.v3v3.0.1 · 1×

For agents

$ claude mcp add spago \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact