model

package
v0.0.6 Latest Latest
Warning

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

Go to latest
Published: Dec 26, 2025 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
	// contains filtered or unexported fields
}
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.WithLSTMSource(rand.Const()))

	x := variable.New(
		1, 2,
	).Reshape(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 k, v := range l.Params().Seq2() {
			fmt.Println(k, v.Grad)
		}
	}

}
Output:

*layer.LSTMT
h2f.w variable(-0.007097596643213066)
h2i.w variable(-0.0062508612982463485)
h2o.w variable(-0.017558407475346018)
h2u.w variable(0.0016808382857761302)
x2f.b variable(0.013515028138341746)
x2f.w variable[2 1]([0.013515028138341746 0.027030056276683492])
x2i.b variable(0.04252623292012907)
x2i.w variable[2 1]([0.04252623292012907 0.08505246584025813])
x2o.b variable(0.05279536845172966)
x2o.w variable[2 1]([0.05279536845172966 0.10559073690345933])
x2u.b variable(-0.00757230286787535)
x2u.w variable[2 1]([-0.00757230286787535 -0.0151446057357507])
*layer.LinearT
b variable(2)
w variable(-1.1705639065492832)
Example (Batch)
package main

import (
	"fmt"

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

func main() {
	m := model.NewLSTM(5, 1, model.WithLSTMSource(rand.Const()))

	x := variable.New(
		1, 2,
		3, 4,

		5, 6,
		7, 8,
	).Reshape(2, 2, 2)

	y := m.Forward(x)
	y.Backward()
	m.Cleargrads()

	fmt.Println(y.Shape())
	fmt.Println(x.Grad.Shape())

	for k, v := range m.Params().Seq2() {
		fmt.Println(k, v.Shape())
	}

}
Output:

[2 2 1]
[2 2 2]
0.h2f.w [5 5]
0.h2i.w [5 5]
0.h2o.w [5 5]
0.h2u.w [5 5]
0.x2f.b [1 5]
0.x2f.w [2 5]
0.x2i.b [1 5]
0.x2i.w [2 5]
0.x2o.b [1 5]
0.x2o.w [2 5]
0.x2u.b [1 5]
0.x2u.w [2 5]
1.b [1 1]
1.w [5 1]

func NewLSTM

func NewLSTM(hiddenSize, outSize int, opts ...LSTMOptionFunc) *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,
	).Reshape(1, 2)

	m.Forward(x)
	m.ResetState()
	m.Forward(x)

	for k, v := range m.Params().Seq2() {
		fmt.Println(k, v.Grad)
	}

}
Output:

0.h2f.w <nil>
0.h2i.w <nil>
0.h2o.w <nil>
0.h2u.w <nil>
0.x2f.b <nil>
0.x2f.w <nil>
0.x2i.b <nil>
0.x2i.w <nil>
0.x2o.b <nil>
0.x2o.w <nil>
0.x2u.b <nil>
0.x2u.w <nil>
1.b <nil>
1.w <nil>

type LSTMOptionFunc

type LSTMOptionFunc func(*LSTM)

func WithLSTMSource

func WithLSTMSource(s randv2.Source) LSTMOptionFunc

type Layer added in v0.0.2

type Layer interface {
	First(x ...*variable.Variable) *variable.Variable
	Forward(x ...*variable.Variable) []*variable.Variable
	Params() L.Parameters
	Cleargrads()
}

type MLP

type MLP struct {
	Activation Activation

	Model
	// contains filtered or unexported fields
}
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.WithMLPSource(rand.Const()),
		model.WithMLPActivation(F.ReLU),
	)

	x := variable.New(
		1, 2,
	).Reshape(1, 2)

	y := m.Forward(x)
	y.Backward()

	for k, v := range m.Params().Seq2() {
		fmt.Println(k, v.Grad)
	}

}
Output:

0.b variable[1 5]([-0 -0 -0.11785627150007956 -0.17275376822987032 -0.1452836777854009])
0.w variable[2 5]([0 0 -0.11785627150007956 -0.17275376822987032 -0.1452836777854009 0 0 -0.23571254300015912 -0.34550753645974064 -0.2905673555708018])
1.b variable(1)
1.w variable[5 1]([0 0 1.62887541989766 0.7662326556923662 1.9766127473463149])
Example (Batch)
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.WithMLPSource(rand.Const()),
		model.WithMLPActivation(F.ReLU),
	)

	x := variable.New(
		1, 2,
		3, 4,

		5, 6,
		7, 8,
	).Reshape(2, 2, 2)

	y := m.Forward(x)
	y.Backward()
	m.Cleargrads()

	fmt.Println(y.Shape())
	fmt.Println(x.Grad.Shape())

	for k, v := range m.Params().Seq2() {
		fmt.Println(k, v.Shape())
	}

}
Output:

[2 2 1]
[2 2 2]
0.b [1 5]
0.w [2 5]
1.b [1 1]
1.w [5 1]
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.WithMLPSource(rand.Const()),
		model.WithMLPActivation(F.ReLU),
	)

	x := variable.New(
		1, 2,
	).Reshape(1, 2)

	y := m.Forward(x)
	y.Backward()
	m.Cleargrads()

	for k, v := range m.Params().Seq2() {
		fmt.Println(k, v.Grad)
	}

}
Output:

0.b <nil>
0.w <nil>
1.b <nil>
1.w <nil>

func NewMLP

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

func (*MLP) Forward

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

type MLPOptionFunc

type MLPOptionFunc func(*MLP)

func WithMLPActivation

func WithMLPActivation(activation Activation) MLPOptionFunc

func WithMLPSource

func WithMLPSource(s randv2.Source) MLPOptionFunc

type MLPOpts

type MLPOpts struct {
	Activation Activation
	Source     randv2.Source
}

type Model

type Model struct {
	Layers []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