{ "cells": [ { "cell_type": "code", "execution_count": 10, "id": "821be114-acbc-4d87-8ef8-03ca6cb2dbeb", "metadata": {}, "outputs": [], "source": [ "import tensorflow as tf\n", "from time import time" ] }, { "cell_type": "code", "execution_count": 107, "id": "a17d7aa5-505e-4306-ae47-f39830b3406b", "metadata": {}, "outputs": [], "source": [ "dataset = tf.data.Dataset.range(10000)\n", "dataset = dataset.map(lambda x: sum([x**i for i in range(1000)]))\n", "dataset = dataset.cache()" ] }, { "cell_type": "code", "execution_count": 108, "id": "b77c9415-f5f3-49fc-a81e-169119e86413", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5.57" ] }, "execution_count": 108, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# The first time reading through the data will generate the data using\n", "# `range` and `map`.\n", "tik = time()\n", "list(dataset.as_numpy_iterator())\n", "tok = time()\n", "round(tok-tik,2)" ] }, { "cell_type": "code", "execution_count": 106, "id": "94724841-417a-4490-ae0e-784e18d35c35", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1.2" ] }, "execution_count": 106, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Subsequent iterations read from the cache.\n", "tik = time()\n", "list(dataset.as_numpy_iterator())\n", "tok = time()\n", "round(tok-tik,2)" ] }, { "cell_type": "code", "execution_count": null, "id": "7929ee4f-75be-4dce-a984-00dff458e180", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.10" } }, "nbformat": 4, "nbformat_minor": 5 }