This practice is refer to the following resources credited to Morvan.
- Run
main1.py# Make sure your current directory is in this folder $ python3 main1.py - If succeed, you will get the following result
var1:0 [1.] a_name_scope/var2:0 [2.] a_name_scope/var2_1:0 [2.1] a_name_scope/var2_2:0 [2.2]
- The name scope will be no effect if you use
tf.get_variableto define the variable - The name of variable will add an identifier (i.e.,
_1,_2, etc.) to distinguish different variables with same name if you usetf.Variableto define the variable
- The name scope will be no effect if you use
- Run
main2.py# Make sure your current directory is in this folder $ python3 main2.py - If succeed, you will get the following result
a_variable_scope/var3:0 [3.] a_variable_scope/var4:0 [4.] a_variable_scope/var4_1:0 [4.] a_variable_scope/var3:0 [3.]
- The variable scope will be affect if you use
tf.get_variableto define the variable tf.Variablewill define new variable every time- To reuse the variable, you need to add
scope.reuse_variables()before reusing; otherwise, it may have error
- The variable scope will be affect if you use
- Run
main3.py# Make sure your current directory is in this folder $ python3 main3.py - If succeed, you will get the following result
input_layer/Weights:0 Traceback (most recent call last): File "main3.py", line 161, in <module> main() File "main3.py", line 141, in main wrongReuseParam(train_config, test_config) File "main3.py", line 120, in wrongReuseParam test_rnn1 = RNN(test_config) File "main3.py", line 33, in __init__ self.buildRNN() File "main3.py", line 48, in buildRNN Weights_in = self.weightVar([self.input_size, self.cell_size]) File "main3.py", line 103, in weightVar return tf.get_variable(shape=shape, initializer=init, name=name) ... ... ValueError: Variable input_layer/Weights already exists, disallowed. Did you mean to set reuse=True or reuse=tf.AUTO_REUSE in VarScope? Originally defined at: File "main3.py", line 103, in weightVar return tf.get_variable(shape=shape, initializer=init, name=name) File "main3.py", line 48, in buildRNN Weights_in = self.weightVar([self.input_size, self.cell_size]) File "main3.py", line 33, in __init__ self.buildRNN()
- In line 145 (
main3.py), we call the methodwrongReuseParambut it causes an error that the variableinput_layer/Weightsalready exists
- In line 145 (
- Comment out the line 145 and remove the comment symbol
#in line 148 as follow# WRONG to reuse parameters in train RNN #wrongReuseParam(train_config, test_config) # NO reuse parameters in train RNN noReuseParam(train_config, test_config)
- Run
main3.pyagain# Make sure your current directory is in this folder $ python3 main3.py - If succeed, you will get the following result
train_rnn/input_layer/Weights:0 test_rnn/input_layer/Weights:0
- In line 148 (
main3.py), we call the methodnoReuseParamthat avoid the error mentioned above; however, it just create two different variable which doesn't reuse the variables in train RNN
- In line 148 (
- Comment out the line 148 and remove the comment symbol
#in line 151 as follow# NO reuse parameters in train RNN #noReuseParam(train_config, test_config) # CORRECT to reuse parameters in train RNN correctReuseParam(train_config, test_config)
- Run
main3.pyagain# Make sure your current directory is in this folder $ python3 main3.py - If succeed, you will get the following result
rnn/input_layer/Weights:0 rnn/input_layer/Weights:0
- In line 151 (
main3.py), we call the methodcorrectReuseParamthat reuse the variables in train RNN
- In line 151 (