对比损失函数适用于暹罗网络,优化器存在问题

时间:2019-01-08 12:11:46

标签: python tensorflow

我已经建立了一个练习用的暹罗网络,已经在站点上检查了一些示例,但是当我尝试运行代码时却显示了一些错误,我不知道尺寸是否存在问题。希望有人可以帮助教我。

batch_size = 32
epoch = 50
kernel_size = (3,3)
pooling_size = (2,2)
learning_rate = 0.001
margin_siamese = 1


siamese_nn = tf.Graph()
with siamese_nn.as_default():

with tf.name_scope('input'):
    anchor_img = tf.placeholder(dtype = tf.float32, shape = [None, 28, 28, 1], name = 'anchor_image')
    compare_img = tf.placeholder(dtype = tf.float32, shape = [None, 28, 28, 1], name = 'compare_image')
    Y_label = tf.placeholder(dtype = tf.int32, shape = [None, 1], name = 'Y_label')
    learning_rate = tf.placeholder(dtype = tf.float32, shape = [1])

with tf.variable_scope('siamese_net'):
    conv_s1_1 = tf.layers.conv2d(anchor_img, filters= 64, kernel_size= kernel_size, activation= tf.nn.relu, name= 'siamese_1')
    conv_s1_1 = tf.layers.max_pooling2d(conv_s1_1, pool_size= pooling_size, strides = (1,1), name= 'siamese_1')

    conv_s1_2 = tf.layers.conv2d(conv_s1_1, filters = 32, kernel_size = kernel_size, activation= tf.nn.relu, name= 'siamese_2')
    conv_s1_2 = tf.layers.max_pooling2d(conv_s1_2, pool_size = pooling_size, strides = (1,1), name= 'siamese_2')

    conv_s1_3 = tf.layers.conv2d(conv_s1_2, filters = 16, kernel_size = kernel_size, activation= tf.nn.relu, name= 'siamese_3')
    conv_s1_3 = tf.layers.max_pooling2d(conv_s1_3, pool_size = pooling_size, strides = (1,1), name= 'siamese_3')

    anchor_vector = tf.layers.flatten(conv_s1_3, name = 'flatten')

with tf.variable_scope('siamese_net', reuse= True):

    conv_s2_1 = tf.layers.conv2d(compare_img, filters= 64, kernel_size= kernel_size, activation= tf.nn.relu, name= 'siamese_1')
    conv_s2_1 = tf.layers.max_pooling2d(conv_s2_1, pool_size= pooling_size, strides = (1,1), name= 'siamese_1')

    conv_s2_2 = tf.layers.conv2d(conv_s2_1, filters = 32, kernel_size = kernel_size, activation= tf.nn.relu, name= 'siamese_2')
    conv_s2_2 = tf.layers.max_pooling2d(conv_s2_2, pool_size = pooling_size, strides = (1,1), name= 'siamese_2')

    conv_s2_3 = tf.layers.conv2d(conv_s2_2, filters = 16, kernel_size = kernel_size, activation= tf.nn.relu, name= 'siamese_3')
    conv_s2_3 = tf.layers.max_pooling2d(conv_s2_3, pool_size = pooling_size, strides = (1,1), name= 'siamese_3')

    compare_vector = tf.layers.flatten(conv_s2_3, name = 'flatten')


with tf.name_scope('loss_function'):
    loss = tf.contrib.losses.metric_learning.contrastive_loss(Y_label, anchor_vector, compare_vector)

with tf.name_scope('training'):
    optimizer = tf.train.AdamOptimizer(learning_rate = learning_rate)
    optimizer.minimize(loss)

错误消息

Shape must be rank 0 but is rank 1 for 'training/Adam/update_siamese_net/siamese_1/kernel/ApplyAdam' (op: 'ApplyAdam') with input shapes: [3,3,1,64], [3,3,1,64], [3,3,1,64], [], [], [1], [], [], [], [3,3,1,64].

1 个答案:

答案 0 :(得分:1)

下面提到giser_yugang的答案是为了社区的利益。

学习速率被声明为一维张量,而应为标量。

因此,更改代码行,

learning_rate = tf.placeholder(dtype = tf.float32, shape = [1])

learning_rate = tf.placeholder(dtype = tf.float32, shape = [])将解决该错误。

如果对您有用,请接受和/或投票赞成答案。谢谢!