Skip to content
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

Add global_counter to Trainer as optional argument #271

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 7 additions & 4 deletions tflearn/helpers/trainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,15 @@ class Trainer(object):
achieved before a model weight's are saved to the best_checkpoint_path. This
allows the user to skip early saves and also set a minimum save point when continuing
to train a reloaded model. Default: 0.0.

global_counter: `int`. A Tensorflow variable to hold a global counter of the update steps.
"""

def __init__(self, train_ops, graph=None, clip_gradients=5.0,
tensorboard_dir="/tmp/tflearn_logs/",
tensorboard_verbose=0, checkpoint_path=None, best_checkpoint_path=None,
max_checkpoints=None,
keep_checkpoint_every_n_hours=10000.0, random_seed=None,
session=None, best_val_accuracy=0.0):
session=None, best_val_accuracy=0.0, global_step=None):

self.graph = tf.get_default_graph()
if graph:
Expand All @@ -87,8 +87,11 @@ def __init__(self, train_ops, graph=None, clip_gradients=5.0,
self.validate_trainop_names()

self.global_loss = None
self.global_step = tf.Variable(0., name='Global_Step',
trainable=False)
if global_step:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here you need to set:

if global_step is None:
    global_step = tf.Variable(0., name='Global_Step', trainable=False)
self.global_step = global_step

because 'if Tensor' raises an exception in TensorFlow.

self.global_step = global_step
else:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be simplified into just checking if global_step = none and if so we create the new variable then assign it to self at the end.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something like self.global_step = tf.Var if global_step is None else global_step

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought about that but putting everything in one line might hurt readability: after all, the definition of tf.Variable(...) is already taking two lines.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, that makes sense. I was thinking more like we don't need the entire if else. It can be something like:

if global_step is None:
    global_step = tf.Variable(0., name='Global_Step', trainable=False)
self.global_step = global_step

but your way is more explicit.

self.global_step = tf.Variable(0., name='Global_Step',
trainable=False)
self.incr_global_step = tf.assign(self.global_step,
tf.add(self.global_step, 1))
self.best_val_accuracy = best_val_accuracy
Expand Down