-
Notifications
You must be signed in to change notification settings - Fork 81
/
Copy pathsgd.go
93 lines (86 loc) · 2.88 KB
/
sgd.go
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/*
* Copyright (c) 2016 Salle, Alexandre <[email protected]>
* Author: Salle, Alexandre <[email protected]>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package main
// #include "sgdcgo.h"
import "C"
import (
"unsafe"
)
var wSubwordIdxs, wSubwordOffsets []idxUint
func sgdStepCgoInit() {
for _, w := range vocabList {
wSubwordOffsets = append(wSubwordOffsets, idxUint(len(wSubwordIdxs)))
for _, swIdx := range w.subwords {
wSubwordIdxs = append(wSubwordIdxs, swIdx)
}
}
C.sgdStepCgoInit(
C.idxUint(vocabSize),
C.realPtr(unsafe.Pointer(&mVec[0])),
C.realPtr(unsafe.Pointer(&mCtx[0])),
C.idxUint(len(wSubwordIdxs)),
C.idxUintPtr(unsafe.Pointer(&wSubwordIdxs[0])),
C.idxUintPtr(unsafe.Pointer(&wSubwordOffsets[0])),
C.idxUint(dim),
)
}
// zVec (gradient for word vector) is supplied by each thread to reduce GC.
func sgdStepCgoBatch(wIdx []idxUint, cIdx []idxUint, y []real, zVec []real, alpha real, n idxUint) real {
return float64(
C.sgdStepCgoBatch(
C.idxUintPtr(unsafe.Pointer(&wIdx[0])),
C.idxUintPtr(unsafe.Pointer(&cIdx[0])),
C.realPtr(unsafe.Pointer(&y[0])),
C.realPtr(unsafe.Pointer(&zVec[0])),
C.real(alpha),
C.idxUint(n)))
}
// zVec (gradient for word vector) is supplied by each thread to reduce GC.
func sgdStep(mapw, mapc *word, y real, zVec []real, alpha real) real {
for j := idxUint(0); j < dim; j++ {
zVec[j] = 0
}
c := mapc.idx
var dot real
zSize := real(len(mapw.subwords))
for _, sw := range mapw.subwords {
for j := idxUint(0); j < dim; j++ {
zVec[j] += mVec[sw*dim+j]
}
}
for j := idxUint(0); j < dim; j++ {
zVec[j] /= zSize
dot += zVec[j] * mCtx[c*dim+j]
}
g := dot - y
err := 0.5 * g * g
g *= alpha
for j := idxUint(0); j < dim; j++ {
mVecG := g * mCtx[c*dim+j] / zSize
mCtxG := g * zVec[j]
for _, sw := range mapw.subwords {
mVec[sw*dim+j] -= mVecG
}
mCtx[c*dim+j] -= mCtxG
}
return err
}