- Mastering TensorFlow 1.x
- Armando Fandango
- 202字
- 2021-06-25 22:50:56
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.
- 用“芯”探核:龍芯派開發(fā)實戰(zhàn)
- 龍芯應用開發(fā)標準教程
- 電腦組裝、維護、維修全能一本通(全彩版)
- 深入淺出SSD:固態(tài)存儲核心技術(shù)、原理與實戰(zhàn)(第2版)
- 計算機維修與維護技術(shù)速成
- Hands-On Machine Learning with C#
- CC2530單片機技術(shù)與應用
- 基于Apache Kylin構(gòu)建大數(shù)據(jù)分析平臺
- STM32嵌入式技術(shù)應用開發(fā)全案例實踐
- 計算機組裝維修與外設配置(高等職業(yè)院校教改示范教材·計算機系列)
- Intel Edison智能硬件開發(fā)指南:基于Yocto Project
- Hands-On Artificial Intelligence for Banking
- 嵌入式系統(tǒng)原理及應用:基于ARM Cortex-M4體系結(jié)構(gòu)
- 微服務實戰(zhàn)
- Learning Less.js