Skip to content

Commit

Permalink
Merge commit for internal changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Vijay Vasudevan committed Jul 13, 2016
2 parents e56888c + c87a7ca commit 3cb73e9
Show file tree
Hide file tree
Showing 172 changed files with 8,287 additions and 1,976 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ and discussion.**
## Installation
*See [Download and Setup](tensorflow/g3doc/get_started/os_setup.md) for instructions on how to install our release binaries or how to build from source.*

People who are a little bit adventurous can also try our nightly binaries:
People who are a little more adventurous can also try our nightly binaries:

* Linux CPU only: [Python 2](http://ci.tensorflow.org/view/Nightly/job/nightly-matrix-cpu/TF_BUILD_CONTAINER_TYPE=CPU,TF_BUILD_IS_OPT=OPT,TF_BUILD_IS_PIP=PIP,TF_BUILD_PYTHON_VERSION=PYTHON2,label=cpu-slave/lastSuccessfulBuild/artifact/pip_test/whl/tensorflow-0.9.0-py2-none-any.whl) ([build history](http://ci.tensorflow.org/view/Nightly/job/nightly-matrix-cpu/TF_BUILD_CONTAINER_TYPE=CPU,TF_BUILD_IS_OPT=OPT,TF_BUILD_IS_PIP=PIP,TF_BUILD_PYTHON_VERSION=PYTHON2,label=cpu-slave/)) / [Python 3.4](http://ci.tensorflow.org/view/Nightly/job/nightly-matrix-cpu/TF_BUILD_CONTAINER_TYPE=CPU,TF_BUILD_IS_OPT=OPT,TF_BUILD_IS_PIP=PIP,TF_BUILD_PYTHON_VERSION=PYTHON3,label=cpu-slave/lastSuccessfulBuild/artifact/pip_test/whl/tensorflow-0.9.0-py3-none-any.whl) ([build history](http://ci.tensorflow.org/view/Nightly/job/nightly-matrix-cpu/TF_BUILD_CONTAINER_TYPE=CPU,TF_BUILD_IS_OPT=OPT,TF_BUILD_IS_PIP=PIP,TF_BUILD_PYTHON_VERSION=PYTHON3,label=cpu-slave/)) / [Python 3.5](http://ci.tensorflow.org/view/Nightly/job/nightly-python35-linux-cpu/lastSuccessfulBuild/artifact/pip_test/whl/tensorflow-0.9.0-py3-none-any.whl) ([build history](http://ci.tensorflow.org/view/Nightly/job/nightly-python35-linux-cpu/))
* Linux GPU: [Python 2](http://ci.tensorflow.org/view/Nightly/job/nigntly-matrix-linux-gpu/TF_BUILD_CONTAINER_TYPE=GPU,TF_BUILD_IS_OPT=OPT,TF_BUILD_IS_PIP=PIP,TF_BUILD_PYTHON_VERSION=PYTHON2,label=gpu-linux/lastSuccessfulBuild/artifact/pip_test/whl/tensorflow-0.9.0-py2-none-any.whl) ([build history](http://ci.tensorflow.org/view/Nightly/job/nigntly-matrix-linux-gpu/TF_BUILD_CONTAINER_TYPE=GPU,TF_BUILD_IS_OPT=OPT,TF_BUILD_IS_PIP=PIP,TF_BUILD_PYTHON_VERSION=PYTHON2,label=gpu-linux/)) / [Python 3.4](http://ci.tensorflow.org/view/Nightly/job/nigntly-matrix-linux-gpu/TF_BUILD_CONTAINER_TYPE=GPU,TF_BUILD_IS_OPT=OPT,TF_BUILD_IS_PIP=PIP,TF_BUILD_PYTHON_VERSION=PYTHON3,label=gpu-linux/lastSuccessfulBuild/artifact/pip_test/whl/tensorflow-0.9.0-py3-none-any.whl) ([build history](http://ci.tensorflow.org/view/Nightly/job/nigntly-matrix-linux-gpu/TF_BUILD_CONTAINER_TYPE=GPU,TF_BUILD_IS_OPT=OPT,TF_BUILD_IS_PIP=PIP,TF_BUILD_PYTHON_VERSION=PYTHON3,label=gpu-linux/)) / [Python 3.5](http://ci.tensorflow.org/view/Nightly/job/nigntly-matrix-linux-gpu/TF_BUILD_CONTAINER_TYPE=GPU,TF_BUILD_IS_OPT=OPT,TF_BUILD_IS_PIP=PIP,TF_BUILD_PYTHON_VERSION=PYTHON3.5,label=gpu-linux/lastSuccessfulBuild/artifact/pip_test/whl/tensorflow-0.9.0-py3-none-any.whl) ([build history](http://ci.tensorflow.org/view/Nightly/job/nigntly-matrix-linux-gpu/TF_BUILD_CONTAINER_TYPE=GPU,TF_BUILD_IS_OPT=OPT,TF_BUILD_IS_PIP=PIP,TF_BUILD_PYTHON_VERSION=PYTHON3.5,label=gpu-linux/))
* Mac CPU only: [Python 2](http://ci.tensorflow.org/view/Nightly/job/nightly-matrix-cpu/TF_BUILD_CONTAINER_TYPE=CPU,TF_BUILD_IS_OPT=OPT,TF_BUILD_IS_PIP=PIP,TF_BUILD_PYTHON_VERSION=PYTHON2,label=mac1-slave/lastSuccessfulBuild/artifact/pip_test/whl/tensorflow-0.9.0-py2-none-any.whl) ([build history](http://ci.tensorflow.org/view/Nightly/job/nightly-matrix-cpu/TF_BUILD_CONTAINER_TYPE=CPU,TF_BUILD_IS_OPT=OPT,TF_BUILD_IS_PIP=PIP,TF_BUILD_PYTHON_VERSION=PYTHON2,label=mac1-slave/)) / [Python 3](http://ci.tensorflow.org/view/Nightly/job/nightly-matrix-cpu/TF_BUILD_CONTAINER_TYPE=CPU,TF_BUILD_IS_OPT=OPT,TF_BUILD_IS_PIP=PIP,TF_BUILD_PYTHON_VERSION=PYTHON3,label=mac1-slave/lastSuccessfulBuild/artifact/pip_test/whl/tensorflow-0.9.0-py3-none-any.whl) ([build history](http://ci.tensorflow.org/view/Nightly/job/nightly-matrix-cpu/TF_BUILD_CONTAINER_TYPE=CPU,TF_BUILD_IS_OPT=OPT,TF_BUILD_IS_PIP=PIP,TF_BUILD_PYTHON_VERSION=PYTHON3,label=mac1-slave/))
* Linux CPU-only: [Python 2](http://ci.tensorflow.org/view/Nightly/job/nightly-matrix-cpu/TF_BUILD_CONTAINER_TYPE=CPU,TF_BUILD_IS_OPT=OPT,TF_BUILD_IS_PIP=PIP,TF_BUILD_PYTHON_VERSION=PYTHON2,label=cpu-slave/lastSuccessfulBuild/artifact/pip_test/whl/tensorflow-0.9.0-cp27-none-linux_x86_64.whl) ([build history](http://ci.tensorflow.org/view/Nightly/job/nightly-matrix-cpu/TF_BUILD_CONTAINER_TYPE=CPU,TF_BUILD_IS_OPT=OPT,TF_BUILD_IS_PIP=PIP,TF_BUILD_PYTHON_VERSION=PYTHON2,label=cpu-slave/)) / [Python 3.4](http://ci.tensorflow.org/view/Nightly/job/nightly-matrix-cpu/TF_BUILD_CONTAINER_TYPE=CPU,TF_BUILD_IS_OPT=OPT,TF_BUILD_IS_PIP=PIP,TF_BUILD_PYTHON_VERSION=PYTHON3,label=cpu-slave/lastSuccessfulBuild/artifact/pip_test/whl/tensorflow-0.9.0-cp34-cp34m-linux_x86_64.whl) ([build history](http://ci.tensorflow.org/view/Nightly/job/nightly-matrix-cpu/TF_BUILD_CONTAINER_TYPE=CPU,TF_BUILD_IS_OPT=OPT,TF_BUILD_IS_PIP=PIP,TF_BUILD_PYTHON_VERSION=PYTHON3,label=cpu-slave/)) / [Python 3.5](http://ci.tensorflow.org/view/Nightly/job/nightly-python35-linux-cpu/lastSuccessfulBuild/artifact/pip_test/whl/tensorflow-0.9.0-cp35-cp35m-linux_x86_64.whl) ([build history](http://ci.tensorflow.org/view/Nightly/job/nightly-python35-linux-cpu/))
* Linux GPU: [Python 2](http://ci.tensorflow.org/view/Nightly/job/nigntly-matrix-linux-gpu/TF_BUILD_CONTAINER_TYPE=GPU,TF_BUILD_IS_OPT=OPT,TF_BUILD_IS_PIP=PIP,TF_BUILD_PYTHON_VERSION=PYTHON2,label=gpu-linux/lastSuccessfulBuild/artifact/pip_test/whl/tensorflow-0.9.0-cp27-none-linux_x86_64.whl) ([build history](http://ci.tensorflow.org/view/Nightly/job/nigntly-matrix-linux-gpu/TF_BUILD_CONTAINER_TYPE=GPU,TF_BUILD_IS_OPT=OPT,TF_BUILD_IS_PIP=PIP,TF_BUILD_PYTHON_VERSION=PYTHON2,label=gpu-linux/)) / [Python 3.4](http://ci.tensorflow.org/view/Nightly/job/nigntly-matrix-linux-gpu/TF_BUILD_CONTAINER_TYPE=GPU,TF_BUILD_IS_OPT=OPT,TF_BUILD_IS_PIP=PIP,TF_BUILD_PYTHON_VERSION=PYTHON3,label=gpu-linux/lastSuccessfulBuild/artifact/pip_test/whl/tensorflow-0.9.0-cp34-cp34m-linux_x86_64.whl) ([build history](http://ci.tensorflow.org/view/Nightly/job/nigntly-matrix-linux-gpu/TF_BUILD_CONTAINER_TYPE=GPU,TF_BUILD_IS_OPT=OPT,TF_BUILD_IS_PIP=PIP,TF_BUILD_PYTHON_VERSION=PYTHON3,label=gpu-linux/)) / [Python 3.5](http://ci.tensorflow.org/view/Nightly/job/nigntly-matrix-linux-gpu/TF_BUILD_CONTAINER_TYPE=GPU,TF_BUILD_IS_OPT=OPT,TF_BUILD_IS_PIP=PIP,TF_BUILD_PYTHON_VERSION=PYTHON3.5,label=gpu-linux/140/artifact/pip_test/whl/tensorflow-0.8.0-cp35-cp35m-linux_x86_64.whl) ([build history](http://ci.tensorflow.org/view/Nightly/job/nigntly-matrix-linux-gpu/TF_BUILD_CONTAINER_TYPE=GPU,TF_BUILD_IS_OPT=OPT,TF_BUILD_IS_PIP=PIP,TF_BUILD_PYTHON_VERSION=PYTHON3.5,label=gpu-linux/))
* Mac CPU-only: [Python 2](http://ci.tensorflow.org/view/Nightly/job/nightly-matrix-cpu/TF_BUILD_CONTAINER_TYPE=CPU,TF_BUILD_IS_OPT=OPT,TF_BUILD_IS_PIP=PIP,TF_BUILD_PYTHON_VERSION=PYTHON2,label=mac1-slave/lastSuccessfulBuild/artifact/pip_test/whl/tensorflow-0.9.0-py2-none-any.whl) ([build history](http://ci.tensorflow.org/view/Nightly/job/nightly-matrix-cpu/TF_BUILD_CONTAINER_TYPE=CPU,TF_BUILD_IS_OPT=OPT,TF_BUILD_IS_PIP=PIP,TF_BUILD_PYTHON_VERSION=PYTHON2,label=mac1-slave/)) / [Python 3](http://ci.tensorflow.org/view/Nightly/job/nightly-matrix-cpu/TF_BUILD_CONTAINER_TYPE=CPU,TF_BUILD_IS_OPT=OPT,TF_BUILD_IS_PIP=PIP,TF_BUILD_PYTHON_VERSION=PYTHON3,label=mac1-slave/lastSuccessfulBuild/artifact/pip_test/whl/tensorflow-0.9.0-py3-none-any.whl) ([build history](http://ci.tensorflow.org/view/Nightly/job/nightly-matrix-cpu/TF_BUILD_CONTAINER_TYPE=CPU,TF_BUILD_IS_OPT=OPT,TF_BUILD_IS_PIP=PIP,TF_BUILD_PYTHON_VERSION=PYTHON3,label=mac1-slave/))
* [Android](http://ci.tensorflow.org/view/Nightly/job/nightly-matrix-android/TF_BUILD_CONTAINER_TYPE=ANDROID,TF_BUILD_IS_OPT=OPT,TF_BUILD_IS_PIP=NO_PIP,TF_BUILD_PYTHON_VERSION=PYTHON2,label=android-slave/lastSuccessfulBuild/artifact/bazel-out/local_linux/bin/tensorflow/examples/android/tensorflow_demo.apk) ([build history](http://ci.tensorflow.org/view/Nightly/job/nightly-matrix-android/TF_BUILD_CONTAINER_TYPE=ANDROID,TF_BUILD_IS_OPT=OPT,TF_BUILD_IS_PIP=NO_PIP,TF_BUILD_PYTHON_VERSION=PYTHON2,label=android-slave/))

#### *Try your first TensorFlow program*
Expand Down
6 changes: 3 additions & 3 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ new_git_repository(
name = "iron_menu_behavior",
build_file = "bower.BUILD",
remote = "https://github.com/polymerelements/iron-menu-behavior.git",
tag = "v1.1.7",
tag = "v1.1.8",
)

new_git_repository(
Expand All @@ -189,7 +189,7 @@ new_git_repository(
name = "iron_overlay_behavior",
build_file = "bower.BUILD",
remote = "https://github.com/polymerelements/iron-overlay-behavior.git",
tag = "v1.8.2",
tag = "v1.8.3",
)

new_git_repository(
Expand Down Expand Up @@ -322,7 +322,7 @@ new_git_repository(
name = "paper_menu_button",
build_file = "bower.BUILD",
remote = "https://github.com/polymerelements/paper-menu-button.git",
tag = "v1.1.1",
tag = "v1.3.0",
)

new_git_repository(
Expand Down
4 changes: 2 additions & 2 deletions eigen.BUILD
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package(default_visibility = ["//visibility:public"])

archive_dir = "eigen-eigen-bbffe8ed5f26"
archive_dir = "eigen-eigen-b4fa9622b809"
cc_library(
name = "eigen",
hdrs = glob([archive_dir+"/**/*.h", archive_dir+"/unsupported/Eigen/CXX11/*", archive_dir+"/Eigen/*"]),
hdrs = glob([archive_dir+"/**/*.h", archive_dir+"/unsupported/Eigen/*", archive_dir+"/unsupported/Eigen/CXX11/*", archive_dir+"/Eigen/*"]),
includes = [ archive_dir ],
visibility = ["//visibility:public"],
)
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,32 @@ def testDistributionEntropy(self):
self.assertAllEqual(entropies[2], entropies[0])
self.assertAllEqual(entropies[1], entropies[0])

def testSurrogateLoss(self):
with self.test_session():
mu = [[3.0, -4.0, 5.0], [6.0, -7.0, 8.0]]
sigma = tf.constant(1.0)

# With default
with sg.value_type(sg.MeanValue(stop_gradient=True)):
dt = sg.DistributionTensor(distributions.Normal, mu=mu, sigma=sigma)
surrogate_loss = dt.surrogate_loss([tf.constant(2.0)])
self.assertTrue(surrogate_loss is not None)
self.assertAllClose(
dt.distribution.log_prob(mu).eval() * 2.0, surrogate_loss.eval())

# With passed-in surrogate_loss_fn.
dt = sg.DistributionTensor(
distributions.Normal,
mu=mu,
sigma=sigma,
dist_value_type=sg.MeanValue(stop_gradient=True),
surrogate_loss_fn=sg.get_score_function_with_baseline(
baseline=tf.constant(8.0)))
surrogate_loss = dt.surrogate_loss([tf.constant(2.0)])
self.assertTrue(surrogate_loss is not None)
self.assertAllClose((dt.distribution.log_prob(mu) * (2.0 - 8.0)).eval(),
surrogate_loss.eval())


class ValueTypeTest(tf.test.TestCase):

Expand Down Expand Up @@ -298,6 +324,16 @@ def testSurrogateLoss(self):
* loss_nodeps_nograd)]),
xs=[mu, sigma])

def testNoSurrogateLoss(self):
with self.test_session():
mu = tf.constant([0.0, 0.1, 0.2])
sigma = tf.constant([1.1, 1.2, 1.3])
with sg.value_type(sg.SampleAndReshapeValue()):
dt = sg.DistributionTensor(NormalNotParam,
mu=mu,
sigma=sigma,
surrogate_loss_fn=None)
self.assertEqual(None, dt.surrogate_loss(tf.constant([2.0])))

if __name__ == "__main__":
tf.test.main()
100 changes: 76 additions & 24 deletions tensorflow/contrib/bayesflow/python/ops/stochastic_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@
@@value_type
@@get_current_value_type
## Stochastic Computation Surrogate Loss Functions
@@score_function
@@get_score_function_with_baseline
## Stochastic Computation Graph Helper Functions
@@surrogate_losses
Expand All @@ -46,6 +51,7 @@
from tensorflow.python.framework import ops
from tensorflow.python.framework import tensor_shape
from tensorflow.python.ops import array_ops
from tensorflow.python.ops import math_ops
from tensorflow.python.platform import tf_logging as logging

STOCHASTIC_TENSOR_COLLECTION = "_stochastic_tensor_collection_"
Expand All @@ -55,9 +61,7 @@
class StochasticTensor(object):
"""Base Class for Tensor-like objects that emit stochastic values."""

def __init__(self, **kwargs):
self._inputs = kwargs

def __init__(self):
# Add self to this graph's Stochsatic Tensor collection for
# purposes of later performing correct surrogate loss calculation.
ops.add_to_collection(STOCHASTIC_TENSOR_COLLECTION, self)
Expand Down Expand Up @@ -306,11 +310,53 @@ def get_current_value_type():
return _STOCHASTIC_VALUE_STACK[thread_id][-1]


def get_score_function_with_baseline(baseline):

def score_function_with_baseline(dist_tensor, value, losses):
advantage = math_ops.add_n(losses) - baseline
return dist_tensor.distribution.log_prob(value) * advantage

return score_function_with_baseline


def score_function(dist_tensor, value, losses):
return dist_tensor.distribution.log_prob(value) * math_ops.add_n(losses)


class DistributionTensor(StochasticTensor):
"""The DistributionTensor is a StochasticTensor backed by a distribution.
"""
"""DistributionTensor is a StochasticTensor backed by a distribution."""

def __init__(self,
dist_cls,
name=None,
dist_value_type=None,
surrogate_loss_fn=score_function,
**dist_args):
"""Construct a `DistributionTensor`.
`surrogate_loss_fn` controls what `surrogate_loss` returns, which is used
in conjunction with the `surrogate_losses` function in this module.
`surrogate_loss_fn` is a callable that takes this `DistributionTensor`, a
`Tensor` with this `DistributionTensor`'s value, and a list of `Tensor`
losses influenced by this `DistributionTensor`; it should return a `Tensor`
surrogate loss. If not provided, it defaults to the score function
surrogate loss: `log_prob(value) * sum(losses)`. If `surrogate_loss_fn` is
None, no surrogate loss will be returned. Currently, a surrogate loss will
only be used if `dist_value_type.stop_gradient=True` or if the value is a
sample from a non-reparameterized distribution.
def __init__(self, dist_cls, name=None, dist_value_type=None, **dist_args):
Args:
dist_cls: a class deriving from `BaseDistribution`.
name: a name for this `DistributionTensor` and its ops.
dist_value_type: a `_StochasticValueType`, which will determine what the
`value` of this `DistributionTensor` will be. If not provided, the
value type set with the `value_type` context manager will be used.
surrogate_loss_fn: callable that takes
`(dt, dt.value(), influenced_losses)`, where `dt` is this
`DistributionTensor`, and returns a `Tensor` surrogate loss.
**dist_args: keyword arguments to be passed through to `dist_cls` on
construction.
"""
self._dist_cls = dist_cls
self._dist_args = dist_args
if dist_value_type is None:
Expand All @@ -326,6 +372,10 @@ def __init__(self, dist_cls, name=None, dist_value_type=None, **dist_args):

self._value_type.declare_inputs(self, dist_args)

if surrogate_loss_fn is not None and not callable(surrogate_loss_fn):
raise TypeError("surrogate_loss_fn must be callable")
self._surrogate_loss_fn = surrogate_loss_fn

with ops.op_scope(dist_args.values(), name, "DistributionTensor") as scope:
self._name = scope
self._dist = dist_cls(**dist_args)
Expand Down Expand Up @@ -374,9 +424,7 @@ def _create_value(self):
raise TypeError(
"Unrecognized Distribution Value Type: %s", self._value_type)

stop_gradient = self._value_type.stop_gradient

if stop_gradient:
if self._value_type.stop_gradient:
# stop_gradient is being enforced by the value type
return array_ops.stop_gradient(value_tensor)

Expand Down Expand Up @@ -411,19 +459,23 @@ def mean(self, name="mean"):
def value(self, name="value"):
return self._value

def surrogate_loss(self, losses, name=None):
# Return a loss term based on losses and the distribution. Return
# None if pathwise derivatives are supported
if self._dist.is_continuous and self._dist.is_reparameterized:
def surrogate_loss(self, losses, name="DistributionSurrogateLoss"):
# Return a loss term based on losses and the distribution. Returns
# None if pathwise derivatives are supported, if the surrogate_loss_fn
# was explicitly set to None, or if the value type is MeanValue.
if self._surrogate_loss_fn is None:
return None

if (self._dist.is_continuous and self._dist.is_reparameterized and
not self._value_type.stop_gradient):
# Can perform pathwise-derivative on this one; no surrogate loss needed.
return None

with ops.op_scope(losses, name, "DistributionSurrogateLoss"):
if isinstance(self._value_type, SampleAndReshapeValue):
# TODO(ebrevdo): use add_n instead of sum(losses) if shapes all match?
return self._dist.log_prob(self._value) * sum(losses)
elif isinstance(self._value_type, SampleValue):
return self._dist.log_prob(self._value) * sum(losses)
with ops.op_scope(losses, name):
if (self._value_type.stop_gradient or
isinstance(self._value_type, SampleAndReshapeValue) or
isinstance(self._value_type, SampleValue)):
return self._surrogate_loss_fn(self, self._value, losses)
elif isinstance(self._value_type, MeanValue):
return None # MeanValue generally provides its own gradient
else:
Expand Down Expand Up @@ -512,14 +564,14 @@ def surrogate_losses(sample_losses, name="SurrogateLosses"):
"No collection of Stochastic Tensors found for current graph.")
return []

surrogate_loss_losses = []
surrogate_losses_ = []

# Iterate through all of the stochastic dependencies, adding
# surrogate terms where necessary.
for (stoch_node, dependent_losses) in stoch_dependencies_map.items():
surrogate_loss = stoch_node.surrogate_loss(list(dependent_losses))
if surrogate_loss is not None:
surrogate_loss_ = stoch_node.surrogate_loss(list(dependent_losses))
if surrogate_loss_ is not None:
with ops.name_scope("SurrogateLoss_%s" % stoch_node.name):
surrogate_loss_losses.append(array_ops.identity(surrogate_loss))
surrogate_losses_.append(array_ops.identity(surrogate_loss_))

return surrogate_loss_losses
return surrogate_losses_
Loading

0 comments on commit 3cb73e9

Please sign in to comment.