From dd26861d3773a9bab6e3d0550113a1b938173a9f Mon Sep 17 00:00:00 2001 From: soumya997 Date: Mon, 19 Oct 2020 12:51:12 +0530 Subject: [PATCH] added a TF 2.0 implimentation of RNN --- .../amazon-fine-food-review.ipynb | 1401 +++++++++++++++++ 1 file changed, 1401 insertions(+) create mode 100644 ML_and_DL/tensorflow_2/amazon-fine-food-review.ipynb diff --git a/ML_and_DL/tensorflow_2/amazon-fine-food-review.ipynb b/ML_and_DL/tensorflow_2/amazon-fine-food-review.ipynb new file mode 100644 index 0000000..329d353 --- /dev/null +++ b/ML_and_DL/tensorflow_2/amazon-fine-food-review.ipynb @@ -0,0 +1,1401 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "
\n", + "
\n", + "
\n", + "\n", + "**Hi** reader, this is a small and simple guide to RNNs, I will discuss all the basic requirements that you need to get started with RNNs from underneath concepts to code implementation. We will be implementing using TensorFlow 2.0.\n", + "\n", + "
\n", + "\n", + "# Table of Content:\n", + "1. **What is RNNs**\n", + "2. **Many to One RNN**\n", + "3. **Some thing about the data**\n", + "4. **Loading Data Using Pandas**\n", + "5. **Some Basic Exploratory Data Analysis**\n", + "6. **Data Preprocessing**\n", + "7. **Model Building**\n", + "8. **Train Test Split**\n", + "9. **Model Training**\n", + "10. **Model Evaluation**\n", + "11. **Conclusion**\n", + "
\n", + "\n", + "### What is RNNs?\n", + "The R in RNN stands for recurrent which literally means repeating. Now you will think what is repeating here? Here repeating refers to the repeating structure of RNNs. There are different kinds of data available, like normal tabular data with different features, image data,time-series data, text data, etc. From the time series, data and text data are the data which has a sequence in it. Let me discuss the meaning of sequence for text data. It's very noticeable that every sentence has a sequence of words, and if you change the sequence of that sentence then the sentence won't have the same meaning. Because of this reason, we can't use MLPs on text data (there are other reasons too like large parameter size,size of i/p word and o/p word can be different, etc.).\n", + "Now, an RNN has many structures as per the problem requirements like- one to one, one to many, many to one, many to many as fig 1.\n", + "\n", + "
\n", + "
\n", + "
\n", + "

Fig: 1

\n", + "
\n", + "But, here I will be discussing many to one network. But, keep in mind that a single RNN cell is the same everywhere, just they are placed in different ways to satisfy the problem requirements.\n", + "\n", + "### Many to One RNN: \n", + "The structure of many to many looks like fig2.The application of many to one network is spam detection, sentiment analysis, etc. Now let's dive deep and understand the bare bones of RNN.\n", + "\n", + "
\n", + "

Fig: 2

" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Every vertical block with input and output arrows is called a RNN cell, at the beginning, it uses a vector with zeros which are named here as $O_0$, this is only for showing the repeating structure of RNN. the circles in the blocks are activation functions. And if you check out carefully, you will observe that there are three types of weights.$W$ which are with input word vector, $W'$ is with RNN cell o/p and finally $W''$ is at the final RNN cell which helps to make the prediction.O/p of RNN cells are taken as an i/p for the next RNN cell, this is to keep the sequence information preserved. Now, $O_i$ is,\n", + "\n", + "> \\begin{equation}\n", + " O_i = f(X_iW+O_iW')\n", + " \\end{equation}\n", + "\n", + "And at the end, the output of the last RNN cell and the $W''$ are taken and passed through an activation function(here sigmoid is taken) to generate the prediction, $Y' = S(O_nW'')$. And rest of the things are taken care of by backpropagation and gradient descent stuff, normal model training.\n", + "
\n", + "> Note, description of all symbols are given in the image itself." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Some thing about the data:\n", + "The data is called `Amazon fine food review` which is basically a dataset of user reviews on some amazon food products. You can download [here](https://www.kaggle.com/snap/amazon-fine-food-reviews). You can check out this Kaggle dataset for more info about the dataset." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Utils" + ] + }, + { + "cell_type": "code", + "execution_count": 43, + "metadata": {}, + "outputs": [], + "source": [ + "import warnings\n", + "warnings.filterwarnings(\"ignore\") #Ignoring unnecessory warnings\n", + "\n", + "import numpy as np #for large and multi-dimensional arrays\n", + "import pandas as pd #for data manipulation and analysis\n", + "import nltk #Natural language processing tool-kit\n", + "\n", + "from nltk.corpus import stopwords #Stopwords corpus\n", + "from nltk.stem import PorterStemmer # Stemmer\n", + "\n", + "from sklearn.feature_extraction.text import CountVectorizer #For Bag of words\n", + "from sklearn.feature_extraction.text import TfidfVectorizer #For TF-IDF\n", + "from gensim.models import Word2Vec #For Word2Vec\n", + "\n", + "from tensorflow.keras.layers import Embedding\n", + "from tensorflow.keras.preprocessing.sequence import pad_sequences\n", + "from tensorflow.keras.models import Sequential\n", + "from tensorflow.keras.preprocessing.text import one_hot\n", + "from tensorflow.keras.layers import LSTM\n", + "from tensorflow.keras.layers import Dropout\n", + "from tensorflow.keras.layers import Dense\n", + "\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Loading Data Using Pandas:" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": { + "_cell_guid": "79c7e3d0-c299-4dcb-8224-4455121ee9b0", + "_uuid": "d629ff2d2480ee46fbb7e2d37f6b5fab8052498a" + }, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
IdProductIdUserIdProfileNameHelpfulnessNumeratorHelpfulnessDenominatorScoreTimeSummaryText
01B001E4KFG0A3SGXH7AUHU8GWdelmartian1151303862400Good Quality Dog FoodI have bought several of the Vitality canned d...
12B00813GRG4A1D87F6ZCVE5NKdll pa0011346976000Not as AdvertisedProduct arrived labeled as Jumbo Salted Peanut...
23B000LQOCH0ABXLMWJIXXAINNatalia Corres \"Natalia Corres\"1141219017600\"Delight\" says it allThis is a confection that has been around a fe...
34B000UA0QIQA395BORC6FGVXVKarl3321307923200Cough MedicineIf you are looking for the secret ingredient i...
45B006K2ZZ7KA1UQRSCLF8GW1TMichael D. Bigham \"M. Wassir\"0051350777600Great taffyGreat taffy at a great price. There was a wid...
\n", + "
" + ], + "text/plain": [ + " Id ProductId UserId ProfileName \\\n", + "0 1 B001E4KFG0 A3SGXH7AUHU8GW delmartian \n", + "1 2 B00813GRG4 A1D87F6ZCVE5NK dll pa \n", + "2 3 B000LQOCH0 ABXLMWJIXXAIN Natalia Corres \"Natalia Corres\" \n", + "3 4 B000UA0QIQ A395BORC6FGVXV Karl \n", + "4 5 B006K2ZZ7K A1UQRSCLF8GW1T Michael D. Bigham \"M. Wassir\" \n", + "\n", + " HelpfulnessNumerator HelpfulnessDenominator Score Time \\\n", + "0 1 1 5 1303862400 \n", + "1 0 0 1 1346976000 \n", + "2 1 1 4 1219017600 \n", + "3 3 3 2 1307923200 \n", + "4 0 0 5 1350777600 \n", + "\n", + " Summary Text \n", + "0 Good Quality Dog Food I have bought several of the Vitality canned d... \n", + "1 Not as Advertised Product arrived labeled as Jumbo Salted Peanut... \n", + "2 \"Delight\" says it all This is a confection that has been around a fe... \n", + "3 Cough Medicine If you are looking for the secret ingredient i... \n", + "4 Great taffy Great taffy at a great price. There was a wid... " + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "import pandas as pd\n", + "pd.pandas.set_option('display.max_columns',None)\n", + "pd.pandas.set_option('display.max_rows',None)\n", + "df = pd.read_csv('../input/amazon-fine-food-reviews/Reviews.csv')\n", + "df.head()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Some Basic Exploratory Data Analysis:" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "RangeIndex: 568454 entries, 0 to 568453\n", + "Data columns (total 10 columns):\n", + " # Column Non-Null Count Dtype \n", + "--- ------ -------------- ----- \n", + " 0 Id 568454 non-null int64 \n", + " 1 ProductId 568454 non-null object\n", + " 2 UserId 568454 non-null object\n", + " 3 ProfileName 568438 non-null object\n", + " 4 HelpfulnessNumerator 568454 non-null int64 \n", + " 5 HelpfulnessDenominator 568454 non-null int64 \n", + " 6 Score 568454 non-null int64 \n", + " 7 Time 568454 non-null int64 \n", + " 8 Summary 568427 non-null object\n", + " 9 Text 568454 non-null object\n", + "dtypes: int64(5), object(5)\n", + "memory usage: 43.4+ MB\n" + ] + } + ], + "source": [ + "df.info()" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(568454, 10)" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df.shape" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "Id 0\n", + "ProductId 0\n", + "UserId 0\n", + "ProfileName 16\n", + "HelpfulnessNumerator 0\n", + "HelpfulnessDenominator 0\n", + "Score 0\n", + "Time 0\n", + "Summary 27\n", + "Text 0\n", + "dtype: int64" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df.isna().sum()" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
IdProductIdUserIdProfileNameHelpfulnessNumeratorHelpfulnessDenominatorScoreTimeSummaryText
01B001E4KFG0A3SGXH7AUHU8GWdelmartian1151303862400Good Quality Dog FoodI have bought several of the Vitality canned d...
12B00813GRG4A1D87F6ZCVE5NKdll pa0011346976000Not as AdvertisedProduct arrived labeled as Jumbo Salted Peanut...
23B000LQOCH0ABXLMWJIXXAINNatalia Corres \"Natalia Corres\"1141219017600\"Delight\" says it allThis is a confection that has been around a fe...
34B000UA0QIQA395BORC6FGVXVKarl3321307923200Cough MedicineIf you are looking for the secret ingredient i...
45B006K2ZZ7KA1UQRSCLF8GW1TMichael D. Bigham \"M. Wassir\"0051350777600Great taffyGreat taffy at a great price. There was a wid...
\n", + "
" + ], + "text/plain": [ + " Id ProductId UserId ProfileName \\\n", + "0 1 B001E4KFG0 A3SGXH7AUHU8GW delmartian \n", + "1 2 B00813GRG4 A1D87F6ZCVE5NK dll pa \n", + "2 3 B000LQOCH0 ABXLMWJIXXAIN Natalia Corres \"Natalia Corres\" \n", + "3 4 B000UA0QIQ A395BORC6FGVXV Karl \n", + "4 5 B006K2ZZ7K A1UQRSCLF8GW1T Michael D. Bigham \"M. Wassir\" \n", + "\n", + " HelpfulnessNumerator HelpfulnessDenominator Score Time \\\n", + "0 1 1 5 1303862400 \n", + "1 0 0 1 1346976000 \n", + "2 1 1 4 1219017600 \n", + "3 3 3 2 1307923200 \n", + "4 0 0 5 1350777600 \n", + "\n", + " Summary Text \n", + "0 Good Quality Dog Food I have bought several of the Vitality canned d... \n", + "1 Not as Advertised Product arrived labeled as Jumbo Salted Peanut... \n", + "2 \"Delight\" says it all This is a confection that has been around a fe... \n", + "3 Cough Medicine If you are looking for the secret ingredient i... \n", + "4 Great taffy Great taffy at a great price. There was a wid... " + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df1 = df[df['Score']!=3]\n", + "df1.head()" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(525814, 10)" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df1.shape" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Data Preprocessing:" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Here, we are dropping the data points where `UseId`,`\"ProfileName`,`Time`,`Text` are same and keeping the first data point." + ] + }, + { + "cell_type": "code", + "execution_count": 32, + "metadata": {}, + "outputs": [], + "source": [ + "df1 = df1.drop_duplicates(subset={\"UserId\",\"ProfileName\",\"Time\",\"Text\"}, keep='first', inplace=False)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Here, we are keeping the values where `HelpfulnessNumerator` <= `HelpfulnessDenominator`." + ] + }, + { + "cell_type": "code", + "execution_count": 33, + "metadata": {}, + "outputs": [], + "source": [ + "df1 = df1[df1['HelpfulnessNumerator']<=df1['HelpfulnessDenominator']]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "As we have taken this dataset as a NLP problem, so we are only considering the text column as the i/p data and `score` as the i/p labels." + ] + }, + { + "cell_type": "code", + "execution_count": 34, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
textscore
0I have bought several of the Vitality canned d...5
1Product arrived labeled as Jumbo Salted Peanut...1
2This is a confection that has been around a fe...4
3If you are looking for the secret ingredient i...2
4Great taffy at a great price. There was a wid...5
\n", + "
" + ], + "text/plain": [ + " text score\n", + "0 I have bought several of the Vitality canned d... 5\n", + "1 Product arrived labeled as Jumbo Salted Peanut... 1\n", + "2 This is a confection that has been around a fe... 4\n", + "3 If you are looking for the secret ingredient i... 2\n", + "4 Great taffy at a great price. There was a wid... 5" + ] + }, + "execution_count": 34, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "list1 = list(df1['Score'])\n", + "list2 = list(df1['Text'])\n", + "\n", + "score_df = pd.DataFrame(list1,columns = ['score'])\n", + "text_df = pd.DataFrame(list2,columns = ['text'])\n", + "\n", + "df2 = pd.concat([text_df,score_df],axis=1)\n", + "df2.head()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Checking out some of the text which starts from 500 index and ends at 550 index." + ] + }, + { + "cell_type": "code", + "execution_count": 35, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "...you can absolutely forget about these. Confirmed by other reviewers, these chips are now total garbage. Like chewing on styrofoam packaging \"peanuts\". Positively awful, no hyperbole or exaggeration. I'll NEVER buy anything from Kettle brand ever again! From a reportedly once great \"premium\" brand, literally any mass market chip I've ever tried tastes better than these. Stale and rancid tasting, and virtually no salty taste whatsoever. Completely awful!\n", + "-----------------------------------------------------\n", + "These chips are nasty. I thought someone had spilled a drink in the bag, no the chips were just soaked with grease. Nasty!!\n", + "-----------------------------------------------------\n", + "Unless you like salt vinegar chips as salty as eating actual pinches of salt and drinking actual vinegar, i doubt you will like these chips. These are the saltiest & sourest chips I have ever had, and the only reason stops me from throwing these away is because I paid for 2 full boxes and dont like to waste food. The brown chips are especially bad, besides being salty and sour, they also taste overcooked and burnt. Unless you are the rare kind that can take this kind of extreme taste, you will not like these chips.

I actually have a high tolerance for sour taste, so I can down a bag of chips with a bit of difficulty. But normal people, please do not try this at home.\n", + "-----------------------------------------------------\n", + "I bought this brand as a trial since I am tired of the Pingos.

It claims that it is natural. I have no argument on this. But the point is that more than 50% in the bag is over-fried and in brown color. I really suffer eating the over-fried chips. I open some other bags and it looks like the same. So I just throw away all of them. I don't know if I was with bad luck or every bag they are selling is the same. But for sure I will never buy this brand any more.\n", + "-----------------------------------------------------\n", + "They are good but wish they were also baked. Have not found baked no salt potato chips anywhere. If there are any I wish someone would post.\n", + "-----------------------------------------------------\n", + "I ORDERED KETTLE CHIPS IN THE FOLLOWING FLAVVORS
SALT & FRESH GROUND PEPPER
TUSCAN 3 CHEESE
N.Y. CHEDDAR AND HERS

THEY ALL TASTED THE SAME TO ME. IF THERE IS A VARIETY PACK SUGGEST U ORDER BEFORE ORDERING LARGE SIZE.

I WISH I COULD RETURN THE UNOPENED BAGS.\n", + "-----------------------------------------------------\n", + "I purchased these because of the low salt, and they were indeed low in salt. However, many, many of the chips in the bag were literally dripping in oil. I have never encountered this problem with other potato chip brands. It was very unappetizing, and who wants all that oil? I will never buy these again, and I would not recommend them to others.\n", + "-----------------------------------------------------\n", + "If you are looking for STRONG S/V flavor these definitely aren't for you. Very mild in comparison to many others. I live in TX and I can tell you I've tried ALL S/V chips I can get ahold of. The best of the best are HEB (storebrand) and World Market. Bob's and Zapp's Aren't bad. From a chip standpoint they have typically great kettle crunch, but if you're looking for S/V flavor these have \"just a touch.\"\n", + "-----------------------------------------------------\n", + "I don't know if it was the case of chips we received or what, but everyone we allowed to sample the chips (in a buffet style setting) agreed that these were slightly on the salty side.
One person jokingly stated that these would be terrific for melting ice and snow, due to the amount of salt in these.

We had ordered these with the expectations that these would be similar to the Lay's Kettle Mesquite Potato Chips, but these Kettle brands outback bbq chips are the exact opposite.

Sorry folks, we just had better expectations from these chips but they a little on the on the salty side.

We are still looking forward to Amazon finally getting the Lay's Kettle Mesquite Potato Chips in stock.
These got a 2 star, because the bbq powder they had used on these were flavorful enough to be unique.\n", + "-----------------------------------------------------\n", + "These chips are greasy and taste burnt-there is grease in the bottom of the bag and the chips are saturated with it (especially at the bottom of the bag. Even if they are trans fat free, they need to be a lot less greasy.\n", + "-----------------------------------------------------\n", + "Don't waste your money on any of the Kettle brand potato chips. I bought a case of these, and a case of the cheddar and sour cream. Both cases ended up in the garbage can.\n", + "-----------------------------------------------------\n", + "DEfintely not as tasty as the Madhouse Munchies which are my family's favorite. Kettle's are dark/burnt,more broken bits, taste greasy-oily and not the light crunch. Oh well.\n", + "-----------------------------------------------------\n", + "I love sour food but this one I can't bear. Too strong sour taste... and even when you open the bag, the sour smell is too strong. I don't like it.\n", + "-----------------------------------------------------\n", + "Unless you really really really like vinegar - AVOID! Those chips should have been called \"Vinegar and Sea Salt\" - not \"Sea Salt and Vinegar\".\n", + "-----------------------------------------------------\n", + "I have loved Kettle Brand Sea Salt and Vinegar chips since the first time i tried them. The fact that i was able to find them on-line for such a great price, was wonderful. I would definitely make the purchase again. THey are a quick and simple snack for lunch and it goes great with my cold sandwich.\n", + "-----------------------------------------------------\n", + "Used to eat the Spicy Thai flavor all the time. MSG makes my body unhappy and this was one of the few flavored chips that was MSG free. Now they have changed the whole recipe and how they make them. Instead of being real food on the ingredients like it used to be its mostly processed chemical garbage, a bunch more salt and MSG stuffed in under the label \"yeast extract\". They removed the NO MSG from the label so they know very well what they did.

The worst part though is that they taste horrible. Instead of crisp, oily and full of character...say as if they were cooked in like...a kettle or something they instead have the color and uniformity of baked lays. The Spicy Thai flavor now tastes like sour cream and onion or ranch that has gone rancid.

What a shame, no more kettle chips for me. It seems they are slowly moving one flavor at a time to this new cheap ingredient list and manufacturing method. Dont be fooled into paying a premium price for these chips as they are not a premium product anymore.\n", + "-----------------------------------------------------\n", + "The chips were great...for the first few bags. However, after the first bag or two, I noticed that the remaining bags were damaged. There were holes in each bag and black, sticky stains on the outside. Other reviewers who bought around the same time that I did are now claiming that they found rodent holes in their bags of chips.

The chips came in a sealed Kettle box, so it's hard to say who screwed up. But someone somewhere screwed up. These chips were a health hazard before the holes in the bag. Now? It's like a game of roulette.\n", + "-----------------------------------------------------\n", + "After waiting a ridiculous amount of time for my case of 15 5oz bags to arrive, upon opening the box noticed that every bag had been chewed open by a mouse. Don't know if it is still in the box, but it is outside on the porch. When my son gets home, I'm going to have pictures taken and email to Amazon. The outside Amazon box was intact, so it had to be either from the Kettle Chips people or the Amazon warehouse, don't know which. I will never buy these again from Amazon. I cannot tell you how disgusted I am with this purchase. It makes me sick to think about it!

I included pictures at the top of the page. Poor little mousie must have high cholesterol now.

Update: I forgot to mention that I offered to email the above pictures of the mouse damage to Amazon but was told they didn't want them.\n", + "-----------------------------------------------------\n", + "Kettle Chips are the best potato chip God has ever invented. I give the Lord thanks every day for delivering unto me such an incredulously delicious blend of ginger and spice, a veritable cornucopia of flavor. I have actually changed my diet to a strict regiment of the Spicy Thai & Sea Salt and Vinegar flavors, alternating days. I have already lost 5 lbs, not to mention the myriad of other health benefits I have been experiencing. Get your life back - with Kettle Chips.\n", + "-----------------------------------------------------\n", + "My daughter that has autism craves hot, spice and pungent foods. These are her absolute favorite chips! She calls them her sour chips and wants them in her lunch all the time. I love the crispy kettle way they're cooked.\n", + "-----------------------------------------------------\n", + "I am a great fan of potato chips and of Thai food. I was so happy when Kettle Chips decided to meld two of my great loves together! (What a concept.....) These chips are spicy enough, without burning a hole in your tongue. Also, they have a nice hint of sweetness that makes them habit forming (careful!). Once in a while, this is a treat definitely worth indulging in.\n", + "-----------------------------------------------------\n", + "Terrible! I cannot believe this, I received this item and EVERY SINGLE BAG WAS OPENED BUT 4!!!! I'm stationed in Afghanistan and this was gonna be a snack for my team while going out on missions. I was so embarrassed when the bags were opened and spilt out all in the box, gross! And the box is filled with grease stains. Idk if it was from the airplane ride all the way here, but the box should have been more insulated and bubble wrap should have been used instead of paper. I'm very unhappy with stale crusty chips out the bag and us soldiers cannot enjoy now.\n", + "-----------------------------------------------------\n", + "After reading some of the reviews, I got nervous and opened a bag from my recent order expecting the worst! No worries here. All bags are in great shape and expiration dates aren't until June. Chips, at least from the first bag, taste like they are supposed to and all is good in the world! By the way, fifteen bags for under thirty dollars is a ton less expensive than the going price around here at the local grocery store so, yay team!\n", + "-----------------------------------------------------\n", + "Kettle Branch Potato Chips New York Cheddar: These are good if you like kettle fried potato chips that are waaaay salty, on the burnt side, and taste rancid, either because the cheese flavoring or the oil it was fried in was already old. I want to like this brand of chips and try their new and other flavors every now and then. But, after having tried all sorts of other brands of kettle cooked chips, these just don't hit the spot for me.\n", + "-----------------------------------------------------\n", + "I am glad I was able to find these on this site. I love this flavor and they are so crunchy. The box is packed inside another box but still some bags end up as crumbs. Most of the bags survived in tact.\n", + "-----------------------------------------------------\n", + "These chips taste awesome. And unlike most other flavored chips, they actually make sure that plenty of the flavory salty goodness gets on each individual chip. Just don't pass gas near any pretty ladies after consumption. They'll totally know it was you.\n", + "-----------------------------------------------------\n", + "With Kettle Chips, you really have to be careful. Some of their flavors are nauseating. With that said, they DO make fantastic plain chips. Thick cuts of potato, fried to a dark golden brown. They are crunchy and lightly salted with sea salt. I can't recommend these chips enough. You won't regret it.

Some people say they are burnt but they aren't. From their website: \"Take a quick look and you'll see an immediate difference: Kettle Brand® Potato Chips are a beautiful tawny gold. During cooking, the natural sugars of our select potatoes caramelize, creating chips from light gold to a deep amber. The results are flavors as deep and rich as the colors, and an artisanal display in every bag\"

Some have also said that these chips are oily, which is true. But they explain that on their website: \"At Kettle Foods we exclusively use expeller pressed high monounsaturated sunflower and/or safflower oil to make Kettle Brand® products. Both of these oils are naturally free of trans fatty acids and are not hydrogenated in any way. We have taken the extra step of sending our products to independent third party labs to test for the presence of trans fats and results indicate we are \"trans fat free\" meaning none were detected. You will see zero trans fats listed on our packaging.\" That oiliness is natural and happens when quality oil is being used.

You will not find chips with such a natural potato flavor. I'm also a fan of their unsalted potato chips. For those of us watching our salt intake, the unsalted chips is the best you can get. Period.\n", + "-----------------------------------------------------\n", + "These are among the best chips I have ever eaten! I first came upon them when I visited a CostPlus World Market store in Opelika, AL, in the Tiger Town Mall, in 2007. I wasn't a big fan of dijon-flavored anything, but I decided to give these chips a try. I was hooked. I probably ended up buying over a dozen bags within just a few months' time. I searched all over my part of Alabama (the northwestern part of the state) and could not find them. I later tried to find them at the World Market in Hoover, at the Patton Creek Shopping Center, and they did not carry this flavor. However, when I learned that they were available on Amazon.com, and also available for PRIME Shipping - I bought them as soon as I read \"Kettle Chips Honey Dijon, 9-Ounce Bags (Pack of 12)!\"

You get a pretty big box, filled with 12 bags of these delicious morsels. They last quite a while, but I will re-order, very, very soon! I suggest you buy them, in this bulk form, and enjoy them - you won't be disappointed! They go great with hamburgers and hot dogs - so they're perfect for spring and this coming summer's outdoor activities and cook-outs!

Take my word for it, these chips are a hidden gem lost in the world of snack foods! Buy some today, and fall in love like me!\n", + "-----------------------------------------------------\n", + "I have never been addicted to anything in my life...until I tasted these chips. I have tried other brands of the sea salt and vinegar flavor and they are just not the same. You've got to stick with this blue bag of chips!\n", + "-----------------------------------------------------\n", + "I bought these when on sale through Amazon. Nice crispy thick chips. Definitely a great buy if you can catch them on sale.\n", + "-----------------------------------------------------\n", + "For those of you who, like me, had to order the Kettle Spicy Thai chips from Amazon because they are so hard to find locally, you will probably be aggrieved to know that the recipe has changed. They are just like regular potatoe chips. I paid for them but now my husband gets to eat them because he didn't like the spicy thai, but these are fine for him.

Yuck!!!\n", + "-----------------------------------------------------\n", + "I've found a new taste treat. I'm not a big one for potato chips, so I am sure I've come late to discover these. I think they are really good. The processing & ingredients list is actually pretty good-no trans fats, etc. So, if you want an indulgence, these are A+ ! They are thicker & more tasty than the mass produced potato chips we've all come to know. They are much more flavorful. Never had a honey-dijon potato chip before. Incredibly good & a flavorful combination of sweet, tangy, salty. Also not overly hot or spicy. Excellent!

If you want a snack, have something REALLY good. It's got to be worth the calories-and these are!\n", + "-----------------------------------------------------\n", + "This is a real good product.I love these chips and so does my customers can't go wrong wit these the best\n", + "-----------------------------------------------------\n", + "I love these chips and now the whole family is hooked on them. These are the only chips I eat\n", + "-----------------------------------------------------\n", + "I don't know what to say that others have not covered. If you like potato chips you will like these. Get em on sale when you can and limit your intake...if you can...\n", + "-----------------------------------------------------\n", + "If you're looking at this you probably already know you like these chips.
This is a great deal and I have not had any problems with them going stale.
The 1oz bags are a great size for a snack and help avoid over-eating.\n", + "-----------------------------------------------------\n", + "While I admit that the oversalted chip is addictive, I really think this one has gone too far. Skin literally peeled off my lips and inside my mouth after eating these. I am surprised that they are still on the market.\n", + "-----------------------------------------------------\n", + "Only 5mg sodium per serving, chips are crisp. I sometimes will dip in a low salt ketchup and that adds a bit to the flavor.\n", + "-----------------------------------------------------\n", + "I have found this product, Kettle Chips Unsalted to be fabulous. I found them by accident on Amazon after hunting all over my local stores. They are crisp and tasty without all of the salt.\n", + "-----------------------------------------------------\n", + "When I saw the Spicy Thai chips, I knew i had to try them. I love spicy and I love Thai food, so why not? First off, these chips are not super spicy. They actually taste sort of sweet at first, with many degrees of flavor. The spiciness is more of a lingering taste that bites afterwards. I'd say these are definitely a good, unique chip. But if you absolutely hate sweet chips, I'd try a different flavor since these are a little sweet.\n", + "-----------------------------------------------------\n", + "Smiles... Thank you Lord, for I have found another healthy snack food that is unsalted, and tastes great. I almost gave up on finding a potato chip like this. The only thing i would ask Kettle to change is the texture. It's slightly hard/crunchy, but not a problem at all. I would just prefer it to be softer. Kettle if you do make that happen, please keep everything else the same. Thank you.\n", + "-----------------------------------------------------\n", + "These are my favorite, but they aren't for everybody. In a way, they sort of taste like Oriental flavor ramen, but with a kick. For me, how can one go wrong with the combination of jalapeno and ginger? They are a bit rich, so they aren't for EveryDayForAMonth snacking. Because this is a case purchase, I recommend trying out a single bag before buying an entire case. The 2oz size seems to be no longer available, which is a shame; I can eat the entire 5oz bag.\n", + "-----------------------------------------------------\n", + "Salt & vinegar chips are my favorite flavor so I think I've tried every brand out there. These are by far the best. I also like them because the sodium content is not very high like some of the others and they are not excessively greasy. They seem to have the right balance of the salt and vinegar taste.\n", + "-----------------------------------------------------\n", + "Package arrived DOUBLE boxed, wrapped, and the inside box(that holds the goods) perfect not a single dent or rough mark, outstanding packaging.

The Product?

These are the best chips on the planet, Salt 'N Vinegar flavor is beyond among the best chips I've ever had. You might, actually I'm sure you probably won't like it AT FIRST BITE 'IF' you have never had these before. But try it again.

Not to mention, this particular flavor has held up in numerous surveys and comparisons against other manufactures.\n", + "-----------------------------------------------------\n", + "If you love jalapeno chips then Kettle is a must try.
Nice and cruncy with the kick of jalapeno.
Delicious.
Addicting.
Recommended.\n", + "-----------------------------------------------------\n", + "My son bought a package of the Jalapeno Potato Chips a couple weeks ago, and he and I have become addicted.

First, Kettle chips appear to be somewhat better in terms of fats etc. than many other chips, as one peruses the dietary information.

Second, individual chips are nice and crisp and have a good crunch value as you munch away at them.

Third, the jalapeno has some kick (not overly hot, of course). Too many products labelled as spicy are not, have no kick, and are as bland as can be. Here, you do get a slow noticeable burn from the jalapeno. My preference? A bit more heat. But I understand that most consumers would prefer somewhat less than I would. Still, decent spicyness.

All in all, I have been pretty happy with this product, in the few weeks since we first bought it. I now purchase a package each week in my foray to the local grocery store. . . .\n", + "-----------------------------------------------------\n", + "After looking at the pictures someone put on here showing a crushed box, I had to write a review. I have bought these chips numerous times from Amazon Warehouse Deals and each time they came packaged perfectly. There was a sale at the end of the summer and I received a few cases/boxes for just over $10.00. The chips were fresh and very hard to eat just one bag. I hid them in the garage and only remembered them when I went out there; that way I didn't eat them all. As far as the pictures, if the person contacted Amazon, he made out ok. Amazon is great with handling complaints and this person was given perfect customer service if he called Amazon. Thanks for a great tasting product, good price and quick shipping. I'll buy these again...soon.\n", + "-----------------------------------------------------\n", + "There is nothing too good or too bad about these chips. If you like plain potato chips, you'd like these. They are thicker than most brands, fresh out of box, very crunchy and not too salty (for me). One bag has a lot, I generally cannot finish it by myself. Part of the reason is that they are too oily (some chips almost look transparent, if you know what I mean). The taste of oil is a bit too overwhelming for me. If you are used to more spicy chips you may not like these since they do not have any spice, except salt, as the name says. I don't think I'll buy these again. The \"best by\" date on my bags are March 2011 and I ordered them at the end of October.\n", + "-----------------------------------------------------\n", + "This is the second purchase of Kettle Potato Chips and we love them just as much as the first time we tried them. We bought them by the a carton of 24 bags. The Kettle chips were in a 2oz bag which is just enough for a single size serving snack or with a meal. The chips are so delicious, baked not fried and lightly salted with Sea Salt. They have 0 fat, no preservatives, nothing artificial and they only use real food ingredients. What more could you ask for in a great tasting chip!\n", + "-----------------------------------------------------\n", + "How to achieve potato chip nirvana? It's simple: Buy these chips, get them tomorrow, eat them!
Repeat.

Why?

Best \"old school\" BBQ flavor out there
Kettle quality
Incredible price with Amazon

Did I mention delicious and a terrific bargain?\n", + "-----------------------------------------------------\n" + ] + } + ], + "source": [ + "for i in range(500,550):\n", + " print(df2['text'].values[i])\n", + " print('-----------------------------------------------------')" + ] + }, + { + "cell_type": "code", + "execution_count": 36, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
textscore
0I have bought several of the Vitality canned d...5
1Product arrived labeled as Jumbo Salted Peanut...1
2This is a confection that has been around a fe...4
3If you are looking for the secret ingredient i...2
4Great taffy at a great price. There was a wid...5
\n", + "
" + ], + "text/plain": [ + " text score\n", + "0 I have bought several of the Vitality canned d... 5\n", + "1 Product arrived labeled as Jumbo Salted Peanut... 1\n", + "2 This is a confection that has been around a fe... 4\n", + "3 If you are looking for the secret ingredient i... 2\n", + "4 Great taffy at a great price. There was a wid... 5" + ] + }, + "execution_count": 36, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df3 = df2.head(50000)\n", + "df3.head()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Here we have created a function that will mark the score from 1 to 3 as negative and 4 to 5 as positive. We are going to do binary classification that's why we are keeping the labels to positive and negative." + ] + }, + { + "cell_type": "code", + "execution_count": 37, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
textscore
0I have bought several of the Vitality canned d...positive
1Product arrived labeled as Jumbo Salted Peanut...negative
2This is a confection that has been around a fe...positive
3If you are looking for the secret ingredient i...negative
4Great taffy at a great price. There was a wid...positive
\n", + "
" + ], + "text/plain": [ + " text score\n", + "0 I have bought several of the Vitality canned d... positive\n", + "1 Product arrived labeled as Jumbo Salted Peanut... negative\n", + "2 This is a confection that has been around a fe... positive\n", + "3 If you are looking for the secret ingredient i... negative\n", + "4 Great taffy at a great price. There was a wid... positive" + ] + }, + "execution_count": 37, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "def partition(x):\n", + " if x < 3:\n", + " return 'negative'\n", + " return 'positive'\n", + "\n", + "score_upd = df3['score']\n", + "t = score_upd.map(partition)\n", + "df3['score']=t\n", + "\n", + "df3.head()" + ] + }, + { + "cell_type": "code", + "execution_count": 38, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "text 0\n", + "score 0\n", + "dtype: int64" + ] + }, + "execution_count": 38, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df3.isna().sum()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Here, df_x is the i/p text data and df_y is the i/p lable." + ] + }, + { + "cell_type": "code", + "execution_count": 39, + "metadata": {}, + "outputs": [], + "source": [ + "df_x = df3['text']\n", + "df_y = df3['score']" + ] + }, + { + "cell_type": "code", + "execution_count": 41, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "179" + ] + }, + "execution_count": 41, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "stop_words = set(stopwords.words('english'))\n", + "len(stop_words) #finding stop words" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Here we are doing the real pre-processing,which are like- keeping only the alphabets,making all the alphabets lower,removing all the stop word." + ] + }, + { + "cell_type": "code", + "execution_count": 42, + "metadata": {}, + "outputs": [], + "source": [ + "import re\n", + "from nltk.corpus import stopwords\n", + "from nltk.stem.porter import PorterStemmer\n", + "snow = nltk.stem.SnowballStemmer('english')\n", + "\n", + "corpus = []\n", + "for i in range(0, len(df3)):\n", + " review = re.sub('[^a-zA-Z]', ' ', df3['text'][i])\n", + " review = review.lower()\n", + " review = review.split()\n", + " \n", + " review = [snow.stem(word) for word in review if not word in stopwords.words('english')]\n", + " review = ' '.join(review)\n", + " corpus.append(review)" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'product arriv label jumbo salt peanut peanut actual small size unsalt sure error vendor intend repres product jumbo'" + ] + }, + "execution_count": 18, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "corpus[1]" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [], + "source": [ + "df_x = corpus" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "list" + ] + }, + "execution_count": 20, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "type(df_x)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Model Building:" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "This is an important step, here we are creating word vectors by doing one hot encoding, and we are only taking 5000 words as the dictionary." + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "list" + ] + }, + "execution_count": 22, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "voc_size=5000\n", + "onehot_repr=[one_hot(words,voc_size)for words in corpus] \n", + "type(onehot_repr)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Padding is one of the most important parts before feeding the data to the model. In a simple way, padding is a way to keep the input size the same for all i/p text by adding zeros at the front. Here we are considering each i/p text corpus will be of 400 words. " + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[ 0 0 0 ... 3458 3330 2186]\n", + " [ 0 0 0 ... 1777 3330 1834]\n", + " [ 0 0 0 ... 1528 4148 1991]\n", + " ...\n", + " [ 0 0 0 ... 2070 3369 4429]\n", + " [ 0 0 0 ... 1245 2665 4883]\n", + " [ 0 0 0 ... 3739 679 3796]]\n" + ] + } + ], + "source": [ + "sent_length=400\n", + "embedded_docs=pad_sequences(onehot_repr,padding='pre',maxlen=sent_length)\n", + "print(embedded_docs)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "This is the model build using TensorFlow 2.0. Think about the whole model like this,\n", + "\n", + "first, it's a sequential model starting with an `Embedding` layer (which is a deep learning version of the word to vec, or more intuitively WV is an example of `Embedding` and we use it because we can be trained, it's not static like WV) and input size is 5000x40 and output size of 400x40. Now we created a dropout layer using `Dropout()` which will reduce the chance of overfitting. After that, we are taking 100 layers of LSTM and feeding the output of the Embedding layer to each of the LSTM layers. Now, each of the `LSTM` layers will spit out a scalar value, and then we will be stacking them out, because of that, the output size after the `LSTM` layer would be 100. Then we again perform a Dropout layer. In the end, we have a single activation unit of `sigmoid` (line 8) which will take the 100 sized vectors and give a prediction or in general, words give a scaler value between 0 to 1. \n", + "\n", + "Now we are setting the optimixer as `adam` and evaluation matrics as `accurecy` and loss as `binary_crossentropy`(this is because we have taken this as a binary classification problem) using the `model.compile()` method.\n", + "At the 10th line we are printing the model architecture using `model.summary()` method." + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Model: \"sequential\"\n", + "_________________________________________________________________\n", + "Layer (type) Output Shape Param # \n", + "=================================================================\n", + "embedding (Embedding) (None, 400, 40) 200000 \n", + "_________________________________________________________________\n", + "dropout (Dropout) (None, 400, 40) 0 \n", + "_________________________________________________________________\n", + "lstm (LSTM) (None, 100) 56400 \n", + "_________________________________________________________________\n", + "dropout_1 (Dropout) (None, 100) 0 \n", + "_________________________________________________________________\n", + "dense (Dense) (None, 1) 101 \n", + "=================================================================\n", + "Total params: 256,501\n", + "Trainable params: 256,501\n", + "Non-trainable params: 0\n", + "_________________________________________________________________\n", + "None\n" + ] + } + ], + "source": [ + "## Creating model\n", + "embedding_vector_features=40\n", + "model=Sequential()\n", + "model.add(Embedding(voc_size,embedding_vector_features,input_length=sent_length))\n", + "model.add(Dropout(0.3))\n", + "model.add(LSTM(100))\n", + "model.add(Dropout(0.3))\n", + "model.add(Dense(1,activation='sigmoid'))\n", + "model.compile(loss='binary_crossentropy',optimizer='adam',metrics=['accuracy'])\n", + "print(model.summary())" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Some preprocessing before feeding the data. We are doing lable encoding of the `score` column." + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "numpy.ndarray" + ] + }, + "execution_count": 25, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "from sklearn.preprocessing import LabelEncoder\n", + "encode = LabelEncoder()\n", + "df_y2 = encode.fit_transform(df_y)\n", + "type(df_y2)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "This is a important part, where we are converting our data to nd numpy arrays as we cant just input a pandas data frame." + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "metadata": {}, + "outputs": [], + "source": [ + "import numpy as np\n", + "X_final=np.array(embedded_docs)\n", + "y_final=np.array(df_y2)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Train Test Split:" + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "metadata": {}, + "outputs": [], + "source": [ + "from sklearn.model_selection import train_test_split\n", + "X_train, X_test, Y_train, Y_test = train_test_split(X_final, y_final, test_size=0.2, random_state=42)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Model Training:" + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Epoch 1/10\n", + "625/625 [==============================] - 17s 27ms/step - loss: 0.3008 - accuracy: 0.8828 - val_loss: 0.2308 - val_accuracy: 0.9092\n", + "Epoch 2/10\n", + "625/625 [==============================] - 17s 27ms/step - loss: 0.2163 - accuracy: 0.9151 - val_loss: 0.2389 - val_accuracy: 0.9041\n", + "Epoch 3/10\n", + "625/625 [==============================] - 17s 27ms/step - loss: 0.1989 - accuracy: 0.9232 - val_loss: 0.2333 - val_accuracy: 0.9098\n", + "Epoch 4/10\n", + "625/625 [==============================] - 17s 27ms/step - loss: 0.1815 - accuracy: 0.9312 - val_loss: 0.2423 - val_accuracy: 0.9090\n", + "Epoch 5/10\n", + "625/625 [==============================] - 16s 26ms/step - loss: 0.1727 - accuracy: 0.9338 - val_loss: 0.2453 - val_accuracy: 0.9052\n", + "Epoch 6/10\n", + "625/625 [==============================] - 17s 27ms/step - loss: 0.1606 - accuracy: 0.9379 - val_loss: 0.2542 - val_accuracy: 0.9059\n", + "Epoch 7/10\n", + "625/625 [==============================] - 17s 27ms/step - loss: 0.1484 - accuracy: 0.9442 - val_loss: 0.2616 - val_accuracy: 0.9011\n", + "Epoch 8/10\n", + "625/625 [==============================] - 17s 27ms/step - loss: 0.1409 - accuracy: 0.9462 - val_loss: 0.2584 - val_accuracy: 0.9067\n", + "Epoch 9/10\n", + "625/625 [==============================] - 16s 26ms/step - loss: 0.1320 - accuracy: 0.9490 - val_loss: 0.2828 - val_accuracy: 0.9075\n", + "Epoch 10/10\n", + "625/625 [==============================] - 17s 28ms/step - loss: 0.1211 - accuracy: 0.9538 - val_loss: 0.2943 - val_accuracy: 0.9012\n" + ] + }, + { + "data": { + "text/plain": [ + "" + ] + }, + "execution_count": 29, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "#we are feeding the training data to the model,and here the model starts trining,i have taken only 10 epochs and batch_size \n", + "#as 64, you an choose lower and higher values for both the variables as per your requirments.\n", + "model.fit(X_train,Y_train,validation_data=(X_test,Y_test),epochs=10,batch_size=64)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Model Evaluation:" + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "metadata": {}, + "outputs": [], + "source": [ + "#As we preserved X_test as the validation data, so here we are validating our model by making prediction using X_test\n", + "y_pred_lstm = model.predict_classes(X_test)" + ] + }, + { + "cell_type": "code", + "execution_count": 31, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "0.9012" + ] + }, + "execution_count": 31, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "#We have taken accurecy score to validate our model \n", + "from sklearn.metrics import accuracy_score\n", + "accuracy_score(Y_test,y_pred_lstm)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Conclusion:" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Our model is giving more that 90% accurecy which is very good.\n", + "This is how you train a RNN using tensorflow 2.0\n", + "\n", + "Thank you for reading." + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "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.7.3" + } + }, + "nbformat": 4, + "nbformat_minor": 4 +}