TensorFlow中的暹罗神经网络 [英] Siamese Neural Network in TensorFlow

查看:363
本文介绍了TensorFlow中的暹罗神经网络的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在TensorFlow中实现暹罗神经网络,但是我在Internet上找不到任何有效的示例(请参阅

I'm trying to implement a Siamese Neural Network in TensorFlow but I cannot really find any working example on the Internet (see Yann LeCun paper).

我要构建的体系结构将由两个共享权重的LSTM组成,并且仅在网络末端连接.

The architecture I'm trying to build would consist of two LSTMs sharing weights and only connected at the end of the network.

我的问题是:如何在TensorFlow中建立两个不同的神经网络,以共享它们的权重(并列权重),以及如何在最后连接它们?

My question is: how to build two different neural networks sharing their weights (tied weights) in TensorFlow and how to connect them at the end?

谢谢:)

编辑:我实现了一个简单而有效的暹罗网络示例,

Edit: I implemented a simple and working example of a siamese network here on MNIST.

推荐答案

使用tf.layers

更新

如果使用tf.layers模块来构建网络,则只需在连体网络的第二部分中使用参数reuse=True即可:

Update with tf.layers

If you use the tf.layers module to build your network, you can simply use the argument reuse=True for the second part of the Siamese network:

x = tf.ones((1, 3))
y1 = tf.layers.dense(x, 4, name='h1')
y2 = tf.layers.dense(x, 4, name='h1', reuse=True)

# y1 and y2 will evaluate to the same values
sess = tf.Session()
sess.run(tf.global_variables_initializer())
print(sess.run(y1))
print(sess.run(y2))  # both prints will return the same values


tf.get_variable

的旧答案

您可以尝试使用功能tf.get_variable(). (请参见教程)


Old answer with tf.get_variable

You can try using the function tf.get_variable(). (See the tutorial)

使用带有reuse=False的可变范围来实现第一个网络:

Implement the first network using a variable scope with reuse=False:

with tf.variable_scope('Inference', reuse=False):
    weights_1 = tf.get_variable('weights', shape=[1, 1],
                              initializer=...)
    output_1 = weights_1 * input_1

然后使用相同的代码实现第二个,除了使用reuse=True

Then implement the second with the same code except using reuse=True

with tf.variable_scope('Inference', reuse=True):
    weights_2 = tf.get_variable('weights')
    output_2 = weights_2 * input_2

第一个实现将创建并初始化LSTM的每个变量,而第二个实现将使用tf.get_variable()来获取第一个网络中使用的相同变量.这样,变量将被共享.

The first implementation will create and initialize every variable of the LSTM, whereas the second implementation will use tf.get_variable() to get the same variables used in the first network. That way, variables will be shared.

然后,您只需使用所需的任何损失(例如,您可以使用两个暹罗网络之间的L2距离),并且梯度将在两个网络中反向传播,并使用梯度的总和更新共享变量.

Then you just have to use whatever loss you want (e.g. you can use the L2 distance between the two siamese networks), and the gradients will backpropagate through both networks, updating the shared variables with the sum of the gradients.

这篇关于TensorFlow中的暹罗神经网络的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