-
Notifications
You must be signed in to change notification settings - Fork 17
/
dnn.go
184 lines (157 loc) · 4.93 KB
/
dnn.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package youtube
import (
"encoding/json"
"github.com/auxten/go-ctr/model"
G "gorgonia.org/gorgonia"
"gorgonia.org/tensor"
)
const (
// magic numbers for din paper
att0_1 = 36
mlp0_1 = 200
mlp1_2 = 80
)
type YoutubeDnn struct {
g *G.ExprGraph
vm G.VM
uProfileDim, uBehaviorSize, uBehaviorDim int
iFeatureDim int
cFeatureDim int
//input nodes
xUserProfile, xUbMatrix, xItemFeature, xCtxFeature *G.Node
//learnable nodes
mlp0, mlp1, mlp2 *G.Node
d0, d1 float32 // dropout probabilities
out *G.Node
}
func (mlp *YoutubeDnn) In() G.Nodes {
return G.Nodes{mlp.xUserProfile, mlp.xUbMatrix, mlp.xItemFeature, mlp.xCtxFeature}
}
type mlpModel struct {
UProfileDim int `json:"uProfileDim"`
UBehaviorSize int `json:"uBehaviorSize"`
UBehaviorDim int `json:"uBehaviorDim"`
IFeatureDim int `json:"iFeatureDim"`
CFeatureDim int `json:"cFeatureDim"`
Mlp0 []float32 `json:"mlp0"`
Mlp1 []float32 `json:"mlp1"`
Mlp2 []float32 `json:"mlp2"`
}
func (mlp *YoutubeDnn) Marshal() (data []byte, err error) {
model := mlpModel{
UProfileDim: mlp.uProfileDim,
UBehaviorSize: mlp.uBehaviorSize,
UBehaviorDim: mlp.uBehaviorDim,
IFeatureDim: mlp.iFeatureDim,
CFeatureDim: mlp.cFeatureDim,
Mlp0: mlp.mlp0.Value().Data().([]float32),
Mlp1: mlp.mlp1.Value().Data().([]float32),
Mlp2: mlp.mlp2.Value().Data().([]float32),
}
return json.Marshal(model)
}
func NewYoutubeDnnFromJson(data []byte) (mlp *YoutubeDnn, err error) {
var m mlpModel
if err = json.Unmarshal(data, &m); err != nil {
return
}
var (
g = G.NewGraph()
uProfileDim = m.UProfileDim
uBehaviorSize = m.UBehaviorSize
uBehaviorDim = m.UBehaviorDim
iFeatureDim = m.IFeatureDim
cFeatureDim = m.CFeatureDim
mlp0_0 = uProfileDim + uBehaviorDim + iFeatureDim + cFeatureDim
)
mlp0 := G.NewMatrix(g, model.DT,
G.WithShape(mlp0_0, mlp0_1),
G.WithName("mlp0"),
G.WithValue(tensor.New(tensor.WithShape(mlp0_0, mlp0_1), tensor.WithBacking(m.Mlp0))),
)
mlp1 := G.NewMatrix(g, model.DT,
G.WithShape(mlp0_1, mlp1_2),
G.WithName("mlp1"),
G.WithValue(tensor.New(tensor.WithShape(mlp0_1, mlp1_2), tensor.WithBacking(m.Mlp1))),
)
mlp2 := G.NewMatrix(g, model.DT,
G.WithShape(mlp1_2, 1),
G.WithName("mlp2"),
G.WithValue(tensor.New(tensor.WithShape(mlp1_2, 1), tensor.WithBacking(m.Mlp2))),
)
mlp = &YoutubeDnn{
uProfileDim: uProfileDim,
uBehaviorSize: uBehaviorSize,
uBehaviorDim: uBehaviorDim,
iFeatureDim: iFeatureDim,
cFeatureDim: cFeatureDim,
g: g,
mlp0: mlp0,
mlp1: mlp1,
mlp2: mlp2,
}
return
}
func (mlp *YoutubeDnn) Vm() G.VM {
return mlp.vm
}
func (mlp *YoutubeDnn) SetVM(vm G.VM) {
mlp.vm = vm
}
func NewYoutubeDnn(
uProfileDim, uBehaviorSize, uBehaviorDim int,
iFeatureDim int,
cFeatureDim int,
) (mlp *YoutubeDnn) {
g := G.NewGraph()
mlp0 := G.NewMatrix(g, G.Float32, G.WithShape(uProfileDim+uBehaviorDim+iFeatureDim+cFeatureDim, mlp0_1), G.WithName("mlp0"), G.WithInit(G.Gaussian(0, 1.0)))
mlp1 := G.NewMatrix(g, G.Float32, G.WithShape(mlp0_1, mlp1_2), G.WithName("mlp1"), G.WithInit(G.Gaussian(0, 1.0)))
mlp2 := G.NewMatrix(g, G.Float32, G.WithShape(mlp1_2, 1), G.WithName("mlp2"), G.WithInit(G.Gaussian(0, 1.0)))
return &YoutubeDnn{
uProfileDim: uProfileDim,
uBehaviorSize: uBehaviorSize,
uBehaviorDim: uBehaviorDim,
iFeatureDim: iFeatureDim,
cFeatureDim: cFeatureDim,
g: g,
d0: 0.003,
d1: 0.003,
mlp0: mlp0,
mlp1: mlp1,
mlp2: mlp2,
}
}
func (mlp *YoutubeDnn) Graph() *G.ExprGraph {
return mlp.g
}
func (mlp *YoutubeDnn) Out() *G.Node {
return mlp.out
}
func (mlp *YoutubeDnn) Learnable() G.Nodes {
return G.Nodes{mlp.mlp0, mlp.mlp1, mlp.mlp2}
}
//Fwd ...
// xUserProfile: [batchSize, userProfileDim]
// xUbMatrix: [batchSize, uBehaviorSize* uBehaviorDim]
// xUserBehaviors: [batchSize, uBehaviorSize, uBehaviorDim]
// xItemFeature: [batchSize, iFeatureDim]
// xContextFeature: [batchSize, cFeatureDim]
func (mlp *YoutubeDnn) Fwd(xUserProfile, ubMatrix, xItemFeature, xCtxFeature *G.Node, batchSize, uBehaviorSize, uBehaviorDim int) (err error) {
// user behaviors
xUserBehaviors := G.Must(G.Reshape(ubMatrix, tensor.Shape{batchSize, uBehaviorSize, uBehaviorDim}))
//avg pooling for user behaviors
xUserBehaviorAvg := G.Must(G.Mean(xUserBehaviors, 1))
// concat
x := G.Must(G.Concat(1, xUserProfile, xUserBehaviorAvg, xItemFeature, xCtxFeature))
// mlp
mlp0Out := G.Must(G.Sigmoid(G.Must(G.Mul(x, mlp.mlp0))))
mlp0Out = G.Must(G.Dropout(mlp0Out, float64(mlp.d0)))
mlp1Out := G.Must(G.Sigmoid(G.Must(G.Mul(mlp0Out, mlp.mlp1))))
mlp1Out = G.Must(G.Dropout(mlp1Out, float64(mlp.d1)))
mlp.out = G.Must(G.Sigmoid(G.Must(G.Mul(mlp1Out, mlp.mlp2))))
mlp.xUserProfile = xUserProfile
mlp.xItemFeature = xItemFeature
mlp.xCtxFeature = xCtxFeature
mlp.xUbMatrix = ubMatrix
return
}