/    /  TensorFlow Operators

TensorFlow Operators

TensorFlow Graph that takes zero or more Tensor objects as input, and produces zero or more as output(Tensor objects). Objects of type Operation are created by calling a Python op constructor (such as tf.matmul) or tf.Graph.create_op.

 

For example:

c = tf.matmul(A1,A2) creates an Operation of type “MatMul” that takes tensors A1 and A2 as input.

After the graph has been launched in a session, an Operation can be executed by  tf.get_default_session().run(op).

 

1. Creates an Operation.(_ _init_ _):

__init__( node_def,g,inputs=None,output_types=None,control_inputs=None, input_types=None,original_op=None,op_def=None)

 

2. Args:

– node_def: node_def_pb2.NodeDef. NodeDef for the Operation. Used for attributes of node_def_pb2.NodeDef, typically name, op, and device. The input attribute is irrelevant here as it will be computed when generating the model.

– g: Graph. The parent graph.

– Inputs: list of Tensor objects. The inputs to this Operation.

– output_types: list of DType objects. List of the types of the Tensors computed by this operation. The length of this list indicates the number of output endpoints of the Operation.

– control_inputs: list of operations or tensors from which to have a control dependency.

– input_types: List of DType objects representing the types of the tensors accepted by the Operation. By default uses [x.dtype.base_dtype for x in inputs]. Operations that expect reference-typed inputs must specify these explicitly.

– original_op: Used to associate the new Operation with an existing Operation (for example, a replica with the op that was replicated).

– op_def: The op_def_pb2.OpDef proto that describes the op type that this Operation represents.

 

Raises:

–  TypeError: if control inputs are not Operations or Tensors, or if node_def is not a NodeDef, or if g is not a Graph, or if inputs are not tensors, or if inputs and input_types are incompatible.

–  ValueError: if the node_def name is not valid.

 

Some Useful TensorFlow operators:

a = tf.constant([3, 6])

 b = tf.constant([2, 2])

tf.add(a, b)# >> [5 8]

tf.add_n([a, b, b]) # >> [7 10]. Equivalent to a + b + b

tf.mul(a, b) # >> [6 12] because mul is element wise

tf.matmul(a, b) # >>ValueError

tf.matmul(tf.reshape(a, [1, 2]), tf.reshape(b, [2, 1])) # >> [[18]]

tf.div(a, b) # >> [1 3]

tf.mod(a, b) # >> [1 0]

 

Example:

# Add

tensor_A1 = tf.constant([[10,12]], dtype = tf.int32)

tensor_A2 = tf.constant([[13, 14]], dtype = tf.int32)



tensor_add = tf.add(tensor_A1, tensor_A2)print(tensor_add)  

Output:-

                        Tensor("Add:0", shape=(1, 2), dtype=int32)

 

 

Code Explanation

Create two tensors:

one tensor with 10 and 12

one tensor with 13 and 1 4

You add up both tensors.

 

Notice: that both tensors must and should have the same shape.

 

# Multiply

tensor_multiply = tf.multiply(tensor_A1, tensor_A2)

print(tensor_multiply)                             

Output: Tensor("Mul:0", shape=(1, 2), dtype=int32)

 

 

Properties:-

control_inputs:The Operation objects on which this op has a control dependency.Before the operation is executed, TensorFlow will ensures that the operations belongs to  self.control_inputs  has been finished. This mechanism can be used to run operations sequentially for performance reasons.

1. Returns:A list of Operation objects.

Device: The name of the device to which this op has been assigned, if any.

2. Returns: The string name of the device to which this op has been assigned, or an empty string if it has not been assigned to a device.

Graph:The Graph that contains this operation.

Inputs:The list of Tensor objects representing the data inputs of this op.

Name:The full name of this operation.

 

Methods:

colocation_groups

colocation_groups():Returns the list of colocation groups of the op.

get_attr

get_attr(name):Returns the value of the attr of this op with the given name.

 

Args:

name: The name of the attr to fetch.

Returns:

The value of the attr, as a Python object.

 

Raises:

ValueError: If this op does not have an attr with the given name.