5 people like it.
Like the snippet!
Linear Regression Gradient Descent
Liner Regression with no reglarization
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
|
open MathNet.Numerics.LinearAlgebra.Double
let gds_orig (X: Matrix) (y: Vector) (theta: Vector) (alpha: float) =
let m = y.Count
let n = X.ColumnCount
let mf = float m
let oldtheta = theta.Clone()
for j = 0 to n - 1 do
printfn "Regression for feature: %i" j
let acc =
[|
for i = 1 to m - 1 do
yield async { return ((X.Row(i) * oldtheta) - y.[i]) * (X.[i,j]) }
|] |> Async.Parallel |> Async.RunSynchronously
|> Array.sum
|> (fun acc -> acc * (alpha / mf))
theta.[j] <- oldtheta.[j] - acc
theta
let gdstep_comb (X: Matrix) (y: Vector) (theta: Vector) (alpha: float) =
theta
|> Vector.mapi (fun j v ->
printfn "Feature: %i" j
let acc =
X
|> Matrix.sumRowsBy (fun i xrow -> (xrow * theta - y.[i]) * X.[i,j])
|> (fun acc -> acc * (alpha / float y.Count))
in v - acc)
let gdstep_golf (X: Matrix) (y: Vector) (θ: Vector) (α: float) =
θ |> Vector.mapi (fun j v ->
v - (X |> Matrix.sumRowsBy (fun i xr ->
(xr * θ - y.[i]) * X.[i,j])
|> (*) (α / float y.Count)))
let gdstep_vector (X: Matrix) (y: Vector) (θ: Vector) (α: float) =
θ - ((X * θ - y) * X * (α / float y.Count))
|
namespace MathNet
namespace MathNet.Numerics
namespace MathNet.Numerics.LinearAlgebra
namespace MathNet.Numerics.LinearAlgebra.Double
val gds_orig : X:Matrix -> y:Vector -> theta:Vector -> alpha:float -> Vector
Full name: Script.gds_orig
val X : Matrix
type Matrix =
inherit Matrix<float>
member Cholesky : unit -> Cholesky<float>
member CoerceZero : threshold:float -> unit
member ColumnAbsoluteSums : unit -> Vector<float>
member ColumnNorms : norm:float -> Vector<float>
member ColumnSums : unit -> Vector<float>
member ConjugateTranspose : unit -> Matrix<float>
member Evd : ?symmetricity:Symmetricity -> Evd<float>
member FrobeniusNorm : unit -> float
member GramSchmidt : unit -> GramSchmidt<float>
member InfinityNorm : unit -> float
...
Full name: MathNet.Numerics.LinearAlgebra.Double.Matrix
val y : Vector
type Vector =
inherit Vector<float>
member AbsoluteMaximum : unit -> float
member AbsoluteMaximumIndex : unit -> int
member AbsoluteMinimum : unit -> float
member AbsoluteMinimumIndex : unit -> int
member CoerceZero : threshold:float -> unit
member InfinityNorm : unit -> float
member L1Norm : unit -> float
member L2Norm : unit -> float
member MaximumIndex : unit -> int
member MinimumIndex : unit -> int
...
Full name: MathNet.Numerics.LinearAlgebra.Double.Vector
val theta : Vector
val alpha : float
Multiple items
val float : value:'T -> float (requires member op_Explicit)
Full name: Microsoft.FSharp.Core.Operators.float
--------------------
type float = System.Double
Full name: Microsoft.FSharp.Core.float
--------------------
type float<'Measure> = float
Full name: Microsoft.FSharp.Core.float<_>
val m : int
property MathNet.Numerics.LinearAlgebra.Vector.Count: int
val n : int
property MathNet.Numerics.LinearAlgebra.Matrix.ColumnCount: int
val mf : float
val oldtheta : MathNet.Numerics.LinearAlgebra.Vector<float>
MathNet.Numerics.LinearAlgebra.Vector.Clone() : MathNet.Numerics.LinearAlgebra.Vector<float>
val j : int
val printfn : format:Printf.TextWriterFormat<'T> -> 'T
Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.printfn
val acc : float
val i : int
val async : AsyncBuilder
Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.async
MathNet.Numerics.LinearAlgebra.Matrix.Row(index: int) : MathNet.Numerics.LinearAlgebra.Vector<float>
MathNet.Numerics.LinearAlgebra.Matrix.Row(index: int, result: MathNet.Numerics.LinearAlgebra.Vector<float>) : unit
MathNet.Numerics.LinearAlgebra.Matrix.Row(rowIndex: int, columnIndex: int, length: int) : MathNet.Numerics.LinearAlgebra.Vector<float>
MathNet.Numerics.LinearAlgebra.Matrix.Row(rowIndex: int, columnIndex: int, length: int, result: MathNet.Numerics.LinearAlgebra.Vector<float>) : unit
Multiple items
type Async
static member AsBeginEnd : computation:('Arg -> Async<'T>) -> ('Arg * AsyncCallback * obj -> IAsyncResult) * (IAsyncResult -> 'T) * (IAsyncResult -> unit)
static member AwaitEvent : event:IEvent<'Del,'T> * ?cancelAction:(unit -> unit) -> Async<'T> (requires delegate and 'Del :> Delegate)
static member AwaitIAsyncResult : iar:IAsyncResult * ?millisecondsTimeout:int -> Async<bool>
static member AwaitTask : task:Task<'T> -> Async<'T>
static member AwaitWaitHandle : waitHandle:WaitHandle * ?millisecondsTimeout:int -> Async<bool>
static member CancelDefaultToken : unit -> unit
static member Catch : computation:Async<'T> -> Async<Choice<'T,exn>>
static member FromBeginEnd : beginAction:(AsyncCallback * obj -> IAsyncResult) * endAction:(IAsyncResult -> 'T) * ?cancelAction:(unit -> unit) -> Async<'T>
static member FromBeginEnd : arg:'Arg1 * beginAction:('Arg1 * AsyncCallback * obj -> IAsyncResult) * endAction:(IAsyncResult -> 'T) * ?cancelAction:(unit -> unit) -> Async<'T>
static member FromBeginEnd : arg1:'Arg1 * arg2:'Arg2 * beginAction:('Arg1 * 'Arg2 * AsyncCallback * obj -> IAsyncResult) * endAction:(IAsyncResult -> 'T) * ?cancelAction:(unit -> unit) -> Async<'T>
static member FromBeginEnd : arg1:'Arg1 * arg2:'Arg2 * arg3:'Arg3 * beginAction:('Arg1 * 'Arg2 * 'Arg3 * AsyncCallback * obj -> IAsyncResult) * endAction:(IAsyncResult -> 'T) * ?cancelAction:(unit -> unit) -> Async<'T>
static member FromContinuations : callback:(('T -> unit) * (exn -> unit) * (OperationCanceledException -> unit) -> unit) -> Async<'T>
static member Ignore : computation:Async<'T> -> Async<unit>
static member OnCancel : interruption:(unit -> unit) -> Async<IDisposable>
static member Parallel : computations:seq<Async<'T>> -> Async<'T []>
static member RunSynchronously : computation:Async<'T> * ?timeout:int * ?cancellationToken:CancellationToken -> 'T
static member Sleep : millisecondsDueTime:int -> Async<unit>
static member Start : computation:Async<unit> * ?cancellationToken:CancellationToken -> unit
static member StartAsTask : computation:Async<'T> * ?taskCreationOptions:TaskCreationOptions * ?cancellationToken:CancellationToken -> Task<'T>
static member StartChild : computation:Async<'T> * ?millisecondsTimeout:int -> Async<Async<'T>>
static member StartChildAsTask : computation:Async<'T> * ?taskCreationOptions:TaskCreationOptions -> Async<Task<'T>>
static member StartImmediate : computation:Async<unit> * ?cancellationToken:CancellationToken -> unit
static member StartWithContinuations : computation:Async<'T> * continuation:('T -> unit) * exceptionContinuation:(exn -> unit) * cancellationContinuation:(OperationCanceledException -> unit) * ?cancellationToken:CancellationToken -> unit
static member SwitchToContext : syncContext:SynchronizationContext -> Async<unit>
static member SwitchToNewThread : unit -> Async<unit>
static member SwitchToThreadPool : unit -> Async<unit>
static member TryCancelled : computation:Async<'T> * compensation:(OperationCanceledException -> unit) -> Async<'T>
static member CancellationToken : Async<CancellationToken>
static member DefaultCancellationToken : CancellationToken
Full name: Microsoft.FSharp.Control.Async
--------------------
type Async<'T>
Full name: Microsoft.FSharp.Control.Async<_>
static member Async.Parallel : computations:seq<Async<'T>> -> Async<'T []>
static member Async.RunSynchronously : computation:Async<'T> * ?timeout:int * ?cancellationToken:System.Threading.CancellationToken -> 'T
module Array
from Microsoft.FSharp.Collections
val sum : array:'T [] -> 'T (requires member ( + ) and member get_Zero)
Full name: Microsoft.FSharp.Collections.Array.sum
val gdstep_comb : X:Matrix -> y:Vector -> theta:Vector -> alpha:float -> 'a
Full name: Script.gdstep_comb
val gdstep_golf : X:Matrix -> y:Vector -> θ:Vector -> α:float -> 'a
Full name: Script.gdstep_golf
val θ : Vector
val α : float
val gdstep_vector : X:Matrix -> y:Vector -> θ:Vector -> α:float -> MathNet.Numerics.LinearAlgebra.Vector<float>
Full name: Script.gdstep_vector
More information