Skip to content

Commit 1ff00a9

Browse files
committed
fixes to SFileSource
1 parent 24c1403 commit 1ff00a9

File tree

11 files changed

+52
-48
lines changed

11 files changed

+52
-48
lines changed

src/main/scala/BIDMach/Learner.scala

+18-3
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,8 @@ case class Learner(
182182
while (paused) Thread.sleep(1000);
183183
if (updater != null) updater.update(ipass, here, gprogress);
184184
}
185-
val scores = model.evalbatchg(mats, ipass, here);
185+
val tmpscores = model.evalbatchg(mats, ipass, here);
186+
val scores = if (tmpscores.ncols > 1) mean(tmpscores, 2) else tmpscores;
186187
if (datasink != null) datasink.put;
187188
reslist.append(scores.newcopy)
188189
samplist.append(here)
@@ -312,7 +313,8 @@ case class Learner(
312313
here += datasource.opts.batchSize
313314
bytes += mats.map(Learner.numBytes _).reduce(_+_);
314315
nsamps += mats(0).ncols;
315-
val scores = model.evalbatchg(mats, 0, here);
316+
val tmpscores = model.evalbatchg(mats, 0, here);
317+
val scores = if (tmpscores.ncols > 1) mean(tmpscores,2) else tmpscores;
316318
if (datasink != null) datasink.put
317319
reslist.append(scores.newcopy);
318320
samplist.append(here);
@@ -562,7 +564,20 @@ case class ParLearner(
562564
Learner.toCPU(models(0).modelmats)
563565
resetGPUs
564566
}
565-
println("Time=%5.4f secs, gflops=%4.2f, samples=%4.2g, MB/sec=%4.2g" format (gf._2, gf._1, 1.0*opts.nthreads*here, bytes/gf._2/1e6))
567+
val perfStr = ("%5.2f%%, score=%6.5f, secs=%3.1f, samps/s=%4.1f, gf=%4.1f, MB/s=%4.1f" format (
568+
100f*lastp,
569+
Learner.scoreSummary(reslist, lasti, reslist.length, opts.cumScore),
570+
gf._1,
571+
gf._2,
572+
bytes*1e9,
573+
bytes/gf._2*1e-6));
574+
val gpuStr = if (useGPU) {
575+
(0 until math.min(opts.nthreads, Mat.hasCUDA)).map((i)=>{
576+
setGPU(i);
577+
if (i==0) (", GPUmem=%3.2f" format GPUmem._1) else (", %3.2f" format GPUmem._1)
578+
});
579+
} else "";
580+
myLogger.info(perfStr + gpuStr);
566581
results = Learner.scores2FMat(reslist) on row(samplist.toList)
567582
}
568583

src/main/scala/BIDMach/datasources/SFileSource.scala

+8-8
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ class SFileSource(override val opts:SFileSource.Opts = new SFileSource.Options)
6060
val omat = omat0.asInstanceOf[SMat]
6161
val ioff = Mat.ioneBased
6262
var idone = done
63-
var innz = omat.nnz
63+
var onnz = 0;//omat.nnz
6464
val lims = fcounts
6565
val nfiles = fcounts.length
6666
val addConstFeat = opts.addConstFeat
@@ -73,7 +73,7 @@ class SFileSource(override val opts:SFileSource.Opts = new SFileSource.Options)
7373
val mat = inmat(j).asInstanceOf[SMat];
7474
var k = mat.jc(icol) - ioff;
7575
var lastk = mat.jc(icol+1) - ioff;
76-
val xoff = innz - k;
76+
val xoff = onnz - k;
7777
// println("here %d %d %d %d %d" format (k, mat.nrows, mat.ncols, lims.length, j))
7878
while (k < lastk && mat.ir(k)-ioff < lims(j)) {
7979
if (xoff + k >= omat.ir.length) {
@@ -89,19 +89,19 @@ class SFileSource(override val opts:SFileSource.Opts = new SFileSource.Options)
8989
}
9090
k += 1;
9191
}
92-
innz = xoff + k
92+
onnz = xoff + k
9393
j += 1
9494
}
9595
icol += 1
9696
idone += 1
9797
if (addConstFeat) {
98-
omat.ir(innz) = omat.nrows - 1 + ioff
99-
omat.data(innz) = 1
100-
innz += 1
98+
omat.ir(onnz) = omat.nrows - 1 + ioff
99+
omat.data(onnz) = 1
100+
onnz += 1
101101
}
102-
omat.jc(idone) = innz + ioff
102+
omat.jc(idone) = onnz + ioff
103103
}
104-
omat.nnz0 = innz
104+
omat.nnz0 = onnz
105105
omat
106106
}
107107

src/main/scala/BIDMach/models/FM.scala

+12-8
Original file line numberDiff line numberDiff line change
@@ -196,8 +196,7 @@ class FM(override val opts:FM.Opts = new FM.Options) extends RegressionModel(opt
196196
}
197197

198198
def meval(in:Mat):FMat = {
199-
val targs = targets * in
200-
min(targs, 1f, targs)
199+
val targs = if (targets.asInstanceOf[AnyRef] != null) {val targs0 = targets * in; min(targs0, 1f, targs0); targs0} else null
201200
val alltargs = if (targmap.asInstanceOf[AnyRef] != null) targmap * targs else targs
202201
val dweights = if (iweight.asInstanceOf[AnyRef] != null) iweight * in else null
203202
meval3(in, alltargs, dweights)
@@ -208,7 +207,8 @@ class FM(override val opts:FM.Opts = new FM.Options) extends RegressionModel(opt
208207
// Evaluate the positive/negative factorizations
209208

210209
def meval3(in:Mat, targ:Mat, dweights:Mat):FMat = {
211-
val ftarg = full(targ)
210+
val ftarg = if (targ.asInstanceOf[AnyRef] != null) full(targ) else null;
211+
val targs = if (targmap.asInstanceOf[AnyRef] != null && ftarg.asInstanceOf[AnyRef] != null) targmap * ftarg else ftarg;
212212
val vt1 = mm1 * in;
213213
var vt2:Mat = null;
214214
if (opts.dim2 > 0) {
@@ -229,11 +229,16 @@ class FM(override val opts:FM.Opts = new FM.Options) extends RegressionModel(opt
229229
}
230230
GLM.preds(eta, eta, mylinks, totflops);
231231
if (ogmats != null) ogmats(0) = eta;
232-
val v = GLM.llfun(eta, ftarg, mylinks, totflops);
233-
if (dweights.asInstanceOf[AnyRef] != null) {
234-
FMat(sum(v dweights, 2) / sum(dweights));
232+
233+
if (targs.asInstanceOf[AnyRef] != null) {
234+
val v = GLM.llfun(eta, targs, mylinks, totflops);
235+
if (dweights.asInstanceOf[AnyRef] != null) {
236+
FMat(sum(v dweights, 2) / sum(dweights));
237+
} else {
238+
FMat(mean(v, 2));
239+
}
235240
} else {
236-
FMat(mean(v, 2));
241+
row(0);
237242
}
238243
}
239244

@@ -322,7 +327,6 @@ object FM {
322327
val nopts = new PredOptions;
323328
nopts.batchSize = math.min(10000, mat1.ncols/30 + 1)
324329
nopts.links = mopts.links.copy;
325-
nopts.putBack = 1;
326330
nopts.dim1 = mopts.dim1;
327331
nopts.dim2 = mopts.dim2;
328332
nopts.strictFM = mopts.strictFM;

src/main/scala/BIDMach/models/GLM.scala

-4
Original file line numberDiff line numberDiff line change
@@ -906,7 +906,6 @@ object GLM {
906906
nopts.links = mopts.links
907907
mopts.links.set(d)
908908
nopts.batchSize = mopts.batchSize
909-
nopts.putBack = 1
910909
val model = new GLM(mopts)
911910
val mm = new Learner(
912911
new MatSource(Array(mat0, targ), mopts),
@@ -1017,7 +1016,6 @@ object GLM {
10171016
val model = model0.asInstanceOf[GLM]
10181017
val nopts = new PredOptions;
10191018
nopts.batchSize = math.min(10000, mat1.ncols/30 + 1)
1020-
nopts.putBack = 0
10211019
val newmod = new GLM(nopts);
10221020
newmod.refresh = false
10231021
newmod.copyFrom(model);
@@ -1070,7 +1068,6 @@ object GLM {
10701068
mopts.reg2weight = 1f
10711069
nopts.links = mopts.links
10721070
nopts.batchSize = mopts.batchSize
1073-
nopts.putBack = 1
10741071
val model = new GLM(mopts)
10751072
val mm = new Learner(
10761073
new MatSource(Array(mat0, targ), mopts),
@@ -1095,7 +1092,6 @@ object GLM {
10951092
nopts.batchSize = math.min(10000, mat1.ncols/30 + 1)
10961093
if (nopts.links == null) nopts.links = izeros(preds.nrows,1)
10971094
nopts.links.set(3)
1098-
nopts.putBack = 1
10991095
val nn = new Learner(
11001096
new MatSource(Array(mat1, preds), nopts),
11011097
model.asInstanceOf[GLM],

src/main/scala/BIDMach/models/Regression.scala

+3-1
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,10 @@ abstract class RegressionModel(override val opts:RegressionModel.Opts) extends M
3434
opts.targmap.nrows
3535
} else if (opts.targets.asInstanceOf[AnyRef] != null) {
3636
opts.targets.nrows
37-
} else {
37+
} else if (refresh) {
3838
mats(1).nrows
39+
} else {
40+
modelmats(0).nrows;
3941
}
4042
val sdat = (sum(data0,2).t + 0.5f).asInstanceOf[FMat]
4143
sp = sdat / sum(sdat)

src/main/scala/BIDMach/networks/Net.scala

+2-2
Original file line numberDiff line numberDiff line change
@@ -288,9 +288,9 @@ class Net(override val opts:Net.Opts = new Net.Options) extends Model(opts) {
288288
}
289289
layers(i).forward;
290290
}
291-
val scores = zeros(score_layers.length, 1);
291+
val scores = zeros(score_layers.length, batchSize);
292292
for (i <- 0 until score_layers.length) {
293-
scores(i) = score_layers(i).score.v;
293+
scores(i,?) = score_layers(i).score;
294294
}
295295
if (og_layers.asInstanceOf[AnyRef] != null) {
296296
for (i <- 0 until og_layers.length) {

src/main/scala/BIDMach/networks/layers/GLMLayer.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class GLMLayer(override val net:Net, override val opts:GLMNodeOpts = new GLMNode
4747

4848
override def score:FMat = {
4949
val v = if (target.asInstanceOf[AnyRef] != null) GLM.llfun(output, target, ilinks, totflops) else row(0);
50-
FMat(mean(mean(v, 2)));
50+
FMat(mean(v));
5151
}
5252

5353
override def clear = {

src/main/scala/BIDMach/networks/layers/Layer.scala

+2-3
Original file line numberDiff line numberDiff line change
@@ -535,11 +535,10 @@ object Layer {
535535

536536
def softmax(a:LayerTerm) = new SoftmaxLayer(null){inputs(0) = a};
537537

538-
def softmaxout(a:LayerTerm)(scoreType:Int=0, doVariance:Boolean=false, lossType:Int = 0, net:Net=null) = {
538+
def softmaxout(a:LayerTerm)(scoreType:Int=0, lossType:Int = 0, net:Net=null) = {
539539
val scoreTyp = scoreType;
540540
val lossTyp = lossType;
541-
val doVar = doVariance;
542-
new SoftmaxOutputLayer(net, new SoftmaxOutputNode{scoreType=scoreTyp; doVariance=doVar; lossType=lossTyp}){inputs(0) = a}
541+
new SoftmaxOutputLayer(net, new SoftmaxOutputNode{scoreType=scoreTyp; lossType=lossTyp}){inputs(0) = a}
543542
}
544543

545544
def softplus(a:LayerTerm) = new SoftplusLayer(null){inputs(0) = a};

src/main/scala/BIDMach/networks/layers/NegsampOutputLayer.scala

+2-2
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,8 @@ class NegsampOutputLayer(override val net:Net, override val opts:NegsampOutputNo
129129
if (coloffsets.asInstanceOf[AnyRef] == null) coloffsets = int(convertMat(irow(0->mprod.ncols)*mprod.nrows));
130130
val inds = int(target) + coloffsets;
131131
opts.scoreType match {
132-
case 2 => FMat(mean(ln(mprod(inds))));
133-
case 3 => FMat(mean(mprod(inds) == maxi(mprod)));
132+
case 2 => FMat(ln(mprod(inds)));
133+
case 3 => FMat(mprod(inds) == maxi(mprod));
134134
}
135135
}
136136
}

src/main/scala/BIDMach/networks/layers/Node.scala

+2-2
Original file line numberDiff line numberDiff line change
@@ -343,10 +343,10 @@ object Node {
343343

344344
def softmax(a:NodeTerm) = new SoftmaxNode{inputs(0) = a};
345345

346-
def softmaxout(a:NodeTerm)(scoreType:Int=0, doVar:Boolean=false, lossType:Int=0) = {
346+
def softmaxout(a:NodeTerm)(scoreType:Int=0, lossType:Int=0) = {
347347
val scoreTyp = scoreType;
348348
val lossTyp = lossType;
349-
new SoftmaxOutputNode{inputs(0) = a; scoreType=scoreTyp; doVariance = doVar; lossType = lossTyp}
349+
new SoftmaxOutputNode{inputs(0) = a; scoreType=scoreTyp; lossType = lossTyp}
350350
}
351351

352352
def softplus(a:NodeTerm) = new SoftplusNode{inputs(0) = a};

src/main/scala/BIDMach/networks/layers/SoftmaxOutputLayer.scala

+2-14
Original file line numberDiff line numberDiff line change
@@ -79,19 +79,9 @@ class SoftmaxOutputLayer(override val net:Net, override val opts:SoftmaxOutputNo
7979
if (coloffsets.asInstanceOf[AnyRef] == null) coloffsets = int(convertMat(irow(0->output.ncols)*output.nrows));
8080
val inds = int(target) + coloffsets;
8181
if (opts.scoreType == SoftmaxOutputLayer.AccuracyScore) {
82-
if (opts.doVariance) {
83-
val matches = (output(inds) == maxi(output));
84-
FMat(mean(matches)) on FMat(variance(matches));
85-
} else {
86-
FMat(mean(output(inds) == maxi(output)));
87-
}
82+
FMat(output(inds) == maxi(output));
8883
} else {
89-
if (opts.doVariance) {
90-
val out = ln(output(inds));
91-
FMat(mean(out)) on FMat(variance(out));
92-
} else {
93-
FMat(mean(ln(output(inds))));
94-
}
84+
FMat(ln(output(inds)));
9585
}
9686
}
9787

@@ -110,15 +100,13 @@ class SoftmaxOutputLayer(override val net:Net, override val opts:SoftmaxOutputNo
110100

111101
trait SoftmaxOutputNodeOpts extends NodeOpts {
112102
var scoreType = 0;
113-
var doVariance = false;
114103
var lossType = 0;
115104
var eps = 1e-5f;
116105

117106
def copyOpts(opts:SoftmaxOutputNodeOpts):SoftmaxOutputNodeOpts = {
118107
super.copyOpts(opts);
119108
opts.scoreType = scoreType;
120109
opts.lossType = lossType;
121-
opts.doVariance = doVariance;
122110
opts.eps = eps;
123111
opts;
124112
}

0 commit comments

Comments
 (0)