Skip to content

Commit 4dd13df

Browse files
Update
1 parent 4ac9242 commit 4dd13df

File tree

1 file changed

+23
-14
lines changed

1 file changed

+23
-14
lines changed

README.md

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,32 @@ Hasktorch Compose is an experimental library built on top of [hasktorch-skeleton
1717

1818
## Sequential
1919

20-
Use `:>>:` operator to join layers.
20+
Use `.*.*` operator of HList to join layers.
2121
This is an example of an MLP implementation, created by combining LinearSpec.
2222

2323
```haskell
24-
type MLPSpec = LinearSpec :>>: ReluSpec :>>: LinearSpec :>>: ReluSpec :>>: LinearSpec
25-
type MLP = Linear :>>: (Relu :>>: (Linear :>>: (Relu :>>: Linear)))
26-
27-
mlpSpec =
28-
LinearSpec 784 64 :>>:
29-
ReluSpec :>>:
30-
LinearSpec 64 32) :>>:
31-
ReluSpec :>>:
32-
LinearSpec 32 10 :>>:
33-
LogSoftMaxSpec
34-
35-
mlp :: (Randomizable MLPSpec MLP, HasForward MLP Tensor Tensor) => MLP -> Tensor -> Tensor
36-
mlp model input = forward model input
24+
newtype MLPSpec = MLPSpec (HList [LinearSpec, ReluSpec, LinearSpec, ReluSpec, LinearSpec, LogSoftMaxSpec]) deriving (Generic, Show, Eq)
25+
newtype MLP = MLP (HList [Linear, Relu, Linear, Relu, Linear, LogSoftMax]) deriving (Generic, Show, Eq)
26+
27+
mlpSpec :: MLPSpec
28+
mlpSpec = MLPSpec $
29+
LinearSpec 784 64 .*.
30+
ReluSpec .*.
31+
LinearSpec 64 32 .*.
32+
ReluSpec .*.
33+
LinearSpec 32 10 .*.
34+
LogSoftMaxSpec .*.
35+
HNil
36+
37+
instance HasForward MLP Tensor Tensor where
38+
forward (MLP model) = forward model
39+
forwardStoch (MLP model) = forwardStoch model
40+
41+
instance Randomizable MLPSpec MLP where
42+
sample (MLPSpec spec) = MLP <$> sample spec
43+
44+
mlp :: MLP -> Tensor -> Tensor
45+
mlp = forward
3746
```
3847

3948
## Extract layer

0 commit comments

Comments
 (0)