tensorflow

package module
v0.0.0-...-a925eb1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 2, 2023 License: Apache-2.0, BSD-2-Clause Imports: 16 Imported by: 0

README

TensorFlow in Go

Construct and execute TensorFlow graphs in Go.

GoDoc

WARNING: The API defined in this package is not stable and can change without notice. The same goes for the package path: (github.com/hdu-hh/tensorflow/tensorflow/go).

GoDocs for the provided packages

The API documentation of the packages can be read online at go.pkg.dev. Please see

Support

This experimental fork of tensorflow has no support.

For generic tensorflow issues use Stack Overflow and/or GitHub issues.

Contributions

Contributions are welcome. If making any signification changes, probably best to discuss on a GitHub issue before investing too much time. GitHub pull requests are used for contributions.

Documentation

Overview

Package tensorflow is a Go binding to TensorFlow.

The API is subject to change and may break at any time.

TensorFlow (www.tensorflow.org) is an open source software library for numerical computation using data flow graphs. This package provides functionality to build and execute such graphs and depends on TensorFlow being available. For installation instructions see https://github.com/hdu-hh/tensorflow/blob/master/tensorflow/go/README.md

This package is from an experimental fork of the upstream Tensorflow go binding that focusses on improving its suitability for building and training tensorflow graphs.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetAllOpList

func GetAllOpList() *pbs.OpList

GetAllOpList() lists all Operations available.

The list provides interesting details even of operations that are not yet wrapped by the op package. This can help to experiment with these missing operations and to resolve issues that prevent them from being wrapped by the op package.

func ListSavedModelDetails

func ListSavedModelDetails(exportDir string) (tags [][]string, signatures []map[string]Signature, err error)

ListSavedModelDetails lists the tags and signatures per MetaGraph from a model previously exported to a directory on disk.

func Version

func Version() string

Version returns a string describing the version of the underlying TensorFlow runtime.

Types

type Consumer

type Consumer struct {
	// Op is the Operation that is consuming the output of another operation.
	Op *Operation

	// Index is the index of the input within Op that the output of another
	// operation is connected to.
	Index int
}

Consumer identifies a specific input of an operation that consumes the output of another operation.

func (Consumer) DataType

func (p Consumer) DataType() DataType

DataType returns the type of the input.

func (Consumer) Producer

func (p Consumer) Producer() Output

Producer returns the Output that is connected to this Consumer.

type Context

type Context struct {
	// contains filtered or unexported fields
}

Context for executing operations eagerly.

A Context allows operations to be executed immediately. It encapsulates information such as the available devices, resource manager etc. It also allows the user to configure execution using a ConfigProto, as they can configure a Session when executing a Graph.

func NewContext

func NewContext(options *ContextOptions) (*Context, error)

NewContext creates a new context for eager execution. options may be nil to use the default options.

func (*Context) ListDevices

func (c *Context) ListDevices() ([]Device, error)

ListDevices returns the list of devices associated with a Context.

type ContextOptions

type ContextOptions struct {
	// Config is a binary-serialized representation of the
	// [pbs.ConfigProto] protocol message
	// (https://www.tensorflow.org/code/tensorflow/core/protobuf/config.proto).
	Config []byte

	// Sets the default execution mode
	Async bool
}

ContextOptions contains configuration information for a session

type DataType

type DataType C.TF_DataType

DataType holds the type for a scalar value. E.g., one element in a tensor.

const (
	Float        DataType = C.TF_FLOAT
	Double       DataType = C.TF_DOUBLE
	Int32        DataType = C.TF_INT32
	Uint32       DataType = C.TF_UINT32
	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
	Uint64       DataType = C.TF_UINT64
	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
	Float8e5m2   DataType = C.TF_FLOAT8_E5M2
	Float8e4m3fn DataType = C.TF_FLOAT8_E4M3FN
	Resource     DataType = C.TF_RESOURCE
	Variant      DataType = C.TF_VARIANT
)

