-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTraining.lua
More file actions
155 lines (143 loc) · 5.13 KB
/
Training.lua
File metadata and controls
155 lines (143 loc) · 5.13 KB
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
require 'FillData'
require 'ReadDataset'
require 'TensorSaveLoad'
require 'TestModel'
function TrainingFromFile(model, listIn, listOut, selector, trainer)
local etime = sys.clock();
-- TRAINING
model:training()
local nFile = table.getn(listIn)
for noFile = 1,nFile do
if Settings.DispFile then plog.info("Training from File: " .. noFile .. "/" .. nFile) end
local dataIn = NactiData(Settings.ListFolder .. listIn[i]);
local dataOut = NactiData(Settings.ListFolder .. listOut[i]);
selector(model, dataIn, dataOut, trainer);
end
log.info("Training " .. Settings.ModelName .. " completed in " .. sys.clock() - etime)
end
function Training(model, dataIn, dataOut, selector, trainer)
local etime = sys.clock();
-- TRAINING
model:training()
-- local nFile = dataIn:size(1)
local nFile = table.getn(dataIn)
for noFile = 1,nFile do
if Settings.DispFile then plog.info("Training from File: " .. noFile .. "/" .. nFile) end
-- selector(model, dataIn:select(1,noFile), dataOut:select(1,noFile), trainer);
selector(model, dataIn[noFile], dataOut[noFile], trainer);
end
log.info("Training " .. Settings.ModelName .. " completed in " .. sys.clock() - etime)
end
function TrainingRaw(model, DataIn, DataOut)
local etime = sys.clock();
-- TRAINING
model:training()
-- local nFile = DataIn:size(1)
local nFile = table.getn(DataIn)
for noFile = 1,nFile do
if Settings.DispFile then plog.info("Training from File: " .. noFile .. "/" .. nFile) end
-- prepare inputs & outputs tensors
-- local dataIn=DataIn:select(1,noFile)
-- local dataOut=DataOut:select(1,noFile)
local dataIn=DataIn[noFile]
local dataOut=DataOut[noFile]
for batch = 1,Settings.BatchN do
if Settings.DispBatch then plog.info("Training Batch: " .. batch .. "/" .. Settings.BatchN) end
local tIn ={}
table.insert(tIn,Settings.BatchSize)
local tOut ={}
table.insert(tOut,Settings.BatchSize)
for i = 2,dataIn:dim() do
table.insert(tIn,dataIn:size(i))
end
for i = 2,dataOut:dim() do
table.insert(tOut,dataOut:size(i))
end
local inputs = torch.Tensor(torch.LongStorage(tIn))
local outputs = torch.Tensor(torch.LongStorage(tOut))
-- process batches
for i = 1, Settings.BatchSize do
local dataM=torch.random(1,dataIn:size(1))
inputs[i] = dataIn:select(1,dataM)
outputs[i] = dataOut:select(1,dataM)
end
TrainOptim(model, inputs, outputs);
end
end
log.info("Training " .. Settings.ModelName .. " completed in " .. sys.clock() - etime)
end
function TestModelFromFile(model, listIn, listOut, selector, tester)
local etime = sys.clock();
-- EVALUATION
model:evaluate();
local err = 0
local all = 0
local nFile = table.getn(listIn)
for noFile = 1,nFile do
if Settings.DispFile then plog.info("Testing from File: " .. noFile .. "/" .. nFile) end
local dataIn = NactiData(Settings.ListFolder .. listIn[nFile]);
local dataOut = NactiData(Settings.ListFolder .. listOut[nFile]);
local f = function(model, dataIn, dataOut)
local e,a = tester(model, dataIn, dataOut);
err=err+e
all=all+a
end
selector(model, dataIn, dataOut, f);
end
-- logs & export model
err = 100*err/all
log.info("Testing " .. Settings.ModelName .. " completed in " .. sys.clock() - etime)
return err
end
function TestModel(model, dataIn, dataOut, selector, tester)
local etime = sys.clock();
-- EVALUATION
model:evaluate();
local err = 0
local all = 0
local nFile = dataIn:size(1)
for noFile = 1,nFile do
if Settings.DispFile then plog.info("Testing from File: " .. noFile .. "/" .. nFile) end
local f = function(model, dataIn, dataOut)
local a,e = tester(model, dataIn, dataOut);
err=err+e
all=all+a
end
selector(model, dataIn:select(1,noFile), dataOut:select(1,noFile), f);
end
-- logs & export model
err = 100*err/all
log.info("Testing " .. Settings.ModelName .. " completed in " .. sys.clock() - etime)
return err
end
function TestModelX(model, dataIn, dataOut, selector, tester)
local etime = sys.clock();
-- EVALUATION
model:evaluate();
local err = torch.Tensor(Settings.OutputSize,Settings.OutputSize):fill(0)
local all = torch.Tensor(Settings.OutputSize):fill(0)
local errn = torch.Tensor(Settings.OutputSize):fill(0)
-- local nFile = dataIn:size(1)
local nFile = table.getn(dataIn)
for noFile = 1,nFile do
if Settings.DispFile then plog.info("Testing from File: " .. noFile .. "/" .. nFile) end
local f = function(model, dataIn, dataOut)
local a,e = tester(model, dataIn, dataOut);
err=err+e
all=all+a
end
selector(model, dataIn[noFile], dataOut[noFile], f);
end
-- logs & export model
for i=1,err:size(1) do
if all[i]==0 then
err[i]=0
else
--err[i]=100*err[i]:sum()/all[i]
err[i]=100*err[i]/all[i]
end
end
--err = 100*err/all
log.info("Testing " .. Settings.ModelName .. " completed in " .. sys.clock() - etime)
return err
end