Skip to content

Commit bb1d6f4

Browse files
authored
Merge pull request #215 from Atry/products
Add Products and HLists to Builtins
2 parents 130bdc0 + 767d115 commit bb1d6f4

File tree

5 files changed

+100
-21
lines changed

5 files changed

+100
-21
lines changed

build.sbt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ lazy val `plugins-Operators` = project
1818

1919
lazy val `plugins-HLists` = project.dependsOn(DeepLearning)
2020

21+
lazy val `plugins-Products` = project.dependsOn(`plugins-HLists`)
22+
2123
lazy val `plugins-FloatTraining` = project.dependsOn(`plugins-Training`)
2224

2325
lazy val `plugins-FloatLiterals` = project.dependsOn(`DeepLearning`)
@@ -40,7 +42,8 @@ lazy val `plugins-CumulativeFloatLayers` =
4042
`plugins-FloatTraining` % Test,
4143
`plugins-FloatLiterals` % Test,
4244
`plugins-FloatWeights` % Test,
43-
`plugins-ImplicitsSingleton` % Test
45+
`plugins-ImplicitsSingleton` % Test,
46+
`plugins-Products` % Test
4447
)
4548

4649
lazy val `plugins-Training` = project.dependsOn(DeepLearning)
@@ -116,6 +119,7 @@ lazy val `plugins-CumulativeDoubleLayers` =
116119

