model

package
v0.0.0-...-a3b61a5 Latest Latest
Warning

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

Go to latest
Published: May 5, 2024 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Activation

type Activation func(x ...*variable.Variable) *variable.Variable

type LSTM

type LSTM struct {
	Model
}
Example
package main

import (
	"fmt"

	"github.com/itsubaki/autograd/model"
)

func main() {
	m := model.NewLSTM(2, 3)

	for _, l := range m.Layers {
		fmt.Printf("%T\n", l)
	}

}
Output:

*layer.LSTMT
*layer.LinearT
Example (Backward)
package main

import (
	"fmt"

	"github.com/itsubaki/autograd/model"
	"github.com/itsubaki/autograd/rand"
	"github.com/itsubaki/autograd/variable"
)

func main() {
	m := model.NewLSTM(1, 1, model.LSTMOpts{
		Source: rand.Const(),
	})

	x := variable.New(1, 2)
	y := m.Forward(x)
	y.Backward()
	y = m.Forward(x)
	y.Backward()

	for _, l := range m.Layers {
		fmt.Printf("%T\n", l)
		for _, p := range l.Params() {
			fmt.Println(p.Name, p.Grad)
		}
	}

}
Output:

*layer.LSTMT
w variable([-0.007097596643213066])
w variable([[0.013515028138341746] [0.027030056276683492]])
w variable([[0.04252623292012907] [0.08505246584025813]])
b variable([0.05279536845172966])
b variable([0.013515028138341746])
b variable([0.04252623292012907])
w variable([[0.05279536845172966] [0.10559073690345933]])
b variable([-0.00757230286787535])
w variable([[-0.00757230286787535] [-0.0151446057357507]])
w variable([-0.0062508612982463485])
w variable([-0.017558407475346018])
w variable([0.0016808382857761302])
*layer.LinearT
b variable([2])
w variable([-1.1705639065492832])

func NewLSTM

func NewLSTM(hiddenSize, outSize int, opts ...LSTMOpts) *LSTM

func (*LSTM) Forward

func (m *LSTM) Forward(x *variable.Variable) *variable.Variable

func (*LSTM) ResetState

func (m *LSTM) ResetState()
Example
package main

import (
	"fmt"

	"github.com/itsubaki/autograd/model"
	"github.com/itsubaki/autograd/variable"
)

func main() {
	m := model.NewLSTM(1, 1)

	x := variable.New(1, 2)
	m.Forward(x)
	m.ResetState()
	m.Forward(x)

	for _, p := range m.Params() {
		fmt.Println(p.Name, p.Grad)
	}

}
Output:

w <nil>
b <nil>
w <nil>
b <nil>
w <nil>
b <nil>
w <nil>
b <nil>
w <nil>
b <nil>
w <nil>
w <nil>
w <nil>
w <nil>

type LSTMOpts

type LSTMOpts struct {
	Source randv2.Source
}

type MLP

type MLP struct {
	Activation Activation
	Model
}
Example
package main

import (
	"fmt"

	"github.com/itsubaki/autograd/model"
)

func main() {
	m := model.NewMLP([]int{1, 2, 3})

	for _, l := range m.Layers {
		fmt.Printf("%T\n", l)
	}

}
Output:

*layer.LinearT
*layer.LinearT
*layer.LinearT
Example (Backward)
package main

import (
	"fmt"

	F "github.com/itsubaki/autograd/function"
	"github.com/itsubaki/autograd/model"
	"github.com/itsubaki/autograd/rand"
	"github.com/itsubaki/autograd/variable"
)

func main() {
	m := model.NewMLP([]int{5, 1}, model.MLPOpts{
		Activation: F.ReLU,
		Source:     rand.Const(),
	})

	x := variable.New(1, 2)
	y := m.Forward(x)
	y.Backward()

	for _, p := range m.Params() {
		fmt.Println(p.Name, p.Grad)
	}

}
Output:

w variable([[0 0 -0.11785627150007956 -0.17275376822987032 -0.1452836777854009] [0 0 -0.23571254300015912 -0.34550753645974064 -0.2905673555708018]])
b variable([1])
w variable([[0] [0] [1.62887541989766] [0.7662326556923662] [1.9766127473463149]])
b variable([0 0 -0.11785627150007956 -0.17275376822987032 -0.1452836777854009])
Example (Cleargrads)
package main

import (
	"fmt"

	F "github.com/itsubaki/autograd/function"
	"github.com/itsubaki/autograd/model"
	"github.com/itsubaki/autograd/rand"
	"github.com/itsubaki/autograd/variable"
)

func main() {
	m := model.NewMLP([]int{5, 1}, model.MLPOpts{
		Activation: F.ReLU,
		Source:     rand.Const(),
	})

	x := variable.New(1, 2)
	y := m.Forward(x)
	y.Backward()
	m.Cleargrads()

	for _, p := range m.Params() {
		fmt.Println(p.Name, p.Grad)
	}

}
Output:

b <nil>
w <nil>
b <nil>
w <nil>

func NewMLP

func NewMLP(outSize []int, opts ...MLPOpts) *MLP

func (*MLP) Forward

func (m *MLP) Forward(x *variable.Variable) *variable.Variable

type MLPOpts

type MLPOpts struct {
	Activation Activation
	Source     randv2.Source
}

type Model

type Model struct {
	Layers []L.Layer
}

func (*Model) Cleargrads

func (m *Model) Cleargrads()

func (Model) Params

func (m Model) Params() L.Parameters

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL