|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "<h3 align=\"center\" style='color:blue'>TF Data Input Pipeline: Exercise Solution</h3>" |
| 8 | + ] |
| 9 | + }, |
| 10 | + { |
| 11 | + "cell_type": "markdown", |
| 12 | + "metadata": {}, |
| 13 | + "source": [ |
| 14 | + "Moview reviews are present as individual text file (one file per review) in review folder. \n", |
| 15 | + "\n", |
| 16 | + "Folder structure looks like this,\n", |
| 17 | + "\n", |
| 18 | + "reviews\n", |
| 19 | + "\n", |
| 20 | + " |__ positive\n", |
| 21 | + " |__pos_1.txt\n", |
| 22 | + " |__pos_2.txt\n", |
| 23 | + " |__pos_3.txt\n", |
| 24 | + " |__ negative\n", |
| 25 | + " |__neg_1.txt\n", |
| 26 | + " |__neg_2.txt\n", |
| 27 | + " |__neg_3.txt\n", |
| 28 | + " \n", |
| 29 | + "You need to read these reviews using tf.data.Dataset and perform following transformations,\n", |
| 30 | + "\n", |
| 31 | + "(1) Read text review and generate a label from folder name. your dataset should have review text and label as a tuple\n", |
| 32 | + "\n", |
| 33 | + "(2) Filter blank text review. Two files are blank in this dataset\n", |
| 34 | + "\n", |
| 35 | + "(3) Do all of the above transformations in single line of code. Also shuffle all the reviews" |
| 36 | + ] |
| 37 | + }, |
| 38 | + { |
| 39 | + "cell_type": "code", |
| 40 | + "execution_count": 168, |
| 41 | + "metadata": {}, |
| 42 | + "outputs": [], |
| 43 | + "source": [ |
| 44 | + "import tensorflow as tf" |
| 45 | + ] |
| 46 | + }, |
| 47 | + { |
| 48 | + "cell_type": "markdown", |
| 49 | + "metadata": {}, |
| 50 | + "source": [ |
| 51 | + "<h3 style='color:purple'>Retrieve review file paths in a tensorflow dataset</h3>" |
| 52 | + ] |
| 53 | + }, |
| 54 | + { |
| 55 | + "cell_type": "code", |
| 56 | + "execution_count": 177, |
| 57 | + "metadata": {}, |
| 58 | + "outputs": [], |
| 59 | + "source": [ |
| 60 | + "reviews_ds = tf.data.Dataset.list_files('reviews/*/*', shuffle=False)" |
| 61 | + ] |
| 62 | + }, |
| 63 | + { |
| 64 | + "cell_type": "code", |
| 65 | + "execution_count": 178, |
| 66 | + "metadata": { |
| 67 | + "scrolled": true |
| 68 | + }, |
| 69 | + "outputs": [ |
| 70 | + { |
| 71 | + "name": "stdout", |
| 72 | + "output_type": "stream", |
| 73 | + "text": [ |
| 74 | + "b'reviews\\\\negative\\\\neg_1.txt'\n", |
| 75 | + "b'reviews\\\\negative\\\\neg_2.txt'\n", |
| 76 | + "b'reviews\\\\negative\\\\neg_3.txt'\n", |
| 77 | + "b'reviews\\\\positive\\\\pos_1.txt'\n", |
| 78 | + "b'reviews\\\\positive\\\\pos_2.txt'\n", |
| 79 | + "b'reviews\\\\positive\\\\pos_3.txt'\n" |
| 80 | + ] |
| 81 | + } |
| 82 | + ], |
| 83 | + "source": [ |
| 84 | + "for file in reviews_ds:\n", |
| 85 | + " print(file.numpy())" |
| 86 | + ] |
| 87 | + }, |
| 88 | + { |
| 89 | + "cell_type": "markdown", |
| 90 | + "metadata": {}, |
| 91 | + "source": [ |
| 92 | + "<h3 style='color:purple'>Extract review text from these files. Extract label from folder name</h3>" |
| 93 | + ] |
| 94 | + }, |
| 95 | + { |
| 96 | + "cell_type": "code", |
| 97 | + "execution_count": 179, |
| 98 | + "metadata": {}, |
| 99 | + "outputs": [], |
| 100 | + "source": [ |
| 101 | + "import os\n", |
| 102 | + "def extract_review_and_label(file_path):\n", |
| 103 | + " return tf.io.read_file(file_path), tf.strings.split(file_path, os.path.sep)[-2]" |
| 104 | + ] |
| 105 | + }, |
| 106 | + { |
| 107 | + "cell_type": "code", |
| 108 | + "execution_count": 180, |
| 109 | + "metadata": {}, |
| 110 | + "outputs": [ |
| 111 | + { |
| 112 | + "name": "stdout", |
| 113 | + "output_type": "stream", |
| 114 | + "text": [ |
| 115 | + "Review: b\"Basically there's a family where a little boy (Jak\"\n", |
| 116 | + "Label: b'negative'\n", |
| 117 | + "Review: b'This show was an amazing, fresh & innovative idea '\n", |
| 118 | + "Label: b'negative'\n", |
| 119 | + "Review: b''\n", |
| 120 | + "Label: b'negative'\n", |
| 121 | + "Review: b'One of the other reviewers has mentioned that afte'\n", |
| 122 | + "Label: b'positive'\n", |
| 123 | + "Review: b'A wonderful little production. <br /><br />The fil'\n", |
| 124 | + "Label: b'positive'\n", |
| 125 | + "Review: b''\n", |
| 126 | + "Label: b'positive'\n" |
| 127 | + ] |
| 128 | + } |
| 129 | + ], |
| 130 | + "source": [ |
| 131 | + "reviews_ds_1 = reviews_ds.map(extract_review_and_label)\n", |
| 132 | + "for review, label in reviews_ds_1:\n", |
| 133 | + " print(\"Review: \",review.numpy()[:50])\n", |
| 134 | + " print(\"Label: \",label.numpy())" |
| 135 | + ] |
| 136 | + }, |
| 137 | + { |
| 138 | + "cell_type": "markdown", |
| 139 | + "metadata": {}, |
| 140 | + "source": [ |
| 141 | + "<h3 style='color:purple'>Filter blank reviews</h3>" |
| 142 | + ] |
| 143 | + }, |
| 144 | + { |
| 145 | + "cell_type": "code", |
| 146 | + "execution_count": 181, |
| 147 | + "metadata": { |
| 148 | + "scrolled": true |
| 149 | + }, |
| 150 | + "outputs": [ |
| 151 | + { |
| 152 | + "name": "stdout", |
| 153 | + "output_type": "stream", |
| 154 | + "text": [ |
| 155 | + "Review: b\"Basically there's a family where a little boy (Jak\"\n", |
| 156 | + "Label: b'negative'\n", |
| 157 | + "Review: b'This show was an amazing, fresh & innovative idea '\n", |
| 158 | + "Label: b'negative'\n", |
| 159 | + "Review: b'One of the other reviewers has mentioned that afte'\n", |
| 160 | + "Label: b'positive'\n", |
| 161 | + "Review: b'A wonderful little production. <br /><br />The fil'\n", |
| 162 | + "Label: b'positive'\n" |
| 163 | + ] |
| 164 | + } |
| 165 | + ], |
| 166 | + "source": [ |
| 167 | + "reviews_ds_2 = reviews_ds_1.filter(lambda review, label: review!=\"\")\n", |
| 168 | + "for review, label in reviews_ds_2.as_numpy_iterator():\n", |
| 169 | + " print(\"Review: \",review[:50])\n", |
| 170 | + " print(\"Label: \",label)" |
| 171 | + ] |
| 172 | + }, |
| 173 | + { |
| 174 | + "cell_type": "markdown", |
| 175 | + "metadata": {}, |
| 176 | + "source": [ |
| 177 | + "<h3 style='color:purple'>Perform map, filter and shuffle all in single line of code</h3>" |
| 178 | + ] |
| 179 | + }, |
| 180 | + { |
| 181 | + "cell_type": "code", |
| 182 | + "execution_count": 182, |
| 183 | + "metadata": {}, |
| 184 | + "outputs": [ |
| 185 | + { |
| 186 | + "name": "stdout", |
| 187 | + "output_type": "stream", |
| 188 | + "text": [ |
| 189 | + "Review: b'This show was an amazing, fresh & innovative idea '\n", |
| 190 | + "Label: b'negative'\n", |
| 191 | + "Review: b\"Basically there's a family where a little boy (Jak\"\n", |
| 192 | + "Label: b'negative'\n", |
| 193 | + "Review: b'A wonderful little production. <br /><br />The fil'\n", |
| 194 | + "Label: b'positive'\n", |
| 195 | + "Review: b'One of the other reviewers has mentioned that afte'\n", |
| 196 | + "Label: b'positive'\n" |
| 197 | + ] |
| 198 | + } |
| 199 | + ], |
| 200 | + "source": [ |
| 201 | + "final_ds = reviews_ds.map(extract_review_and_label).filter(lambda review, label: review!=\"\").shuffle(3)\n", |
| 202 | + "for review, label in final_ds.as_numpy_iterator():\n", |
| 203 | + " print(\"Review:\",review[:50])\n", |
| 204 | + " print(\"Label:\",label)" |
| 205 | + ] |
| 206 | + } |
| 207 | + ], |
| 208 | + "metadata": { |
| 209 | + "kernelspec": { |
| 210 | + "display_name": "Python 3", |
| 211 | + "language": "python", |
| 212 | + "name": "python3" |
| 213 | + }, |
| 214 | + "language_info": { |
| 215 | + "codemirror_mode": { |
| 216 | + "name": "ipython", |
| 217 | + "version": 3 |
| 218 | + }, |
| 219 | + "file_extension": ".py", |
| 220 | + "mimetype": "text/x-python", |
| 221 | + "name": "python", |
| 222 | + "nbconvert_exporter": "python", |
| 223 | + "pygments_lexer": "ipython3", |
| 224 | + "version": "3.8.5" |
| 225 | + } |
| 226 | + }, |
| 227 | + "nbformat": 4, |
| 228 | + "nbformat_minor": 4 |
| 229 | +} |
0 commit comments