Safe Haskell | None |
---|---|
Language | Haskell2010 |
The core functionality of TensorFlow.
Unless you are defining ops, you do not need to import other modules from this package.
Basic ops are provided in the tensorflow-ops and tensorflow-core-ops packages.
- data Session a
- data Options
- sessionConfig :: Lens' Options ConfigProto
- sessionTarget :: Lens' Options ByteString
- sessionTracer :: Lens' Options Tracer
- runSession :: Session a -> IO a
- runSessionWithOptions :: Options -> Session a -> IO a
- build :: Build a -> Session a
- buildAnd :: (a -> Session b) -> Build a -> Session b
- buildWithSummary :: forall a. Build a -> Session (a, [SummaryTensor])
- class Nodes t => Fetchable t a
- newtype Scalar a = Scalar {
- unScalar :: a
- class Nodes t
- run :: Fetchable t a => t -> Session a
- run_ :: Nodes t => t -> Session ()
- data Feed
- feed :: Tensor v a -> TensorData a -> Feed
- runWithFeeds :: Fetchable t a => [Feed] -> t -> Session a
- runWithFeeds_ :: Nodes t => [Feed] -> t -> Session ()
- asyncProdNodes :: Nodes t => t -> Session ()
- type Build = BuildT Identity
- data BuildT m a
- render :: Tensor v a -> Build (Tensor v a)
- asGraphDef :: Build a -> GraphDef
- addGraphDef :: GraphDef -> Build ()
- data ControlNode
- data Tensor v a
- data Value
- data Ref
- data TensorKind v where
- tensorAttr :: Attribute attr => Text -> Traversal' (Tensor v a) attr
- value :: Tensor v a -> Tensor Value a
- tensorFromName :: TensorKind v -> Text -> Tensor v a
- data TensorData a
- class TensorType a where
- decodeTensorData :: TensorData a -> Vector a
- encodeTensorData :: Shape -> Vector a -> TensorData a
- newtype Shape = Shape [Int64]
- type OneOf ts a = (TensorType a, TensorTypes ts, NoneOf (AllTensorTypes \\ ts) a)
- type family a /= b :: Constraint
- colocateWith :: forall a v b. Tensor v b -> Build a -> Build a
- newtype Device = Device {
- deviceName :: Text
- withDevice :: Maybe Device -> Build a -> Build a
- withNameScope :: Text -> Build a -> Build a
- named :: TensorType a => Text -> Tensor v a -> Tensor v a
- withControlDependencies :: Nodes t => t -> Build a -> Build a
- group :: Nodes t => t -> Build ControlNode
- identity :: TensorType a => Tensor v a -> Tensor v a
- noOp :: ControlNode
Session
Customization for session. Use the lenses to update:
sessionTarget
, sessionTracer
, sessionConfig
.
sessionConfig :: Lens' Options ConfigProto Source
Uses the specified config for the created session.
sessionTarget :: Lens' Options ByteString Source
Target can be: "local", ip:port, host:port. The set of supported factories depends on the linked in libraries.
sessionTracer :: Lens' Options Tracer Source
Uses the given logger to monitor session progress.
runSession :: Session a -> IO a Source
Run Session
actions in a new TensorFlow session.
runSessionWithOptions :: Options -> Session a -> IO a Source
Run Session
actions in a new TensorFlow session created with
the given option setter actions (sessionTarget
, sessionConfig
).
Building graphs
buildAnd :: (a -> Session b) -> Build a -> Session b Source
Helper combinator for doing something with the result of a Build
action.
Example usage:
buildAnd run :: Fetchable t a => Build t -> Session a
buildWithSummary :: forall a. Build a -> Session (a, [SummaryTensor]) Source
Running graphs
class Nodes t => Fetchable t a Source
Types that tensor representations (e.g. Tensor
, ControlNode
) can be
fetched into.
Includes collections of tensors (e.g. tuples).
(~) * a () => Fetchable ControlNode a Source | |
Fetchable t a => Fetchable [t] [a] Source | |
(TensorType a, (~) * a a') => Fetchable (Tensor v a) (Scalar a') Source | |
(TensorType a, (~) * a a') => Fetchable (Tensor v a) (Vector a') Source | |
(Fetchable t1 a1, Fetchable t2 a2) => Fetchable (t1, t2) (a1, a2) Source | |
(Fetchable t1 a1, Fetchable t2 a2, Fetchable t3 a3) => Fetchable (t1, t2, t3) (a1, a2, a3) Source |
Eq a => Eq (Scalar a) Source | |
Floating a => Floating (Scalar a) Source | |
Fractional a => Fractional (Scalar a) Source | |
Num a => Num (Scalar a) Source | |
Ord a => Ord (Scalar a) Source | |
Real a => Real (Scalar a) Source | |
RealFloat a => RealFloat (Scalar a) Source | |
RealFrac a => RealFrac (Scalar a) Source | |
Show a => Show (Scalar a) Source | |
IsString a => IsString (Scalar a) Source | |
(TensorType a, (~) * a a') => Fetchable (Tensor v a) (Scalar a') Source |
Types that contain ops which can be run.
run :: Fetchable t a => t -> Session a Source
Run a subgraph t
, rendering any dependent nodes that aren't already
rendered, and fetch the corresponding values for a
.
run_ :: Nodes t => t -> Session () Source
Run a subgraph t
, rendering and extending any dependent nodes that aren't
already rendered. This behaves like run
except that it doesn't do any
fetches.
feed :: Tensor v a -> TensorData a -> Feed Source
runWithFeeds :: Fetchable t a => [Feed] -> t -> Session a Source
Run a subgraph t
, rendering any dependent nodes that aren't already
rendered, feed the given input values, and fetch the corresponding result
values for a
.
runWithFeeds_ :: Nodes t => [Feed] -> t -> Session () Source
Run a subgraph t
, rendering any dependent nodes that aren't already
rendered, feed the given input values, and fetch the corresponding result
values for a
. This behaves like runWithFeeds
except that it doesn't do
any fetches.
Async
Starts a concurrent thread which evaluates the given Nodes forever until runSession exits or an exception occurs. Graph extension happens synchronously, but the resultant run proceeds as a separate thread.
Build
An action for building nodes in a TensorFlow graph.
Used to manage build state internally as part of the Session
monad.
render :: Tensor v a -> Build (Tensor v a) Source
Render a Tensor
, fixing its name, scope, device and control inputs from
the Build
context. Also renders any dependencies of the Tensor
that
weren't already rendered.
This operation is idempotent; render >=> render === render
. However,
rendering a (previously un-rendered) Tensor
in two different contexts
may result in two different Tensor
s.
asGraphDef :: Build a -> GraphDef Source
Produce a GraphDef proto representation of the nodes that are rendered in
the given Build
action.
addGraphDef :: GraphDef -> Build () Source
Tensor
data ControlNode Source
A type of graph node which has no outputs. These nodes are valuable for causing side effects when they are run.
BuildOp ControlNode Source | |
OpResult ControlNode Source | |
Nodes ControlNode Source | |
(~) * a () => Fetchable ControlNode a Source |
A named output of a TensorFlow operation.
The type parameter a
is the type of the elements in the Tensor
. The
parameter v
is either Value
or Ref
, depending on whether the graph is
treating this op output as an immutable Value
or a stateful Ref
(e.g., a
variable). Note that a Tensor Ref
can be casted into a Tensor Value
via
value
.
BuildOp [Tensor Value a] Source | |
BuildOp f => BuildOp ([Tensor v a] -> f) Source | |
BuildOp f => BuildOp (Tensor v a -> f) Source | |
BuildOp (Tensor Ref a) Source | |
BuildOp (Tensor Value a) Source | |
OpResult (Tensor Ref a) Source | |
OpResult (Tensor Value a) Source | |
Nodes (Tensor v a) Source | |
(TensorType a, (~) * a a') => Fetchable (Tensor v a) (Scalar a') Source | |
(TensorType a, (~) * a a') => Fetchable (Tensor v a) (Vector a') Source |
data TensorKind v where Source
ValueKind :: TensorKind Value | |
RefKind :: TensorKind Ref |
tensorAttr :: Attribute attr => Text -> Traversal' (Tensor v a) attr Source
Lens for the attributes of a tensor.
Only valid if the tensor has not yet been rendered. If the tensor has been rendered, the traversal will be over nothing (nothing can be read or written).
value :: Tensor v a -> Tensor Value a Source
Cast a 'Tensor *' into a 'Tensor Value'. Common usage is to cast a Ref into Value. This behaves like a no-op.
tensorFromName :: TensorKind v -> Text -> Tensor v a Source
Create a Tensor
for a given name. This can be used to reference nodes
in a GraphDef
that was loaded via addGraphDef
.
TODO(judahjacobson): add more safety checks here.
Element types
data TensorData a Source
Data about a tensor that is encoded for the TensorFlow APIs.
class TensorType a where Source
The class of scalar types supported by tensorflow.
decodeTensorData :: TensorData a -> Vector a Source
Decode the bytes of a TensorData into a Vector.
encodeTensorData :: Shape -> Vector a -> TensorData a Source
Encode a Vector into a TensorData.
The values should be in row major order, e.g.,
element 0: index (0, ..., 0) element 1: index (0, ..., 1) ...
Shape (dimensions) of a tensor.
type OneOf ts a = (TensorType a, TensorTypes ts, NoneOf (AllTensorTypes \\ ts) a) Source
A Constraint
specifying the possible choices of a TensorType
.
We implement a Constraint
like OneOf '[Double, Float] a
by turning the
natural representation as a conjunction, i.e.,
a == Double || a == Float
into a disjunction like
a /= Int32 && a /= Int64 && a /= ByteString && ...
using an enumeration of all the possible TensorType
s.
type family a /= b :: Constraint Source
A constraint checking that two types are different.
a /= a = TypeError a ~ ExcludedCase | |
a /= b = () |
Op combinators
colocateWith :: forall a v b. Tensor v b -> Build a -> Build a Source
Places all nodes rendered in the given Build
action on the same
device as the given Tensor (see also withDevice
). Make sure that
the action has side effects of rendering the desired tensors. A pure
return would not have the desired effect.
A device that a node can be assigned to. There's a naming convention where the device names are constructed from job and replica names.
Device | |
|
withDevice :: Maybe Device -> Build a -> Build a Source
Set a device for all nodes rendered in the given Build
action
(unless further overridden by another use of withDevice).
withNameScope :: Text -> Build a -> Build a Source
Prepend a scope to all nodes rendered in the given Build
action.
named :: TensorType a => Text -> Tensor v a -> Tensor v a Source
Returns a Tensor
with a given name and the same shape and contents as
the input.
TODO(judahjacobson): This breaks when used with uninitialize Tensor Ref
s,
since RefIdentity
doesn't have SetAllowsUninitializedInput(). Look into
whether we can change that op.
Dependencies
withControlDependencies :: Nodes t => t -> Build a -> Build a Source
Modify a Build
action, such that all new ops rendered in it will depend
on the nodes in the first argument.
group :: Nodes t => t -> Build ControlNode Source
Create an op that groups multiple operations.
When this op finishes, all ops in the input n
have finished. This op has
no output.
Misc
identity :: TensorType a => Tensor v a -> Tensor v a Source
Returns a Tensor
with the same shape and contents as the input.
Does nothing. Only useful as a placeholder for control edges.