aboutsummaryrefslogtreecommitdiff
path: root/nerv/examples/gen_global_transf.lua
diff options
context:
space:
mode:
Diffstat (limited to 'nerv/examples/gen_global_transf.lua')
-rw-r--r--nerv/examples/gen_global_transf.lua62
1 files changed, 62 insertions, 0 deletions
diff --git a/nerv/examples/gen_global_transf.lua b/nerv/examples/gen_global_transf.lua
new file mode 100644
index 0000000..c4a3b42
--- /dev/null
+++ b/nerv/examples/gen_global_transf.lua
@@ -0,0 +1,62 @@
+if #arg < 1 then
+ return
+end
+
+dofile(arg[1])
+
+gconf.mmat_type = nerv.MMatrixFloat
+gconf.cumat_type = nerv.CuMatrixFloat
+local scp_file = gconf.tr_scp
+local loc_type = nerv.ParamRepo.LOC_TYPES.ON_HOST
+local reader_spec = make_readers(scp_file)[1]
+local reader = reader_spec.reader
+local width = reader_spec.data['main_scp']
+local mean = gconf.mmat_type(1, width)
+local std = gconf.mmat_type(1, width)
+local colsum = gconf.mmat_type(1, width)
+local total = 0.0
+local EPS = 1e-7
+
+mean:fill(0)
+std:fill(0)
+
+local cnt = 0
+while (true) do
+ ret = reader:get_data()
+ if ret == nil then
+ break
+ end
+
+ local utt = ret['main_scp']
+ colsum = utt:colsum()
+ mean:add(mean, colsum, 1, 1)
+
+ utt:mul_elem(utt, utt)
+ colsum = utt:colsum()
+ std:add(std, colsum, 1, 1)
+
+ total = total + utt:nrow()
+ cnt = cnt + 1
+ if cnt == 1000 then
+ nerv.info("accumulated %d utterances", cnt)
+ cnt = 0
+ end
+end
+
+local bparam = nerv.BiasParam("bias0", gconf)
+bparam.trans = gconf.mmat_type(1, width)
+mean:add(mean,mean, -1.0 / total, 0) -- -E(X)
+bparam.trans:copy_fromh(mean)
+
+mean:mul_elem(mean, mean) -- E^2(X)
+std:add(std, mean, 1 / total, -1) -- sigma ^ 2
+
+for i = 0, width - 1 do
+ std[0][i] = math.sqrt(std[0][i] + EPS)
+ std[0][i] = 1 / (std[0][i] + EPS)
+end
+
+local wparam = nerv.BiasParam("window0", gconf)
+wparam.trans = std
+local pr = nerv.ParamRepo({bparam, wparam}, loc_type)
+pr:export("global_transf.nerv", nil)