Tensorflow中constant、Varibles、placeholder在graph中的结点表示

函数定义

tf.constant(value, dtype=None, shape=None, name='Const', verify_shape=False)
tf.Variable(initializer, name)
tf.placeholder(dtype, shape=None, name=None)

constant

import tensorflow as tf

def dump_graph(g, filename):
    print(filename)
    print(g.as_graph_def())

g = tf.get_default_graph()
var = tf.constant([1, 2, 3, 4, 5, 6, 7])
dump_graph(g, 'after_var_creation.graph')

init = tf.global_variables_initializer()
dump_graph(g, 'after_initializer_creation.graph')

with tf.Session() as sess:
    sess.run(init)
    dump_graph(g, 'after_initializer_run.graph')

在执行完var = tf.constant([1, 2, 3, 4, 5, 6, 7])后,图中生成了以下结点:

  • Const:用来保存var常量;

可见,常量在会话中是不需要进行所谓初始化的。

在执行完tf.global_variables_initializer()后,图中结点为:

  • Const
  • init

下面我只展示,dump_graph(g, 'after_initializer_run.graph')的输出。
由输出可以看到,虽然函数global_variables_initializer()的执行在图中添加了一个init的结点,但是没有任何操作。
同时,我们可以看到关于常量的类型,形状、具体的值都已经在一个node中包含了。

node {
  name: "Const"
  op: "Const"
  attr {
    key: "dtype"
    value {
      type: DT_INT32
    }
  }
  attr {
    key: "value"
    value {
      tensor {
        dtype: DT_INT32
        tensor_shape {
          dim {
            size: 7
          }
        }
        tensor_content: "\001\000\000\000\002\000\000\000\003\000\000\000\004\000\000\000\005\000\000\000\006\000\000\000\007\000\000\000"
      }
    }
  }
}
node {
  name: "init"
  op: "NoOp"
}
versions {
  producer: 22
}

Variables

import tensorflow as tf

def dump_graph(g, filename):
    print(filename)
    print(g.as_graph_def())

g = tf.get_default_graph()
var = tf.Variable(2)
dump_graph(g, 'after_var_creation.graph')

init = tf.global_variables_initializer()
dump_graph(g, 'after_initializer_creation.graph')

with tf.Session() as sess:
    sess.run(init)
    dump_graph(g, 'after_initializer_run.graph')

variables.py

在执行完tf.Variable(2)以后,图中生成了以下几个结点:

  • Variable/initial_value:the stateful TensorFlow op that owns the memory for the variable. Every time you run that op, it emits the buffer(as a "ref tensor") so that other ops can read or write it.
  • Variable:the tensor that you provided as the initial_value argument of the tf.Variable constructor.
  • Variable/Assign: the initializer operation that writes the initial value into the variable's memory. It is typically run once, when you do sess.run(tf.global_variables_initializer()) in your program.
  • Variable/read:an operation that "dereferences" the Variable op's "ref tensor" output.

The "loc:@Variable" syntax is used to colocate nodes on the same device.
In particular, any op that has this value for its _class attr will be placed on the same device as the Variable operation.

接着执行完tf.global_variables_initializer()后,图中结点变为:

  • Variable/initial_value
  • Variable
  • Variable/Assign
  • Variable/read
  • init : 图中变量初始化的作用

输出为:

  • after_var_creation.graph
after_var_creation.graph
node {
  name: "Variable/initial_value"
  op: "Const"
  attr {
    key: "dtype"
    value {
      type: DT_INT32
    }
  }
  attr {
    key: "value"
    value {
      tensor {
        dtype: DT_INT32
        tensor_shape {
        }
        int_val: 2
      }
    }
  }
}
node {
  name: "Variable"
  op: "VariableV2"
  attr {
    key: "container"
    value {
      s: ""
    }
  }
  attr {
    key: "dtype"
    value {
      type: DT_INT32
    }
  }
  attr {
    key: "shape"
    value {
      shape {
      }
    }
  }
  attr {
    key: "shared_name"
    value {
      s: ""
    }
  }
}
node {
  name: "Variable/Assign"
  op: "Assign"
  input: "Variable"
  input: "Variable/initial_value"
  attr {
    key: "T"
    value {
      type: DT_INT32
    }
  }
  attr {
    key: "_class"
    value {
      list {
        s: "loc:@Variable"
      }
    }
  }
  attr {
    key: "use_locking"
    value {
      b: true
    }
  }
  attr {
    key: "validate_shape"
    value {
      b: true
    }
  }
}
node {
  name: "Variable/read"
  op: "Identity"
  input: "Variable"
  attr {
    key: "T"
    value {
      type: DT_INT32
    }
  }
  attr {
    key: "_class"
    value {
      list {
        s: "loc:@Variable"
      }
    }
  }
}
  • after_initializer_creation.graph
