Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does tensorflow "op" do?

Below I have created a tf placeholder named "op testing":

self.center_words = tf.placeholder(tf.int32, shape=[self.batch_size], name='op testing')
print("Extracting the op", self.center_words.op)

When I print that self.center_words.op it prints out a structure like this:

op: "Placeholder"
attr {
  key: "dtype"
  value {
    type: DT_INT32
  }
}
attr {
  key: "shape"
  value {
    shape {
      dim {
        size: 128
      }
    }
  }
}

This works for any TensorFlow variable, function output, etc. What is this .op?

like image 463
Shamane Siriwardhana Avatar asked Apr 08 '17 05:04

Shamane Siriwardhana


3 Answers

Short answer.

Ops are say the core of tensorflow.

TensorFlow is a programming system in which you represent computations as graphs. Nodes in the graph are called ops (short for operations). An op takes zero or more Tensors, performs some computation, and produces zero or more Tensors.

self.center_words.op in your example prints out the features of self.center_words in that json-like format

like image 166
tagoma Avatar answered Oct 28 '22 03:10

tagoma


TensorFlow Operations, also known as Ops, are nodes that perform computations on or with Tensor objects. After computation, they return zero or more tensors, which can be used by other Ops later in the graph. To create an Operation, you call its constructor in Python, which takes in whatever Tensor parameters needed for its calculation, known as inputs, as well as any additional information needed to properly create the Op, known as attributes. The Python constructor returns a handle to the Operation’s output (zero or more Tensor objects), and it is this output which can be passed on to other Operations or Session.run

like image 16
Chidume Nnamdi Avatar answered Oct 28 '22 02:10

Chidume Nnamdi


Simply put, it prints the property of the particular tensor object. That is, it gives out details about

  • which operation is responsible for the generation of the tensor
  • what is its return type
  • what is its dimension

and all possible information about the tensor object in question.

Minimal Example:

In [74]: with tf.Session() as sess:
    ...:     zer = tf.zeros(shape=(32, 32))
    ...:     print(zer.op)
    ...:     
name: "zeros_11"
op: "Const"
attr {
  key: "dtype"
  value {
    type: DT_FLOAT
  }
}
attr {
  key: "value"
  value {
    tensor {
      dtype: DT_FLOAT
      tensor_shape {
        dim {
          size: 32
        }
        dim {
          size: 32
        }
      }
      float_val: 0.0
    }
  }
}

P.S.: Ignore the number(_11) in (zeros_11) (that is in the value of the key name). It is just a counter to track the runs. It keeps incrementing on every run within a session.


Source Implementation:

Code: tf.Tensor.op
Docs: tf.Tensor.op

like image 7
kmario23 Avatar answered Oct 28 '22 03:10

kmario23