Types of scalar values in the TensorFlow type system.

func (DataType) DeRef

func (dtype DataType) DeRef() DataType

DeRef returns the underlying data type of a reference type

type Device

type Device struct {
	Name, Type       string
	MemoryLimitBytes int64
}

Device structure contains information about a device associated with a session, as returned by [ListDevices].

func (Device) String

func (d Device) String() string

String describes d and implements fmt.Stringer.

type FeedMap

type FeedMap map[Output]*Tensor

type Func

type Func struct {
	// contains filtered or unexported fields
}

Func represents a tensorflow function.

It can be created from a

func ImportFunc

func ImportFunc(proto []byte) (*Func, error)

ImportFunc creates a Func from the binary serialization of a pbs.FunctionDef protocol buffer.

func (*Func) Delete

func (fn *Func) Delete()

Delete the tensorflow function. Deleting a function does not remove it from any graphs it was copied to.

func (*Func) Name

func (fn *Func) Name() string

Name returns the name of the tensorflow function.

func (*Func) SetAttrFrom

func (fn *Func) SetAttrFrom(attrName string, proto []byte) error

SetAttrFrom sets the function attribute from the binary serialization of a pbs.AttrValue protocol buffer.

func (*Func) Signature

func (fn *Func) Signature() *pbs.OpDef

Signature returns the function signature as pbs.OpDef object

func (*Func) WriteAttrTo

func (fn *Func) WriteAttrTo(attrName string, w io.Writer) (int64, error)

WriteAttrTo writes out the function attribute as a binary serialization of a pbs.AttrValue protocol buffer.

func (*Func) WriteTo

func (fn *Func) WriteTo(w io.Writer) (int64, error)

WriteTo writes out the function as a binary serialization of a pbs.FunctionDef protocol buffer.

Implements the io.WriterTo interface.

type Graph

type Graph struct {
	// contains filtered or unexported fields
}

Graph represents a computation graph. Graphs may be shared between sessions.

func NewGraph

func NewGraph() *Graph

NewGraph returns a new Graph.

func (*Graph) AddGradients

func (g *Graph) AddGradients(prefix string, y []Output, x []Output, dx []Output) ([]Output, error)

AddGradients adds operations to compute the partial derivatives of the sum of tensors in y with respect to tensors in x, i.e., d(y[0] + y[1] + ...) / d x[0], d(y[0] + y[1] + ... ) / d x[1] etc.

  • prefix, if non-empty, is the name prefix used for all operations added to the graph to compute these gradients.

func (*Graph) AddOperation

func (g *Graph) AddOperation(args OpSpec) (*Operation, error)

AddOperation adds an operation to the graph.

func (*Graph) AsFunc

func (g *Graph) AsFunc(name string, inputs, outputs []Output, outNames []string, desc string) (*Func, error)

AsFunc returns the tensorflow function (Func) corresponding to the graph.

func (*Graph) Functions

func (g *Graph) Functions() []*Func

Functions returns the list of functions (Func) registered in the graph.

func (*Graph) Import

func (g *Graph) Import(def []byte, prefix string) error

Import imports the nodes and edges from a serialized representation of a pbs.GraphDef protocol buffer into the graph.

Names of imported nodes will be prefixed with prefix.

func (*Graph) ImportWithOptions

func (g *Graph) ImportWithOptions(def []byte, options GraphImportOptions) error

ImportWithOptions imports the nodes and edges from a serialized representation of a pbs.GraphDef protocol buffer into the graph.

Multiple options can be specified for the newly imported nodes.

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) Operations

func (g *Graph) Operations() []Operation

Operations returns a list of all operations in the graph

func (*Graph) RegisterFunc

func (g *Graph) RegisterFunc(fn, grad *Func) error

RegisterFunc copies and registers the tensorflow function and its eventual gradient into the graph. The function must not be nil but the gradient may be nil.

func (*Graph) UpdateEdge

