package module
Version: v0.2.0 Latest Latest

This package is not in the latest version of its module.

Go to latest
Published: Jun 1, 2021 License: MIT Imports: 8 Imported by: 0



Go Coverage Status Go Report Card Go Reference

fonet is a deep neural network package for Go. It's mainly created because I wanted to learn about neural networks and create my own package. I'm planning to continue the development of the package and add more function to it, for example exporting/importing a model.


It's the same as everywhere, you just have to run the

go get


I focused (and still focusing) on creating an easy to use package, but let me know if something is not clear.

Creating a network

As in the xor example, it's not so complicated to create a network. When you creating the network, you always have to define the layers.

n := fonet.NewNetwork([]int{2, 3, 1}, fonet.Sigmond)
2 nodes in the INPUT LAYER
3 nodes in the HIDDEN LAYER
1 node in the OUTPUT LAYER

But my goal was also to create a package, which can create deep neural networks too, so here is another example for that.

n := fonet.NewNetwork([]int{6, 12, 8, 4}, fonet.Sigmond)
6 nodes in the INPUT LAYER
12 nodes in the HIDDEN LAYER (1)
8 nodes in the HIDDEN LAYER (2)
4 nodes in the OUTPUT LAYER
Train the network

After creating the network, you have to train your network. To do that, you have to specify your training set, which should be like the next

var trainingData = [][][]float64{
    [][]float64{ // The actual training sample
            The INPUT data
            The OUTPUT data

After giving the training data, you can set the epoch and the learning rate.

n.Train(trainingData, epoch, lrate, true)
// Train(trainingData [][][]float64, epochs int, lrate float64, debug bool)

Note: When 'debug' is true, it'll show when and which epoch is finished

Predict the output

After training your network, using the Predict(..) function you can calculate the output for the given input.

In the case of XOR, it looks like the next

input := []float64{
out := n.Predict(input)




This section is empty.


View Source
var (
	// ErrNotEnoughLayers is returned when trying to create a new network with too few layers.
	ErrNotEnoughLayers = errors.New("too few layers, minimum of 3 required")


This section is empty.


type ActivationFunction

type ActivationFunction int

ActivationFunction is the type of function to use for the neural network.

const (
	Sigmond ActivationFunction = iota

Activation functions available to this package. These are taken from

func (ActivationFunction) String

func (a ActivationFunction) String() string

type Network

type Network struct {
	// contains filtered or unexported fields

Network is containing all the needed settings/variables.

func Load

func Load(r io.Reader) (*Network, error)

Load will load a network from the provided reader.

func LoadFrom

func LoadFrom(bs []byte) (*Network, error)

LoadFrom will load a network from the provided byte slice.

func NewNetwork

func NewNetwork(ls []int, activationFunc ActivationFunction) (*Network, error)

NewNetwork is for creating a new network with the defined layers.

func (*Network) Export

func (n *Network) Export(w io.Writer) error

Export will serialize the network, and write it to the provided writer.

func (*Network) MarshalJSON

func (n *Network) MarshalJSON() ([]byte, error)

MarshalJSON implements the Marshaler interface for JSON encoding.

func (*Network) Predict

func (n *Network) Predict(input []float64) []float64

Predict calculates the output for the given input.

func (*Network) Train

func (n *Network) Train(trainingData [][][]float64, epochs int, lrate float64, debug bool)

Train is for training the network with the specified dataset, epoch and learning rate. The last bool parameter is for tracking where the training is. It'll log each epoch.

func (*Network) UnmarshalJSON

func (n *Network) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the Unmarshaler interface for JSON encoding.


Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL