-
-
Save kiransair/f8c5bae4c4d95562520e4ba527a460dc to your computer and use it in GitHub Desktop.
TF_Forum_11404.ipynb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"nbformat": 4, | |
"nbformat_minor": 0, | |
"metadata": { | |
"colab": { | |
"provenance": [], | |
"authorship_tag": "ABX9TyMWeJfNXq8JqdlZk0IGFCoN", | |
"include_colab_link": true | |
}, | |
"kernelspec": { | |
"name": "python3", | |
"display_name": "Python 3" | |
}, | |
"language_info": { | |
"name": "python" | |
} | |
}, | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "view-in-github", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"<a href=\"https://colab.research.google.com/gist/kiransair/f8c5bae4c4d95562520e4ba527a460dc/tf_forum_11404.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": { | |
"id": "9pvWAFpCGsKC" | |
}, | |
"outputs": [], | |
"source": [ | |
"import csv\n", | |
"import pandas as pd\n", | |
"import tensorflow as tf" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"data = [\n", | |
" {'gender': 'M', 'products': [1, 2]},\n", | |
" {'gender': 'M', 'products': [1, 7, 5, 8]}\n", | |
"]" | |
], | |
"metadata": { | |
"id": "rmLW7W7QGys2" | |
}, | |
"execution_count": 3, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"file_name = 'tset.csv'" | |
], | |
"metadata": { | |
"id": "ZlcaiiL6G524" | |
}, | |
"execution_count": 4, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"with open(file_name, mode='w', newline='') as file:\n", | |
" writer = csv.DictWriter(file, fieldnames=['gender', 'products'])\n", | |
" writer.writeheader()\n", | |
" for row in data:\n", | |
" writer.writerow(row)\n" | |
], | |
"metadata": { | |
"id": "YhiEcDhOG7rt" | |
}, | |
"execution_count": 5, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"data=pd.read_csv('/content/tset.csv')" | |
], | |
"metadata": { | |
"id": "MhKkYCojG9JK" | |
}, | |
"execution_count": 6, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"ds = tf.data.Dataset.from_tensor_slices((data['gender'].values, data['products'].values))" | |
], | |
"metadata": { | |
"id": "I3-HtzhzG--M" | |
}, | |
"execution_count": 7, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"def convert_to_ragged(gender, products):\n", | |
" products = tf.strings.regex_replace(products, \"[\\[\\]']\", \"\")\n", | |
" split_products = tf.strings.split(products, ',')\n", | |
" ragged_tensor = tf.RaggedTensor.from_row_lengths(split_products, row_lengths=tf.strings.length(split_products))\n", | |
" return gender, ragged_tensor" | |
], | |
"metadata": { | |
"id": "Ab3KchTRHA9c" | |
}, | |
"execution_count": 8, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"rds = ds.map(convert_to_ragged)" | |
], | |
"metadata": { | |
"id": "bXeSpIAmHCzv" | |
}, | |
"execution_count": 9, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"rds" | |
], | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "PbPWwYAaHFEL", | |
"outputId": "8c6b1a53-6d29-46a0-e704-7c660493e894" | |
}, | |
"execution_count": 10, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"<_MapDataset element_spec=(TensorSpec(shape=(), dtype=tf.string, name=None), RaggedTensorSpec(TensorShape([None, None]), tf.string, 1, tf.int32))>" | |
] | |
}, | |
"metadata": {}, | |
"execution_count": 10 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [], | |
"metadata": { | |
"id": "K701nYogHF2o" | |
}, | |
"execution_count": null, | |
"outputs": [] | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment