Skip to content

Instantly share code, notes, and snippets.

@aphlysia
Created December 25, 2018 23:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aphlysia/59e0e2f5610d30a45d4213f9bef85514 to your computer and use it in GitHub Desktop.
Save aphlysia/59e0e2f5610d30a45d4213f9bef85514 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "control_dependencies.ipynb",
"version": "0.3.2",
"provenance": [],
"collapsed_sections": []
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"accelerator": "GPU"
},
"cells": [
{
"metadata": {
"id": "tr1-hpjljUK5",
"colab_type": "text"
},
"cell_type": "markdown",
"source": [
"control_dependencies で直感と反するケース"
]
},
{
"metadata": {
"id": "gaS0Ok29lkq2",
"colab_type": "code",
"outputId": "4dbbf04c-a558-4f36-f3a8-25ff7dd98b62",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
}
},
"cell_type": "code",
"source": [
"import tensorflow as tf\n",
"tf.__version__"
],
"execution_count": 41,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"'1.12.0'"
]
},
"metadata": {
"tags": []
},
"execution_count": 41
}
]
},
{
"metadata": {
"id": "WWKepU5DUIcu",
"colab_type": "code",
"outputId": "83cfdc87-8144-4ffd-d1c6-96c42446e058",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 241
}
},
"cell_type": "code",
"source": [
"tf.reset_default_graph()\n",
"\n",
"x = tf.random.normal((1,))\n",
"y = tf.get_variable('y', shape=(1,), dtype=tf.float32)\n",
"assign = y.assign(x)\n",
"z = y + 1\n",
"with tf.control_dependencies([assign]):\n",
" w = z + 1\n",
" \n",
"sess = tf.InteractiveSession()\n",
"sess.run(tf.initializers.global_variables())\n",
"\n",
"for _ in range(10):\n",
" print(sess.run([y, z, w]))"
],
"execution_count": 42,
"outputs": [
{
"output_type": "stream",
"text": [
"[array([0.21503043], dtype=float32), array([1.2150304], dtype=float32), array([2.2150304], dtype=float32)]\n",
"[array([0.28964567], dtype=float32), array([1.2896457], dtype=float32), array([2.2896457], dtype=float32)]\n",
"[array([0.00648436], dtype=float32), array([-0.7190591], dtype=float32), array([0.2809409], dtype=float32)]\n",
"[array([0.49580196], dtype=float32), array([1.0064844], dtype=float32), array([2.0064845], dtype=float32)]\n",
"[array([-1.0288614], dtype=float32), array([1.4958019], dtype=float32), array([2.495802], dtype=float32)]\n",
"[array([-2.034961], dtype=float32), array([-0.0288614], dtype=float32), array([0.9711386], dtype=float32)]\n",
"[array([-2.034961], dtype=float32), array([-1.034961], dtype=float32), array([-0.03496099], dtype=float32)]\n",
"[array([0.02698239], dtype=float32), array([1.0269824], dtype=float32), array([2.0269823], dtype=float32)]\n",
"[array([0.7145383], dtype=float32), array([1.7145383], dtype=float32), array([2.7145383], dtype=float32)]\n",
"[array([2.2762003], dtype=float32), array([3.2762003], dtype=float32), array([4.2762003], dtype=float32)]\n"
],
"name": "stdout"
},
{
"output_type": "stream",
"text": [
"/usr/local/lib/python3.6/dist-packages/tensorflow/python/client/session.py:1702: UserWarning: An interactive session is already active. This can cause out-of-memory errors in some cases. You must explicitly call `InteractiveSession.close()` to release resources held by the other session(s).\n",
" warnings.warn('An interactive session is already active. This can '\n"
],
"name": "stderr"
}
]
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment