Skip to content

Commit

Permalink
Merge changes from github.
Browse files Browse the repository at this point in the history
Change: 128401884
  • Loading branch information
martinwicke authored and tensorflower-gardener committed Jul 25, 2016
1 parent ed28197 commit 21716d8
Show file tree
Hide file tree
Showing 105 changed files with 2,585 additions and 1,153 deletions.
5 changes: 4 additions & 1 deletion ISSUE_TEMPLATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ If installed from binary pip package, provide:
1. Which pip package you installed.
2. The output from `python -c "import tensorflow; print(tensorflow.__version__)"`.

If installed from sources, provide the commit hash:
If installed from source, provide

1. The commit hash (`git rev-parse HEAD`)
2. The output of `bazel version`

### Steps to reproduce
1.
Expand Down
5 changes: 2 additions & 3 deletions eigen.BUILD
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package(default_visibility = ["//visibility:public"])

archive_dir = "eigen-eigen-b4fa9622b809"
cc_library(
name = "eigen",
hdrs = glob([archive_dir+"/**/*.h", archive_dir+"/unsupported/Eigen/*", archive_dir+"/unsupported/Eigen/CXX11/*", archive_dir+"/Eigen/*"]),
includes = [ archive_dir ],
hdrs = glob(["**/*.h", "unsupported/Eigen/*", "unsupported/Eigen/CXX11/*", "Eigen/*"]),
includes = [ '.' ],
visibility = ["//visibility:public"],
)
23 changes: 23 additions & 0 deletions gif.BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
SOURCES = [
"dgif_lib.c",
"egif_lib.c",
"gif_font.c",
"gif_hash.c",
"gifalloc.c",
"openbsd-reallocarray.c",
"gif_err.c",
"quantize.c",
]

prefix_dir = "giflib-5.1.4/lib"

cc_library(
name = "gif",
srcs = [prefix_dir + "/" + source for source in SOURCES],
hdrs = [prefix_dir + "/gif_lib.h"],
includes = [prefix_dir],
defines = [
"HAVE_CONFIG_H",
],
visibility = ["//visibility:public"],
)
24 changes: 19 additions & 5 deletions tensorflow/contrib/cmake/external/eigen.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,30 @@

include (ExternalProject)

set(eigen_archive_hash "b4fa9622b809")
# We parse the current Eigen version and archive hash from the bazel configuration
file(STRINGS ${PROJECT_SOURCE_DIR}/../../workspace.bzl workspace_contents)
foreach(line ${workspace_contents})
string(REGEX MATCH ".*eigen_version.*=.*\"(.*)\"" has_version ${line})
if(has_version)
set(eigen_version ${CMAKE_MATCH_1})
break()
endif()
endforeach()
foreach(line ${workspace_contents})
string(REGEX MATCH ".*eigen_sha256.*=.*\"(.*)\"" has_hash ${line})
if(has_hash)
set(eigen_hash ${CMAKE_MATCH_1})
break()
endif()
endforeach()

set(eigen_INCLUDE_DIRS
${CMAKE_CURRENT_BINARY_DIR}
${CMAKE_CURRENT_BINARY_DIR}/external/eigen_archive
${CMAKE_CURRENT_BINARY_DIR}/external/eigen_archive/eigen-eigen-${eigen_archive_hash}
${tensorflow_source_dir}/third_party/eigen3
)
set(eigen_URL https://bitbucket.org/eigen/eigen/get/${eigen_archive_hash}.tar.gz)
set(eigen_HASH SHA256=2862840c2de9c0473a4ef20f8678949ae89ab25965352ee53329e63ba46cec62)
set(eigen_URL https://bitbucket.org/eigen/eigen/get/${eigen_version}.tar.gz)
set(eigen_HASH SHA256=${eigen_hash})
set(eigen_BUILD ${CMAKE_CURRENT_BINARY_DIR}/eigen/src/eigen)
set(eigen_INSTALL ${CMAKE_CURRENT_BINARY_DIR}/eigen/install)

Expand All @@ -30,5 +44,5 @@ ExternalProject_Add(eigen
-DCMAKE_BUILD_TYPE:STRING=Release
-DCMAKE_VERBOSE_MAKEFILE:BOOL=OFF
-DCMAKE_INSTALL_PREFIX:STRING=${eigen_INSTALL}
-DINCLUDE_INSTALL_DIR:STRING=${CMAKE_CURRENT_BINARY_DIR}/external/eigen_archive/eigen-eigen-${eigen_archive_hash}
-DINCLUDE_INSTALL_DIR:STRING=${CMAKE_CURRENT_BINARY_DIR}/external/eigen_archive
)
25 changes: 7 additions & 18 deletions tensorflow/contrib/factorization/python/ops/kmeans.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,8 @@ def __init__(self,
distance_metric=clustering_ops.SQUARED_EUCLIDEAN_DISTANCE,
random_seed=0,
use_mini_batch=True,
batch_size=128,
steps=10,
kmeans_plus_plus_num_retries=2,
continue_training=False,
config=None,
verbose=1):
config=None):
"""Creates a model for running KMeans training and inference.
Args:
Expand All @@ -73,25 +69,17 @@ def __init__(self,
random_seed: Python integer. Seed for PRNG used to initialize centers.
use_mini_batch: If true, use the mini-batch k-means algorithm. Else assume
full batch.
batch_size: See TensorFlowEstimator
steps: See TensorFlowEstimator
kmeans_plus_plus_num_retries: For each point that is sampled during
kmeans++ initialization, this parameter specifies the number of
additional points to draw from the current distribution before selecting
the best. If a negative value is specified, a heuristic is used to
sample O(log(num_to_sample)) additional points.
continue_training: See TensorFlowEstimator
config: See TensorFlowEstimator
verbose: See TensorFlowEstimator
config: See Estimator
"""
super(KMeansClustering, self).__init__(
model_dir=model_dir,
config=config)
self.batch_size = batch_size
self.steps = steps
self.kmeans_plus_plus_num_retries = kmeans_plus_plus_num_retries
self.continue_training = continue_training
self.verbose = verbose
self._num_clusters = num_clusters
self._training_initial_clusters = initial_clusters
self._training_graph = None
Expand Down Expand Up @@ -135,11 +123,11 @@ def step_end(self, step, output):
return relative_change < self._tolerance
# pylint: enable=protected-access

def fit(self, x, y=None, monitors=None, logdir=None, steps=None,
def fit(self, x, y=None, monitors=None, logdir=None, steps=None, batch_size=128,
relative_tolerance=None):
"""Trains a k-means clustering on x.
Note: See TensorFlowEstimator for logic for continuous training and graph
Note: See Estimator for logic for continuous training and graph
construction across multiple calls to fit.
Args:
Expand All @@ -151,6 +139,7 @@ def fit(self, x, y=None, monitors=None, logdir=None, steps=None,
visualization.
steps: number of training steps. If not None, overrides the value passed
in constructor.
batch_size: mini-batch size to use. Requires `use_mini_batch=True`.
relative_tolerance: A relative tolerance of change in the loss between
iterations. Stops learning if the loss changes less than this amount.
Note that this may not work correctly if use_mini_batch=True.
Expand All @@ -162,7 +151,7 @@ def fit(self, x, y=None, monitors=None, logdir=None, steps=None,
if logdir is not None:
self._model_dir = logdir
self._data_feeder = data_feeder.setup_train_data_feeder(
x, None, self._num_clusters, self.batch_size)
x, None, self._num_clusters, batch_size if self._use_mini_batch else None)
if relative_tolerance is not None:
if monitors is not None:
monitors += [self._StopWhenConverged(relative_tolerance)]
Expand All @@ -173,7 +162,7 @@ def fit(self, x, y=None, monitors=None, logdir=None, steps=None,
or (self.steps is not None))
self._train_model(input_fn=self._data_feeder.input_builder,
feed_fn=self._data_feeder.get_feed_dict_fn(),
steps=steps or self.steps,
steps=steps,
monitors=monitors,
init_feed_fn=self._data_feeder.get_feed_dict_fn())
return self
Expand Down
68 changes: 29 additions & 39 deletions tensorflow/contrib/factorization/python/ops/kmeans_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,14 @@ def setUp(self):

self.kmeans = KMeans(self.num_centers,
initial_clusters=kmeans_ops.RANDOM_INIT,
batch_size=self.batch_size,
use_mini_batch=self.use_mini_batch,
steps=30,
continue_training=True,
config=run_config.RunConfig(tf_random_seed=14),
config=self.config(14),
random_seed=12)

@staticmethod
def config(tf_random_seed):
return run_config.RunConfig(tf_random_seed=tf_random_seed)

@property
def batch_size(self):
return self.num_points
Expand All @@ -86,7 +87,7 @@ def make_random_points(centers, num_points, max_offset=20):

def test_clusters(self):
kmeans = self.kmeans
kmeans.fit(x=self.points, steps=0)
kmeans.fit(x=self.points, steps=1, batch_size=8)
clusters = kmeans.clusters()
self.assertAllEqual(list(clusters.shape),
[self.num_centers, self.num_dims])
Expand All @@ -97,10 +98,11 @@ def test_fit(self):
return
kmeans = self.kmeans
kmeans.fit(x=self.points,
steps=1)
steps=1, batch_size=self.batch_size)
score1 = kmeans.score(x=self.points)
kmeans.fit(x=self.points,
steps=15 * self.num_points // self.batch_size)
steps=15 * self.num_points // self.batch_size,
batch_size=self.batch_size)
score2 = kmeans.score(x=self.points)
self.assertTrue(score1 > score2)
self.assertNear(self.true_score, score2, self.true_score * 0.05)
Expand All @@ -111,39 +113,36 @@ def test_monitor(self):
return
kmeans = KMeans(self.num_centers,
initial_clusters=kmeans_ops.RANDOM_INIT,
batch_size=self.batch_size,
use_mini_batch=self.use_mini_batch,
# Force it to train forever until the monitor stops it.
steps=None,
continue_training=True,
config=run_config.RunConfig(tf_random_seed=14),
random_seed=12)

kmeans.fit(x=self.points,
# Force it to train forever until the monitor stops it.
steps=None,
batch_size=self.batch_size,
relative_tolerance=1e-4)
score = kmeans.score(x=self.points)
self.assertNear(self.true_score, score, self.true_score * 0.005)

def test_infer(self):
kmeans = self.kmeans
kmeans.fit(x=self.points)
kmeans.fit(x=self.points, steps=10, batch_size=128)
clusters = kmeans.clusters()

# Make a small test set
points, true_assignments, true_offsets = self.make_random_points(clusters,
10)
# Test predict
assignments = kmeans.predict(points)
assignments = kmeans.predict(points, batch_size=self.batch_size)
self.assertAllEqual(assignments, true_assignments)

# Test score
score = kmeans.score(points)
score = kmeans.score(points, batch_size=128)
self.assertNear(score, np.sum(true_offsets), 0.01 * score)

# Test transform
transform = kmeans.transform(points)
transform = kmeans.transform(points, batch_size=128)
true_transform = np.maximum(
0,
np.sum(np.square(points), axis=1, keepdims=True) -
Expand All @@ -161,12 +160,9 @@ def test_fit_with_cosine_distance(self):
initial_clusters=kmeans_ops.RANDOM_INIT,
distance_metric=kmeans_ops.COSINE_DISTANCE,
use_mini_batch=self.use_mini_batch,
batch_size=4,
steps=30,
continue_training=True,
config=run_config.RunConfig(tf_random_seed=2),
config=self.config(2),
random_seed=12)
kmeans.fit(x=points)
kmeans.fit(x=points, steps=10, batch_size=4)
centers = normalize(kmeans.clusters())
self.assertAllClose(np.sort(centers, axis=0),
np.sort(true_centers, axis=0))
Expand All @@ -184,18 +180,16 @@ def test_transform_with_cosine_distance(self):
initial_clusters=kmeans_ops.RANDOM_INIT,
distance_metric=kmeans_ops.COSINE_DISTANCE,
use_mini_batch=self.use_mini_batch,
batch_size=8,
continue_training=True,
config=run_config.RunConfig(tf_random_seed=3))
kmeans.fit(x=points, steps=30)
config=self.config(3))
kmeans.fit(x=points, steps=30, batch_size=8)

centers = normalize(kmeans.clusters())
self.assertAllClose(np.sort(centers, axis=0),
np.sort(true_centers, axis=0),
atol=1e-2)

true_transform = 1 - cosine_similarity(points, centers)
transform = kmeans.transform(points)
transform = kmeans.transform(points, batch_size=8)
self.assertAllClose(transform, true_transform, atol=1e-3)

def test_predict_with_cosine_distance(self):
Expand All @@ -217,20 +211,18 @@ def test_predict_with_cosine_distance(self):
initial_clusters=kmeans_ops.RANDOM_INIT,
distance_metric=kmeans_ops.COSINE_DISTANCE,
use_mini_batch=self.use_mini_batch,
batch_size=8,
continue_training=True,
config=run_config.RunConfig(tf_random_seed=3))
kmeans.fit(x=points, steps=30)
config=self.config(3))
kmeans.fit(x=points, steps=30, batch_size=8)

centers = normalize(kmeans.clusters())
self.assertAllClose(np.sort(centers, axis=0),
np.sort(true_centers, axis=0), atol=1e-2)

assignments = kmeans.predict(points)
assignments = kmeans.predict(points, batch_size=8)
self.assertAllClose(centers[assignments],
true_centers[true_assignments], atol=1e-2)

score = kmeans.score(points)
score = kmeans.score(points, batch_size=8)
self.assertAllClose(score, true_score, atol=1e-2)

def test_predict_with_cosine_distance_and_kmeans_plus_plus(self):
Expand All @@ -254,29 +246,27 @@ def test_predict_with_cosine_distance_and_kmeans_plus_plus(self):
initial_clusters=kmeans_ops.KMEANS_PLUS_PLUS_INIT,
distance_metric=kmeans_ops.COSINE_DISTANCE,
use_mini_batch=self.use_mini_batch,
batch_size=12,
continue_training=True,
config=run_config.RunConfig(tf_random_seed=3))
kmeans.fit(x=points, steps=30)
config=self.config(3))
kmeans.fit(x=points, steps=30, batch_size=12)

centers = normalize(kmeans.clusters())
self.assertAllClose(sorted(centers.tolist()),
sorted(true_centers.tolist()),
atol=1e-2)

assignments = kmeans.predict(points)
assignments = kmeans.predict(points, batch_size=12)
self.assertAllClose(centers[assignments],
true_centers[true_assignments], atol=1e-2)

score = kmeans.score(points)
score = kmeans.score(points, batch_size=12)
self.assertAllClose(score, true_score, atol=1e-2)

def test_fit_raise_if_num_clusters_larger_than_num_points_random_init(self):
points = np.array([[2.0, 3.0], [1.6, 8.2]])

with self.assertRaisesOpError('less'):
kmeans = KMeans(num_clusters=3, initial_clusters=kmeans_ops.RANDOM_INIT)
kmeans.fit(x=points)
kmeans.fit(x=points, steps=10, batch_size=8)

def test_fit_raise_if_num_clusters_larger_than_num_points_kmeans_plus_plus(
self):
Expand All @@ -285,7 +275,7 @@ def test_fit_raise_if_num_clusters_larger_than_num_points_kmeans_plus_plus(
with self.assertRaisesOpError(AssertionError):
kmeans = KMeans(num_clusters=3,
initial_clusters=kmeans_ops.KMEANS_PLUS_PLUS_INIT)
kmeans.fit(x=points)
kmeans.fit(x=points, steps=10, batch_size=8)


class MiniBatchKMeansTest(KMeansTest):
Expand Down
9 changes: 9 additions & 0 deletions tensorflow/contrib/ios_examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,14 @@ rundown:
unused because no other code references the variables, but in fact their
constructors have the important side effect of registering the class.

- C++11 support (or later) should be enabled by setting `C++ Language Dialect` to
`GNU++11` (or `GNU++14`), and `C++ Standard Library` to `libc++`.

- The library doesn't currently support bitcode, so you'll need to disable that
in your project settings.

- Remove any use of the `-all_load` flag in your project. The protocol buffers
libraries (full and lite versions) contain duplicate symbols, and the `-all_load`
flag will cause these duplicates to become link errors. If you were using
`-all_load` to avoid issues with Objective-C categories in static libraries,
you may be able to replace it with the `-ObjC` flag.
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ def __init__(self, model_fn, n_classes, model_dir=None, config=None):
Args:
model_fn: (targets, predictions, mode) -> logits, loss, train_op
n_classes: Number of classes
model_dir: Base directory for output data
model_dir: Directory to save model parameters, graph and etc. This can also
be used to load checkpoints from the directory into a estimator to continue
training a previously saved model.
config: Configuration object (optional)
"""
self._n_classes = n_classes
Expand Down
Loading

0 comments on commit 21716d8

Please sign in to comment.