-
Notifications
You must be signed in to change notification settings - Fork 0
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
09d74e2
commit 6b958b4
Showing
11 changed files
with
462 additions
and
20 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,19 @@ | ||
module KS.FsDNN.Tests.Net | ||
|
||
open KS.FsDNN | ||
open MathNet.Numerics.LinearAlgebra | ||
open Xunit | ||
|
||
[<Fact>] | ||
let ``Forward propagate - 1 hidden layer`` () = | ||
let il = { nx = 2 } | ||
let ll = SoftMax {| nc = 2 |} | ||
|
||
let net = Net.makeLayers 0 il [] ll | ||
|
||
let X = matrix [[ 0.5 ]; [ 1.3 ]] | ||
|
||
let scores = Net.forward net X | ||
|
||
let expected = [[ 0.10289726 ]; [ 0.89710273 ]] | ||
scores |> shouldBeEquivalent expected |
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,27 @@ | ||
namespace KS.FsDNN.Tests | ||
|
||
[<AutoOpen>] | ||
module TestHelpers = | ||
|
||
open FluentAssertions | ||
open FluentAssertions.Equivalency | ||
open System | ||
open MathNet.Numerics.LinearAlgebra | ||
|
||
[<Literal>] | ||
let precision = 5e-7; | ||
|
||
let doubleComparisonOptions<'TExpectation> (o: EquivalencyAssertionOptions<'TExpectation>): EquivalencyAssertionOptions<'TExpectation> = | ||
let action = fun (ctx: IAssertionContext<double>) -> ctx.Subject.Should().BeApproximately(ctx.Expectation, precision, String.Empty, Array.Empty<obj>()) |> ignore | ||
o.Using<double>(Action<IAssertionContext<double>>(action)).WhenTypeIs<double>() | ||
|
||
let shouldBeEquivalent (a: double list list) (m: Matrix<double>) = | ||
m.ToArray().Should().BeEquivalentTo(array2D a, doubleComparisonOptions, String.Empty, Array.empty) |> ignore | ||
|
||
let shouldBeEquivalentM2 (a: double [,]) (m: Matrix<double>) = | ||
m.ToArray().Should().BeEquivalentTo(a, doubleComparisonOptions, String.Empty, Array.empty) |> ignore | ||
|
||
let shouldBeApproximately (a1: double) (a2) = | ||
a1.Should().BeApproximately(a2, precision, String.Empty, Array.empty) |> ignore | ||
|
||
let toM (rs: double list list) = rs |> array2D |> CreateMatrix.DenseOfArray |
This file was deleted.
Oops, something went wrong.
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,6 @@ | ||
namespace KS.FsDNN | ||
|
||
module Constants = | ||
|
||
[<Literal>] | ||
let DivideBy0Guard = 1E-12 |
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,24 @@ | ||
namespace KS.FsDNN | ||
|
||
open MathNet.Numerics.LinearAlgebra | ||
|
||
[<AutoOpen>] | ||
module Domain = | ||
|
||
type InputLayer = | ||
{ nx: int } | ||
|
||
type Layer = | ||
| FullyConnected of {| n: int |} | ||
|
||
type LossLayer = | ||
| SoftMax of {| nc: int |} | ||
|
||
type Transformer = | ||
{ n: int | ||
W: Matrix<double> | ||
b: Matrix<double> | ||
forward: Matrix<double> -> Matrix<double> } | ||
|
||
type Net = | ||
{ Transformers: Transformer list } |
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 was deleted.
Oops, something went wrong.
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,33 @@ | ||
namespace KS.FsDNN | ||
|
||
open System | ||
open MathNet.Numerics.Distributions | ||
open MathNet.Numerics.LinearAlgebra | ||
|
||
module Net = | ||
|
||
let _makeHiddenTransformers seed (inputLayer: InputLayer) (hiddenLayers: Layer list) (lossLayer: LossLayer): Transformer list = | ||
let n = match lossLayer with | SoftMax svm -> svm.nc | ||
|
||
let W = DenseMatrix.random<double> n inputLayer.nx (Normal(0., 1., Random(seed + 1))) | ||
let b = DenseMatrix.create n 1 0. | ||
|
||
[ { n = n; W = W; b = b; forward = fun a -> W * a + b } ] | ||
|
||
let _makeLossTransformer (lossLayer: LossLayer): Transformer = | ||
let n = match lossLayer with | SoftMax svm -> svm.nc | ||
let f (x: Matrix<double>) = | ||
let expx = x.PointwiseExp(); | ||
expx.Divide(expx.ColumnSums().Sum() + Constants.DivideBy0Guard) | ||
{ n = n; W = null; b = null; forward = f } | ||
|
||
let makeLayers seed (inputLayer: InputLayer) (hiddenLayers: Layer list) (lossLayer: LossLayer) = | ||
let its = { n = inputLayer.nx; W = null; b = null; forward = id } | ||
let hts = _makeHiddenTransformers seed inputLayer hiddenLayers lossLayer | ||
let lts = _makeLossTransformer lossLayer | ||
|
||
{ Transformers = List.concat [ [ its ]; hts; [ lts ] ] } | ||
|
||
let forward (net: Net) (X: Matrix<double>): Matrix<double> = | ||
net.Transformers | ||
|> List.fold (fun acc e -> e.forward acc) X |
Oops, something went wrong.