-
-
Notifications
You must be signed in to change notification settings - Fork 168
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
1 parent
713f294
commit 63fbb25
Showing
6 changed files
with
143 additions
and
5 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
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,18 @@ | ||
package stats | ||
|
||
import "math" | ||
|
||
// Sigmoid returns the input values in the range of -1 to 1 | ||
// along the sigmoid or s-shaped curve, commonly used in | ||
// machine learning while training neural networks as an | ||
// activation function. | ||
func Sigmoid(input Float64Data) ([]float64, error) { | ||
if input.Len() == 0 { | ||
return Float64Data{}, EmptyInput | ||
} | ||
s := make([]float64, len(input)) | ||
for i, v := range input { | ||
s[i] = 1 / (1 + math.Exp(-v)) | ||
} | ||
return s, nil | ||
} |
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,41 @@ | ||
package stats | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
) | ||
|
||
func ExampleSigmoid() { | ||
s, _ := Sigmoid([]float64{3.0, 1.0, 0.2}) | ||
fmt.Println(s) | ||
// Output: [0.9525741268224334 0.7310585786300049 0.549833997312478] | ||
} | ||
|
||
func TestSigmoidEmptyInput(t *testing.T) { | ||
_, err := Sigmoid([]float64{}) | ||
if err != EmptyInputErr { | ||
t.Errorf("Should have returned empty input error") | ||
} | ||
} | ||
|
||
func TestSigmoid(t *testing.T) { | ||
sm, err := Sigmoid([]float64{-0.54761371, 17.04850603, 4.86054302}) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
|
||
a := 0.3664182235138545 | ||
if sm[0] != a { | ||
t.Errorf("%v != %v", sm[0], a) | ||
} | ||
|
||
a = 0.9999999605608187 | ||
if sm[1] != a { | ||
t.Errorf("%v != %v", sm[1], a) | ||
} | ||
|
||
a = 0.9923132671908277 | ||
if sm[2] != a { | ||
t.Errorf("%v != %v", sm[2], a) | ||
} | ||
} |
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,25 @@ | ||
package stats | ||
|
||
import "math" | ||
|
||
// SoftMax returns the input values in the range of 0 to 1 | ||
// with sum of all the probabilities being equal to one. It | ||
// is commonly used in machine learning neural networks. | ||
func SoftMax(input Float64Data) ([]float64, error) { | ||
if input.Len() == 0 { | ||
return Float64Data{}, EmptyInput | ||
} | ||
|
||
s := 0.0 | ||
c, _ := Max(input) | ||
for _, e := range input { | ||
s += math.Exp(e - c) | ||
} | ||
|
||
sm := make([]float64, len(input)) | ||
for i, v := range input { | ||
sm[i] = math.Exp(v-c) / s | ||
} | ||
|
||
return sm, nil | ||
} |
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,41 @@ | ||
package stats | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
) | ||
|
||
func ExampleSoftMax() { | ||
sm, _ := SoftMax([]float64{3.0, 1.0, 0.2}) | ||
fmt.Println(sm) | ||
// Output: [0.8360188027814407 0.11314284146556013 0.05083835575299916] | ||
} | ||
|
||
func TestSoftMaxEmptyInput(t *testing.T) { | ||
_, err := SoftMax([]float64{}) | ||
if err != EmptyInputErr { | ||
t.Errorf("Should have returned empty input error") | ||
} | ||
} | ||
|
||
func TestSoftMax(t *testing.T) { | ||
sm, err := SoftMax([]float64{3.0, 1.0, 0.2}) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
|
||
a := 0.8360188027814407 | ||
if sm[0] != a { | ||
t.Errorf("%v != %v", sm[0], a) | ||
} | ||
|
||
a = 0.11314284146556013 | ||
if sm[1] != a { | ||
t.Errorf("%v != %v", sm[1], a) | ||
} | ||
|
||
a = 0.05083835575299916 | ||
if sm[2] != a { | ||
t.Errorf("%v != %v", sm[1], a) | ||
} | ||
} |