func (g *Graph) UpdateEdge(newSrc Output, dstOp *Operation, idx int)

UpdateEdge updates a single graph edge so an op gets another input. For mass updates the Graph.ImportWithOptions() method may be faster.

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 GraphImportOptions

type GraphImportOptions struct {
	// Node prefix
	Prefix string

	// Execution device
	Device string
	// contains filtered or unexported fields
}

The GraphImportOptions struct holds parameters for the [ImportWithOptions] function.

func (*GraphImportOptions) AddInputMapping

func (o *GraphImportOptions) AddInputMapping(src string, srcIndex int, dst Output)

AddInputMapping adds a mapping between an Output in the imported graph and an Output in the destination graph that it should be replaced with, where src:srcIndex is the name of the Operation and Output index to replace and dst is the output to replace it with.

type Input

type Input interface {
	// contains filtered or unexported methods
}

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 LibraryHandler

type LibraryHandler struct {
	// contains filtered or unexported fields
}

func LoadLibrary

func LoadLibrary(path string) (*LibraryHandler, error)

Load library content into current context, useful to load ops implementation into non-monolithic TF build. Returns LibraryHandler or nil and error.

type OpSpec

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{}

	// Operations that must be executed before executing the operation
	// being added.
	ControlDependencies []*Operation

	// The device on which the operation should be executed.
	// If omitted, an appropriate device will automatically be selected.
	//
	// For example, if set of "/device:GPU:0", then the operation will
	// execute on GPU #0.
	Device string
}

OpSpec is the specification of an Operation to be added to a Graph (using Graph.AddOperation).

type Operation

type Operation struct {
	// contains filtered or unexported fields
}

Operation that has been added to the graph.

func (*Operation) Attr

func (op *Operation) Attr(name string) (interface{}, error)

Attr returns the value of an attribute on op. It returns an error if the attribute does not exist.

func (*Operation) AttrMap

func (op *Operation) AttrMap() (attrMap map[string]interface{}, err error)

GetAttrNames returns the attribute map of an op or nil when the op has no attributes

func (*Operation) ControlInputs

func (op *Operation) ControlInputs() []*Operation

ControlInputs returns the control inputs of an operation.

func (*Operation) ControlOutputs

func (op *Operation) ControlOutputs() []*Operation

ControlOutputs returns the operations that have op as a control input.

func (*Operation) Device

func (op *Operation) Device() string

Device returns a specification of the device on which this operation will be executed, or the empty string if there is no such specification.

func (*Operation) Name

func (op *Operation) Name() string

Name returns the name of the operation.

func (*Operation) NumInputs

func (op *Operation) NumInputs() int

NumInputs returns the number of inputs of op.

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) Outputs

func (op *Operation) Outputs() (outs []Output)

Outputs returns all outputs of op.

func (*Operation) Type

func (op *Operation) Type() string

Type returns the name of the operator used by this operation.

type Output

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
}

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.

func (Output) Consumers

func (p Output) Consumers() []Consumer

Consumers returns the inputs that consume this output.

func (Output) DataType

func (p Output) DataType() DataType

DataType returns the type of elements in the tensor produced by the output.

func (Output) Shape

func (p Output) Shape() Shape

Shape returns the (possibly incomplete) shape of the tensor produced by the output.

type OutputList

type OutputList []Output

OutputList represents a list of Outputs that can be provided as input to another operation.

type PartialRun

type PartialRun struct {
	// contains filtered or unexported fields
}

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.

Example
var (
	// Create a graph: a + 2 + 3 + b.
	//
	// Skipping error handling for brevity of this example.
	// The 'op' package can be used to make graph construction code
	// with error handling more succinct.
	g     = NewGraph()
	a     = _Placeholder(g, "a", Int32)
	b     = _Placeholder(g, "b", Int32)
	two   = _Const(g, "Two", int32(2))
	three = _Const(g, "Three", int32(3))

	plus2 = _Add(g, "plus2", a, two)       // a + 2
	plus3 = _Add(g, "plus3", plus2, three) // (a + 2) + 3
	plusB = _Add(g, "plusB", plus3, b)     // ((a + 2) + 3) + b

)
sess, err := NewSession(g, nil)
if err != nil {
	panic(err)
}
defer sess.Close()

