typedflow-0.9: Typed frontend to TensorFlow and higher-order deep learning

Copyright(c) Jean-Philippe Bernardy 2017
LicenseLGPL-3
Maintainerjean-philippe.bernardy@gu.se
Stabilityexperimental
Safe HaskellNone
LanguageHaskell2010

TypedFlow.TF

Contents

Description

This module provides direct access to the most commonly used TensorFlow functions. Higher-level functions are not defined here.

Synopsis

Variables, Parameters

Parameters

parameter' :: forall shape t. (KnownTyp t, KnownShape shape) => String -> T shape t -> Gen (T shape t) Source #

Declare a parameter to optimize. The shape of parameter should not depend on dimensions which can change between runs, such as the batch size.

parameter :: forall p. KnownTensors p => String -> p -> Gen p Source #

Create a parameter.

parameterDefault :: forall p. ParamWithDefault p => String -> Gen p Source #

Create a parameter and initialize it with a suitable default for its type. Control the exact initializer using parameter.

class KnownTensors p => ParamWithDefault p where Source #

Minimal complete definition

defaultInitializer

Instances

(KnownNat numObjects, KnownBits NBits b, KnownNat embeddingSize) => ParamWithDefault (EmbeddingP numObjects embeddingSize b) Source # 

Methods

defaultInitializer :: EmbeddingP numObjects embeddingSize b Source #

(KnownNat n, KnownNat m, KnownBits NBits b) => ParamWithDefault (DenseP b n m) Source # 
(KnownNat n, KnownNat x, KnownBits NBits t) => ParamWithDefault (GRUP t n x) Source # 
(KnownNat n, KnownNat x, KnownBits NBits t) => ParamWithDefault (LSTMP t n x) Source # 
(KnownNat outChannels, KnownNat inChannels, KnownShape filterSpatialShape, KnownBits NBits t) => ParamWithDefault (ConvP t outChannels inChannels filterSpatialShape) Source # 

Methods

defaultInitializer :: ConvP t outChannels inChannels filterSpatialShape Source #

(KnownNat n, KnownNat k, KnownNat v, KnownBits NBits t) => ParamWithDefault (AdditiveScoringP k v n t) Source # 

getParameters :: Gen UntypedExpression Source #

Return a list of parameters.

Persistent variables

persistent :: forall shape t. (KnownTyp t, KnownShape shape) => Bool -> String -> T shape t -> Gen (T shape t) Source #

Declare variable which persists between calls to session.run.

modifyPersistent :: T s t -> T s t -> T s t Source #

Modify a mutable tensor. Attention: for the assignment to happen, the resulting tensor must be evaluated!

Placeholders and outputs

placeholder :: forall t s. (KnownShape s, KnownTyp t) => String -> Gen (T s t) Source #

Placeholder (to fill)

peekAt :: String -> Tensor s t -> Gen () Source #

Name a tensor so that it is made available for session.run.

peekAtMany :: String -> HTV t xs -> Gen () Source #

Operations

Constants

zeros :: forall t shape. KnownShape shape => KnownTyp t => T shape t Source #

Zeros

ones :: forall t shape. KnownShape shape => KnownTyp t => T shape t Source #

Ones

constant :: forall s w. KnownShape s => KnownBits w => Float -> T s (Typ Float w) Source #

Constant

indexwise unary operators

round :: forall s t. Tensor s (Typ Float t) -> Tensor s (Typ Float t) Source #

sigmoid :: forall s t. Tensor s (Typ Float t) -> Tensor s (Typ Float t) Source #

tanh :: forall s t. Tensor s (Typ Float t) -> Tensor s (Typ Float t) Source #

log :: forall s t. Tensor s (Typ Float t) -> Tensor s (Typ Float t) Source #

relu :: forall s t. Tensor s (Typ Float t) -> Tensor s (Typ Float t) Source #

floor :: forall s t. Tensor s (Typ Float t) -> Tensor s (Typ Float t) Source #

negate :: forall s t. T s t -> T s t Source #

Indexwise binary operators

