Copyright | (c) Sam Stites 2017 |
---|---|
License | BSD3 |
Maintainer | sam@stites.io |
Stability | experimental |
Portability | non-portable |
Safe Haskell | None |
Language | Haskell2010 |
Linear layers
Synopsis
- newtype Linear i o = Linear {
- getTensors :: (Tensor '[i, o], Tensor '[o])
- update_ :: (KnownDim i, KnownDim o) => Linear i o -> HsReal -> Linear i o -> IO ()
- update :: (KnownDim i, KnownDim o) => Linear i o -> HsReal -> Linear i o -> Linear i o
- weights :: Linear i o -> Tensor '[i, o]
- bias :: Linear i o -> Tensor '[o]
- inputSize :: forall i o. KnownDim i => Linear i o -> Int
- outputSize :: forall i o kW dW. KnownDim o => Linear i o -> Int
- mkLinear :: (KnownDim i, KnownDim o) => (forall d. Dimensions d => IO (Tensor d)) -> IO (Linear i o)
- linear :: forall s i o. Reifies s W => All KnownDim '[i, o] => BVar s (Linear i o) -> BVar s (Tensor '[i]) -> BVar s (Tensor '[o])
- linearBatch :: forall s i o b. Reifies s W => All KnownDim '[b, i, o] => BVar s (Linear i o) -> BVar s (Tensor '[b, i]) -> BVar s (Tensor '[b, o])
- linearBatchIO :: forall i o b. All KnownDim '[b, i, o] => Linear i o -> Tensor '[b, i] -> IO (Tensor '[b, o], Tensor '[b, o] -> IO (Linear i o, Tensor '[b, i]))
- linearBatchWithIO :: forall i o b. All KnownDim '[b, i, o] => Maybe (Tensor '[b, o]) -> Maybe (Tensor '[b, i]) -> Maybe (Linear i o) -> Linear i o -> Tensor '[b, i] -> IO (Tensor '[b, o], Tensor '[b, o] -> IO (Linear i o, Tensor '[b, i]))
Documentation
datatype representing a linear layer with bias. Represents
y = Ax + b
.
Linear | |
|
Instances
Eq (Linear i o) Source # | |
(KnownDim i, KnownDim o) => Num (Linear i o) Source # | |
Defined in Torch.Indef.Static.NN.Linear | |
(KnownDim i, KnownDim o) => Show (Linear i o) Source # | |
Generic (Linear i o) Source # | |
(KnownDim i, KnownDim o) => Backprop (Linear i o) Source # | |
(KnownDim i, KnownDim o) => Pairwise (Linear i o) HsReal Source # | |
type Rep (Linear i o) Source # | |
Defined in Torch.Indef.Static.NN.Linear type Rep (Linear i o) = D1 (MetaData "Linear" "Torch.Indef.Static.NN.Linear" "hasktorch-indef-0.0.1.0-inplace-2pb660JGMmy35M7wFEyvsQ" True) (C1 (MetaCons "Linear" PrefixI True) (S1 (MetaSel (Just "getTensors") NoSourceUnpackedness NoSourceStrictness DecidedLazy) (Rec0 (Tensor (i ': (o ': ([] :: [Nat]))), Tensor (o ': ([] :: [Nat])))))) |
:: (KnownDim i, KnownDim o) | |
=> Linear i o | layer to update |
-> HsReal | learning rate |
-> Linear i o | gradient |
-> IO () |
update a Conv2d layer inplace
:: (KnownDim i, KnownDim o) | |
=> Linear i o | layer to update |
-> HsReal | learning rate |
-> Linear i o | gradient |
-> Linear i o | updated layer |
update a Conv2d layer
outputSize :: forall i o kW dW. KnownDim o => Linear i o -> Int Source #
The output size of a linear layer
mkLinear :: (KnownDim i, KnownDim o) => (forall d. Dimensions d => IO (Tensor d)) -> IO (Linear i o) Source #
linear :: forall s i o. Reifies s W => All KnownDim '[i, o] => BVar s (Linear i o) -> BVar s (Tensor '[i]) -> BVar s (Tensor '[o]) Source #
Linear
module = nn.Linear(inputDimension, outputDimension, [bias = true])
Applies a linear transformation to the incoming data, i.e. y = Ax + b. The input tensor given in forward(input) must be either a vector (1D tensor) or matrix (2D tensor). If the input is a matrix, then each row is assumed to be an input sample of given batch. The layer can be used without bias by setting bias = false.
You can create a layer in the following way:
module = nn.Linear(10, 5) -- 10 inputs, 5 outputs
Usually this would be added to a network of some kind, e.g.:
mlp = nn.Sequential() mlp:add(module)
The weights and biases (A and b) can be viewed with:
print(module.weight) print(module.bias)
The gradients for these weights can be seen with:
print(module.gradWeight) print(module.gradBias)
As usual with nn modules, applying the linear transformation is performed with:
x = torch.Tensor(10) -- 10 inputs y = module:forward(x)
linearBatch :: forall s i o b. Reifies s W => All KnownDim '[b, i, o] => BVar s (Linear i o) -> BVar s (Tensor '[b, i]) -> BVar s (Tensor '[b, o]) Source #
linear
with a batch dimension