I referred to this implementation in tensorflow. Itrequires the shape of the output batch embeddings but I can't get the actual shape of an mxnet symbol. Any ideas how?
Asked
Active
Viewed 28 times
1 Answers
0
You can use infer_shape() to get the shape of a symbol. For implementing triplet loss in MXNet you may want to check out this thread: https://github.com/apache/incubator-mxnet/issues/6909 Accordingly you can implement it the following way:
kernels = [(1, feature_size), (2, feature_size), (3, feature_size)]
for i in range(len(kernels)):
conv_weight.append(mx.sym.Variable('conv' + str(i) + '_weight'))
conv_bias.append(mx.sym.Variable('conv' + str(i) + '_bias'))
fa = get_conv(data=anchor,
kernels=kernels, conv_weight=conv_weight, conv_bias=conv_bias,
entity_weight=entity_weight, entity_bias=entity_bias,
feature_name='fa') # share weight.
fs = get_conv(data=same,
kernels=kernels, conv_weight=conv_weight, conv_bias=conv_bias,
entity_weight=entity_weight, entity_bias=entity_bias,
feature_name='fs')
fd = get_conv(data=diff,
kernels=kernels, conv_weight=conv_weight, conv_bias=conv_bias,
entity_weight=entity_weight, entity_bias=entity_bias,
feature_name='fd')
"""
triple-loss
"""
fs = fa - fs
fd = fa - fd
fs = fs * fs
fd = fd * fd
fs = mx.sym.sum(fs, axis=1, keepdims=1)
fd = mx.sym.sum(fd, axis=1, keepdims=1)
loss = fd - fs
loss = one - loss # a scalar
loss = mx.sym.Activation(data=loss, act_type='relu') # acts like a norm.
triple_loss = mx.sym.MakeLoss(loss)

NRauschmayr
- 131
- 1