Skip to content

Commit 7a1a275

Browse files
committed
Add the excludeUnmatchedFineGrainedNetwork parameter
1 parent 95f7399 commit 7a1a275

File tree

1 file changed

+66
-10
lines changed
  • benchmark/src/jmh/scala/com/thoughtworks/deeplearning/benchmark

1 file changed

+66
-10
lines changed

benchmark/src/jmh/scala/com/thoughtworks/deeplearning/benchmark/benchmark.scala

+66-10
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ object benchmark {
4040

4141
@Threads(value = 1)
4242
@State(Scope.Benchmark)
43-
class FourLayer {
43+
class BranchNetBenchmark {
4444

45-
@Param(Array("8"))
45+
@Param(Array("8", "16"))
4646
protected var batchSize: Int = _
4747

4848
@Param(Array("1", "2", "4"))
@@ -54,6 +54,9 @@ object benchmark {
5454
@Param(Array("16", "8"))
5555
protected var numberOfBranches: Int = _
5656

57+
@Param(Array("false", "true"))
58+
protected var excludeUnmatchedFineGrainedNetwork: Boolean = _
59+
5760
private implicit var executionContext: ExecutionContextExecutorService = _
5861

5962
private lazy val batches = {
@@ -103,7 +106,7 @@ object benchmark {
103106
}
104107
}
105108

106-
val fineProbabilityModel = Seq.fill(Cifar100.NumberOfCoarseClasses)(new (INDArrayLayer => INDArrayLayer) {
109+
val fineScoreModels = Seq.fill(Cifar100.NumberOfCoarseClasses)(new (INDArrayLayer => INDArrayLayer) {
107110
object Dense2 extends (INDArrayLayer => INDArrayLayer) {
108111

109112
object Dense1 extends (INDArrayLayer => INDArrayLayer) {
@@ -127,24 +130,77 @@ object benchmark {
127130
val bias = INDArrayWeight(Nd4j.randn(1, Cifar100.NumberOfFineClassesPerCoarseClass))
128131

129132
def apply(coarseFeatures: INDArrayLayer) = {
130-
val scores = Dense2(coarseFeatures) dot weight + bias
131-
132-
val expScores = exp(scores)
133-
expScores / expScores.sum(1)
133+
Dense2(coarseFeatures) dot weight + bias
134134
}
135135
})
136136

137-
def loss(coarseLabel: Int, batch: Batch): DoubleLayer = {
137+
// val fineProbabilityModel = Seq.fill(Cifar100.NumberOfCoarseClasses)(new (INDArrayLayer => INDArrayLayer) {
138+
// object Dense2 extends (INDArrayLayer => INDArrayLayer) {
139+
//
140+
// object Dense1 extends (INDArrayLayer => INDArrayLayer) {
141+
// val weight = INDArrayWeight(Nd4j.randn(numberOfHiddenFeatures, numberOfHiddenFeatures))
142+
// val bias = INDArrayWeight(Nd4j.randn(1, numberOfHiddenFeatures))
143+
//
144+
// def apply(coarseFeatures: INDArrayLayer) = {
145+
// max(coarseFeatures dot weight + bias, 0.0)
146+
// }
147+
// }
148+
//
149+
// val weight = INDArrayWeight(Nd4j.randn(numberOfHiddenFeatures, numberOfHiddenFeatures))
150+
// val bias = INDArrayWeight(Nd4j.randn(1, numberOfHiddenFeatures))
151+
//
152+
// def apply(coarseFeatures: INDArrayLayer) = {
153+
// max(Dense1(coarseFeatures) dot weight + bias, 0.0)
154+
// }
155+
// }
156+
//
157+
// val weight = INDArrayWeight(Nd4j.randn(numberOfHiddenFeatures, Cifar100.NumberOfFineClassesPerCoarseClass))
158+
// val bias = INDArrayWeight(Nd4j.randn(1, Cifar100.NumberOfFineClassesPerCoarseClass))
159+
//
160+
// def apply(coarseFeatures: INDArrayLayer) = {
161+
// val scores = Dense2(coarseFeatures) dot weight + bias
162+
//
163+
// val expScores = exp(scores)
164+
// expScores / expScores.sum(1)
165+
// }
166+
// })
167+
168+
def loss(expectedCoarseLabel: Int, batch: Batch): DoubleLayer = {
138169
def crossEntropy(prediction: INDArrayLayer, expectOutput: INDArray): DoubleLayer = {
139170
-(hyperparameters.log(prediction) * expectOutput).mean
140171
}
141172

142173
val Array(batchSize, width, height, channels) = batch.pixels.shape()
143174
val coarseFeatures = CoarseFeatures(batch.pixels.reshape(batchSize, width * height * channels))
144175
val coarseProbabilities = CoarseProbabilityModel(coarseFeatures)
145-
val fineProbabilities = fineProbabilityModel(coarseLabel)(coarseFeatures)
146176

147-
crossEntropy(coarseProbabilities, batch.coarseClasses) + crossEntropy(fineProbabilities, batch.localFineClasses)
177+
crossEntropy(coarseProbabilities, batch.coarseClasses) + {
178+
if (excludeUnmatchedFineGrainedNetwork) {
179+
val fineScores = fineScoreModels(expectedCoarseLabel)(coarseFeatures)
180+
val expScores = exp(fineScores)
181+
val fineProbabilities = expScores / expScores.sum(1)
182+
crossEntropy(fineProbabilities, batch.localFineClasses)
183+
} else {
184+
val expScoresByCoarseLabel = for (coarseLabel <- 0 until Cifar100.NumberOfCoarseClasses) yield {
185+
val fineScores = fineScoreModels(expectedCoarseLabel)(coarseFeatures)
186+
exp(fineScores)
187+
}
188+
val expSum = expScoresByCoarseLabel.map(_.sum(1)).reduce(_ + _)
189+
val lossPerCoarseLabel = for ((expScores, coarseLabel) <- expScoresByCoarseLabel.zipWithIndex) yield {
190+
val fineProbabilities = expScores / expSum
191+
192+
crossEntropy(
193+
fineProbabilities,
194+
if (coarseLabel == expScoresByCoarseLabel) {
195+
batch.localFineClasses
196+
} else {
197+
Nd4j.zeros(batchSize, Cifar100.NumberOfFineClassesPerCoarseClass)
198+
}
199+
)
200+
}
201+
lossPerCoarseLabel.reduce(_ + _)
202+
}
203+
}
148204
}
149205

150206
def train(coarseLabel: Int, batch: Batch) = {

0 commit comments

Comments
 (0)