node {
  name: "Variable/initial_value"
  op: "Const"
  attr {
    key: "dtype"
    value {
      type: DT_INT32
    }
  }
  attr {
    key: "value"
    value {
      tensor {
        dtype: DT_INT32
        tensor_shape {
        }
        int_val: 2
      }
    }
  }
}
node {
  name: "Variable"
  op: "VariableV2"
  attr {
    key: "container"
    value {
      s: ""
    }
  }
  attr {
    key: "dtype"
    value {
      type: DT_INT32
    }
  }
  attr {
    key: "shape"
    value {
      shape {
      }
    }
  }
  attr {
    key: "shared_name"
    value {
      s: ""
    }
  }
}
node {
  name: "Variable/Assign"
  op: "Assign"
  input: "Variable"
  input: "Variable/initial_value"
  attr {
    key: "T"
    value {
      type: DT_INT32
    }
  }
  attr {
    key: "_class"
    value {
      list {
        s: "loc:@Variable"
      }
    }
  }
  attr {
    key: "use_locking"
    value {
      b: true
    }
  }
  attr {
    key: "validate_shape"
    value {
      b: true
    }
  }
}
node {
  name: "Variable/read"
  op: "Identity"
  input: "Variable"
  attr {
    key: "T"
    value {
      type: DT_INT32
    }
  }
  attr {
    key: "_class"
    value {
      list {
        s: "loc:@Variable"
      }
    }
  }
}
node {
  name: "init"
  op: "NoOp"
  input: "^Variable/Assign"
}
versions {
  producer: 22
}
  • after_initializer_run.graph
node {
  name: "Variable/initial_value"
  op: "Const"
  attr {
    key: "dtype"
    value {
      type: DT_INT32
    }
  }
  attr {
    key: "value"
    value {
      tensor {
        dtype: DT_INT32
        tensor_shape {
        }
        int_val: 2
      }
    }
  }
}
node {
  name: "Variable"
  op: "VariableV2"
  attr {
    key: "container"
    value {
      s: ""
    }
  }
  attr {
    key: "dtype"
    value {
      type: DT_INT32
    }
  }
  attr {
    key: "shape"
    value {
      shape {
      }
    }
  }
  attr {
    key: "shared_name"
    value {
      s: ""
    }
  }
}
node {
  name: "Variable/Assign"
  op: "Assign"
  input: "Variable"
  input: "Variable/initial_value"
  attr {
    key: "T"
    value {
      type: DT_INT32
    }
  }
  attr {
    key: "_class"
    value {
      list {
        s: "loc:@Variable"
      }
    }
  }
  attr {
    key: "use_locking"
    value {
      b: true
    }
  }
  attr {
    key: "validate_shape"
    value {
      b: true
    }
  }
}
node {
  name: "Variable/read"
  op: "Identity"
  input: "Variable"
  attr {
    key: "T"
    value {
      type: DT_INT32
    }
  }
  attr {
    key: "_class"
    value {
      list {
        s: "loc:@Variable"
      }
    }
  }
}
node {
  name: "init"
  op: "NoOp"
  input: "^Variable/Assign"
}
versions {
  producer: 22
}

placeholder

import tensorflow as tf

def dump_graph(g, filename):
    print(filename)
    print(g.as_graph_def())

g = tf.get_default_graph()
var = tf.placeholder(tf.float32, None)
dump_graph(g, 'after_var_creation.graph')

init = tf.global_variables_initializer()
dump_graph(g, 'after_initializer_creation.graph')

with tf.Session() as sess:
    sess.run(init)
    dump_graph(g, 'after_initializer_run.graph')

在执行完placeholder(tf.float32, None)后,图中生成了一个结点:

  • Placeholder

输出如下:
可见关于Placeholder的所有信息都在其结点中表示出来了。

after_initializer_run.graph
node {
  name: "Placeholder"
  op: "Placeholder"
  attr {
    key: "dtype"
    value {
      type: DT_FLOAT
    }
  }
  attr {
    key: "shape"
    value {
      shape {
        unknown_rank: true
      }
    }
  }
}
node {
  name: "init"
  op: "NoOp"
}
versions {
  producer: 22
}