官术网_书友最值得收藏!

Getting Variables with tf.get_variable()

If you define a variable with a name that has been defined before, then TensorFlow throws an exception. Hence, it is convenient to use the tf.get_variable() function instead of tf.Variable(). The function tf.get_variable() returns the existing variable with the same name if it exists, and creates the variable with the specified shape and initializer if it does not exist. For example: 

w = tf.get_variable(name='w',shape=[1],dtype=tf.float32,initializer=[.3])
b = tf.get_variable(name='b',shape=[1],dtype=tf.float32,initializer=[-.3])

The initializer can be a tensor or list of values as shown in above examples or one of the inbuilt initializers:

  • tf.constant_initializer
  • tf.random_normal_initializer
  • tf.truncated_normal_initializer
  • tf.random_uniform_initializer
  • tf.uniform_unit_scaling_initializer
  • tf.zeros_initializer
  • tf.ones_initializer
  • tf.orthogonal_initializer

In distributed TensorFlow where we can run the code across machines, the tf.get_variable() gives us global variables. To get the local variables TensorFlow has a function with similar signature: tf.get_local_variable().

Sharing or Reusing Variables: Getting already-defined variables promotes reuse. However, an exception will be thrown if the reuse flags are not set by using tf.variable_scope.reuse_variable() or tf.variable.scope(reuse=True).

Now that you have learned how to define tensors, constants, operations, placeholders, and variables, let's learn about the next level of abstraction in TensorFlow, that combines these basic elements together to form a basic unit of computation, the data flow graph or computational graph.

主站蜘蛛池模板: 汝阳县| 马山县| 林甸县| 攀枝花市| 彰化市| 米林县| 龙州县| 福州市| 鄂伦春自治旗| 三穗县| 广水市| 赤城县| 平南县| 镇江市| 延安市| 玉环县| 阿拉善右旗| 沙田区| 青州市| 富蕴县| 长寿区| 浦江县| 托里县| 华池县| 汽车| 叶城县| 蒙自县| 若尔盖县| 银川市| 托克逊县| 华蓥市| 景宁| 伊金霍洛旗| 诏安县| 嘉义县| 长兴县| 呼玛县| 呼图壁县| 长治县| 兴化市| 黑水县|