From 858c51c0cde0ec92c26cc72f4dcf52fbc5510bdb Mon Sep 17 00:00:00 2001 From: itsubaki <1759459+itsubaki@users.noreply.github.com> Date: Wed, 11 Oct 2023 22:58:20 +0900 Subject: [PATCH] Update some files --- function/softmax.go | 3 +-- function/softmax_cross_entropy.go | 12 ++++++------ variable/max.go | 5 +---- 3 files changed, 8 insertions(+), 12 deletions(-) diff --git a/function/softmax.go b/function/softmax.go index 76f8251..a84b46e 100644 --- a/function/softmax.go +++ b/function/softmax.go @@ -28,9 +28,8 @@ func (f *SoftmaxT) Forward(x ...*variable.Variable) []*variable.Variable { func (f *SoftmaxT) Backward(gy ...*variable.Variable) []*variable.Variable { gyy := Mul(gy[0], f.y) // gyy = gy * y sum := SumTo(len(gyy.Data), 1)(gyy) // sum = sum(gx, axis=1) - gx := Sub(gyy, Mul(f.y, sum)) // gx = gyy - y * sum return []*variable.Variable{ - gx, + Sub(gyy, Mul(f.y, sum)), // gyy - y * sum } } diff --git a/function/softmax_cross_entropy.go b/function/softmax_cross_entropy.go index 7dd5b32..52bfa8d 100644 --- a/function/softmax_cross_entropy.go +++ b/function/softmax_cross_entropy.go @@ -29,14 +29,14 @@ func (f *SoftmaxCrossEntropyT) Forward(x ...*variable.Variable) []*variable.Vari } func (f *SoftmaxCrossEntropyT) Backward(gy ...*variable.Variable) []*variable.Variable { - xs := variable.Shape(f.x) - N, C := xs[0], xs[1] + shape := variable.Shape(f.x) + N, C := shape[0], shape[1] + + t := variable.NewOf(onehot(f.t.Data[0], C)...) // t = onehot(t, C) + y := Softmax(f.x) // y = softmax(x) - t := variable.NewOf(onehot(f.t.Data[0], C)...) // t = onehot(t, C) - y := Softmax(f.x) // y = softmax(x) - gx := Mul(Sub(y, t), MulC(1.0/float64(N), gy[0])) // (y - t) * gy / N return []*variable.Variable{ - gx, + Mul(Sub(y, t), MulC(1.0/float64(N), gy[0])), // (y - t) * gy / N } } diff --git a/variable/max.go b/variable/max.go index 536591d..6936bef 100644 --- a/variable/max.go +++ b/variable/max.go @@ -25,11 +25,8 @@ func (f *MaxT) Forward(x ...*Variable) []*Variable { func (f *MaxT) Backward(gy ...*Variable) []*Variable { mask := NewOf(matrix.F2(f.x.Data, f.y.Data, cond)...) - gybr := BroadcastTo(Shape(mask)...)(gy[0]) - - gx := Mul(gybr, mask) return []*Variable{ - gx, + Mul(gy[0], mask), } }