{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Demo" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Import the required dependencies" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "from typing import Tuple\n", "\n", "from numpy import arange\n", "from tensorflow import Tensor\n", "from tensorflow.python.data import Dataset\n", "\n", "from tfdatacompose.map.map import Map\n", "from tfdatacompose.filter.filter import Filter\n", "from tfdatacompose.pipeline import Pipeline\n", "from tfdatacompose.skip import Skip\n", "from tfdatacompose.take import Take" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Define the transformations" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "class Double(Map):\n", " def map(self, number: int) -> Tuple[Tensor, ...]:\n", " return number * 2\n", "\n", "class RemoveOdd(Filter):\n", " def filter(self, number: int) -> bool:\n", " return number % 2 == 0 " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Create a transformation pipeline using our custom transformations" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "pipeline = Pipeline([\n", " Skip(10),\n", " RemoveOdd(),\n", " Double(),\n", " Take(10),\n", "])\n", "\n", "pipeline" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Apply our transformation to a dataset" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(<_TensorSliceDataset element_spec=TensorSpec(shape=(), dtype=tf.int32, name=None)>,\n", " <_TakeDataset element_spec=TensorSpec(shape=(), dtype=tf.int32, name=None)>)" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dataset = Dataset.from_tensor_slices(range(100))\n", "result = pipeline(dataset)\n", "\n", "dataset, result" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Print the result of our dataset" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[20, 24, 28, 32, 36, 40, 44, 48, 52, 56]" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "resultList = list(result.as_numpy_iterator())\n", "resultList" ] } ], "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.10.16" }, "title": "demo" }, "nbformat": 4, "nbformat_minor": 4 }