How can the preprocessed data be shuffled using Tensorflow and Python?

PythonServer Side ProgrammingProgramming

<p>Tensorflow is a machine learning framework that is provided by Google. It is an open-source framework used in conjunction with Python to implement algorithms, deep learning applications, and much more. It is used in research and for production purposes. It has optimization techniques that help in performing complicated mathematical operations quickly. This is because it uses NumPy and multi-dimensional arrays. These multi-dimensional arrays are also known as &lsquo;tensors&rsquo;. The framework supports working with a deep neural network.</p><p>The &lsquo;tensorflow&rsquo; package can be installed on Windows using the below line of code &minus;</p><pre class="prettyprint notranslate">pip install tensorflow</pre><p>Tensor is a data structure used in TensorFlow. It helps connect edges in a flow diagram. This flow diagram is known as the &lsquo;Data flow graph&rsquo;. Tensors are nothing but a multidimensional array or a list.</p><p>We will be using the Illiad&rsquo;s dataset, which contains text data of three translation works from William Cowper, Edward (Earl of Derby) and Samuel Butler. The model is trained to identify the translator when a single line of text is given. The text files used have been preprocessing. This includes removing the document header and footer, line numbers and chapter titles.</p><p>We are using the Google Colaboratory to run the below code. Google Colab or Colaboratory helps run Python code over the browser and requires zero configuration and free access to GPUs (Graphical Processing Units). Colaboratory has been built on top of Jupyter Notebook.</p><h2>Example</h2><p>Following is the code snippet &minus;</p><pre class="prettyprint notranslate">print(&quot;Combine the labelled dataset and reshuffle it&quot;) BUFFER_SIZE = 50000 BATCH_SIZE = 64 VALIDATION_SIZE = 5000 all_labeled_data = labeled_data_sets[0] for labeled_dataset in labeled_data_sets[1:]: &nbsp; &nbsp;all_labeled_data = all_labeled_data.concatenate(labeled_dataset) all_labeled_data = all_labeled_data.shuffle( &nbsp; &nbsp;BUFFER_SIZE, reshuffle_each_iteration=False) print(&quot;Displaying a few samples of input data&quot;) for text, label in all_labeled_data.take(8): &nbsp; &nbsp;print(&quot;The sentence is : &quot;, text.numpy()) &nbsp; &nbsp;print(&quot;The label is :&quot;, label.numpy())</pre><p>Code credit &minus; <a href="" rel="nofollow" target="_blank"></a></p><h2>Output</h2><pre class="result notranslate">Combine the labelled dataset and reshuffle it Displaying a few samples of input data The sentence is : b&#39;But I have now both tasted food, and given&#39; The label is : 0 The sentence is : b&#39;All these shall now be thine: but if the Gods&#39; The label is : 1 The sentence is : b&#39;Their spiry summits waved. There, unperceived&#39; The label is : 0 The sentence is : b&#39;&quot;I pray you, would you show your love, dear friends,&#39; The label is : 1 The sentence is : b&#39;Entering beneath the clavicle the point&#39; The label is : 0 The sentence is : b&#39;But grief, his father lost, awaits him now,&#39; The label is : 1 The sentence is : b&#39;in the fore-arm where the sinews of the elbow are united, whereon he&#39; The label is : 2 The sentence is : b&#39;For, as I think, I have already chased&#39; The label is : 0</pre><h2>Explanation</h2><ul class="list"><li><p>After preprocessing the data, a few samples from the dataset are displayed on the console.</p></li><li><p>The data is not grouped, which means every entry in the &lsquo;all_labeled_data&rsquo; maps to one data point.</p></li></ul>
Updated on 19-Jan-2021 07:47:52