Tensorflow - What does ops constructors mean?
在这个链接的建筑图标题下面有一条线
"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."
号
constructor这个词是什么意思?它是在面向对象编程的上下文中,还是在组装图形的上下文中?
这实际上介于两者之间。"ops constructor"是指创建对象的新实例的函数。例如,
尤其是具有实际逻辑的事物,如
例如:
1 2 3 4 5 6 7 8 | 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 |
会导致
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | 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) |
号
"在后台"创建了三个操作,而您仍然在张量级别上操作(由
一个简单的基于Python的示例可能更有用,因此tf会这样做:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | 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() |
如您所见,