Skip to content

Commit

Permalink
Fixes issues with the tf.train.Saver in the 0.7.0 release.
Browse files Browse the repository at this point in the history
1. The cifar10_train.py example model was emitting warnings, because of
   non-Variable objects in the `tf.moving_average_variables()`
   collection. This change fixes that by only adding `Variable`-typed
   objects to that collection in `moving_averages.py` (which better
   agrees with the definition in `tf.GraphKeys.MOVING_AVERAGES_VARIABLES`).

2. Saver.save() now calls `tf.gfile.MakeDirs(os.path.dirname(save_path))`,
   which fails if `save_path` does not contain a directory component.
   This change fixes the implementation of `tf.gfile.MakeDirs('')` to be a
   no-op (which better matches the internal library that it is shadowing).

Fixes #1123. Fixes #1135.
Change: 114895020
  • Loading branch information
mrry authored and tensorflower-gardener committed Feb 17, 2016
1 parent 73f11ec commit 42f06d8
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 7 deletions.
11 changes: 5 additions & 6 deletions tensorflow/python/platform/default/_gfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,17 +282,16 @@ def MakeDirs(path, mode=0o755): # pylint: disable=invalid-name
"""Recursively create the directory "path" with the given mode.
Args:
path: The directory path
path: The directory path.
mode: The file mode for the created directories
Returns:
None
Raises:
OSError: if the path already exists
"""
os.makedirs(path, mode)
# NOTE(mrry): MakeDirs("") should be a no-op to match other
# implementations of tf.gfile.
if path:
os.makedirs(path, mode)


def RmDir(directory): # pylint: disable=invalid-name
Expand Down
17 changes: 17 additions & 0 deletions tensorflow/python/platform/default/gfile_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from __future__ import division
from __future__ import print_function

import contextlib
import os
import shutil
import time
Expand Down Expand Up @@ -148,6 +149,22 @@ def testMkDirsGlobAndRmDirs(self):
gfile.DeleteRecursively(self.tmp + "test_dir")
self.assertFalse(gfile.Exists(self.tmp + "test_dir"))

@contextlib.contextmanager
def _working_directory(self, wd):
original_cwd = os.getcwd()
os.chdir(wd)
try:
yield
finally:
os.chdir(original_cwd)

def testMakeDirsWithEmptyString(self):
gfile.MakeDirs(self.tmp + "test_dir")
with self._working_directory(self.tmp + "test_dir"):
gfile.MakeDirs("")
# Should succeed because MakeDirs("") is a no-op.
gfile.RmDir(self.tmp + "test_dir")

def testErrors(self):
self.assertRaises(
OSError, lambda: gfile.RmDir(self.tmp + "dir_doesnt_exist"))
Expand Down
4 changes: 3 additions & 1 deletion tensorflow/python/training/moving_averages.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,12 +269,14 @@ def apply(self, var_list=None):
avg = slot_creator.create_slot(
var, var.initialized_value(), self._name,
colocate_with_primary=True)
# NOTE(mrry): We only add `tf.Variable` objects to the
# `MOVING_AVERAGE_VARIABLES` collection.
ops.add_to_collection(ops.GraphKeys.MOVING_AVERAGE_VARIABLES, var)
else:
avg = slot_creator.create_zeros_slot(
var, self._name,
colocate_with_primary=(var.op.type == "Variable"))
self._averages[var] = avg
ops.add_to_collection(ops.GraphKeys.MOVING_AVERAGE_VARIABLES, var)

with ops.name_scope(self._name) as scope:
decay = ops.convert_to_tensor(self._decay, name="decay")
Expand Down
2 changes: 2 additions & 0 deletions tensorflow/python/training/moving_averages_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ def _CheckDecay(self, ema, actual_decay, dim):
avg1 = ema.average(var1)
avg2 = ema.average(tensor2)

self.assertItemsEqual([var0, var1], tf.moving_average_variables())

self.assertFalse(avg0 in tf.trainable_variables())
self.assertFalse(avg1 in tf.trainable_variables())
self.assertFalse(avg2 in tf.trainable_variables())
Expand Down

0 comments on commit 42f06d8

Please sign in to comment.