我正在尝试使用Tensorflow来实现一个暹罗网络。还是很新的。
我对所显示的Adam优化器变量感到困惑。在暹罗网络中,这些亚当变量的两条腿是否应该相同?为什么变量要创建两次?
我搜索了暹罗网络的几个在线TF示例。找不到与TF.estimator和TF.Keras一起使用的人(尽管有很多纯Keras示例)。因此,开始构建以下代码作为实验。
这是我创建网络的方式。保持网络简单,以便于解释问题。
def create_base_network(image, image_shape):
with tf.variable_scope('Base_Net', reuse=tf.AUTO_REUSE):
x=tf.keras.layers.Conv2D(16(3,3), activation='relu',
input_shape=image_shape, name="Conv_1")(image)
x = tf.keras.layers.Flatten(name="Flatten_1")(x)
embedding = tf.keras.layers.Dense(32,activation="sigmoid",
name="Dense_1")(x)
feature_columns = list(get_feature_columns().values())
input_a = tf.feature_column.input_layer(features=features, feature_columns=feature_columns[0])
input_a = tf.reshape(input_a, shape=feature_shape)
input_b = tf.feature_column.input_layer(features=features, feature_columns=feature_columns[1])
input_b = tf.reshape(input_b, shape=feature_shape)
with tf.variable_scope('Embedding'):
embedding_a = create_base_network(input_a, image_shape)
embedding_b = create_base_network(input_b, image_shape)
with tf.variable_scope('L1_Lambda_Layer'):
#layer to merge two encoded inputs with the l1 distance between them
L1_layer = tf.keras.layers.Lambda(lambda tensors:K.abs(tensors[0] - tensors[1]))
#call this layer on list of two input tensors.
L1_distance = L1_layer([embedding_a, embedding_b])
with tf.variable_scope('Prediction'):
#prediction = Dense(1,activation='sigmoid',bias_initializer=b_init)(L1_distance)
prediction = Dense(1,activation='sigmoid')(L1_distance)
with tf.variable_scope('AdamOpt'):
optimizer = tf.train.AdamOptimizer(learning_rate=0.001,beta1=0.9,beta2=0.999,epsilon=1e-08,use_locking=False,name='Adam')
这是我的e = estimator.get_variable_names()的输出。为什么每个层的内核和偏差都有“ Adam”和“ Adam_1”?我没有明确创建它们。这里有问题吗?
Embedding/Base_Net/Conv_1/bias
Embedding/Base_Net/Conv_1/bias/Adam
Embedding/Base_Net/Conv_1/bias/Adam_1
Embedding/Base_Net/Conv_1/kernel
Embedding/Base_Net/Conv_1/kernel/Adam
Embedding/Base_Net/Conv_1/kernel/Adam_1
Embedding/Base_Net/Dense_1/bias
Embedding/Base_Net/Dense_1/bias/Adam
Embedding/Base_Net/Dense_1/bias/Adam_1
Embedding/Base_Net/Dense_1/kernel
Embedding/Base_Net/Dense_1/kernel/Adam
Embedding/Base_Net/Dense_1/kernel/Adam_1
Prediction/dense/bias
Prediction/dense/bias/Adam
Prediction/dense/bias/Adam_1
Prediction/dense/kernel
Prediction/dense/kernel/Adam
Prediction/dense/kernel/Adam_1
beta1_power
beta2_power