hasktorch-gradually-typed-0.2.0.0: experimental project for hasktorch
Safe HaskellSafe-Inferred
LanguageHaskell2010

Torch.GraduallyTyped.Tensor.MathOperations.Reduction

Synopsis

Documentation

>>> import Torch.GraduallyTyped.Prelude.List (SList (..))
>>> import Torch.GraduallyTyped
>>> import Prelude hiding (all, any)

type ReductionErrorMessage reduction by dims = (("Cannot apply '" <> reduction) <> "' on the dimension matching") % ("" % (((" '" <> by) <> "'") % ("" % ("in the shape" % ("" % (((" '" <> dims) <> "'.") % "")))))) Source #

type family ReductionCheckF reduction by dims result where ... Source #

Equations

ReductionCheckF reduction by dims 'Nothing = TypeError (ReductionErrorMessage reduction by dims) 
ReductionCheckF _ _ _ ('Just dims') = dims' 

type family BoolReductionF reduction selectDim shape where ... Source #

all :: forall requiresGradient layout device dataType shape m. MonadThrow m => Tensor requiresGradient layout device dataType shape -> m (Tensor requiresGradient layout device ('DataType 'Bool) ('Shape '[])) Source #

Tests if all element in input evaluates to True.

>>> g <- sMkGenerator (SDevice SCPU) 0
>>> shape = SShape $ SName @"*" :&: SSize @2 :|: SName @"*" :&: SSize @4 :|: SNil
>>> (t, _) <- sRandn (TensorSpec (SGradient SWithoutGradient) (SLayout SDense) (SDevice SCPU) (SDataType SFloat) shape) g
>>> t' <- all =<< bool t
>>> :type t'
t'
  :: Tensor
       ('Gradient 'WithoutGradient)
       ('Layout 'Dense)
       ('Device 'CPU)
       ('DataType 'Bool)
       ('Shape '[])

sAllDim :: forall selectDim gradient layout device dataType shape shape' m. (MonadThrow m, shape' ~ BoolReductionF "all" selectDim shape, Catch shape') => SSelectDim selectDim -> Tensor gradient layout device dataType shape -> m (Tensor gradient layout device ('DataType 'Bool) shape') Source #

Reduces each row of the input tensor in the selected dimension to True if all elements in the row evaluate to True and False otherwise. For a version that accepts non-singleton parameters see allDim.

>>> g <- sMkGenerator (SDevice SCPU) 0
>>> shape = SShape $ SName @"*" :&: SSize @2 :|: SName @"*" :&: SSize @4 :|: SNil
>>> (t, _) <- sRandn (TensorSpec (SGradient SWithoutGradient) (SLayout SDense) (SDevice SCPU) (SDataType SFloat) shape) g
>>> t' <- sAllDim (SSelectDim (SByIndex @1)) =<< bool t
>>> :type t'
t'
  :: Tensor
       ('Gradient 'WithoutGradient)
       ('Layout 'Dense)
       ('Device 'CPU)
       ('DataType 'Bool)
       ('Shape '[ 'Dim ('Name "*") ('Size 2), 'Dim ('Name "*") ('Size 1)])
>>> sAllDim (SUncheckedSelectDim (ByIndex 3)) t
*** Exception: HasktorchException "Exception: Dimension out of range (expected to be in range of [-2, 1], but got 3)...

type AllDimF selectDim shape = BoolReductionF "all" selectDim shape Source #

allDim :: forall selectDim gradient layout device dataType shape shape' m. (SingI selectDim, MonadThrow m, shape' ~ AllDimF selectDim shape, Catch shape') => Tensor gradient layout device dataType shape -> m (Tensor gradient layout device ('DataType 'Bool) shape') Source #

Reduces each row of the input tensor in the selected dimension to True if all elements in the row evaluate to True and False otherwise. For a version that accepts singleton parameters see sAllDim.

>>> g <- sMkGenerator (SDevice SCPU) 0
>>> type Shape' = 'Shape '[ 'Dim ('Name "*") ('Size 2), 'Dim ('Name "*") ('Size 4) ]
>>> (t, _) <- randn @('Gradient 'WithoutGradient) @('Layout 'Dense) @('Device 'CPU) @('DataType 'Float) @Shape' g
>>> t' <- allDim @('SelectDim ('ByIndex 1)) =<< bool t
>>> :type t'
t'
  :: Tensor
       ('Gradient 'WithoutGradient)
       ('Layout 'Dense)
       ('Device 'CPU)
       ('DataType 'Bool)
       ('Shape '[ 'Dim ('Name "*") ('Size 2), 'Dim ('Name "*") ('Size 1)])

any :: forall requiresGradient layout device dataType shape m. MonadThrow m => Tensor requiresGradient layout device dataType shape -> m (Tensor requiresGradient layout device ('DataType 'Bool) ('Shape '[])) Source #

Tests if any element in input evaluates to True.

>>> g <- sMkGenerator (SDevice SCPU) 0
>>> shape = SShape $ SName @"*" :&: SSize @2 :|: SName @"*" :&: SSize @4 :|: SNil
>>> (t, _) <- sRandn (TensorSpec (SGradient SWithoutGradient) (SLayout SDense) (SDevice SCPU) (SDataType SFloat) shape) g
>>> t' <- any =<< bool t
>>> :type t'
t'
  :: Tensor
       ('Gradient 'WithoutGradient)
       ('Layout 'Dense)
       ('Device 'CPU)
       ('DataType 'Bool)
       ('Shape '[])

type AnyDimF selectDim shape = BoolReductionF "any" selectDim shape Source #

sAnyDim :: forall selectDim gradient layout device shape dataType shape' m. (MonadThrow m, shape' ~ AnyDimF selectDim shape, Catch shape') => SSelectDim selectDim -> Tensor gradient layout device dataType shape -> m (Tensor gradient layout device ('DataType 'Bool) shape') Source #

Reduces each row of the input tensor in the selected dimension to True if any element in the row evaluates to True and False otherwise. For a version that accepts non-singleton parameters see anyDim.

>>> g <- sMkGenerator (SDevice SCPU) 0
>>> shape = SShape $ SName @"*" :&: SSize @2 :|: SName @"*" :&: SSize @4 :|: SNil
>>> (t, _) <- sRandn (TensorSpec (SGradient SWithoutGradient) (SLayout SDense) (SDevice SCPU) (SDataType SFloat) shape) g
>>> t' <- sAnyDim (SSelectDim (SByIndex @1)) =<< bool t
>>> :type t'
t'
  :: Tensor
       ('Gradient 'WithoutGradient)
       ('Layout 'Dense)
       ('Device 'CPU)
       ('DataType 'Bool)
       ('Shape '[ 'Dim ('Name "*") ('Size 2), 'Dim ('Name "*") ('Size 1)])
>>> sAnyDim (SUncheckedSelectDim (ByIndex 3)) t
*** Exception: HasktorchException "Exception: Dimension out of range (expected to be in range of [-2, 1], but got 3)...

anyDim :: forall selectDim gradient layout device dataType shape shape' m. (SingI selectDim, MonadThrow m, shape' ~ AnyDimF selectDim shape, Catch shape') => Tensor gradient layout device dataType shape -> m (Tensor gradient layout device ('DataType 'Bool) shape') Source #

Reduces each row of the input tensor in the selected dimension to True if any element in the row evaluates to True and False otherwise. For a version that accepts singleton parameters see sAnyDim.

>>> g <- sMkGenerator (SDevice SCPU) 0
>>> type Shape' = 'Shape '[ 'Dim ('Name "*") ('Size 2), 'Dim ('Name "*") ('Size 4) ]
>>> (t, _) <- randn @('Gradient 'WithoutGradient) @('Layout 'Dense) @('Device 'CPU) @('DataType 'Float) @Shape' g
>>> t' <- anyDim @('SelectDim ('ByIndex 1)) =<< bool t
>>> :type t'
t'
  :: Tensor
       ('Gradient 'WithoutGradient)
       ('Layout 'Dense)
       ('Device 'CPU)
       ('DataType 'Bool)
       ('Shape '[ 'Dim ('Name "*") ('Size 2), 'Dim ('Name "*") ('Size 1)])

type family MeanSelectDimsF (bys :: [By Symbol Nat]) (dims :: [Dim (Name Symbol) (Size Nat)]) :: [Dim (Name Symbol) (Size Nat)] where ... Source #

Equations

MeanSelectDimsF '[] dims = dims 
MeanSelectDimsF (by ': bys) dims = MeanSelectDimsF bys (ReductionCheckF "mean" by dims (ReplaceDimSizeImplF by dims ('Size 1))) 

type family MeanF (selectDims :: SelectDims [By Symbol Nat]) (shape :: Shape [Dim (Name Symbol) (Size Nat)]) :: Shape [Dim (Name Symbol) (Size Nat)] where ... Source #

sMeanDims :: forall selectDims gradient layout device dataType shape shape' m. (MonadThrow m, shape' ~ MeanF selectDims shape, Catch shape') => SSelectDims selectDims -> Tensor gradient layout device dataType shape -> m (Tensor gradient layout device dataType shape') Source #

Reduces the mean value over each row of the input tensor in the dimensions selected by selectDims. For a version that accepts non-singleton parameters see meanDim.

>>> g <- sMkGenerator (SDevice SCPU) 0
>>> shape = SShape $ SName @"batch" :&: SSize @8 :|: SName @"width" :&: SSize @224 :|: SName @"height" :&: SSize @224 :|: SNil
>>> (t, _) <- sRandn (TensorSpec (SGradient SWithoutGradient) (SLayout SDense) (SDevice SCPU) (SDataType SFloat) shape) g
>>> t' <- sMeanDims (SSelectDims $ SByName @"width" :|: SByName @"height" :|: SNil) t
>>> :type t'
t'
  :: Tensor
       ('Gradient 'WithoutGradient)
       ('Layout 'Dense)
       ('Device 'CPU)
       ('DataType 'Float)
       ('Shape
          '[ 'Dim ('Name "batch") ('Size 8), 'Dim ('Name "width") ('Size 1),
             'Dim ('Name "height") ('Size 1)])
>>> sMeanDims (SUncheckedSelectDims [ByName "feature"]) t
*** Exception: HasktorchException "Exception: Name 'feature' not found in Tensor['batch', 'width', 'height']...

meanDims :: forall selectDims gradient layout device dataType shape shape' m. (SingI selectDims, MonadThrow m, shape' ~ MeanF selectDims shape, Catch shape') => Tensor gradient layout device dataType shape -> m (Tensor gradient layout device dataType shape') Source #

Reduce the mean value over each row of the input tensor in the dimensions selected by selectDims. For a version that accepts singleton parameters see sMeanDim.

>>> g <- sMkGenerator (SDevice SCPU) 0
>>> type Shape' = 'Shape '[ 'Dim ('Name "batch") ('Size 8), 'Dim ('Name "feature") ('Size 4) ]
>>> (t, _) <- randn @('Gradient 'WithoutGradient) @('Layout 'Dense) @('Device 'CPU) @('DataType 'Float) @Shape' g
>>> t' <- meanDims @('SelectDims '[ 'ByName "feature" ]) t
>>> :type t'
t'
  :: Tensor
       ('Gradient 'WithoutGradient)
       ('Layout 'Dense)
       ('Device 'CPU)
       ('DataType 'Float)
       ('Shape
          '[ 'Dim ('Name "batch") ('Size 8),
             'Dim ('Name "feature") ('Size 1)])

type DimPositiveMessage reduction dim = (("Cannot apply '" <> reduction) <> "' because the dimension") % ("" % (((" '" <> dim) <> "'") % ("" % "is not positive."))) Source #

type family DimPositiveF reduction dim where ... Source #

Equations

DimPositiveF _ ('Dim _ 'UncheckedSize) = () 
DimPositiveF reduction ('Dim name ('Size 0)) = TypeError (DimPositiveMessage reduction ('Dim name ('Size 0))) 
DimPositiveF _ ('Dim _ ('Size _size)) = () 

type family AllDimsPositiveImplF reduction dims where ... Source #

Equations

AllDimsPositiveImplF _ '[] = () 
AllDimsPositiveImplF reduction (dim ': dims) = (DimPositiveF reduction dim, AllDimsPositiveImplF reduction dims) 

type family AllDimsPositiveF reduction shape where ... Source #

Equations

AllDimsPositiveF _ 'UncheckedShape = () 
AllDimsPositiveF reduction ('Shape dims) = AllDimsPositiveImplF reduction dims 

type MeanAllCheckF shape = AllDimsPositiveF "meanAll" shape Source #

meanAll :: forall gradient layout device dataType shape. MeanAllCheckF shape => Tensor gradient layout device dataType shape -> Tensor gradient layout device dataType ('Shape '[]) Source #

Reduces a tensor by calculating the mean value over all dimensions.

type ArgmaxF selectDim shape = BoolReductionF "argmax" selectDim shape Source #

argmax :: forall selectDims gradient layout device dataType shape shape' m. (MonadThrow m, shape' ~ ArgmaxF selectDims shape, Catch shape') => SSelectDim selectDims -> Tensor gradient layout device dataType shape -> m (Tensor ('Gradient 'WithoutGradient) layout device ('DataType 'Int64) shape') Source #

Argmax of a tensor given a dimension.

>>> g <- sMkGenerator (SDevice SCPU) 0
>>> spec = TensorSpec (SGradient SWithGradient) (SLayout SDense) (SDevice SCPU) (SDataType SFloat) (SShape $ SNoName :&: SSize @2 :|: SNoName :&: SSize @5 :|: SNil)
>>> (t, _) <- sRandn spec g
>>> r <- argmax (SSelectDim $ SByIndex @1) t
>>> :type r
r :: Tensor
       ('Gradient 'WithoutGradient)
       ('Layout 'Dense)
       ('Device 'CPU)
       ('DataType 'Int64)
       ('Shape '[ 'Dim ('Name "*") ('Size 2), 'Dim ('Name "*") ('Size 1)])
>>> r
Tensor Int64 [2,1] [[ 0],
                    [ 2]]

type MaxAllCheckF shape = AllDimsPositiveF "maxAll" shape Source #

maxAll :: forall gradient layout device dataType shape. MaxAllCheckF shape => Tensor gradient layout device dataType shape -> Tensor gradient layout device dataType ('Shape '[]) Source #