Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save analyticsindiamagazine/d937efaacbb54593ecd07be98b28f491 to your computer and use it in GitHub Desktop.
Save analyticsindiamagazine/d937efaacbb54593ecd07be98b28f491 to your computer and use it in GitHub Desktop.
TensorFlow Eager Execution
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "Eager_execution_d.ipynb",
"provenance": [],
"collapsed_sections": []
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
}
},
"cells": [
{
"cell_type": "code",
"metadata": {
"id": "8bzIFsLrNz3e",
"colab_type": "code",
"colab": {}
},
"source": [
"import tensorflow as tf\n",
"\n",
"tf.compat.v1.disable_eager_execution()\n"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "joWTC7PzPIW_",
"colab_type": "code",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
},
"outputId": "afe15c26-4bea-4964-e1b8-5b09af1446c3"
},
"source": [
"tf.executing_eagerly()"
],
"execution_count": 7,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"False"
]
},
"metadata": {
"tags": []
},
"execution_count": 7
}
]
},
{
"cell_type": "code",
"metadata": {
"attributes": {
"classes": [
"py"
],
"id": ""
},
"colab_type": "code",
"outputId": "301f0cce-d7ff-4a55-db3a-e72dae8ff1fe",
"id": "vSTmGyALNcUn",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
}
},
"source": [
"x = [[2.]]\n",
"m = tf.matmul(x, x)\n",
"print(\"hello, {}\".format(m))"
],
"execution_count": 8,
"outputs": [
{
"output_type": "stream",
"text": [
"hello, Tensor(\"MatMul:0\", shape=(1, 1), dtype=float32)\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"colab_type": "code",
"outputId": "33b34bce-624c-4eb2-f95b-6068461398c0",
"id": "xk136sOrNcUq",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
}
},
"source": [
"a = tf.constant([[1, 2],[3, 4]])\n",
"print(a)"
],
"execution_count": 9,
"outputs": [
{
"output_type": "stream",
"text": [
"Tensor(\"Const:0\", shape=(2, 2), dtype=int32)\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"attributes": {
"classes": [
"py"
],
"id": ""
},
"colab_type": "code",
"id": "Ui025t1qqEfm",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 334
},
"outputId": "e1f74f08-92fc-4882-95bd-52bd254e2302"
},
"source": [
"# Use NumPy values\n",
"import numpy as np\n",
"\n",
"c = np.multiply(a, a)\n",
"print(c)"
],
"execution_count": 10,
"outputs": [
{
"output_type": "error",
"ename": "NotImplementedError",
"evalue": "ignored",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mNotImplementedError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-10-60ab3ac37743>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mnumpy\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0mnp\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0mc\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mnp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mmultiply\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ma\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0ma\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 4\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mc\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/framework/ops.py\u001b[0m in \u001b[0;36m__array__\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 734\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0m__array__\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 735\u001b[0m raise NotImplementedError(\"Cannot convert a symbolic Tensor ({}) to a numpy\"\n\u001b[0;32m--> 736\u001b[0;31m \" array.\".format(self.name))\n\u001b[0m\u001b[1;32m 737\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 738\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0m__len__\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mNotImplementedError\u001b[0m: Cannot convert a symbolic Tensor (Const:0) to a numpy array."
]
}
]
}
]
}
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment