def build_model(tparams, options):
"""
Construct computation graph for the whole model
"""
# inputs (image, sentence, contrast images, constrast sentences)
im = tensor.matrix('im', dtype='float32')
s = tensor.matrix('s', dtype='float32')
cim = tensor.matrix('cim', dtype='float32')
cs = tensor.matrix('cs', dtype='float32')
# image embedding
lim = get_layer('ff')[1](tparams, im, options, prefix='ff_im', activ='linear')
lcim = get_layer('ff')[1](tparams, cim, options, prefix='ff_im', activ='linear')
# sentence embedding
ls = get_layer('ff')[1](tparams, s, options, prefix='ff_s', activ='linear')
lcs = get_layer('ff')[1](tparams, cs, options, prefix='ff_s', activ='linear')
# L2 norm for sentences
ls = l2norm(ls)
lcs = l2norm(lcs)
# Tile by number of contrast terms
lim = tensor.tile(lim, (options['ncon'], 1))
ls = tensor.tile(ls, (options['ncon'], 1))
# pairwise ranking loss
cost_im = options['margin'] - (lim * ls).sum(axis=1) + (lim * lcs).sum(axis=1)
cost_im = cost_im * (cost_im > 0.)
cost_im = cost_im.sum(0)
cost_s = options['margin'] - (ls * lim).sum(axis=1) + (ls * lcim).sum(axis=1)
cost_s = cost_s * (cost_s > 0.)
cost_s = cost_s.sum(0)
cost = cost_im + cost_s
return [im, s, cim, cs], cost
# build an encoder
评论列表
文章目录