117120
lazy val `plugins-Builtins` =
118121
project.dependsOn(
122+
`plugins-Products`,
119123
`plugins-HLists`,
120124
`plugins-ImplicitsSingleton`,
121125
`plugins-Layers`,

plugins-Builtins/src/main/scala-2.11/com/thoughtworks/deeplearning/plugins/Builtins.scala

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,9 @@ trait Builtins
103103
with INDArrayLiterals
104104
with INDArrayWeights
105105
with INDArrayLayers
106-
with CumulativeINDArrayLayers {
106+
with CumulativeINDArrayLayers
107+
with HLists
108+
with Products {
107109

108110
trait ImplicitsApi
109111
extends super[Layers].ImplicitsApi
@@ -118,6 +120,8 @@ trait Builtins
118120
with super[INDArrayTraining].ImplicitsApi
119121
with super[INDArrayLiterals].ImplicitsApi
120122
with super[INDArrayLayers].ImplicitsApi
123+
with super[HLists].ImplicitsApi
124+
with super[Products].ImplicitsApi
121125

122126
type Implicits <: ImplicitsApi
123127

plugins-Builtins/src/main/scala-2.12/com/thoughtworks/deeplearning/plugins/Builtins.scala

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ trait Builtins
2020
with DoubleLiterals
2121
with DoubleWeights
2222
with DoubleLayers
23-
with CumulativeDoubleLayers {
23+
with CumulativeDoubleLayers
24+
with HLists
25+
with Products {
2426

2527
trait ImplicitsApi
2628
extends super[Layers].ImplicitsApi
@@ -32,6 +34,8 @@ trait Builtins
3234
with super[DoubleTraining].ImplicitsApi
3335
with super[DoubleLiterals].ImplicitsApi
3436
with super[DoubleLayers].ImplicitsApi
37+
with super[HLists].ImplicitsApi
38+
with super[Products].ImplicitsApi
3539

3640
type Implicits <: ImplicitsApi
3741

plugins-CumulativeFloatLayers/src/test/scala/com/thoughtworks/deeplearning/plugins/CumulativeFloatLayersSpec.scala

Lines changed: 45 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -379,31 +379,58 @@ final class CumulativeFloatLayersSpec
379379

380380
}
381381

382-
"EagerExecution" in {
382+
"eager execution" - {
383+
"single expression" in {
384+
val hyperparameters =
385+
Factory[FloatTraining with Operators with FloatLiterals with CumulativeFloatLayers with ImplicitsSingleton with FixedLearningRate]
386+
.newInstance(fixedLearningRate = 1.0f)
383387

384-
val hyperparameters =
385-
Factory[FloatTraining with Operators with FloatLiterals with CumulativeFloatLayers with ImplicitsSingleton with FixedLearningRate]
386-
.newInstance(fixedLearningRate = 1.0f)
388+
import hyperparameters.implicits._
387389

388-
import hyperparameters.implicits._
390+
val weight = hyperparameters.FloatWeight(1.0f)
389391

390-
val weight = hyperparameters.FloatWeight(1.0f)
392+
def myNetwork(input: Float): hyperparameters.FloatLayer = {
393+
6.7f + !(input + weight) + weight + 5.5f
394+
}
391395

392-
def myNetwork(input: Float): hyperparameters.FloatLayer = {
393-
6.7f + !(input + weight) + weight + 5.5f
394-
}
396+
def train(inputData: Float): Future[Float] = {
397+
myNetwork(inputData).train
398+
}
395399

396-
def train(inputData: Float): Future[Float] = {
397-
myNetwork(inputData).train
400+
for {
401+
_ <- train(1.0f)
402+
_ <- train(1.0f)
403+
_ <- train(1.0f)
404+
_ <- train(1.0f)
405+
_ <- train(1.0f)
406+
} yield weight.data should be(-4)
398407
}
399408

400-
for {
401-
_ <- train(1.0f)
402-
_ <- train(1.0f)
403-
_ <- train(1.0f)
404-
_ <- train(1.0f)
405-
_ <- train(1.0f)
406-
} yield weight.data should be(-4)
409+
"multiple expression" in {
410+
val hyperparameters =
411+
Factory[Products with FloatTraining with Operators with FloatLiterals with CumulativeFloatLayers with ImplicitsSingleton with FixedLearningRate]
412+
.newInstance(fixedLearningRate = 1.0f)
413+
414+
import hyperparameters.implicits._
407415

416+
val weight = hyperparameters.FloatWeight(1.0f)
417+
418+
def myNetwork(input: Float): hyperparameters.FloatLayer = {
419+
val (a, b, c) = !(input + weight, 2.0f, weight)
420+
6.7f + a + b + c + weight + 5.5f
421+
}
422+
423+
def train(inputData: Float): Future[Float] = {
424+
myNetwork(inputData).train
425+
}
426+
427+
for {
428+
_ <- train(1.0f)
429+
_ <- train(1.0f)
430+
_ <- train(1.0f)
431+
_ <- train(1.0f)
432+
_ <- train(1.0f)
433+
} yield weight.data should be(-4)
434+
}
408435
}
409436
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.thoughtworks.deeplearning
2+
package plugins
3+
import com.thoughtworks.continuation.UnitContinuation
4+
import com.thoughtworks.deeplearning.DeepLearning.Tape
5+
import com.thoughtworks.raii.asynchronous._
6+
import shapeless.{::, Generic, HList}
7+
import scalaz.syntax.all._
8+
import shapeless.ops.hlist.Tupler
9+
10+
/**
11+
* @author 杨博 (Yang Bo)
12+
*/
13+
trait Products extends HLists {
14+
15+
trait ImplicitsApi extends super.ImplicitsApi {
16+
17+
implicit def productDeepLearning[Operand, L, DataHList <: HList, DeltaHList <: HList, DataTuple, DeltaTuple](
18+
implicit generic: Generic.Aux[Operand, L],
19+
deepLearning: DeepLearning.Aux[L, DataHList, DeltaHList],
20+
dataTupler: Tupler.Aux[DataHList, DataTuple],
21+
deltaTupler: Tupler.Aux[DeltaHList, DeltaTuple],
22+
genericDelta: Generic.Aux[DeltaTuple, DeltaHList]
23+
): DeepLearning.Aux[Operand, DataTuple, DeltaTuple] =
24+
new DeepLearning[Operand] {
25+
type Data = DataTuple
26+
type Delta = DeltaTuple
27+
def forward(differentiable: Operand): Do[Tape[Data, Delta]] = {
28+
deepLearning.forward(generic.to(differentiable)).map {
29+
case Tape(hlistData, hlistForward) =>
30+
def forward(doDeltaTuple: Do[DeltaTuple]): UnitContinuation[Unit] = {
31+
hlistForward(doDeltaTuple.map(genericDelta.to))
32+
}
33+
Tape(dataTupler(hlistData), forward)
34+
}
35+
}
36+
}
37+
}
38+
39+
type Implicits <: ImplicitsApi
40+
}

0 commit comments

Comments
 (0)