add :: forall s d t. Tensor (d ++ s) t -> Tensor d t -> Tensor (d ++ s) t Source #

Add two tensors, broacasting along shape s

(+) :: forall d s t. Tensor (d ++ s) t -> Tensor d t -> Tensor (d ++ s) t infixl 6 Source #

Add two tensors, broacasting along shape s

(⊕) :: forall s t. Tensor s t -> Tensor s t -> Tensor s t infixl 6 Source #

Indexwise operator

(⊝) :: forall s t. Tensor s t -> Tensor s t -> Tensor s t infixl 6 Source #

Indexwise operator

(⊙) :: forall s t. Tensor s t -> Tensor s t -> Tensor s t infixl 7 Source #

Indexwise operator

(⊘) :: forall s t. Tensor s t -> Tensor s t -> Tensor s t infixl 7 Source #

Indexwise operator

equal :: Tensor d t -> Tensor d t -> Tensor d TFBool Source #

Indexwise equality test.

Products

(∙) :: Tensor '[cols, rows] t -> Tensor '[cols, batchSize] t -> Tensor '[rows, batchSize] t infixl 7 Source #

Product of a matrix of weight with a (batched) vector .

· :: forall cols batchSize t. Tensor '[cols, batchSize] t -> Tensor '[cols, batchSize] t -> Tensor '[batchSize] t infixl 7 Source #

Dot product between two batched vectors.

matmul :: Tensor (o ': (n ': s)) t -> Tensor (m ': (o ': s)) t -> Tensor (m ': (n ': s)) t Source #

Matrix multiplication (note that shape s is preserved)

Reducers

reduceMeanAll :: forall s t. Tensor s t -> Tensor '[] t Source #

Mean value of the input tensor.

reduceSumAll :: forall s t. Tensor s t -> Tensor '[] t Source #

Mean value of the input tensor.

reduceSum :: forall n s t. (KnownLen s, KnownPeano n) => T s t -> T (Take n s ++ Drop (Succ n) s) t Source #

Sum along a given dimension

reduceMean :: forall n s t. (KnownLen s, KnownPeano n) => T s t -> T (Take n s ++ Drop (Succ n) s) t Source #

Sum along a given dimension

