Gary Grey
Gary Grey

Reputation: 145

Tensorflow - What does ops constructors mean?

In this link under the building the graph heading there's a line that says

"The ops constructors in the Python library return objects that stand for the output of the constructed ops. You can pass these to other ops constructors to use as inputs."

What does the word constructor mean? Is it in the context of Object oriented programming or is it in the context of assembling the graph?

Upvotes: 1

Views: 623

Answers (1)

lejlot
lejlot

Reputation: 66815

This is actually something in between. "Ops constructor" refers to functions creating new instances of objects being Ops. For example tf.constant constructs a new op, but actualy returns a reference to Tensor being a result of this operation, namely instance of tensorflow.python.framework.ops.Tensor, but it is not a constructor in the OOP sense.

In particular things that have actual logic, like tf.add will create both tensorflow.python.framework.ops.Operation (to perform addition) and tensorflow.python.framework.ops.Tensor (to store the result of the operation), and only tensor will be returned (this is what cited part of documentation tries to explain).

For example:

import tensorflow as tf

tensor = tf.add(tf.constant(1), tf.constant(2))

for op in tf.get_default_graph().get_operations():
  print op

print tensor

will result in

name: "Const"
op: "Const"
attr {
  key: "dtype"
  value {
    type: DT_INT32
  }
}
attr {
  key: "value"
  value {
    tensor {
      dtype: DT_INT32
      tensor_shape {
      }
      int_val: 1
    }
  }
}

name: "Const_1"
op: "Const"
attr {
  key: "dtype"
  value {
    type: DT_INT32
  }
}
attr {
  key: "value"
  value {
    tensor {
      dtype: DT_INT32
      tensor_shape {
      }
      int_val: 2
    }
  }
}

name: "Add"
op: "Add"
input: "Const"
input: "Const_1"
attr {
  key: "T"
  value {
    type: DT_INT32
  }
}


Tensor("Add:0", shape=TensorShape([]), dtype=int32)

Three ops have been created "in background", while you still operate on Tensor level (which has been returned by tf.add). It's name (created by default) suggests that it is a tensor produced by Add operation.

Update

A simple python-based example might be more usefull, so TF does something like this:

class B:

  def __init__(self):
    print 'Constructor of class B is called'
    pass

class A:

  def __init__(self):
    print 'Constructor of class A is called'
    self.b = B()

def create_something():
  print 'Function is called'
  a = A()
  b = a.b
  print 'Function is ready to return'
  return b

print create_something()

as you can see create_something is not a constructor, it calls a constructors of some classes and returns some instance, but itself is not constructor (nor initializer) in the OOP sense. It is more like a factory design paradim.

Upvotes: 1

Related Questions