Miscellaneous Utility Functions


Returns the constant value of the given tensor, if efficiently calculable.

This function attempts to partially evaluate the given tensor, and returns its value as a numpy ndarray if this succeeds.

TODO(mrry): Consider whether this function should use a registration mechanism like gradients and ShapeFunctions, so that it is easily extensible.

NOTE: If constant_value(tensor) returns a non-None result, it will no longer be possible to feed a different value for tensor. This allows the result of this function to influence the graph that is constructed, and permits static shape optimizations.

  • tensor: The Tensor to be evaluated.

A numpy ndarray containing the constant value of the given tensor, or None if it cannot be calculated.

  • TypeError: if tensor is not an ops.Tensor.

tf.contrib.util.make_tensor_proto(values, dtype=None, shape=None)

Create a TensorProto.

  • values: Values to put in the TensorProto.
  • dtype: Optional tensor_pb2 DataType value.
  • shape: List of integers representing the dimensions of tensor.

A TensorProto. Depending on the type, it may contain data in the "tensor_content" attribute, which is not directly useful to Python programs. To access the values you should convert the proto back to a numpy ndarray with tensor_util.MakeNdarray(proto).

  • TypeError: if unsupported types are provided.
  • ValueError: if arguments have inappropriate values.

make_tensor_proto accepts "values" of a python scalar, a python list, a numpy ndarray, or a numpy scalar.

If "values" is a python scalar or a python list, make_tensor_proto first convert it to numpy ndarray. If dtype is None, the conversion tries its best to infer the right numpy data type. Otherwise, the resulting numpy array has a compatible data type with the given dtype.

In either case above, the numpy ndarray (either the caller provided or the auto converted) must have the compatible type with dtype.

make_tensor_proto then converts the numpy array to a tensor proto.

If "shape" is None, the resulting tensor proto represents the numpy array precisely.

Otherwise, "shape" specifies the tensor's shape and the numpy array can not have more elements than what "shape" specifies.


Create a numpy ndarray from a tensor.

Create a numpy ndarray with the same shape and data as the tensor.

  • tensor: A TensorProto.

A numpy array with the tensor contents.

  • TypeError: if tensor has unsupported type.


Collect the list of ops used by a graph.

Does not validate that the ops are all registered.

  • graph_def: A GraphDef proto, as from graph.as_graph_def().

A list of strings, each naming an op used by the graph.


Collect the stripped OpDefs for ops used by a graph.

This function computes the stripped_op_list field of MetaGraphDef and similar protos. The result can be communicated from the producer to the consumer, which can then use the C++ function RemoveNewDefaultAttrsFromGraphDef to improve forwards compatibility.

  • graph_def: A GraphDef proto, as from graph.as_graph_def().

An OpList of ops used by the graph.

  • ValueError: If an unregistered op is used.