// All the feeds, fetches and targets for subsequent PartialRun.Run
// calls must be provided at setup.
pr, err := sess.NewPartialRun(
	[]Output{a, b},
	[]Output{plus2, plusB},
	[]*Operation{plus3.Op},
)
if err != nil {
	panic(err)
}

// Feed 'a=1', fetch 'plus2', and compute (but do not fetch) 'plus3'.
// Imagine this to be the forward pass of unsupervised neural network
// training of a robot.
val, _ := NewTensor(int32(1))
fetches, err := pr.Run(
	map[Output]*Tensor{a: val},
	[]Output{plus2},
	nil)
if err != nil {
	panic(err)
}
v1 := fetches[0].Value().(int32)

// Now, feed 'b=4', fetch 'plusB=a+2+3+b'
// Imagine this to be the result of actuating the robot to determine
// the error produced by the current state of the neural network.
val, _ = NewTensor(int32(4))
fetches, err = pr.Run(
	map[Output]*Tensor{b: val},
	[]Output{plusB},
	nil)
if err != nil {
	panic(err)
}
v2 := fetches[0].Value().(int32)

fmt.Println(v1, v2)
Output:

3 10

func (*PartialRun) Run

func (pr *PartialRun) Run(feeds FeedMap, 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

type SavedModel struct {
	Session    *Session
	Graph      *Graph
	Signatures map[string]Signature
}

SavedModel represents the contents of loaded SavedModel. TODO(jhseu): Add and document metagraphdef when we pregenerate protobufs.

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.

Tags per graph can be listed using the ListSavedModelDetails function.

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

type Session struct {
	// contains filtered or unexported fields
}

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().

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) ListDevices

func (s *Session) ListDevices() ([]Device, error)

ListDevices returns the list of devices associated with a Session.

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 FeedMap, 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

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
	// [pbs.ConfigProto] protocol message
	// (https://www.tensorflow.org/code/tensorflow/core/protobuf/config.proto).
	Config []byte
}

SessionOptions contains configuration information for a session.

type Shape

type Shape struct {
	// contains filtered or unexported fields
}

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.

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) MustNumElements

func (s Shape) MustNumElements() (numElems int64)

MustNumElements returns the number of elements of a shape. It panics if the number of elements is not known.

func (Shape) MustSlice

func (s Shape) MustSlice() []int64

MustSlice returns the shape as an int64 slice. It panics if a dimension or the number of dimensions is not known.

func (Shape) MustSlice32

func (s Shape) MustSlice32() []int32

MustSlice32 returns the shape as an int32 slice.

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. For a negative dim argument the dim+NumDimension dimension is returned. Returns -1 if the dimension is unknown.

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 Signature

type Signature struct {
	Inputs, Outputs map[string]TensorInfo
	MethodName      string
}

A Signature defines the signature of a computation supported by a TensorFlow graph.

For example, a model with two loss computations, sharing a single input, might have the following signature_def map.

Note that across the two Signatures "loss_A" and "loss_B", the input key, output key, and method_name are identical, and will be used by system(s) that implement or rely upon this particular loss method. The output tensor names differ, demonstrating how different outputs can exist for the same method.

signature_def {
  key: "loss_A"
  value {
    inputs {
      key: "input"
      value {
        name: "input:0"
        dtype: DT_STRING
        tensor_shape: ...
      }
    }
    outputs {
      key: "loss_output"
      value {
        name: "loss_output_A:0"
        dtype: DT_FLOAT
        tensor_shape: ...
      }
    }
  }
  ...
  method_name: "some/package/compute_loss"
}

signature_def {
  key: "loss_B"
  value {
    inputs {
      key: "input"
      value {
        name: "input:0"
        dtype: DT_STRING
        tensor_shape: ...
      }
    }
    outputs {
      key: "loss_output"
      value {
        name: "loss_output_B:0"
        dtype: DT_FLOAT
        tensor_shape: ...
      }
    }
  }
  ...
  method_name: "some/package/compute_loss"
}

type Tensor

type Tensor struct {
	// contains filtered or unexported fields
}

Tensor holds a multi-dimensional array of elements of a single data type.

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) MustReshape

func (t *Tensor) MustReshape(newShape ...int64) *Tensor

Reshape updates a tensor's shape in place. It panics if the reshape failed.

func (*Tensor) Reshape

func (t *Tensor) Reshape(newShape []int64) error

Reshape updates tensor's shape in place if this is possible or returns an error otherwise.

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.

type TensorHandle

type TensorHandle struct {
	// contains filtered or unexported fields
}

TensorHandle is a handle to a tensor on a device.

A Tensor referenced by a TensorHandle may be on any device, whereas a Tensor always resides in the host CPU's memory.

A Tensor referenced by a TensorHandle may not have been computed yet. For example, a TensorHandle might reference the output of an operation that has not finished executing. Because of this, various methods, such as Shape() may block until the tensor has been instantiated.

This allows multiple operations to be performed on tensors on a device (e.g. a GPU) without sending these values back to the host CPU in between every operation.

func NewTensorHandle

func NewTensorHandle(t *Tensor) (*TensorHandle, error)

NewTensorHandle creates a new tensor handle from a tensor.

func (*TensorHandle) BackingDeviceName

func (th *TensorHandle) BackingDeviceName() (string, error)

BackingDeviceName returns the name of the device in whose memory tensor handle th resides. This function will block till the operation that produces th has completed.

func (*TensorHandle) CopyToDevice

func (th *TensorHandle) CopyToDevice(c *Context, deviceName string) (*TensorHandle, error)

CopyToDevice creates a new TensorHandle with the same contents as this TensorHandle but placed in the memory of the device 'deviceName'. If source and destination are the same device, then this creates a new handle that shares the underlying buffer. Otherwise, it currently requires at least one of the source or destination devices to be CPU (i.e., for the source or destination tensor to be placed in host memory).

func (*TensorHandle) DataType

func (th *TensorHandle) DataType() DataType

DataType returns the TensorHandle's datatype.

func (*TensorHandle) DeviceName

func (th *TensorHandle) DeviceName() (string, error)

DeviceName returns the name of the device of the operation that produced the TensorHandle. If the handle was produced by a copy, it returns the destination device of the copy. Note that returned device name is not always the device holding the tensor handle's memory. If you want the latter, use BackingDeviceName. This function will block till the operation that produces th has completed.

func (*TensorHandle) Shape

func (th *TensorHandle) Shape() ([]int64, error)

Shape returns the shape of the Tensor referenced by th.

func (*TensorHandle) ToTensor

func (th *TensorHandle) ToTensor() (*Tensor, error)

ToTensor returns the Tensor referenced by th. It may block if this tensor is not yet computed.

type TensorInfo

type TensorInfo struct {
	Name  string
	DType DataType
	Shape Shape
}

A TensorInfo contains the information about a Tensor necessary for feeding or retrieval.

Directories

Path Synopsis
Command genop generates a Go source file with functions for TensorFlow ops.
Command genop generates a Go source file with functions for TensorFlow ops.
internal
Package internal generates Go source code with functions for TensorFlow operations.
Package internal generates Go source code with functions for TensorFlow operations.
Package op defines functions for adding TensorFlow operations to a Graph.
Package op defines functions for adding TensorFlow operations to a Graph.
Package pbs contains generated go code for tensorflow protocol buffer objects.
Package pbs contains generated go code for tensorflow protocol buffer objects.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL