Skip to content

Commit 34f3657

Browse files
authored
Merge pull request #59 from DiffSharp/arraySlicing
Correction to matrix slicing for start point of AddSubMatrix
2 parents 855b192 + b3358d9 commit 34f3657

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed

src/DiffSharp/AD.Float32.fs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1474,7 +1474,7 @@ and DM =
14741474
match d with
14751475
| DM(ap) -> DM(ap.[rowStart..rowFinish, colStart..colFinish])
14761476
| DMF(ap, at, ai) -> DMF(ap.[rowStart..rowFinish, colStart..colFinish], at.[rowStart..rowFinish, colStart..colFinish], ai)
1477-
| DMR(ap, _, ai, _) -> let cp = ap.[rowStart..rowFinish, colStart..colFinish] in DM.R(cp, Slice_DM(d, rowStart, rowFinish), ai)
1477+
| DMR(ap, _, ai, _) -> let cp = ap.[rowStart..rowFinish, colStart..colFinish] in DM.R(cp, Slice_DM(d, rowStart, colStart), ai)
14781478

14791479
member d.GetSlice(row, colStart, colFinish) =
14801480
let colStart = defaultArg colStart 0

src/DiffSharp/AD.Float64.fs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1474,7 +1474,7 @@ and DM =
14741474
match d with
14751475
| DM(ap) -> DM(ap.[rowStart..rowFinish, colStart..colFinish])
14761476
| DMF(ap, at, ai) -> DMF(ap.[rowStart..rowFinish, colStart..colFinish], at.[rowStart..rowFinish, colStart..colFinish], ai)
1477-
| DMR(ap, _, ai, _) -> let cp = ap.[rowStart..rowFinish, colStart..colFinish] in DM.R(cp, Slice_DM(d, rowStart, rowFinish), ai)
1477+
| DMR(ap, _, ai, _) -> let cp = ap.[rowStart..rowFinish, colStart..colFinish] in DM.R(cp, Slice_DM(d, rowStart, colStart), ai)
14781478

14791479
member d.GetSlice(row, colStart, colFinish) =
14801480
let colStart = defaultArg colStart 0

tests/DiffSharp.Tests/AD.Float32.fs

+20-1
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,23 @@ let ``Gradient descent``() =
5454
let g = (x*Wg)
5555
cos g.[0,0]
5656

57-
minimize lossFunction (DV.create 5 1.0f) //Smoke test
57+
minimize lossFunction (DV.create 5 1.0f) //Smoke test
58+
59+
60+
[<Property>]
61+
let ``Gradient descent (with arrays)``() =
62+
63+
let minimize (f:DV->D) (x0:DV) =
64+
let eta = 1e-2f
65+
let mutable W = x0
66+
for _ in [0..10] do
67+
let L,g = grad' f W
68+
W <- W - eta*g
69+
70+
let n = 5
71+
let lossFunction (w:DV) =
72+
let x = DM.init n n (fun i j -> w.[n*i+j])
73+
let x' = x.GetSlice(None, None, None, None)
74+
cos x'.[0,0]
75+
76+
minimize lossFunction (DV.create (n*n) 1.0f) //Smoke test

0 commit comments

Comments
 (0)