{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "Handling TensorFlow graphs and sessions\n", "--\n", "\n", "##### Check [How GPflow relates to TensorFlow: tips & tricks](../tips_and_tricks.ipynb) for more examples and information." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "import gpflow\n", "import tensorflow as tf" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "TensorFlow gives you a default graph, which you fill with tensors and operations - nodes and edges in the graph respectively. See the [TensorFlow documentation on building a `tf.Graph`](https://www.tensorflow.org/guide/graphs#building_a_tfgraph) for details on how to change the default graph or exploit multiple graphs.\n", "\n", "A TensorFlow graph is a representation of your computation. To execute your code you need a [TensorFlow session](https://www.tensorflow.org/guide/graphs#executing_a_graph_in_a_tfsession). For example, you can think of graphs and sessions as binary sources and actual command running it in a terminal. Normally, TensorFlow doesn't provide the default session; however, GPflow creates a default session that you can access as shown in the following example:" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "session = gpflow.get_default_session()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To change GPflow's default session:" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "gpflow.reset_default_session()\n", "assert session is not gpflow.get_default_session()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can manipulate sessions manually, but you have to make them the default for GPflow:" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "W0816 11:52:23.051765 140208876939072 deprecation_wrapper.py:119] From /home/alexandra/virtual_environments/prowler_venv/lib/python3.6/site-packages/gpflow/core/node.py:109: The name tf.get_default_graph is deprecated. Please use tf.compat.v1.get_default_graph instead.\n", "\n", "W0816 11:52:23.058769 140208876939072 deprecation_wrapper.py:119] From /home/alexandra/virtual_environments/prowler_venv/lib/python3.6/site-packages/gpflow/params/parameter.py:388: The name tf.placeholder is deprecated. Please use tf.compat.v1.placeholder instead.\n", "\n" ] } ], "source": [ "with tf.Session() as session:\n", " k = gpflow.kernels.SquaredExponential(input_dim=1)\n", " k.lengthscales = 2.0\n", " k.variance = 3.0" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now, TensorFlow variables and tensors for the created `SquaredExponential` kernel are initialised with the session that was closed when the Python context ended. You can re-use the `SquaredExponential` object by re-initialising it." ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " | class | \n", "prior | \n", "transform | \n", "trainable | \n", "shape | \n", "fixed_shape | \n", "value | \n", "
---|---|---|---|---|---|---|---|
SquaredExponential/lengthscales | \n", "Parameter | \n", "None | \n", "+ve | \n", "True | \n", "() | \n", "True | \n", "2.0 | \n", "
SquaredExponential/variance | \n", "Parameter | \n", "None | \n", "+ve | \n", "True | \n", "() | \n", "True | \n", "3.0 | \n", "