-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathHSMClass.lua
More file actions
executable file
·26 lines (20 loc) · 998 Bytes
/
Copy pathHSMClass.lua
File metadata and controls
executable file
·26 lines (20 loc) · 998 Bytes
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
--[[
This code is borrowed from: https://github.com/yoonkim/lstm-char-cnn
]]--
local HSMClass = {}
function HSMClass.hsm(input_size, n_clusters, n_max_class_in_cluster)
--inputs[1] is the input (batch_size by input_size)
--inputs[2] is the target cluster (batch_size)
local inputs = {nn.Identity()(), nn.Identity()()}
local class_bias_layer = nn.LookupTable(n_clusters, n_max_class_in_cluster)
local class_vec_layer = nn.LookupTable(n_clusters, input_size*n_max_class_in_cluster)
local class_mat = nn.View(n_max_class_in_cluster, input_size)(
class_vec_layer(inputs[2]))
class_bias_layer.name = 'class_bias'
class_vec_layer.name = 'class_weight'
local input_mat = nn.View(input_size, 1)(inputs[1])
local class_scores = nn.Squeeze()(nn.MM()({class_mat, input_mat}))
local output = nn.LogSoftMax()(nn.CAddTable()({class_bias_layer(inputs[2]), class_scores}))
return nn.gModule(inputs, {output})
end
return HSMClass