简体   繁体   English

Tensorflow - ops 构造函数是什么意思?

[英]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." “Python 库中的 ops 构造函数返回代表构造 ops 输出的对象。您可以将这些对象传递给其他 ops 构造函数以用作输入。”

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?它是在面向对象编程的上下文中还是在组装图的上下文中?

This is actually something in between.这实际上介于两者之间。 "Ops constructor" refers to functions creating new instances of objects being Ops. “Ops 构造函数”是指创建 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.例如tf.constant构造了一个新的 op,但实际上返回了一个对 Tensor 的引用作为这个操作的结果,即tensorflow.python.framework.ops.Tensor实例,但它不是 OOP 意义上的构造函数。

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).特别是具有实际逻辑的事物,例如tf.add将同时创建tensorflow.python.framework.ops.Operation (执行加法)和tensorflow.python.framework.ops.Tensor (存储操作结果),并且只会返回张量(这是文档中引用的部分试图解释的内容)。

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 ).已经在“后台”创建了三个操作,而您仍然在张量级别(已由tf.add返回)进行操作。 It's name (created by default) suggests that it is a tensor produced by Add operation.它的名称(默认创建)表明它是由Add操作生成的张量。

Update更新

A simple python-based example might be more usefull, so TF does something like this:一个简单的基于 python 的例子可能更有用,所以 TF 会做这样的事情:

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.如您所见, create_something不是构造函数,它调用某些类的构造函数并返回某些实例,但它本身不是 OOP 意义上的构造函数(也不是初始化程序)。 It is more like a factory design paradim.它更像是一个工厂设计范式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM