Skip to content

Migrate to TensorFlow 2.0 #539

@matthewfeickert

Description

@matthewfeickert

Description

The API in TensorFlow 2.0 changes quite a bit, and the concept of sessions goes away. There is a migration guide here. The most work that will need to be done is just in the optimizer, but as an example of how things will be different in the documentation see the following example for pyhf.tensorlib.clip in TF v1.14 code

>>> import pyhf
>>> import tensorflow as tf
>>> sess = tf.Session()
...
>>> pyhf.set_backend(pyhf.tensor.tensorflow_backend(session=sess))
>>> a = pyhf.tensorlib.astensor([-2, -1, 0, 1, 2])
>>> with sess.as_default():
...   sess.run(pyhf.tensorlib.clip(a, -1, 1))
...
array([-1., -1.,  0.,  1.,  1.], dtype=float32)

vs. TF 2.0 code

>>> import pyhf
>>> pyhf.set_backend(pyhf.tensor.tensorflow_backend())
>>> a = pyhf.tensorlib.astensor([-2, -1, 0, 1, 2])
>>> pyhf.tensorlib.clip(a, -1, 1)
<tf.Tensor: id=5, shape=(5,), dtype=float32, numpy=array([-1., -1.,  0.,  1.,  1.], dtype=float32)>
>>> pyhf.tensorlib.clip(a, -1, 1).numpy()
array([-1., -1.,  0.,  1.,  1.], dtype=float32)

or for later evaluation with other actions

>>> import pyhf
>>> import tensorflow as tf
>>> pyhf.set_backend(pyhf.tensor.tensorflow_backend())
>>> a = pyhf.tensorlib.astensor([-2, -1, 0, 1, 2])
>>> g = tf.function(pyhf.tensorlib.clip) # g for graph, btw
>>> g(a, -1, 1)
<tf.Tensor: id=82, shape=(5,), dtype=float32, numpy=array([-1., -1.,  0.,  1.,  1.], dtype=float32)>
>>> g(a, -1, 1).numpy()
array([-1., -1.,  0.,  1.,  1.], dtype=float32)

Metadata

Metadata

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions