-
Notifications
You must be signed in to change notification settings - Fork 216
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Sampling, batch_all, non-zero, optimizer-flag #33
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -102,9 +102,13 @@ | |
help='Which metric to use for the distance between embeddings.') | ||
|
||
parser.add_argument( | ||
'--loss', default='batch_hard', choices=loss.LOSS_CHOICES.keys(), | ||
'--loss', default='batch_hard', choices=loss.LOSS_CHOICES, | ||
help='Enable the super-mega-advanced top-secret sampling stabilizer.') | ||
|
||
parser.add_argument( | ||
'--loss_ignore_zero', default=False, const=True, nargs='?', type=common.positive_float, | ||
help='Average only over non-zero loss values, called "=/=0" in the paper.') | ||
|
||
parser.add_argument( | ||
'--learning_rate', default=3e-4, type=common.positive_float, | ||
help='The initial value of the learning-rate, before it kicks in.') | ||
|
@@ -141,6 +145,11 @@ | |
' embeddings, losses and FIDs seen in each batch during training.' | ||
' Everything can be re-constructed and analyzed that way.') | ||
|
||
parser.add_argument( | ||
'--optim', default='AdamOptimizer(learning_rate)', | ||
help='Which optimizer to use. This is actual TensorFlow code that will be' | ||
' eval\'d. Use `learning_rate` for the learning-rate with schedule.') | ||
|
||
|
||
def sample_k_fids_for_pid(pid, all_fids, all_pids, batch_k): | ||
""" Given a PID, select K FIDs of that specific PID. """ | ||
|
@@ -294,16 +303,24 @@ def main(): | |
losses, train_top1, prec_at_k, _, neg_dists, pos_dists = loss.LOSS_CHOICES[args.loss]( | ||
dists, pids, args.margin, batch_precision_at_k=args.batch_k-1) | ||
|
||
# Count the number of active entries, and compute the total batch loss. | ||
num_active = tf.reduce_sum(tf.cast(tf.greater(losses, 1e-5), tf.float32)) | ||
loss_mean = tf.reduce_mean(losses) | ||
# Count how many entries in the batch are (possibly approximately) non-zero. | ||
if args.loss_ignore_zero is True: | ||
nnz = tf.count_nonzero(losses, dtype=tf.float32) | ||
else: | ||
nnz = tf.reduce_sum(tf.to_float(tf.greater(losses, args.loss_ignore_zero or 1e-5))) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is the point of this supposed to be just for logging? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, actually. It's type-magic and can be a little obscure, hence why I still need to write documentation in the README :) The There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Read our paper, we explain them in there :) But really it's not a good time investment to play with that parameter. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am going to delete that comment because it makes no sense sorry. 😆 Currently have it printed and highlighted in front of me trying to get to grips! |
||
|
||
# Compute the total batch-loss by either averaging all, or averaging non-zeros only. | ||
if args.loss_ignore_zero is False: | ||
loss_mean = tf.reduce_mean(losses) | ||
else: | ||
loss_mean = tf.reduce_sum(losses) / (1e-33 + nnz) | ||
|
||
# Some logging for tensorboard. | ||
tf.summary.histogram('loss_distribution', losses) | ||
tf.summary.scalar('loss', loss_mean) | ||
tf.summary.scalar('batch_top1', train_top1) | ||
tf.summary.scalar('batch_prec_at_{}'.format(args.batch_k-1), prec_at_k) | ||
tf.summary.scalar('active_count', num_active) | ||
tf.summary.scalar('active_count', nnz) | ||
tf.summary.histogram('embedding_dists', dists) | ||
tf.summary.histogram('embedding_pos_dists', pos_dists) | ||
tf.summary.histogram('embedding_neg_dists', neg_dists) | ||
|
@@ -341,9 +358,7 @@ def main(): | |
else: | ||
learning_rate = args.learning_rate | ||
tf.summary.scalar('learning_rate', learning_rate) | ||
optimizer = tf.train.AdamOptimizer(learning_rate) | ||
# Feel free to try others! | ||
# optimizer = tf.train.AdadeltaOptimizer(learning_rate) | ||
optimizer = eval("tf.train." + args.optim) | ||
|
||
# Update_ops are used to update batchnorm stats. | ||
with tf.control_dependencies(tf.get_collection(tf.GraphKeys.UPDATE_OPS)): | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ohh misread this to mean it can only be boolean. I am going to start playing with this then. 🍾