1. 程式人生 > >Tensorflow--tf.Variable與tf.get_variable()

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

兩個函式,都是得到變數。

區別:

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