| Safe Haskell | None |
|---|---|
| Language | GHC2021 |
Test.Tensor
Description
Tensors (n-dimensional arrays)
This is an implementation of tensors that emphasizes simplicify above all; it is meant for use in QuickCheck tests.
Intended for qualified import.
import Test.Tensor (Tensor) import Test.Tensor qualified as Tensor
Synopsis
- data Tensor (n :: Nat) a where
- getScalar :: Tensor 'Z a -> a
- getTensor :: forall (n :: Nat) a. Tensor ('S n) a -> [Tensor n a]
- scalar :: a -> Tensor Nat0 a
- dim1 :: [a] -> Tensor Nat1 a
- dim2 :: [[a]] -> Tensor Nat2 a
- dim3 :: [[[a]]] -> Tensor Nat3 a
- dim4 :: [[[[a]]]] -> Tensor Nat4 a
- dim5 :: [[[[[a]]]]] -> Tensor Nat5 a
- dim6 :: [[[[[[a]]]]]] -> Tensor Nat6 a
- dim7 :: [[[[[[[a]]]]]]] -> Tensor Nat7 a
- dim8 :: [[[[[[[[a]]]]]]]] -> Tensor Nat8 a
- dim9 :: [[[[[[[[[a]]]]]]]]] -> Tensor Nat9 a
- type Size (n :: Nat) = Vec n Int
- size :: forall (n :: Nat) a. Tensor n a -> Size n
- sizeAtLeast :: forall (n :: Nat) a. Size n -> Tensor n a -> Bool
- zipWith :: forall a b c (n :: Nat). (a -> b -> c) -> Tensor n a -> Tensor n b -> Tensor n c
- replicate :: forall (n :: Nat) a. Size n -> a -> Tensor n a
- rotate :: forall (n :: Nat) a. Tensor n a -> Tensor n a
- distrib :: forall f (n :: Nat) a. Functor f => Size n -> f (Tensor n a) -> Tensor n (f a)
- transpose :: Tensor Nat2 a -> Tensor Nat2 a
- foreach :: forall (n :: Nat) a (m :: Nat) b. Tensor ('S n) a -> (Tensor n a -> Tensor m b) -> Tensor ('S m) b
- foreachWith :: forall (n :: Nat) a x (m :: Nat) b. Tensor ('S n) a -> [x] -> (Tensor n a -> x -> Tensor m b) -> Tensor ('S m) b
- subs :: forall (n :: Nat) a. Size n -> Tensor n a -> Tensor n (Tensor n a)
- subsWithStride :: forall (n :: Nat) a. Vec n Int -> Size n -> Tensor n a -> Tensor n (Tensor n a)
- convolve :: forall (n :: Nat) a. (SNatI n, Num a) => Tensor n a -> Tensor n a -> Tensor n a
- convolveWithStride :: forall (n :: Nat) a. Num a => Vec n Int -> Tensor n a -> Tensor n a -> Tensor n a
- padWith :: forall (n :: Nat) a. SNatI n => a -> Int -> Tensor n a -> Tensor n a
- padWith' :: forall (n :: Nat) a. a -> Vec n (Int, Int) -> Tensor n a -> Tensor n a
- type family Lists (n :: Nat) a where ...
- toLists :: forall (n :: Nat) a. Tensor n a -> Lists n a
- fromLists :: forall (n :: Nat) a. SNatI n => Lists n a -> Tensor n a
- fromList :: forall (n :: Nat) a. HasCallStack => Size n -> [a] -> Tensor n a
- arbitraryOfSize :: forall (n :: Nat) a. Size n -> Gen a -> Gen (Tensor n a)
- shrinkWith :: forall a (n :: Nat). Maybe (Zero a) -> (a -> [a]) -> Tensor n a -> [Tensor n a]
- shrinkWith' :: forall (n :: Nat) a. [Axe n] -> Maybe (Zero a) -> (a -> [a]) -> Tensor n a -> [Tensor n a]
- shrinkElem :: forall (n :: Nat) a. Maybe (Zero a) -> (a -> [a]) -> Tensor n a -> [Tensor n a]
- data Axe (n :: Nat) where
- allAxes :: forall (n :: Nat). Size n -> [Axe n]
- axeWith :: forall (n :: Nat) a. Axe n -> Tensor n a -> Tensor n a
- axeSize :: forall (n :: Nat). Size n -> Axe n -> Int
- data Zero a where
- zero :: (Num a, Eq a) => Zero a
- zeroWith :: forall (n :: Nat) a. Zero a -> Axe n -> Tensor n a -> Maybe (Tensor n a)
- toStorable :: forall a (n :: Nat). Storable a => Tensor n a -> Vector a
- fromStorable :: forall a (n :: Nat). (HasCallStack, Storable a) => Size n -> Vector a -> Tensor n a
- unsafeWithCArray :: forall a (n :: Nat) r. Storable a => Tensor n a -> (Ptr a -> IO r) -> IO r
- unsafeFromCArray :: forall a (n :: Nat). Storable a => Size n -> ForeignPtr a -> Tensor n a
- unsafeFromPrealloc :: forall a (n :: Nat) r. Storable a => Size n -> (Ptr a -> IO r) -> IO (Tensor n a, r)
- unsafeFromPrealloc_ :: forall a (n :: Nat). Storable a => Size n -> (Ptr a -> IO ()) -> IO (Tensor n a)
Definition
data Tensor (n :: Nat) a where Source #
N-dimensional tensor
Invariants:
- The dimension must be strictly positive (zero is not allowed)
- Tensors must be rectangular
(These invariants could in principle be enforced by using more precise types, but at the cost of much more complex code.)
Constructors
| Scalar :: forall a. a -> Tensor 'Z a | |
| Tensor :: forall (n1 :: Nat) a. [Tensor n1 a] -> Tensor ('S n1) a |
Instances
| SNatI n => Arbitrary1 (Tensor n) Source # | Lift generators and shrinkers NOTE: Since we cannot put any constraints on the type of the elements here,
we cannot use any zero elements. Using |
Defined in Test.Tensor Methods liftArbitrary :: Gen a -> Gen (Tensor n a) # liftShrink :: (a -> [a]) -> Tensor n a -> [Tensor n a] # | |
| Foldable (Tensor n) Source # | |
Defined in Test.Tensor Methods fold :: Monoid m => Tensor n m -> m # foldMap :: Monoid m => (a -> m) -> Tensor n a -> m # foldMap' :: Monoid m => (a -> m) -> Tensor n a -> m # foldr :: (a -> b -> b) -> b -> Tensor n a -> b # foldr' :: (a -> b -> b) -> b -> Tensor n a -> b # foldl :: (b -> a -> b) -> b -> Tensor n a -> b # foldl' :: (b -> a -> b) -> b -> Tensor n a -> b # foldr1 :: (a -> a -> a) -> Tensor n a -> a # foldl1 :: (a -> a -> a) -> Tensor n a -> a # elem :: Eq a => a -> Tensor n a -> Bool # maximum :: Ord a => Tensor n a -> a # minimum :: Ord a => Tensor n a -> a # | |
| Traversable (Tensor n) Source # | |
| Functor (Tensor n) Source # | |
| (SNatI n, Arbitrary a, Num a, Eq a) => Arbitrary (Tensor n a) Source # | |
| Show a => Show (Tensor n a) Source # | |
| Eq a => Eq (Tensor n a) Source # | |
Convenience constructors
Size
sizeAtLeast :: forall (n :: Nat) a. Size n -> Tensor n a -> Bool Source #
Check that each dimension has at least the specified size
Standard operations
zipWith :: forall a b c (n :: Nat). (a -> b -> c) -> Tensor n a -> Tensor n b -> Tensor n c Source #
Analogue of zipWith
rotate :: forall (n :: Nat) a. Tensor n a -> Tensor n a Source #
Analogue of reverse
This amounts to a 180 degrees rotation of the tensor.
distrib :: forall f (n :: Nat) a. Functor f => Size n -> f (Tensor n a) -> Tensor n (f a) Source #
Analogue of distribute (distributive package)
Since we don't track the complete size of the tensor at the type level, we must be told how large the resulting tensor is going to be.
transpose :: Tensor Nat2 a -> Tensor Nat2 a Source #
Transpose
This is essentially a special case of distrib.
foreach :: forall (n :: Nat) a (m :: Nat) b. Tensor ('S n) a -> (Tensor n a -> Tensor m b) -> Tensor ('S m) b Source #
Map element over the first dimension of the tensor
foreachWith :: forall (n :: Nat) a x (m :: Nat) b. Tensor ('S n) a -> [x] -> (Tensor n a -> x -> Tensor m b) -> Tensor ('S m) b Source #
Variation of foreach with an auxiliary list
Subtensors
subs :: forall (n :: Nat) a. Size n -> Tensor n a -> Tensor n (Tensor n a) Source #
Subtensors of the specified size
Generalization of subs allowing for non-default stride
Arguments
| :: forall (n :: Nat) a. (SNatI n, Num a) | |
| => Tensor n a | Kernel |
| -> Tensor n a | Input |
| -> Tensor n a |
Convolution
See padWith for adjusting boundary conditions.
Arguments
| :: forall (n :: Nat) a. Num a | |
| => Vec n Int | Stride |
| -> Tensor n a | Kernel |
| -> Tensor n a | Input |
| -> Tensor n a |
Generalization of convolve when using a non-default stride
padWith :: forall (n :: Nat) a. SNatI n => a -> Int -> Tensor n a -> Tensor n a Source #
Add uniform padding
padWith' :: forall (n :: Nat) a. a -> Vec n (Int, Int) -> Tensor n a -> Tensor n a Source #
Generalization of padWith with different padding per dimension
Conversions
fromList :: forall (n :: Nat) a. HasCallStack => Size n -> [a] -> Tensor n a Source #
Inverse to toList
Throws a pure exception if the list does not contain enough elements.
QuickCheck support
Generation
Shrinking
Arguments
| :: forall a (n :: Nat). Maybe (Zero a) | Optional zero element (see |
| -> (a -> [a]) | Shrink individual elements |
| -> Tensor n a | |
| -> [Tensor n a] |
Shrink tensor
Arguments
| :: forall (n :: Nat) a. [Axe n] | Shrink the size of the tensor (see |
| -> Maybe (Zero a) | Optional zero element (see |
| -> (a -> [a]) | Shrink elements of the tensor |
| -> Tensor n a | |
| -> [Tensor n a] |
Generalization of shrinkWith
Arguments
| :: forall (n :: Nat) a. Maybe (Zero a) | Optional zero element |
| -> (a -> [a]) | Shrink individual elements |
| -> Tensor n a | |
| -> [Tensor n a] |
Shrink an element of the tensor, leaving the size of the tensor unchanged
If a zero element is specified, we will first try to replace entire regions of the tensor by zeroes; this can dramatically speed up shrinking.
Axes
data Axe (n :: Nat) where Source #
Constructors
| AxeHere :: forall (n1 :: Nat). (Int, Int) -> Axe ('S n1) | Axe some elements from the current dimension We record which elements to drop as an |
| AxeNested :: forall (n1 :: Nat). Axe n1 -> Axe ('S n1) | Axe some elements from a nested dimension In order to keep the tensor square, we must apply the same axe for every element of the current dimension |
allAxes :: forall (n :: Nat). Size n -> [Axe n] Source #
All possible ways to axe some elements
This is adopted from the implementation of shrinkList (in a way, an Axe
is an explanation of the decisions made by shrinkList, generalized to
multiple dimensions).
Axes are sorted to remove as many elements as early as possible.
axeWith :: forall (n :: Nat) a. Axe n -> Tensor n a -> Tensor n a Source #
Remove elements from the tensor (shrink dimensions)
axeSize :: forall (n :: Nat). Size n -> Axe n -> Int Source #
How many elements are removed by this axe?
Examples:
axeSize (2 ::: 100 ::: VNil) (AxeHere (0, 1)) == 100 axeSize (2 ::: 100 ::: VNil) (AxeNested (AxeHere (0, 99))) == 198
Zeroing
zeroWith :: forall (n :: Nat) a. Zero a -> Axe n -> Tensor n a -> Maybe (Tensor n a) Source #
Zero elements in the tensor (leaving dimensions the same)
Returns Nothing if the specified region was already zero everywhere.
FFI
toStorable :: forall a (n :: Nat). Storable a => Tensor n a -> Vector a Source #
Translate to storable vector
The tensor is laid out in order specified (outer dimensions before inner).
fromStorable :: forall a (n :: Nat). (HasCallStack, Storable a) => Size n -> Vector a -> Tensor n a Source #
Translate from storable vector
Throws an exception if the vector does not contain enough elements.
unsafeWithCArray :: forall a (n :: Nat) r. Storable a => Tensor n a -> (Ptr a -> IO r) -> IO r Source #
Get pointer to elements of the tensor
See toStorable for discussion of the layout.
The data should not be modified through the pointer, and the pointer should not be used outside its scope.
unsafeFromCArray :: forall a (n :: Nat). Storable a => Size n -> ForeignPtr a -> Tensor n a Source #
Construct tensor from C array
The data should not be modified through the pointer after the tensor has been constructed.
unsafeFromPrealloc :: forall a (n :: Nat) r. Storable a => Size n -> (Ptr a -> IO r) -> IO (Tensor n a, r) Source #
Construct tensor from preallocated C array
Allocates sufficient memory to hold the elements of the tensor; writing more data will result in invalid memory access. The pointer should not be used outside its scope.