If I run the following code (tensorflow 1.15) I can get a list of the trainable variables in two different ways.
from tensorflow.keras.layers import AveragePooling2D, Conv2D, Dense, Flatten, Input
from tensorflow.keras.models import Model
import tensorflow as tf
x_in = Input((32, 32, 1))
x = Conv2D(filters=6, kernel_size=(5, 5), activation='relu', input_shape=(32,32,1))(x_in)
x = AveragePooling2D(pool_size=(2, 2))(x)
x = Flatten()(x)
x = Dense(units=120, activation='relu')(x)
x = Dense(units=10, activation='softmax')(x)
m = Model(inputs=x_in, outputs=x)
v1 = m.trainable_variables
v2 = tf.compat.v1.trainable_variables()
Both v1
and v2
have the same value.
If I add in a call to tf.compat.v1.enable_eager_execution()
before creating my model, v2
becomes empty; tf.compat.v1.trainable_variables()
returns an empty list.
Why is this?