Day 40: 60 days of Data Science and Machine Learning Series
Tensorflow with a project ..

Welcome back peeps. In this post we are going to understand the basics of Tensorflow with a project.
Some of the other best Series —
100 days : Your Data Science and Machine Learning Degree Series with projects
Complete Data Visualization and Pre-processing Series with projects
Projects Videos —
All the projects, data structures, SQL, algorithms, system design, Data Science and ML , Data Analytics, Data Engineering, , Implemented Data Science and ML projects, Implemented Data Engineering Projects, Implemented Deep Learning Projects, Implemented Machine Learning Ops Projects, Implemented Time Series Analysis and Forecasting Projects, Implemented Applied Machine Learning Projects, Implemented Tensorflow and Keras Projects, Implemented PyTorch Projects, Implemented Scikit Learn Projects, Implemented Big Data Projects, Implemented Cloud Machine Learning Projects, Implemented Neural Networks Projects, Implemented OpenCV Projects,Complete ML Research Papers Summarized, Implemented Data Analytics projects, Implemented Data Visualization Projects, Implemented Data Mining Projects, Implemented Natural Leaning Processing Projects, MLOps and Deep Learning, Applied Machine Learning with Projects Series, PyTorch with Projects Series, Tensorflow and Keras with Projects Series, Scikit Learn Series with Projects, Time Series Analysis and Forecasting with Projects Series, ML System Design Case Studies Series videos will be published on our youtube channel ( just launched).
Subscribe today!
Tech Newsletter —
If you are interested, you can join my newsletter through which I send tech interview tips, techniques, patterns, hacks — Software Development, ML, Data Science, Startups and Technology projects to more than 30K readers. You can subscribe to Tech Brew :
Tensorflow is an open source platform for machine learning and deep learning developed by Google Brain Team and written in C++, Python, and CUDA created for large numerical computations and deep learning. It ingests the data in the form of tensors which are nothing but multi-dimensional arrays of higher dimensions to handle large amounts of data. It works on the data flow graphs that have nodes and edges and supports both CPUs and GPUs. It works by preprocessing the data, building the model, training and estimating the model.

A good reference to Tensorflow ( used in this project as well )—
In this project we will perform sentiment analysis using using Keras with TensorFlow as its backend.
Let’s Dive in!
Import necessary libraries
from tensorflow.python.keras.preprocessing.sequence import pad_sequences
from tensorflow.python.keras.datasets import imdb
from tensorflow.python.keras.models import Sequential
from tensorflow.python.keras.layers import Dense, Embedding, GlobalAveragePooling1D
from tensorflow.python.keras.callbacks import LambdaCallback
import matplotlib.pyplot as plt
%matplotlib inline
import numpy as npLoad the data and Data Preprocessing
( x_train, y_train), ( x_test,y_test) = imdb.load_data(num_words=10000)
class_names = ['Negative','Positive']
wi = imdb.get_word_index()
print(wi['hey'])Output —
1397Word embedding
Word Embeddings lets us use an efficient, dense representation in which similar words have a similar encoding.

A good reference for Word Embeddings —
Decode and Padding
reverse_word_index = dict((value,key) for key,value in wi.items())def decode(review):
text = ''
for i in review:
text+=reverse_word_index[i]
text += ' '
return text
decode(x_train[2])Output —
"the as there in at by br of sure many br of proving no only women was than doesn't as you never of hat night that with ignored they bad out superman plays of how star so stories film comes defense date of wide they don't do that had with of hollywood br of my seeing fan this of pop out body shots in having because cause it's stick passing first were enjoys for from look seven sense from me and die in character as cuban issues but is you that isn't one song just is him less are strongly not are you that different just even by this of you there is eight when it part are film's love film's 80's was big also light don't and as it in character looked cinematography so stories is far br man acting "Padding —
x_train = pad_sequences(x_train,value=wi['the'],padding = 'post',maxlen=256)
x_test = pad_sequences(x_test,value=wi['the'],padding = 'post',maxlen=256)
decode(x_train[2])Output —
"the as there in at by br of sure many br of proving no only women was than doesn't as you never of hat night that with ignored they bad out superman plays of how star so stories film comes defense date of wide they don't do that had with of hollywood br of my seeing fan this of pop out body shots in having because cause it's stick passing first were enjoys for from look seven sense from me and die in character as cuban issues but is you that isn't one song just is him less are strongly not are you that different just even by this of you there is eight when it part are film's love film's 80's was big also light don't and as it in character looked cinematography so stories is far br man acting the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the the "Create a neural network model
model = Sequential(
[
Embedding(10000,16),
GlobalAveragePooling1D(),
Dense(16,activation='relu'),
Dense(1,activation='sigmoid')
])
model.compile(
loss ='binary_crossentropy',
optimizer = 'adam',
metrics = ['accuracy']
)
model.summary()Output —
Layer (type) Output Shape Param #
=================================================================
embedding (Embedding) (None, None, 16) 160000
_________________________________________________________________
global_average_pooling1d (Gl (None, 16) 0
_________________________________________________________________
dense (Dense) (None, 16) 272
_________________________________________________________________
dense_1 (Dense) (None, 1) 17
=================================================================
Total params: 160,289
Trainable params: 160,289
Non-trainable params: 0
_________________________________________________________________Train the model
sl = LambdaCallback(on_epoch_end = lambda e, l:print(e,end='.'))E = 20h = model.fit(
x_train, y_train,
validation_split= 0.2,
epochs = E,
callbacks = [sl],
verbose = False
)Evaluate the model
plt.plot(range(E),h.history['acc'],label='Training')
plt.plot(range(E),h.history['val_acc'],label = 'Validation')
plt.legend()
plt.show()Output —

Prediction
loss,acc = model.evaluate(x_test,y_test)
print(acc*100)
p = model.predict(np.expand_dims(x_test[2],axis=0))
print(class_names[np.argmax(p[0])])
print(decode(x_test[2]))Output —
25000/25000 [==============================] - 1s 45us/step
84.34Negative"even cliche to purchased is money easily egypt and glory any is and i i liam film as and set actually easily like outdated sequel any of and ryan made film is and br and constant and of 90s letting deep in act made of road in of and movie and rural vhs of share in reaching fact of and polly spinal of 90s to them book are is unfamiliar mercy and mode they funniest is white courage and vegas wooden br of gender and unfortunately of 1968 no of years hokey and true up and and but 3 all ordinary be oblivious to and were deserve film clone and of creative br comes their kung who is assuming bias out new all it incomprehensible it episode much that's including i i cartoon of my certain no as rooting over you with way to cartoon of enough for that with way who is finished and they of rukh br for and expressing stunts black that story at actual in can as movie is and has though songs and action it's action his one me and grass this second no all way and not lee and be moves br figure of you boss movie is and 9 br propaganda and and after at of smoke splendid snow saturday it's results this of load it's think class br think cop for games make southern things to it jolly who and if is boyfriend you which is tony by this make residents too not make above it even background "Learnings —
How to create, train, and evaluate a neural network in TensorFlow and solve text classification with neural networks.
Day 41: Coming soon!
Follow and Stay tuned. Keep coding :)
For other projects, tune to —
Build Machine Learning Pipelines( With Code)
Recurrent Neural Network with Keras
Clustering Geolocation Data in Python using DBSCAN and K-Means
Facial Expression Recognition using Keras
Hyperparameter Tuning with Keras Tuner
Custom Layers in Keras
That’s it fellas. Peace out and keep coding :)
Stay Tuned and of-course let me end this post with a quote by Steve Jobs ;)
“You have to be burning with an idea, or a problem, or a wrong that you want to right. If you’re not passionate enough from the start, you’ll never stick it out.”