argmax :: forall n u m s t. (KnownLen s, KnownPeano n, KnownBits u) => Tensor (Take n s ++ (m ': Drop n s)) t -> Tensor s (Typ Int u) Source #

Argmax along dimension n

argmax0 :: forall u n s t. (KnownLen s, KnownBits u) => T (n ': s) t -> T s (Typ Int u) Source #

Argmax along the first dimension

argmax1 :: forall u m n s t. (KnownLen s, KnownBits u) => T (m ': (n ': s)) t -> T (m ': s) (Typ Int u) Source #

Argmax along the second dimension

softmax0 :: T (n ': s) (Typ Float w) -> T (n ': s) (Typ Float w) Source #

Softmax along the first dimension

softmax1 :: forall n m s w. KnownLen s => T (m ': (n ': s)) (Typ Float w) -> T (m ': (n ': s)) (Typ Float w) Source #

Softmax along the second dimension

Gradients

grad :: T s Float32 -> UntypedExpression -> UntypedExpression Source #

Gradient of wrt. given parameters.

clipByValue :: Float -> Float -> T s (Flt t) -> T s (Flt t) Source #

Clip a tensor

Indexing

last0 :: forall n s t. KnownNat n => KnownLen s => T (n ': s) t -> Tensor s t Source #

Access the last element in a tensor (in the 0th dimension)

nth0 :: forall n s t. KnownLen s => Integer -> T (n ': s) t -> Tensor s t Source #

Access the nth element in a tensor (in the 0th dimension)

nth0' :: forall n m s t. KnownNat n => KnownLen s => n < m => T (m ': s) t -> Tensor s t Source #

Access the nth element in a tensor (in the 0th dimension), with a static index

gather :: forall s n indexShape t. T (s ++ '[n]) t -> T indexShape Int32 -> T (s ++ indexShape) t Source #

Split and concatenate

split0 :: forall n m batchShape t. (KnownNat n, KnownNat m, KnownLen batchShape) => Tensor ((n + m) ': batchShape) t -> Gen (Tensor (n ': batchShape) t, Tensor (m ': batchShape) t) Source #

Split a tensor on the first dimension

slice :: forall n i j s t. KnownNat j => KnownNat i => (i < j, j <= At n s, KnownPeano n, KnownLen s) => Tensor s t -> Tensor (Take n s ++ ((j - i) ': Drop (Succ n) s)) t Source #

Take a slice at dimension n from i to j.

slice1 :: forall i j m n s t. KnownNat j => KnownNat i => (i < j, j <= m, KnownLen s) => Tensor (n ': (m ': s)) t -> Tensor (n ': ((j - i) ': s)) t Source #

stack0 :: forall s n t. KnownLen s => V n (T s t) -> Tensor (n ': s) t Source #

Concatenate n tensors along the first dimension

unstack0 :: forall s n t. (KnownLen s, KnownNat n) => Tensor (n ': s) t -> Gen (V n (T s t)) Source #

Split a tensors into n tensors along the first dimension

stackN :: forall s n t. V n (T s t) -> Tensor (s ++ '[n]) t Source #

Concatenate n tensors along the last dimension

stack1 :: forall s n m t. KnownLen s => V n (T (m ': s) t) -> Tensor (m ': (n ': s)) t Source #

Concatenate n tensors along the first dimension

concatT :: forall n d1 d2 s t. (KnownPeano n, KnownLen s, (d1 + d2) ~ At n s) => T (Take n s ++ (d1 ': Drop (Succ n) s)) t -> T (Take n s ++ (d2 ': Drop (Succ n) s)) t -> T s t Source #

Concatenate tensors on dimension n

concat0 :: forall ys d1 d2 t. KnownLen ys => T (d1 ': ys) t -> T (d2 ': ys) t -> T ((d1 + d2) ': ys) t Source #

Concatenate tensors on the first dimension

concat1 :: forall n ys d1 d2 t. KnownLen ys => T (n ': (d1 ': ys)) t -> T (n ': (d2 ': ys)) t -> T (n ': ((d1 + d2) ': ys)) t Source #

Concatenate tensors on the second dimension

Reshaping

expandDim :: forall n s t. (KnownLen s, KnownPeano n) => Tensor s t -> Tensor (Take n s ++ (1 ': Drop n s)) t Source #

Add an extra dimension at axis (n) of size 1.

expandDim0 :: forall s t. KnownLen s => Tensor s t -> Tensor (1 ': s) t Source #

Add an extra dimension at axis (0) of size 1.

squeeze0 :: forall s t. KnownLen s => Tensor (1 ': s) t -> Tensor s t Source #

Remove the first dimension if its size is 1.

expandDim1 :: forall n s t. KnownShape s => Tensor (n ': s) t -> Tensor (n ': (1 ': s)) t Source #

Add an extra dimension at axis (1) of size 1.

squeeze1 :: forall n s t. KnownLen s => Tensor (n ': (1 ': s)) t -> Tensor (n ': s) t Source #

Remove the second dimension if its size is 1.

flatten2 :: forall m n s t. (KnownNat m, KnownNat n, KnownShape s) => Tensor (m ': (n ': s)) t -> Tensor ((m * n) ': s) t Source #

Reshape a tensor so that the first two dimensions are collapsed

inflate2 :: forall m n s t. (KnownNat m, KnownNat n, KnownShape s) => Tensor ((m * n) ': s) t -> Tensor (m ': (n ': s)) t Source #

Reshape a tensor so that the first dimension is expanded into two.

flattenN2 :: forall s m n t. (KnownNat m, KnownNat n, KnownShape s) => Tensor (s ++ '[m, n]) t -> Tensor (s ++ '[m * n]) t Source #

Reshape a tensor so that the last two dimensions are collapsed

flatten3 :: forall m n o s t. (KnownNat m, KnownNat n, KnownNat o, KnownShape s) => Tensor (m ': (n ': (o ': s))) t -> Tensor (((m * n) * o) ': s) t Source #

Reshape a tensor so that the first three dimensions are collapsed

inflate3 :: forall m n o s t. (KnownNat m, KnownNat n, KnownNat o, KnownShape s) => Tensor (((m * n) * o) ': s) t -> Tensor (m ': (n ': (o ': s))) t Source #

Reshape a tensor so that the first dimension is expanded into three.

reshape :: forall s2 s1 t. KnownShape s2 => Product s1 ~ Product s2 => Tensor s1 t -> Tensor s2 t Source #

flattenAll :: forall s t. KnownShape s => Tensor s t -> Tensor '[Product s] t Source #

Flatten all the dimensions of the tensor

inflateAll :: forall s t. KnownShape s => Tensor '[Product s] t -> Tensor s t Source #

Transposition

transpose :: forall s t. T (Reverse s) t -> T s t Source #

Transposition. See the type for the permutation of dimensions.

transposeN :: forall s n t. KnownLen s => T (n ': s) t -> T (s ++ '[n]) t Source #

Transposition. See the type for the permutation of dimensions.

transposeN' :: forall s n t. KnownLen s => T (s ++ '[n]) t -> T (n ': s) t Source #

Transposition. See the type for the permutation of dimensions.

transpose01 :: forall s m n t. KnownLen s => T (m ': (n ': s)) t -> T (n ': (m ': s)) t Source #

Transposition. See the type for the permutation of dimensions.

transposeN01 :: forall s m n t. T (s ++ '[m, n]) t -> T (s ++ '[n, m]) t Source #

Transposition. See the type for the permutation of dimensions.

Sequences

reverseSequences :: forall bs n x t. KnownLen x => LastEqual bs x => T '[bs] Int32 -> T (n ': x) t -> T (n ': x) t Source #

sequenceMask :: forall maxlen bs. KnownNat maxlen => Tensor '[bs] Int32 -> Tensor '[maxlen, bs] TFBool Source #

Generate a mask of given length for each sequence.

Misc

cast :: forall u s t. KnownTyp u => T s t -> T s u Source #

Cast the element type.

convolution Source #

Arguments

:: KnownLen filterSpatialShape 
=> Length filterSpatialShape <= 3 
=> (1 + Length filterSpatialShape) ~ Length s 
=> T ('[inChannels] ++ s) t

input tensor (batched)

-> T ('[outputChannels, inChannels] ++ filterSpatialShape) t

filters

-> T ('[outputChannels] ++ s) t 

Size-preserving convolution operation.

oneHot :: forall n numClasses s w t. KnownNat numClasses => KnownBits t => (KnownLen s, KnownPeano n) => Tensor s (Typ Int w) -> Tensor (Take n s ++ (numClasses ': Drop n s)) (Flt t) Source #

One hot vector along axis n

oneHot0 :: forall numClasses w batchSize t. KnownNat numClasses => KnownBits t => Tensor '[batchSize] (Typ Int w) -> Tensor '[numClasses, batchSize] (Flt t) Source #

One hot vector along axis 0

oneHot1 :: forall numClasses w batchSize m t. KnownNat numClasses => KnownBits t => Tensor '[m, batchSize] (Typ Int w) -> Tensor '[m, numClasses, batchSize] (Flt t) Source #

One hot vector along axis 1

Testing conditions

if_ :: Scalar TFBool -> T s t -> T s t -> T s t Source #

Selection of a tensor (note: this is a strict operation)

where_ :: T s TFBool -> T s t -> T s t -> T s t Source #

(where_ c x y)[i] = if c[i] then x[i] else y[i]

Contrib

Mapping

mapT :: forall s t r u n. KnownTyp u => KnownLen r => KnownLen s => (T s t -> T r u) -> T (n ': s) t -> Gen (T (n ': r) u) Source #

Map a function along the first dimension of a tensor

mapTN :: forall n s t r u. KnownTyp u => (T s t -> T r u) -> T (s ++ '[n]) t -> Gen (T (r ++ '[n]) u) Source #

Map a function along the last dimension of a tensor

zipWithT :: forall s t s1 t1 s2 n t2. KnownNat n => (KnownLen s, KnownLen s2, KnownLen s1) => KnownTyp t2 => (T s t -> T s1 t1 -> T s2 t2) -> Tensor (n ': s) t -> Tensor (n ': s1) t1 -> Gen (Tensor (n ': s2) t2) Source #

zipWithTN :: forall n s t s1 t1 s2 t2. KnownTyp t2 => (T s t -> T s1 t1 -> T s2 t2) -> Tensor (s ++ '[n]) t -> Tensor (s1 ++ '[n]) t1 -> Gen (Tensor (s2 ++ '[n]) t2) Source #

Losses

sigmoidCrossEntropyWithLogits Source #

Arguments

:: Tensor s (Flt w)

labels

-> Tensor s (Flt w)

logits

-> Tensor s (Flt w) 

Computes sigmoid cross entropy given logits. Measures the probability error in discrete classification tasks in which each class is independent and not mutually exclusive. For instance, one could perform multilabel classification where a picture can contain both an elephant and a dog at the same time. See https://www.tensorflow.org/api_docs/python/tf/nn/sigmoid_cross_entropy_with_logits

softmaxCrossEntropyWithLogits Source #

Arguments

:: Tensor '[numClasses, batchSize] Float32

labels

-> Tensor '[numClasses, batchSize] Float32

logits

-> Tensor '[batchSize] Float32 

(dense) softmax cross entropy with logits.

sparseSoftmaxCrossEntropyWithLogits Source #

Arguments

:: Tensor s Int32

desired labels

-> Tensor (numClasses ': s) (Flt t)

predictions

-> Tensor s (Flt t) 

sparse softmax cross entropy with logits.

Initializers

truncatedNormal :: forall s w. KnownShape s => KnownBits w => Float -> T s (Typ Float w) Source #

Generate a random tensor where each individual element is picked in a normal distribution with given standard deviation.

randomUniform :: forall s t. (KnownShape s, KnownTyp t) => Float -> Float -> T s t Source #

Generate a random tensor where each individual element is picked in a uniform distribution with given bounds.

randomOrthogonal :: forall n s t. (KnownBits t, KnownNat n, KnownShape s) => T (n ': s) (Typ Float t) Source #

Generate an orthorgonal matrix. If the output has more dimensions than 2 the matrix is reshaped.

varianceScaling :: forall inDim outDim t. KnownNat inDim => (KnownNat outDim, KnownBits t) => Float -> VarianceScaleMode -> Distrib -> Tensor '[inDim, outDim] (Typ Float t) Source #

Random tensor with variance scaling according to deeplearning lore.

glorotUniform :: forall inDim outDim t. KnownNat inDim => (KnownNat outDim, KnownBits t) => Tensor '[inDim, outDim] (Typ Float t) Source #

Heterogeneous vectors

repeatT :: forall ss t. All KnownShape ss => KnownLen ss => (forall s. KnownShape s => T s t) -> HTV t ss Source #

Repeat a flexible-shape constant vector to form a heterogeneous tensor vector.

flattenHTV :: KnownTyp t => All KnownShape xs => HTV t xs -> Tensor '[Sum (Ap (FMap CProduct) xs)] t Source #

inflateHTV :: forall xs s t. (All KnownShape xs, KnownLen s, KnownLen xs) => Tensor '[Sum (Ap (FMap CProduct) xs)] t -> Gen (HTV t xs) Source #

class KnownTensors p where Source #

Minimal complete definition

travTensor

Methods

travTensor :: (forall s t. (KnownTyp t, KnownShape s) => String -> T s t -> Gen (T s t)) -> String -> p -> Gen p Source #

traverse all the tensors over tuples of tensors

Instances

(KnownTensors p, KnownTensors q) => KnownTensors (p, q) Source # 

Methods

travTensor :: (forall s t. (KnownTyp Typ t, KnownShape s) => String -> T s t -> Gen (T s t)) -> String -> (p, q) -> Gen (p, q) Source #

(KnownTyp Typ t, KnownShape shape) => KnownTensors (T shape t) Source # 

Methods

travTensor :: (forall s a. (KnownTyp Typ a, KnownShape s) => String -> T s a -> Gen (T s a)) -> String -> T shape t -> Gen (T shape t) Source #

(KnownTyp Typ t, All [Nat] KnownShape ys) => KnownTensors (HTV t ys) Source # 

Methods

travTensor :: (forall s a. (KnownTyp Typ a, KnownShape s) => String -> T s a -> Gen (T s a)) -> String -> HTV t ys -> Gen (HTV t ys) Source #

(KnownTensors p1, KnownTensors p2, KnownTensors p3) => KnownTensors (p1, p2, p3) Source # 

Methods

travTensor :: (forall s t. (KnownTyp Typ t, KnownShape s) => String -> T s t -> Gen (T s t)) -> String -> (p1, p2, p3) -> Gen (p1, p2, p3) Source #

(KnownNat numObjects, KnownBits NBits b, KnownNat embeddingSize) => KnownTensors (EmbeddingP numObjects embeddingSize b) Source # 

Methods

travTensor :: (forall s t. (KnownTyp Typ t, KnownShape s) => String -> T s t -> Gen (T s t)) -> String -> EmbeddingP numObjects embeddingSize b -> Gen (EmbeddingP numObjects embeddingSize b) Source #

(KnownNat a, KnownNat b, KnownBits NBits t) => KnownTensors (DenseP t a b) Source # 

Methods

travTensor :: (forall s c. (KnownTyp Typ c, KnownShape s) => String -> T s c -> Gen (T s c)) -> String -> DenseP t a b -> Gen (DenseP t a b) Source #

(KnownNat n, KnownNat x, KnownBits NBits t) => KnownTensors (GRUP t n x) Source # 

Methods

travTensor :: (forall s a. (KnownTyp Typ a, KnownShape s) => String -> T s a -> Gen (T s a)) -> String -> GRUP t n x -> Gen (GRUP t n x) Source #

(KnownNat n, KnownNat x, KnownBits NBits t) => KnownTensors (LSTMP t n x) Source # 

Methods

travTensor :: (forall s a. (KnownTyp Typ a, KnownShape s) => String -> T s a -> Gen (T s a)) -> String -> LSTMP t n x -> Gen (LSTMP t n x) Source #

(KnownTensors p1, KnownTensors p2, KnownTensors p3, KnownTensors p4) => KnownTensors (p1, p2, p3, p4) Source # 

Methods

travTensor :: (forall s t. (KnownTyp Typ t, KnownShape s) => String -> T s t -> Gen (T s t)) -> String -> (p1, p2, p3, p4) -> Gen (p1, p2, p3, p4) Source #

(KnownNat outChannels, KnownNat inChannels, KnownShape filterSpatialShape, KnownBits NBits t) => KnownTensors (ConvP t outChannels inChannels filterSpatialShape) Source # 

Methods

travTensor :: (forall s a. (KnownTyp Typ a, KnownShape s) => String -> T s a -> Gen (T s a)) -> String -> ConvP t outChannels inChannels filterSpatialShape -> Gen (ConvP t outChannels inChannels filterSpatialShape) Source #

(KnownNat n, KnownNat k, KnownNat v, KnownBits NBits t) => KnownTensors (AdditiveScoringP k v n t) Source # 

Methods

travTensor :: (forall s a. (KnownTyp Typ a, KnownShape s) => String -> T s a -> Gen (T s a)) -> String -> AdditiveScoringP k v n t -> Gen (AdditiveScoringP k v n t) Source #

class LastEqual x xs Source #

Instances

LastEqual [a] k x ((:) a y2 xs) => LastEqual [a] k x ((:) a y ((:) a y2 xs)) Source # 
LastEqual [a] a x ((:) a x ([] a)) Source #