-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
238 additions
and
137 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1 @@ | ||
package layer_test | ||
|
||
import ( | ||
"fmt" | ||
|
||
L "github.com/itsubaki/autograd/layer" | ||
) | ||
|
||
func ExampleLayer() { | ||
l := &L.Layer{} | ||
|
||
l.Add(L.Linear(1)) | ||
l.Add(L.Linear(2)) | ||
l.Add(L.Linear(3)) | ||
|
||
for _, v := range l.Params() { | ||
fmt.Println(v) | ||
} | ||
|
||
// Unordered output: | ||
// b([0]) | ||
// b([0 0]) | ||
// b([0 0 0]) | ||
} | ||
|
||
func ExampleLayer_forwarder() { | ||
l := &L.Layer{ | ||
Forwarder: L.Linear(1), | ||
} | ||
|
||
for _, v := range l.Params() { | ||
fmt.Println(v) | ||
} | ||
|
||
// Output: | ||
// b([0]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package layer | ||
|
||
import ( | ||
"math/rand" | ||
"time" | ||
|
||
F "github.com/itsubaki/autograd/function" | ||
"github.com/itsubaki/autograd/variable" | ||
) | ||
|
||
type RNNOpts struct { | ||
Source rand.Source | ||
} | ||
|
||
func RNN(hiddenSize int, opts ...RNNOpts) *RNNT { | ||
s := rand.NewSource(time.Now().UnixNano()) | ||
if len(opts) != 0 && opts[0].Source != nil { | ||
s = opts[0].Source | ||
} | ||
|
||
l := make(Layers) | ||
l.Add("x2h", Linear(hiddenSize, LinearOpts{Source: s})) | ||
l.Add("h2h", Linear(hiddenSize, LinearOpts{Source: s, NoBias: true})) | ||
|
||
return &RNNT{ | ||
hiddenSize: hiddenSize, | ||
Layers: l, | ||
} | ||
} | ||
|
||
type RNNT struct { | ||
hiddenSize int | ||
h *variable.Variable | ||
Layers | ||
} | ||
|
||
func (l *RNNT) ResetState() { | ||
l.h = nil | ||
} | ||
|
||
func (l *RNNT) First(x ...*variable.Variable) *variable.Variable { | ||
return l.Forward(x...)[0] | ||
} | ||
|
||
func (l *RNNT) Forward(x ...*variable.Variable) []*variable.Variable { | ||
if l.h == nil { | ||
l.h = F.Tanh(l.Layers["x2h"].First(x...)) | ||
return []*variable.Variable{ | ||
l.h, | ||
} | ||
} | ||
|
||
l.h = F.Tanh(F.Add(l.Layers["x2h"].First(x...), (l.Layers["h2h"].First(l.h)))) | ||
return []*variable.Variable{ | ||
l.h, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package layer_test | ||
|
||
import ( | ||
"fmt" | ||
"math/rand" | ||
|
||
L "github.com/itsubaki/autograd/layer" | ||
"github.com/itsubaki/autograd/variable" | ||
) | ||
|
||
func ExampleRNN() { | ||
l := L.RNN(5, L.RNNOpts{ | ||
Source: rand.NewSource(1), | ||
}) | ||
|
||
x := variable.New(1) | ||
y := l.Forward(x) | ||
fmt.Printf("%.4f\n", y[0].Data) | ||
|
||
for _, v := range l.Params() { | ||
fmt.Println(v) | ||
} | ||
|
||
// Unordered output: | ||
// [[-0.8437 -0.1257 -0.4785 0.9795 0.3120]] | ||
// b([0 0 0 0 0]) | ||
// w([-1.233758177597947 -0.12634751070237293 -0.5209945711531503 2.28571911769958 0.3228052526115799]) | ||
} | ||
|
||
func ExampleRNN_backward() { | ||
l := L.RNN(3, L.RNNOpts{ | ||
Source: rand.NewSource(1), | ||
}) | ||
|
||
x := variable.New(1) | ||
y := l.First(x) | ||
y.Backward() | ||
|
||
for _, v := range l.Params() { | ||
fmt.Println(v.Name, v.Grad) | ||
} | ||
|
||
y = l.First(x) | ||
y.Backward() | ||
|
||
for _, v := range l.Params() { | ||
fmt.Println(v.Name, v.Grad) | ||
} | ||
|
||
// Unordered output: | ||
// b variable([0.28822771944106673 0.984204675359483 0.7710690810213434]) | ||
// w variable([0.28822771944106673 0.984204675359483 0.7710690810213434]) | ||
// b variable([0.39396398439363184 1.6894261397851316 1.764807089541906]) | ||
// w variable([0.39396398439363184 1.6894261397851316 1.764807089541906]) | ||
// w variable([[-0.020396585815502664 -0.4758483540449156 -0.3614345140035484] [-0.003038443883017692 -0.07088630095596118 -0.053842270374037576] [-0.01156749092376784 -0.269867298688821 -0.204979916643439]]) | ||
} | ||
|
||
func ExampleRNN_cleargrads() { | ||
l := L.RNN(3, L.RNNOpts{ | ||
Source: rand.NewSource(1), | ||
}) | ||
|
||
x := variable.New(1) | ||
y := l.First(x) | ||
y.Backward() | ||
|
||
l.Cleargrads() | ||
for _, v := range l.Params() { | ||
fmt.Println(v.Name, v.Grad) | ||
} | ||
|
||
// Unordered output: | ||
// b <nil> | ||
// w <nil> | ||
} | ||
|
||
func ExampleRNNT_ResetState() { | ||
l := L.RNN(3) | ||
|
||
x := variable.New(1) | ||
l.Forward(x) // set hidden state | ||
l.ResetState() // reset hidden state | ||
l.Forward(x) // h2h is not used | ||
|
||
for _, v := range l.Params() { | ||
fmt.Println(v.Name) | ||
} | ||
|
||
// Output: | ||
// b | ||
// w | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.