Tensorflow--tf.Variable与tf.get_variable()

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/newchenxf/article/details/79862481

两个函数,都是得到变量。

区别:

tf.Variable(),每次都在创建新对象。
get_variable(),如果已经创建的变量对象,就把那个对象返回,如果没有创建变量对象的话,就创建一个新的。

从代码可见区别:

import tensorflow as tf

with tf.variable_scope("scope1"):
    w1 = tf.get_variable("w1", shape=[])
    w2 = tf.Variable(0.0, name="w2")
with tf.variable_scope("scope1", reuse=True):
    w1_p = tf.get_variable("w1", shape=[])
    w2_p = tf.Variable(1.0, name="w2")

print(w1 is w1_p, w2 is w2_p)
#输出
#True  False

参考:

https://blog.csdn.net/u012436149/article/details/53696970

猜你喜欢

转载自blog.csdn.net/newchenxf/article/details/79862481
今日推荐