Package tensorflow
Overview ▹
▹ Example
Index ▹
func Version ¶
func Version() string
Version returns a string describing the version of the underlying TensorFlow runtime.
type DataType ¶
DataType holds the type for a scalar value. E.g., one slot in a tensor.
type DataType C.TF_DataType
Types of scalar values in the TensorFlow type system.
const ( Float DataType = C.TF_FLOAT Double DataType = C.TF_DOUBLE Int32 DataType = C.TF_INT32 Uint8 DataType = C.TF_UINT8 Int16 DataType = C.TF_INT16 Int8 DataType = C.TF_INT8 String DataType = C.TF_STRING Complex64 DataType = C.TF_COMPLEX64 Complex DataType = C.TF_COMPLEX Int64 DataType = C.TF_INT64 Bool DataType = C.TF_BOOL Qint8 DataType = C.TF_QINT8 Quint8 DataType = C.TF_QUINT8 Qint32 DataType = C.TF_QINT32 Bfloat16 DataType = C.TF_BFLOAT16 Qint16 DataType = C.TF_QINT16 Quint16 DataType = C.TF_QUINT16 Uint16 DataType = C.TF_UINT16 Complex128 DataType = C.TF_COMPLEX128 Half DataType = C.TF_HALF )
type Graph ¶
Graph represents a computation graph. Graphs may be shared between sessions.
type Graph struct {
// contains filtered or unexported fields
}
func NewGraph ¶
func NewGraph() *Graph
NewGraph returns a new Graph.
func (*Graph) AddOperation ¶
func (g *Graph) AddOperation(args OpSpec) (*Operation, error)
AddOperation adds an operation to g.
func (*Graph) Import ¶
func (g *Graph) Import(def []byte, prefix string) error
Import imports the nodes and edges from a serialized representation of another Graph into g.
Names of imported nodes will be prefixed with prefix.
func (*Graph) Operation ¶
func (g *Graph) Operation(name string) *Operation
Operation returns the Operation named name in the Graph, or nil if no such operation is present.
func (*Graph) WriteTo ¶
func (g *Graph) WriteTo(w io.Writer) (int64, error)
WriteTo writes out a serialized representation of g to w.
Implements the io.WriterTo interface.
type Input ¶
Input is the interface for specifying inputs to an operation being added to a Graph.
Operations can have multiple inputs, each of which could be either a tensor produced by another operation (an Output object), or a list of tensors produced by other operations (an OutputList). Thus, this interface is implemented by both Output and OutputList.
See OpSpec.Input for more information.
type Input interface {
// contains filtered or unexported methods
}
type OpSpec ¶
OpSpec is the specification of an Operation to be added to a Graph (using Graph.AddOperation).
type OpSpec struct { // Type of the operation (e.g., "Add", "MatMul"). Type string // Name by which the added operation will be referred to in the Graph. // If omitted, defaults to Type. Name string // Inputs to this operation, which in turn must be outputs // of other operations already added to the Graph. // // An operation may have multiple inputs with individual inputs being // either a single tensor produced by another operation or a list of // tensors produced by multiple operations. For example, the "Concat" // operation takes two inputs: (1) the dimension along which to // concatenate and (2) a list of tensors to concatenate. Thus, for // Concat, len(Input) must be 2, with the first element being an Output // and the second being an OutputList. Input []Input // Map from attribute name to its value that will be attached to this // operation. Attrs map[string]interface{} }
type Operation ¶
Operation that has been added to the graph.
type Operation struct {
// contains filtered or unexported fields
}
func (*Operation) Name ¶
func (op *Operation) Name() string
Name returns the name of the operation.
func (*Operation) NumOutputs ¶
func (op *Operation) NumOutputs() int
NumOutputs returns the number of outputs of op.
func (*Operation) Output ¶
func (op *Operation) Output(i int) Output
Output returns the i-th output of op.
func (*Operation) OutputListSize ¶
func (op *Operation) OutputListSize(output string) (int, error)
OutputListSize returns the size of the list of Outputs that is produced by a named output of op.
An Operation has multiple named outputs, each of which produces either a single tensor or a list of tensors. This method returns the size of the list of tensors for a specific output of the operation, identified by its name.
func (*Operation) Type ¶
func (op *Operation) Type() string
Type returns the name of the operator used by this operation.
type Output ¶
Output represents one of the outputs of an operation in the graph. Has a DataType (and eventually a Shape). May be passed as an input argument to a function for adding operations to a graph, or to a Session's Run() method to fetch that output as a tensor.
type Output struct { // Op is the Operation that produces this Output. Op *Operation // Index specifies the index of the output within the Operation. Index int }
func (Output) DataType ¶
func (p Output) DataType() DataType
DataType returns the type of elements in the tensor produced by p.
func (Output) Shape ¶
func (p Output) Shape() Shape
Shape returns the (possibly incomplete) shape of the tensor produced p.
type OutputList ¶
OutputList represents a list of Outputs that can be provided as input to another operation.
type OutputList []Output
type PartialRun ¶
PartialRun enables incremental evaluation of graphs.
PartialRun allows the caller to pause the evaluation of a graph, run arbitrary code that depends on the intermediate computation of the graph, and then resume graph execution. The results of the arbitrary code can be fed into the graph when resuming execution. In contrast, Session.Run executes the graph to compute the requested fetches using the provided feeds and discards all intermediate state (e.g., value of intermediate tensors) when it returns.
For example, consider a graph for unsupervised training of a neural network model. PartialRun can be used to pause execution after the forward pass of the network, let the caller actuate the output (e.g., play a game, actuate a robot etc.), determine the error/loss and then feed this calculated loss when resuming the backward pass of the graph.
type PartialRun struct {
// contains filtered or unexported fields
}
▹ Example
func (*PartialRun) Run ¶
func (pr *PartialRun) Run(feeds map[Output]*Tensor, fetches []Output, targets []*Operation) ([]*Tensor, error)
Run resumes execution of the graph to compute the requested fetches and targets with the provided feeds.
type SavedModel ¶
SavedModel represents the contents of loaded SavedModel. TODO(jhseu): Add and document metagraphdef when we pregenerate protobufs.
type SavedModel struct { Session *Session Graph *Graph }
func LoadSavedModel ¶
func LoadSavedModel(exportDir string, tags []string, options *SessionOptions) (*SavedModel, error)
LoadSavedModel creates a new SavedModel from a model previously exported to a directory on disk.
Exported models contain a set of graphs and, optionally, variable values. Tags in the model identify a single graph. LoadSavedModel initializes a session with the identified graph and with variables initialized to from the checkpoints on disk.
The tensorflow package currently does not have the ability to export a model to a directory from Go. This function thus currently targets loading models exported in other languages, such as using tf.saved_model.builder in Python. See: https://www.tensorflow.org/code/tensorflow/python/saved_model/
type Session ¶
Session drives a TensorFlow graph computation.
When a Session is created with a given target, a new Session object is bound to the universe of resources specified by that target. Those resources are available to this session to perform computation described in the GraphDef. After creating the session with a graph, the caller uses the Run() API to perform the computation and potentially fetch outputs as Tensors. A Session allows concurrent calls to Run().
type Session struct {
// contains filtered or unexported fields
}
func NewSession ¶
func NewSession(graph *Graph, options *SessionOptions) (*Session, error)
NewSession creates a new execution session with the associated graph. options may be nil to use the default options.
func (*Session) Close ¶
func (s *Session) Close() error
Close a session. This contacts any other processes associated with this session, if applicable. Blocks until all previous calls to Run have returned.
func (*Session) NewPartialRun ¶
func (s *Session) NewPartialRun(feeds, fetches []Output, targets []*Operation) (*PartialRun, error)
NewPartialRun sets up the graph for incremental evaluation.
All values of feeds, fetches and targets that may be provided to Run calls on the returned PartialRun need to be provided to NewPartialRun.
See documentation for the PartialRun type.
func (*Session) Run ¶
func (s *Session) Run(feeds map[Output]*Tensor, fetches []Output, targets []*Operation) ([]*Tensor, error)
Run the graph with the associated session starting with the supplied feeds to compute the value of the requested fetches. Runs, but does not return Tensors for operations specified in targets.
On success, returns the fetched Tensors in the same order as supplied in the fetches argument. If fetches is set to nil, the returned Tensor fetches is empty.
type SessionOptions ¶
SessionOptions contains configuration information for a session.
type SessionOptions struct { // Target indicates the TensorFlow runtime to connect to. // // If 'target' is empty or unspecified, the local TensorFlow runtime // implementation will be used. Otherwise, the TensorFlow engine // defined by 'target' will be used to perform all computations. // // "target" can be either a single entry or a comma separated list // of entries. Each entry is a resolvable address of one of the // following formats: // local // ip:port // host:port // ... other system-specific formats to identify tasks and jobs ... // // NOTE: at the moment 'local' maps to an in-process service-based // runtime. // // Upon creation, a single session affines itself to one of the // remote processes, with possible load balancing choices when the // "target" resolves to a list of possible processes. // // If the session disconnects from the remote process during its // lifetime, session calls may fail immediately. Target string // Config is a binary-serialized representation of the // tensorflow.ConfigProto protocol message // (https://www.tensorflow.org/code/tensorflow/core/protobuf/config.proto). Config []byte }
type Shape ¶
Shape represents the (possibly partially known) shape of a tensor that will be produced by an operation.
The zero-value of a Shape represents a shape with an unknown number of dimensions.
type Shape struct {
// contains filtered or unexported fields
}
func MakeShape ¶
func MakeShape(shape ...int64) Shape
MakeShape returns a Shape with the provided size of each dimension.
A value of -1 implies that the size of the corresponding dimension is not known.
func ScalarShape ¶
func ScalarShape() Shape
ScalarShape returns a Shape representing a scalar.
func (Shape) IsFullySpecified ¶
func (s Shape) IsFullySpecified() bool
IsFullySpecified returns true iff the size of all the dimensions of s are known.
func (Shape) NumDimensions ¶
func (s Shape) NumDimensions() int
NumDimensions returns the number of dimensions represented by s, or -1 if unknown.
func (Shape) Size ¶
func (s Shape) Size(dim int) int64
Size returns the size of the dim-th dimension of the shape, or -1 if it is unknown.
REQUIRES: 0 <= dim < s.NumDimensions()
func (Shape) String ¶
func (s Shape) String() string
func (Shape) ToSlice ¶
func (s Shape) ToSlice() ([]int64, error)
ToSlice returns the (possibly partially known) shape represented by s as a slice, or an error if the number of dimensions is not known.
type Tensor ¶
Tensor holds a multi-dimensional array of elements of a single data type.
type Tensor struct {
// contains filtered or unexported fields
}
func NewTensor ¶
func NewTensor(value interface{}) (*Tensor, error)
NewTensor converts from a Go value to a Tensor. Valid values are scalars, slices, and arrays. Every element of a slice must have the same length so that the resulting Tensor has a valid shape.
func ReadTensor ¶
func ReadTensor(dataType DataType, shape []int64, r io.Reader) (*Tensor, error)
ReadTensor constructs a Tensor with the provided type and shape from the serialized tensor contents in r.
See also WriteContentsTo.
func (*Tensor) DataType ¶
func (t *Tensor) DataType() DataType
DataType returns the scalar datatype of the Tensor.
func (*Tensor) Shape ¶
func (t *Tensor) Shape() []int64
Shape returns the shape of the Tensor.
func (*Tensor) Value ¶
func (t *Tensor) Value() interface{}
Value converts the Tensor to a Go value. For now, not all Tensor types are supported, and this function may panic if it encounters an unsupported DataType.
The type of the output depends on the Tensor type and dimensions. For example: Tensor(int64, 0): int64 Tensor(float64, 3): [][][]float64
func (*Tensor) WriteContentsTo ¶
func (t *Tensor) WriteContentsTo(w io.Writer) (int64, error)
WriteContentsTo writes the serialized contents of t to w.
Returns the number of bytes written. See ReadTensor for reconstructing a Tensor from the serialized form.
WARNING: WriteContentsTo is not comprehensive and will fail if t.DataType() is non-numeric (e.g., String). See https://github.com/tensorflow/tensorflow/issues/6003.